def read(self, filename, pointer=None, additional=None): if filename == "-": f = sys.stdin else: f = open(filename, "r") if filename.endswith('.json'): input = json.load(f, object_pairs_hook=OrderedDict) elif filename.endswith(('.yml', '.yaml')): input = yaml.load(f) else: raise ValueError( "Unrecognized file extension, use '*.json' or '*.yml': %s" % filename) if additional: for a in additional: a_input = jsonpointer.resolve_pointer(input, pointer) Schema.parse(a_input, name=a) name = 'root' if pointer: input = jsonpointer.resolve_pointer(input, pointer) name = pointer self.schema_raw = input self.schema = Schema.parse(input, name=name) if f is not sys.stdin: f.close()
def parse(self, obj): """Parses a Python data object representing a schema. :param obj: The Python object containig the schema data. :type obj: dict """ # Common properties with Parser(obj, "<servicdef>", self) as parser: parser.parse("$schema", required=True, save_as="schema") if self.schema not in SUPPORTED_SCHEMAS: raise UnsupportedSchema("Unsupported schema format: %s" % self.schema) parser.parse("id", required=True) parsed_id = urlparse.urlparse(self.id) if not parsed_id.netloc: raise ParseError("Service definition 'id' property must be a " "fully qualified URI: %s" % id) # Preform some preprocessing: # - Expand all relative $ref targets to full absoslute references parser.preprocess_input(self.id) parser.parse("provider", required=True) parser.parse("name", required=True) parser.parse("version", required=True, types=[str, unicode]) parser.parse("title", "") parser.parse("status", "") # 'description' is a doc property, supporting either: # 'description' : <string> # 'description' : { 'file': <filename>, 'format': <format> } # 'description' : { 'text': <string>, 'format': <format> } # where 'format' is optional and defaults to 'md' parser.parse("description", "") parser.parse("documentationLink", "") parser.parse("defaultAuthorization") self.types = OrderedDict() for type_ in parser.parse("types", [], save=False): self.types[type_] = Schema.parse( obj["types"][type_], name=type_, id="#/types/%s" % type_, servicedef=self ) self.resources = OrderedDict() for resource in parser.parse("resources", [], save=False): input_ = obj["resources"][resource] sch = Schema.parse(input_, name=resource, id="#/resources/%s" % resource, servicedef=self) self.resources[resource] = sch parser.parse("tasks") parser.parse("request_headers") parser.parse("response_headers") parser.parse("errors") parser.parse("tags", {}, types=dict)