Prototype object based on the schema, in Python code with comments. Args: schema: object, Parsed JSON schema file. Returns: Prototype object based on the schema, in Python code with comments.
(self, schema)
| 240 | self.dent -= 1 |
| 241 | |
| 242 | def _to_str_impl(self, schema): |
| 243 | """Prototype object based on the schema, in Python code with comments. |
| 244 | |
| 245 | Args: |
| 246 | schema: object, Parsed JSON schema file. |
| 247 | |
| 248 | Returns: |
| 249 | Prototype object based on the schema, in Python code with comments. |
| 250 | """ |
| 251 | stype = schema.get("type") |
| 252 | if stype == "object": |
| 253 | self.emitEnd("{", schema.get("description", "")) |
| 254 | self.indent() |
| 255 | if "properties" in schema: |
| 256 | properties = schema.get("properties", {}) |
| 257 | sorted_properties = OrderedDict(sorted(properties.items())) |
| 258 | for pname, pschema in sorted_properties.items(): |
| 259 | self.emitBegin('"%s": ' % pname) |
| 260 | self._to_str_impl(pschema) |
| 261 | elif "additionalProperties" in schema: |
| 262 | self.emitBegin('"a_key": ') |
| 263 | self._to_str_impl(schema["additionalProperties"]) |
| 264 | self.undent() |
| 265 | self.emit("},") |
| 266 | elif "$ref" in schema: |
| 267 | schemaName = schema["$ref"] |
| 268 | description = schema.get("description", "") |
| 269 | s = self.from_cache(schemaName, seen=self.seen) |
| 270 | parts = s.splitlines() |
| 271 | self.emitEnd(parts[0], description) |
| 272 | for line in parts[1:]: |
| 273 | self.emit(line.rstrip()) |
| 274 | elif stype == "boolean": |
| 275 | value = schema.get("default", "True or False") |
| 276 | self.emitEnd("%s," % str(value), schema.get("description", "")) |
| 277 | elif stype == "string": |
| 278 | value = schema.get("default", "A String") |
| 279 | self.emitEnd('"%s",' % str(value), schema.get("description", "")) |
| 280 | elif stype == "integer": |
| 281 | value = schema.get("default", "42") |
| 282 | self.emitEnd("%s," % str(value), schema.get("description", "")) |
| 283 | elif stype == "number": |
| 284 | value = schema.get("default", "3.14") |
| 285 | self.emitEnd("%s," % str(value), schema.get("description", "")) |
| 286 | elif stype == "null": |
| 287 | self.emitEnd("None,", schema.get("description", "")) |
| 288 | elif stype == "any": |
| 289 | self.emitEnd('"",', schema.get("description", "")) |
| 290 | elif stype == "array": |
| 291 | self.emitEnd("[", schema.get("description")) |
| 292 | self.indent() |
| 293 | self.emitBegin("") |
| 294 | self._to_str_impl(schema["items"]) |
| 295 | self.undent() |
| 296 | self.emit("],") |
| 297 | else: |
| 298 | self.emit("Unknown type! %s" % stype) |
| 299 | self.emitEnd("", "") |