def main(): parser = ArgumentParser() parser.add_argument('--csv_file', '-c', dest='csv_file', required=True, metavar='FILE', help='CSV containing region metadata', type=lambda x: valid_file(parser, x)) parser.add_argument('--api_version', '-a', dest='api_version', required=False, default=strftime('%Y-%m-%d'), help='Version of API') parser.add_argument('--basedir', '-b', dest='base_dir', required=True, help='Base directory to write feeds out to') args = parser.parse_args() csv = CSVHandler(args.csv_file) csv.restructure() version_directory = os.path.join(args.base_dir, args.api_version) latest_directory = os.path.join(args.base_dir, 'latest') schema = SchemaBuilder() schema.add_object(csv.data) schema.to_schema() for directory in (version_directory, latest_directory): if not os.path.exists(directory): os.makedirs(directory) with open(os.path.join(directory, 'regions.json'), 'w') as f: f.write(csv.to_json()) with open(os.path.join(directory, 'regions.schema.json'), 'w') as f: f.write(schema.to_json()) generate_version_list(args.base_dir)
def _make_schema(self, filename): self.load(filename) from genson import SchemaBuilder sc = SchemaBuilder() sc.add_object(self) # filename = filename[:filename.rfind('.')] return sc.to_json(indent=4) + '\n'
def __create_shema(attnames, atttypes, catname): b1 = SchemaBuilder() shema_name = int( hashlib.sha1(catname.encode('utf-8')).hexdigest(), 16) % (10**8) for i, name in enumerate(attnames): b1.add_schema({ "type": "object", "properties": { "{0}".format(name): { "type": "{0}".format(atttypes[i]) } } }) b1.add_schema({"required": attnames}) shema = str(b1.to_json()) # with codecs.open('attSchemas/{0}.json'.format(shema_name),'w', 'utf-8') as fp: # fp.write(json.dumps(shema, ensure_ascii=False)) with open('attSchemas/{0}.json'.format(shema_name), 'w', encoding='utf8') as f: f.write(shema) # b1.to_json('attSchemas/{0}.json'.format(shema_name)) return '{0}.json'.format(shema_name)
def generate_json_schema(self, data): ''' Uses the genson package to introspect json type data and generate the skeleton of a JSON Schema document (Draft 6) for further documentation. :param data: must be one of the following - python dictionary object, python list of dictionaries, json string that can be loaded to a dictionary or list of dictionaries :return: json string containing the generated json schema skeleton ''' if isinstance(data, str): data = json.loads(data) if isinstance(data, dict): data = [data] if len(data) == 0: return "Error: your list of objects (dictionaries) must contain at least one object to process" if not isinstance(data[0], dict): return "Error: your list must contain a dictionary type object" try: builder = SchemaBuilder() builder.add_schema({"type": "object", "properties": {}}) for r in data: for k, v in r.items(): builder.add_object({k: v}) except Exception as e: return f"Error: {e}" return builder.to_json()
def main(indianApiUrlList, globalUrlList, deepsetUrlList): for i in globalUrlList: resp = requests.get(i[1]) data = resp.json() builder = SchemaBuilder() # builder.add_object(data) # latestSchema = json.dumps(builder.to_json()) # apiStatus = statusCheck(resp,i) # print(apiStatus) builder.add_object(data) latestSchema = builder.to_json() schema_update = schemaUpdate(data, latestSchema, i) print(schema_update) # api_check = schemaUpdate(data,i) # print(api_check) print("\n\n") for i in deepsetUrlList: resp = requests.get(i[1]) data = resp.json() builder = SchemaBuilder() # builder.add_object(data) # latestSchema = json.dumps(builder.to_json()) # apiStatus = statusCheck(resp,i) # print(apiStatus)builder = SchemaBuilder() builder.add_object(data) latestSchema = builder.to_json() schema_update = schemaUpdate(data, latestSchema, i) print(schema_update) # api_check = schemaUpdate(data,i) # print(api_check) print("\n\n") for i in indianApiUrlList: resp = requests.get(i[1]) data = resp.json() builder = SchemaBuilder() # builder.add_object(data) # latestSchema = json.dumps(builder.to_json()) # apiStatus = statusCheck(resp,i) # print(apiStatus) builder.add_object(data) latestSchema = builder.to_json() schema_update = schemaUpdate(data, latestSchema, i) print(schema_update) # api_check = schemaUpdate(data,i) # print(api_check) print("\n\n")
def main(): print(str(sys.argv)) mesh = sys.argv[2] dataLocation = os.path.join(sys.argv[1], sys.argv[3]) material = os.path.join(sys.argv[1], "materials/" + sys.argv[4]) builder = SchemaBuilder() builder.add_schema({ "frame_time": float(sys.argv[5]), "frame_steps": int(sys.argv[6]), "end_time": int(sys.argv[7]), "cloths": [{ "mesh": mesh, "materials": [{ "data": material, "thicken": 2 }], "remeshing": { "refine_angle": 0.3, "refine_compression": 0.005, "refine_velocity": 0.5, "size": [10e-3, 200e-3], "aspect_min": 0.2 } }], "handles": [{ "end_time": 7 }], "motions": [[{ "time": 0, "transform": { "scale": 1.0 }, }]], "obstacles": [{ "mesh": dataLocation + "/body.obj", "motion": 0 }], "gravity": [0, 0, -9.8], "disable": ["popfilter", "remeshing"], "magic": { "repulsion_thickness": 5e-3, "collision_stiffness": 1e6 } }) builder.to_schema() file = open(os.path.join(str(dataLocation), 'conf.json'), 'w') file.write(builder.to_json(indent=1)) file.close()
def generate_json_schema(self, data, return_type="json", build_definitions=False): ''' Uses the genson package to introspect json type data and generate the skeleton of a JSON Schema document (Draft 6) for further documentation. :param data: must be one of the following - python dictionary object, python list of dictionaries, json string that can be loaded to a dictionary or list of dictionaries :param return_type: JSON string or defaults to dictionary :param build_definitions: Run a process to prompt for title and description on schema and properties :return: json string containing the generated json schema skeleton ''' if isinstance(data, str): data = json.loads(data) if isinstance(data, dict): data = [data] if len(data) == 0: return "Error: your list of objects (dictionaries) must contain at least one object to process" if not isinstance(data[0], dict): return "Error: your list must contain a dictionary type object" try: builder = SchemaBuilder() builder.add_schema({"type": "object", "properties": {}}) for r in data: for k, v in r.items(): builder.add_object({k: v}) except Exception as e: return f"Error: {e}" schema = json.loads(builder.to_json()) if build_definitions: schema["title"] = input("schema title: ") schema["description"] = input("schema description: ") new_props = dict() for item in schema["properties"].items(): new_props[item[0]] = dict() new_props[item[0]]["type"] = item[1]["type"] new_props[item[0]]["title"] = input(f"title for {item[0]}: ") new_props[item[0]]["description"] = input( f"description for {item[0]}: ") schema["properties"] = new_props if return_type == "json": schema = json.dumps(schema) return schema
def main(indianApiUrlList, globalUrlList, deepsetUrlList): questionFormat = data = '{"questions":["string"],"filters":{"additionalProp1":"string","additionalProp2":"string","additionalProp3":"string"},"top_k_reader":0,"top_k_retriever":0}' headers = { 'accept': 'application/json', 'Content-Type': 'application/json', } for i in globalUrlList: resp = requests.get(i[1]) data = resp.json() builder = SchemaBuilder() builder.add_object(data) latestSchema = builder.to_json() schema_update = schemaUpdate(data, latestSchema, i) print(schema_update) print( "----------------------------------------------------------------") for i in deepsetUrlList: resp = requests.post(i[1], headers=headers, data=questionFormat) data = resp.json() builder = SchemaBuilder() builder.add_object(data) latestSchema = builder.to_json() schema_update = schemaUpdate(data, latestSchema, i) print(schema_update) print( "----------------------------------------------------------------") for i in indianApiUrlList: resp = requests.get(i[1]) data = resp.json() builder = SchemaBuilder() builder.add_object(data) latestSchema = builder.to_json() schema_update = schemaUpdate(data, latestSchema, i) print(schema_update) print( "----------------------------------------------------------------")
def generate(): host = os.environ['UNIFI_HOST'] port = int(os.environ['UNIFI_PORT']) username = os.environ['UNIFI_USER'] password = os.environ['UNIFI_PASS'] site = os.environ['UNIFI_SITE'] jinja = Environment(loader=PackageLoader('generate', 'templates')) template = jinja.get_template('client.py') with open('uniman' + os.path.sep + 'client.py', 'w') as client_py: client_py.write(template.render(endpoints=Endpoints)) payloads = { Endpoints.LOGIN.name: {'username': username, 'password': password} } session = Session() for endpoint in Endpoints: request = endpoint.value.to_request(host, port, site, payload=payloads.get(endpoint.name, {})) response = session.send(session.prepare_request(request), verify=False) builder = SchemaBuilder() builder.add_object(response.json()) builder.add_schema({'title': endpoint.name.title().replace('_', '')}) builder.add_schema({'required': []}) builder.add_schema({'properties': {'data': {'items': {'required': []}}}}) schema = json.loads(builder.to_json()) schema_path = 'uniman' + os.path.sep + 'schema' + os.path.sep + endpoint.name.lower() + '.schema.json' schema_file = open(schema_path, 'w') schema_file.write(json.dumps(schema, indent=4)) schema_file.close() json_schema = json_ref_dict.materialize( RefDict.from_uri(schema_path), context_labeller=statham.titles.title_labeller() ) parsed_schema = statham.schema.parser.parse(json_schema) python_class = serialize_python(*parsed_schema) python_class_file = open('uniman' + os.path.sep + 'model' + os.path.sep + endpoint.name.lower() + '.py', 'w') python_class_file.write(python_class) python_class_file.close()
def draft_schema_from(self, path, save_path=None): """ Create a draft jsonschema from a json file of data. :param path: path to the json file. :param save_path: path where to save the generated schema. :return: the generated schema. """ file = self.load_json(path) builder = SchemaBuilder() builder.add_schema({"type": "object", "properties": {}}) builder.add_object(file) draft_schema = builder.to_json() if save_path is not None: with open(save_path, "w") as outfile: outfile.write(draft_schema) return draft_schema
def main(d,p,o): dir = '{}'.format(d) print('\nLooking for files matching "{}" in directory "{}" to generate schema file "{}"'.format(p, dir, o)) matching_files = [f for f in os.listdir(dir) if re.match(p,f) is not None] print('\nGenerating schema with following files: ') for f in matching_files: print('{}\{}'.format(dir, f)) builder = SchemaBuilder() for file in matching_files: with open('{}/{}'.format(dir, file)) as curFile: builder.add_object(json.loads(curFile.read())) with open(o, 'w+') as curFile: curFile.write(str(builder.to_json())) click.echo('\nCreated schema file {}.'.format(o))
'targets': targets, 'conservations': conservations} ############################################################################### # RUN level definition ############################################################################### run = {'id': _string, 'ip': sample, 'control': sample, 'ip_results': ip_results} ############################################################################### # END RUN level definition ############################################################################### builder = SchemaBuilder(schema_uri="http://json-schema.org/draft-07/schema#") builder.add_object(run) print(builder.to_json(indent=3)) js = builder.to_json(indent=3) with open("chips_schema.json", "w") as outfile: outfile.write(js)
def load_jsons(path): jj = [] if os.path.isdir(path): files = os.listdir(path) for f in files: j = json.load(open("%s/%s" % (path, f))) jj.append(j) else: j = json.load(open(path)) jj.append(j) return jj if __name__ == "__main__": if len(sys.argv) < 2: print("args <path_json_or_dir>") exit() path = sys.argv[1] builder = SchemaBuilder() builder.add_schema({"type": "object", "properties": {}}) jj = load_jsons(path) for j in jj: #print(jj) builder.add_object(j) #print(builder.to_schema()) print(builder.to_json(indent=2))
res_demo = { 'id': '20210501', 'age': 18, 'address': ['hefei', 'PDS', 1], 'tuple': ('a'), 'n': '', 'no': None } #添加要转换的对象 builder.add_object(res_demo) #生成schema模板 -dict j_schema = builder.to_schema() print(type(j_schema)) #生成json缩进显示 -str j_json = builder.to_json(indent=2) print(type(j_json)) #2、断言 from jsonschema import validate #1)使用 schema 断言 data = { 'id': 'abc', 'age': 18, 'address': ['beijing'], 'tuple': ('b'), 'n': '', 'no': None }
from jsonschema import validate from genson import SchemaBuilder import json import requests resp = requests.get('https://covid-backend.deepset.ai/models/1/feedback') data = resp.json() builder = SchemaBuilder() builder.add_object(data) schema = builder.to_json() # with open('deepSetApiFeedbackCurrentSchema.json','w+') as f: # f.write(schema) # print(builder.to_json(indent=2)) with open('deepSetApiFeedbackCurrentSchema.json','r') as f: currentSchema = json.loads(f.read()) try: validate(instance=data,schema=currentSchema) print("JSON schema is up to date") except: print("JSON schema is not up to date") # Globals # indianApiUrl = ['IndianApi','https://api.covid19india.org/data.json','IndianApiCurrentSchema'] # globalApiUrl = ['globalApi','https://corona.lmao.ninja/all','globalApiCurrentSchema'] # globalApiCountriesUrl = ['globalApi --> Countries','https://corona.lmao.ninja/countries','globalApiCountriesCurrentSchema'] # globalApiUsaUrl = ['globalApi --> USA','https://corona.lmao.ninja/countries/USA','globalApiUSACurrentSchema'] # deepsetApiFaqUrl = ['DeepsetApi --> FAQ','https://covid-backend.deepset.ai/models/1/faq-qa','deepSetApiFaqCurrentSchema'] # deepsetApiFeedbackUrl = ['DeepSetApi --> Feedback','https://covid-backend.deepset.ai/models/1/feedback','deepSetApiFeedbackCurrentSchema'] # indianApiUrlList = [indianApiUrl] # globalUrlList = [globalApiUrl,globalApiCountriesUrl,globalApiUsaUrl] # deepsetUrlList =[deepsetApiFaqUrl,deepsetApiFeedbackUrl]
"test6": "Hello World" } } # test_config.json 생성 with open(TEST_CONFIG_FILE_JSON, 'w') as f: f.write(json.dumps(TEST_CONFIG, indent=4, ensure_ascii=False)) # test_config.yaml 생성 with open(TEST_CONFIG_FILE_YAML, 'w') as f: f.write(yaml.dump(TEST_CONFIG, indent=4, allow_unicode=True)) # test_schema 생성 sc = SchemaBuilder() sc.add_object(TEST_CONFIG) TEST_SCHEMA = json.loads(sc.to_json(indent=4)) # test_schema.json 생성 with open(TEST_SCHEMA_FILE_JSON, 'w') as f: f.write(json.dumps(TEST_SCHEMA, indent=4)) # test_schema.yaml 생성 with open(TEST_SCHEMA_FILE_YAML, 'w') as f: f.write(yaml.dump(TEST_SCHEMA, indent=4)) TEST_WRONG_CONFIG = { "name": 123, "cellphone": "010-1345-7764", "address": "이상국 행복리 234", "age": "33", "test_1": [1, 2, 3],