mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-16 21:42:19 +00:00
Fix #2518. Don't fail on non-existent schemas to allow for custom schemas
This commit is contained in:
@@ -46,61 +46,52 @@ SCHEMA_FILES = {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db = None
|
||||||
# singleton doc database
|
|
||||||
# required so that database would be loaded only once
|
|
||||||
class DocDatabase:
|
|
||||||
def __new__(cls):
|
|
||||||
if not hasattr(cls, "db"):
|
|
||||||
db = {ifc_version: dict() for ifc_version in SCHEMA_FILES}
|
|
||||||
files_missing = False
|
|
||||||
for ifc_version in SCHEMA_FILES:
|
|
||||||
for data_type in SCHEMA_FILES[ifc_version]:
|
|
||||||
schema_path = SCHEMA_FILES[ifc_version][data_type]
|
|
||||||
if not schema_path.is_file():
|
|
||||||
print(f"Schema file {schema_path} wasn't found.")
|
|
||||||
files_missing = True
|
|
||||||
continue
|
|
||||||
|
|
||||||
with open(schema_path, "r") as fi:
|
|
||||||
db[ifc_version][data_type] = json.load(fi)
|
|
||||||
|
|
||||||
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):
|
def get_db(version):
|
||||||
if version_name not in SCHEMA_FILES:
|
global db
|
||||||
raise Exception(
|
if not db:
|
||||||
f"Version: {version_name} is not supported. " f'Supported version: {", ".join(SCHEMA_FILES.keys())}'
|
db = {ifc_version: dict() for ifc_version in SCHEMA_FILES}
|
||||||
)
|
for ifc_version in SCHEMA_FILES:
|
||||||
return version_name
|
for data_type in SCHEMA_FILES[ifc_version]:
|
||||||
|
schema_path = SCHEMA_FILES[ifc_version][data_type]
|
||||||
|
if not schema_path.is_file():
|
||||||
|
print(f"Schema file {schema_path} wasn't found.")
|
||||||
|
files_missing = True
|
||||||
|
continue
|
||||||
|
|
||||||
|
with open(schema_path, "r") as fi:
|
||||||
|
db[ifc_version][data_type] = json.load(fi)
|
||||||
|
return db.get(version)
|
||||||
|
|
||||||
|
|
||||||
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:
|
||||||
|
|||||||
Reference in New Issue
Block a user