Fix #2518. Don't fail on non-existent schemas to allow for custom schemas

This commit is contained in:
Dion Moult
2022-10-20 15:03:11 +11:00
parent d09286eeca
commit 9a0f29170a
@@ -46,14 +46,13 @@ SCHEMA_FILES = {
}, },
} }
db = None
# singleton doc database
# required so that database would be loaded only once def get_db(version):
class DocDatabase: global db
def __new__(cls): if not db:
if not hasattr(cls, "db"):
db = {ifc_version: dict() for ifc_version in SCHEMA_FILES} db = {ifc_version: dict() for ifc_version in SCHEMA_FILES}
files_missing = False
for ifc_version in SCHEMA_FILES: for ifc_version in SCHEMA_FILES:
for data_type in SCHEMA_FILES[ifc_version]: for data_type in SCHEMA_FILES[ifc_version]:
schema_path = SCHEMA_FILES[ifc_version][data_type] schema_path = SCHEMA_FILES[ifc_version][data_type]
@@ -64,43 +63,35 @@ class DocDatabase:
with open(schema_path, "r") as fi: with open(schema_path, "r") as fi:
db[ifc_version][data_type] = json.load(fi) db[ifc_version][data_type] = json.load(fi)
return db.get(version)
if files_missing:
raise Exception(
"Some schema files are missing - they contain neccessary data for DocAPI to work. \n"
"Make sure those files are present. To generate them you can run DocExtractor extract functions \n"
"but it will require corresponding Ifc docs to be in the same directory as the script."
)
cls.db = db
return cls.db
def check_version_name(version_name):
if version_name not in SCHEMA_FILES:
raise Exception(
f"Version: {version_name} is not supported. " f'Supported version: {", ".join(SCHEMA_FILES.keys())}'
)
return version_name
def get_entity_doc(version, entity): def get_entity_doc(version, entity):
version = check_version_name(version) db = get_db(version)
return DocDatabase()[version]["entities"][entity] if db:
return db["entities"].get(entity)
def get_attribute_doc(version, entity, attribute): def get_attribute_doc(version, entity, attribute):
version = check_version_name(version) db = get_db(version)
return DocDatabase()[version]["entities"][entity]["attributes"][attribute] if db:
entity = db["entities"].get(entity)
if entity:
return entity["attributes"].get(attribute)
def get_property_set_doc(version, pset): def get_property_set_doc(version, pset):
version = check_version_name(version) db = get_db(version)
return DocDatabase()[version]["properties"][pset] if db:
return db["properties"].get(pset)
def get_property_doc(version, pset, prop): def get_property_doc(version, pset, prop):
version = check_version_name(version) db = get_db(version)
return DocDatabase()[version]["properties"][pset]["properties"][prop] if db:
pset = db["properties"].get(pset)
if pset:
return pset["properties"].get(prop)
class DocExtractor: class DocExtractor: