Пример #1
0
    def generate_schema(data):
        schema = genson.Schema()
        schema.add_schema({})

        # To get rid of nasty type errors, pingpong forth and back to JSON compatible: JSON
        json_data = json.dumps(data)
        dictionary = json.loads(json_data)
        schema.add_object(dictionary)

        # Replace MongoDB-incompatible dot with unicode FULLWIDTH FULLSTOP
        schema_dict = schema.to_dict()
        for key, value in schema_dict['items']['properties'].items():
            if '.' in key:
                new_key = key.replace('.', '\uff0e')
                schema_dict['items']['properties'][new_key] = schema_dict[
                    'items']['properties'].pop(key)
        required_ = schema_dict['items']['required']

        for required_val in required_:
            if '.' in required_val:
                required_.append(
                    required_.pop(required_.index(required_val)).replace(
                        '.', '\uff0e'))

        return schema_dict
Пример #2
0
def generateJsonObjects(num):
    s = genson.Schema()

    for o in range(num):
        s.add_schema({str(o): generateJsonObject()})

    return s.to_json()
Пример #3
0
 def json_to_schema(self, data):
     schema = genson.Schema()
     schema.add_object(data)
     schema = schema.to_json()
     if isinstance(schema, str):
         schema = json.loads(schema)
     return schema
Пример #4
0
def create_json_schema(response_json_file_path):
    json_obj = json.loads(
        codecs.open(response_json_file_path, 'r', 'utf-8').read())
    schema = genson.Schema()
    schema.add_object(json_obj)
    schema_file_path = os.path.join(os.path.dirname(response_json_file_path),
                                    'schema.json')
    schema_file = codecs.open(schema_file_path, 'w', 'utf-8')
    schema_file.write(schema.to_json(ensure_ascii=False, indent=4))
    schema_file.close()
Пример #5
0
    def __init__(self, yaml_spec_fn):
        super(YamlCommandContent, self).__init__(yaml_spec_fn.__doc__)

        schema = yaml_spec_fn()
        if schema is not None:
            if type(schema) is list:
                schema_generator = genson.Schema()
                for schema_example in schema:
                    schema_generator.add_object(schema_example)
                schema = schema_generator.to_dict()
        self._schema = schema
Пример #6
0
def schema(data, typ):
    """ Build schema from data
    """
    _ = typ
    yaml = Td4aYaml()
    obj_data = yaml.load(data['p1'])
    json_schema = genson.Schema()
    json_schema.add_object(obj_data)
    schema_dict = json_schema.to_dict()
    schema_yaml = yaml.load(yaml.dump(schema_dict))
    sorted_schema_yaml = sort_commented_map(commented_map=schema_yaml)
    sorted_schema_string = yaml.dump(sorted_schema_yaml)
    return sorted_schema_string
Пример #7
0
def to_schema(fname, *ds):
    s = genson.Schema()
    s.add_schema({
        "type":
        "object",
        "properties": {},
        "title": (lambda fname: fname[:fname.find(".")])(path.basename(fname)),
    })
    s.schema_uri = "http://json-schema.org/draft-04/schema"
    for d in ds:
        s.add_object(d)
    with open(path.join(schemas_dir, fname.replace(".yml", ".schema.json")),
              "wt") as f:
        f.write(s.to_json(indent=4))
Пример #8
0
def convert_to_jsonschema(args):
    stream = file(args.json_source)
    ymal_data = yaml.load(stream)

    logger.info(ymal_data)

    s = genson.Schema()
    s.add_object(ymal_data)

    # indent forces pretty print
    json_schema = s.to_json(indent=4, sort_keys=False)
    logger.info(json_schema)
    with open(args.json_schema_file_path, 'w') as json_schema_file:
        json_schema_file.write(json_schema)
Пример #9
0
def timeline():
    t = Twython('92UbSi1YbvPI5umlJVV6xwL62',
                'EMM846L8yfj3hP5T14mdxHKDII1rb9AV7whUUjwVy4X4ZTcx0c',
                '2257672447-BSkO8JVaFWzrPebefP3bCrY09AAh11Tq1yaX66c',
                'BrgI4HvNpqs1tg7u1q0JBYuBH5uePGHW5i1GjRrhZxo8U')

    tl = t.get_home_timeline()
    json.dumps(tl)
    s = genson.Schema()
    s.add_object(tl)
    tweets = []
    for i in range(0, len(tl)):
        # pprint(tl[i]['text'])
        tweets.append(tl[i]['text'])
    tweets_dict = {'tweets': tweets}
    return tweets_dict
Пример #10
0
def _generate_schema_from_example_and_description(input, description):
    '''
    With an example input, a schema is automatically generated that conforms
    to the example in json-schema.org. The description given by the users
    is then added to the schema.
    '''
    s = _genson.Schema()
    s.add_object(input)
    input_schema = s.to_dict()

    if description is not None:
        if 'properties' in input_schema:
            # Case for input = {'x':1}, input_description='not a dict'
            if not isinstance(description, dict):
                msg = f'{input} and {description} do not match'
                logger.error(msg)
                raise Exception(msg)

            for key in description:
                # Case for input = {'x':1},
                # input_description={'x':'x value', 'y':'y value'}
                if key not in input_schema['properties']:
                    msg = f'{key} not found in {input}'
                    logger.error(msg)
                    raise Exception(msg)
                else:
                    input_schema['properties'][key][
                        'description'] = description[key]
        else:
            if isinstance(description, dict):
                raise Exception(f'{input} and {description} do not match')
            else:
                input_schema['description'] = description

    try:
        # This should not fail unless there are bugs with either genson or
        # jsonschema.
        _validate(input, input_schema)
    except Exception as e:
        logger.error(f'Internal error validating schema: {str(e)}')
        raise

    return input_schema
Пример #11
0
    def parse_body(self, keep_list_item):
        self.decoded_body = util.decode_body(self.headers, self.body)

        # noinspection PyBroadException
        try:
            obj = json.loads(self.decoded_body)
        except:
            self.simplified_body = self.decoded_body
            return

        # 简化数据,只保留部分数据
        util.simplify(obj, keep_list_item)
        self.simplified_body = json.dumps(obj,
                                          ensure_ascii=False,
                                          sort_keys=True,
                                          indent=2)

        # 保存数据 Schema 以供比较
        self.schema = genson.Schema()
        self.schema.add_object(obj)
Пример #12
0
    def generate_models(self, files, generate_schema: bool = False, cmake: bool = False):

        loader = JmgLoader()

        for file_path in (f for fileGlob in files for f in glob.glob(fileGlob)):

            if generate_schema:
                #
                # Generate the a JSON Schema file from the given JSON file
                #
                schema = genson.Schema()

                with open(file_path) as f:
                    json_sample = json.load(f)
                    schema.add_object(json_sample)

                schema_base_name = os.path.basename(file_path)
                schema_root_name, schema_ext = os.path.splitext(schema_base_name)
                schema_file_path = os.path.join(self.outdir, schema_root_name + '.schema' + schema_ext)

                with open(schema_file_path, 'w') as s_f:
                    s_f.write(schema.to_json())

            else:
                schema_file_path = file_path

            if self.root_name:
                scope = [self.root_name]
            else:
                base_name = os.path.basename(schema_file_path)
                base_uri = os.path.splitext(base_name)[0]
                base_uri = base_uri.replace('.schema', '')
                scope = [base_uri]

            with open(schema_file_path) as jsonFile:

                # root_schema = json.load(jsonFile)
                # base_uri = 'file://' + os.path.split(os.path.realpath(f))[0]
                base_uri = 'file://' + os.path.realpath(schema_file_path)
                root_schema = jsonref.load(jsonFile, base_uri=base_uri, jsonschema=True, loader=loader)

                if self.validate:
                    # TODO: Add exception handling
                    try:
                        Draft4Validator.check_schema(root_schema)
                    except SchemaError as e:
                        print(e)

                assert isinstance(root_schema, dict)

                if JsonSchema2Model.SCHEMA_URI not in root_schema:
                    root_schema[JsonSchema2Model.SCHEMA_URI] = schema_file_path

                self.create_model(root_schema, scope)
        
        if not cmake:
            self.render_models()
            self.copy_static_files()
            if self.include_dependencies:
                self.copy_dependencies()
        else:
            self.print_for_cmake()
Пример #13
0
def infer_schema(json_objects):
    s = genson.Schema()
    s.add_schema({"type": "object", "properties": {}})
    for o in json_objects:
        s.add_object(o)
    return s.to_dict()
Пример #14
0
    if definitions:
        print('The Definition - {}'.format(definitions[0].text.strip()))
    else:
        print('Definition not found on web:(\n')


# This connects to my twitter account and brings the tweets

t = Twython('92UbSi1YbvPI5umlJVV6xwL62',
            'EMM846L8yfj3hP5T14mdxHKDII1rb9AV7whUUjwVy4X4ZTcx0c',
            '2257672447-BSkO8JVaFWzrPebefP3bCrY09AAh11Tq1yaX66c',
            'BrgI4HvNpqs1tg7u1q0JBYuBH5uePGHW5i1GjRrhZxo8U')

tl = t.get_home_timeline()
json.dumps(tl)
s = genson.Schema()
s.add_object(tl)
tweets = []


def timeline():
    for i in range(0, len(tl)):
        # pprint(tl[i]['text'])
        tweets.append(tl[i]['text'])
    tweets_dict = {'tweets': tweets}
    return tweets_dict


# This function is to load all the dictionary word and
# this function will return all the words.
Пример #15
0
def generateJsonObject():
    s = genson.Schema()
    s.add_schema(generateObject())
    return s.to_json()