本文整理汇总了Python中jsonref.loads方法的典型用法代码示例。如果您正苦于以下问题:Python jsonref.loads方法的具体用法?Python jsonref.loads怎么用?Python jsonref.loads使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jsonref
的用法示例。
在下文中一共展示了jsonref.loads方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _init
# 需要导入模块: import jsonref [as 别名]
# 或者: from jsonref import loads [as 别名]
def _init(cls):
if cls.package_name is None:
raise NotImplementedError("Package name not given")
if cls.spec_template_file is None:
raise NotImplementedError("Spec file not given")
loader = PackageLoader(cls.package_name, "")
env = Environment(loader=loader)
template = env.get_template(cls.spec_template_file)
tdict = yaml.safe_load(StringIO(template.render()))
tdict = jsonref.loads(json.dumps(tdict))
# TODO - Check if keys are present
cls.provider_spec = tdict["components"]["schemas"]["provider_spec"]
cls.Validator = StrictDraft7Validator(cls.provider_spec)
示例2: fetch_schema_cityobjects
# 需要导入模块: import jsonref [as 别名]
# 或者: from jsonref import loads [as 别名]
def fetch_schema_cityobjects(self, folder_schemas=None):
if folder_schemas is None:
#-- fetch proper schema from the stored ones
tmp = resource_listdir(__name__, '/schemas/')
tmp.sort()
v = tmp[-1]
try:
schema = resource_filename(__name__, '/schemas/%s/cityjson.schema.json' % (v))
except:
return (False, None)
else:
schema = os.path.join(folder_schemas, 'cityjson.schema.json')
abs_path = os.path.abspath(os.path.dirname(schema))
sco_path = abs_path + '/cityobjects.schema.json'
#-- because Windows uses \ and not /
if platform == "darwin" or platform == "linux" or platform == "linux2":
base_uri = 'file://{}/'.format(abs_path)
else:
base_uri = 'file:///{}/'.format(abs_path.replace('\\', '/'))
jsco = jsonref.loads(open(sco_path).read(), jsonschema=True, base_uri=base_uri)
# jsco = json.loads(open(sco_path).read())
return (True, jsco)
示例3: _load_all_schemas
# 需要导入模块: import jsonref [as 别名]
# 或者: from jsonref import loads [as 别名]
def _load_all_schemas(schema_file="main.yaml.jinja2"):
loader = PackageLoader(__name__, "schemas")
env = Environment(loader=loader)
template = env.get_template(schema_file)
tdict = yaml.safe_load(StringIO(template.render()))
# Check if all references are resolved
tdict = jsonref.loads(json.dumps(tdict))
# print(json.dumps(tdict, cls=EntityJSONEncoder, indent=4, separators=(",", ": ")))
schemas = tdict["components"]["schemas"]
return schemas
示例4: read
# 需要导入模块: import jsonref [as 别名]
# 或者: from jsonref import loads [as 别名]
def read(self, file, ignore_duplicate_keys=False):
if ignore_duplicate_keys == True:
self.j = json.loads(file.read())
else:
try:
self.j = json.loads(file.read(), object_pairs_hook=validation.dict_raise_on_duplicates)
except ValueError as err:
raise ValueError(err)
#-- a CityJSON file?
if "type" in self.j and self.j["type"] == "CityJSON":
pass
else:
self.j = {}
raise ValueError("Not a CityJSON file")
示例5: fetch_schema
# 需要导入模块: import jsonref [as 别名]
# 或者: from jsonref import loads [as 别名]
def fetch_schema(self, folder_schemas=None):
v = "-1"
if folder_schemas is None:
#-- fetch latest from x.y version (x.y.z)
tmp = resource_listdir(__name__, '/schemas/')
tmp.sort()
v = tmp[-1]
try:
schema = resource_filename(__name__, '/schemas/%s/cityjson.schema.json' % (v))
except:
return (False, None, '')
else:
schema = os.path.join(folder_schemas, 'cityjson.schema.json')
#-- open the schema
try:
fins = open(schema)
except:
return (False, None, '')
abs_path = os.path.abspath(os.path.dirname(schema))
#-- because Windows uses \ and not /
if platform == "darwin" or platform == "linux" or platform == "linux2":
base_uri = 'file://{}/'.format(abs_path)
else:
base_uri = 'file:///{}/'.format(abs_path.replace('\\', '/'))
js = jsonref.loads(fins.read(), jsonschema=True, base_uri=base_uri)
if v == "-1":
v = schema
return (True, js, v)
示例6: process_bundles
# 需要导入模块: import jsonref [as 别名]
# 或者: from jsonref import loads [as 别名]
def process_bundles(source_dir=None, target_dir=None):
bundles = {}
source_dir = source_dir
bundle_dir = os.path.join(source_dir, "bundles")
with open("./schemas/anchore-engine-api.yaml", 'r') as FH:
anchore_engine_api_swagger = jsonref.loads(json.dumps(yaml.safe_load(FH)))
bundleschema = anchore_engine_api_swagger['definitions']['PolicyBundle']
if os.path.isdir(bundle_dir):
for bundlefile in os.listdir(bundle_dir):
patt = re.match("(.*)\.json$", bundlefile)
if patt:
bundlename = patt.group(1)
thefile = os.path.join(bundle_dir, bundlefile)
try:
print ("\tOpening bundle {}...".format(thefile), end='')
with open(thefile, 'r') as FH:
print ("done!")
buf = FH.read()
digest = "{}:{}".format("sha256", hashlib.sha256(buf.encode('utf8')).hexdigest())
bundle = json.loads(buf)
print ("\tValidating bundle {}...".format(thefile), end='')
jsonschema.validate(bundle, bundleschema)
print ("done!")
print ("\tProcessing bundle {}...".format(thefile), end='')
bundle_record = {
'location': '/'.join(['bundles', bundlename, "{}.json".format(digest)]),
'type': 'bundle',
'name': bundlename,#bundle.get('name', 'N/A'),
'description': bundle.get('description', bundle.get('comment', "N/A")),
'digest': digest,
}
bundles[bundlename] = bundle_record
print ("done!")
except Exception as err:
print("\nERROR parsing bundle {} - exception: {}".format(bundlefile, err))
raise err
return(bundles)
示例7: populate_target
# 需要导入模块: import jsonref [as 别名]
# 或者: from jsonref import loads [as 别名]
def populate_target(source_dir=None, target_dir=None, bundles={}):
with open("./schemas/hub-index.json", 'r') as FH:
manifestschema = jsonref.loads(FH.read())
# create the manifest
manifest = {
'metadata': {
'digest': "",
'last_updated': datetime.datetime.utcnow().isoformat(),
},
'content': []
}
manifest['content'] = list(bundles.values())
digest = "{}:{}".format('sha256', hashlib.sha256(json.dumps(manifest['content'], sort_keys=True, indent=4).encode('utf8')).hexdigest())
manifest['metadata']['digest'] = digest
# validate the index manifest before proceeding
print ("\tValidating generated manifest index ...", end='')
jsonschema.validate(manifest, manifestschema)
print ("done!")
# prep the target location
if not target_dir or not source_dir:
raise Exception("empty target_dir({}) or source_dir({}) passed in".format(target_dir, source_dir))
if not os.path.isdir(target_dir):
os.mkdir(target_dir)
for subdir in ['bundles']:
dst_dir = os.path.join(target_dir, subdir)
if not os.path.exists(dst_dir):
os.mkdir(dst_dir)
# finally, copy/write all input and manifest data to the target location
for bundlename in bundles.keys():
src_file = os.path.join(source_dir, "bundles", "{}.json".format(bundlename))
dst_dir = os.path.join(target_dir, "bundles", bundlename)
if not os.path.isdir(dst_dir):
os.mkdir(dst_dir)
dst_file = os.path.join(dst_dir, "{}.json".format(bundles[bundlename]['digest']))
with open(src_file, 'r') as FH:
bundledata = json.loads(FH.read())
bundledata['name'] = bundlename
bundledata['id'] = bundlename
with open(dst_file, 'w') as OFH:
OFH.write(json.dumps(bundledata, indent=4, sort_keys=True))
#copyfile(src_file, dst_file)
manifestfile = os.path.join(target_dir, "index.json")
with open(manifestfile, 'w') as OFH:
OFH.write(json.dumps(manifest, sort_keys=True, indent=4))
src_file = os.path.join(source_dir, "static", "index.html")
dst_file = os.path.join(target_dir, "index.html")
copyfile(src_file, dst_file)
return(True)
# environment settings