Source code for eppy3000.epschema
# Copyright (c) 2018-2019 Santosh Philip
# =======================================================================
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# =======================================================================
"""class for the epschema file"""
import json
from munch import Munch
[docs]def read_epschema_asmunch(fhandle):
"""read the epschema json as a munch"""
try:
epjs = json.load(fhandle)
as_munch = EPSchemaMunch.fromDict(epjs)
except AttributeError as e:
try:
fhandle = open(fhandle, "r")
epjs = json.load(fhandle)
as_munch = EPSchemaMunch.fromDict(epjs)
except TypeError as e:
if isinstance(fhandle, EPSchemaMunch):
return fhandle
else:
raise TypeError(
f"expected str, bytes, os.PathLike object or Munch, not {type(fhandle)}"
) # noqa: E501
return as_munch
[docs]class EPSchemaMunch(Munch):
"""Munch subcalssed to for the EPSchema json"""
def __init__(self, *args, **kwargs):
super(EPSchemaMunch, self).__init__(*args, **kwargs)
[docs] def fieldnames(self):
"""field names of the EPSchema object"""
return [key for key in self.keys()]
[docs] def fieldnames_list(self):
"""fieldnames that contain lists"""
pass
[docs] def fieldproperty(self, fieldname):
"""field names of the EPSchema object"""
return self[fieldname]
[docs]class EPSchema(object):
"""hold the data from the json epschema file"""
def __init__(self, epschemaname):
super(EPSchema, self).__init__()
self.epschemaname = epschemaname
self.read()
[docs] def read(self):
"""read the json file"""
def prop_in_patternProp(val):
"""return the property in patternProperty"""
# assume that val['patternProperties'] has a single key, val
# key is either ".*" or "^.*\\S.*$"
for key in val["patternProperties"].keys():
return val["patternProperties"][key]
self.epschema = read_epschema_asmunch(self.epschemaname)
self.version = self.epschema["epJSON_schema_version"]
self.required = self.epschema["required"]
# self.epschemaobjects = {key: val['patternProperties']['.*']['properties']
# for key, val in self.epschema['properties'].items()}
# the above line stopped working in E+ V9.3. ".*" stopped being the only key
# workaround is below
self.epschemaobjects = {
key: prop_in_patternProp(val)["properties"]
for key, val in self.epschema["properties"].items()
}