示例#1
0
    def load_from_stream(self, f, format="yaml"):
        """Loads and parses a JSON or YAML schema.

        Support both JSON(.json) and YAML(.yml/.yaml) file formats
        as detected by filename extensions.

        :param f: An open file object.
        :raises ValueError: if the file has an unsupported extension.
        """

        if format == "json":
            if reschema.settings.MARKED_LOAD:
                obj = json_loader.marked_load(f)
            else:
                obj = json.load(f, object_pairs_hook=OrderedDict)

        elif format == "yaml":
            if reschema.settings.MARKED_LOAD:
                obj = yaml_loader.marked_load(f)
            else:
                obj = yaml_loader.ordered_load(f)

        else:
            raise ValueError("Unrecognized format, use 'json' or 'yaml': %s" % format)

        self.parse(obj)
示例#2
0
    def parse_text(self, text, format="yaml"):
        """Loads and parses a schema from a string.

        :param text: The string containing the schema.
        :param format: Either 'json' (the default), 'yaml' or 'yml'.
                       This much match the format of the data in the string.
        """

        stream = StringIO(text)
        if format == "json":
            obj = json_loader.marked_load(stream)
        elif format == "yaml" or format == "yml":
            obj = yaml_loader.marked_load(stream)

        return self.parse(obj)