2019-07-01 11:45:02 +02:00
|
|
|
from datetime import datetime
|
2019-07-01 13:08:48 +02:00
|
|
|
from .elements import SRef
|
|
|
|
from .reader import ProgressGetter
|
2019-07-01 11:45:02 +02:00
|
|
|
|
|
|
|
class Library(object):
|
2019-07-02 14:47:36 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.version = 0
|
|
|
|
self.name = "NONAME"
|
2019-07-01 11:45:02 +02:00
|
|
|
|
2019-07-02 14:47:36 +02:00
|
|
|
self.last_access = datetime.now()
|
|
|
|
self.last_mod = datetime.now()
|
2019-07-01 11:45:02 +02:00
|
|
|
|
2019-07-02 14:47:36 +02:00
|
|
|
# unit setup
|
|
|
|
self.units_per_dbunit = 1
|
|
|
|
self.meters_per_unit = 1
|
2019-07-01 11:45:02 +02:00
|
|
|
|
2019-07-02 14:47:36 +02:00
|
|
|
self.structures = {}
|
2019-07-01 13:08:48 +02:00
|
|
|
|
|
|
|
class LinkError(Exception):
|
|
|
|
element = None
|
|
|
|
pass
|
|
|
|
|
2019-07-01 14:44:34 +02:00
|
|
|
def link_refs(self, progress_callback=None):
|
2019-07-01 13:08:48 +02:00
|
|
|
class Progress(ProgressGetter):
|
|
|
|
total = 0
|
|
|
|
current = 0
|
|
|
|
|
|
|
|
def __init__(self, lib):
|
|
|
|
for key, value in lib.structures.items():
|
|
|
|
self.total += len(value.references)
|
|
|
|
|
|
|
|
def progress(self):
|
|
|
|
return float(self.current) / self.total
|
|
|
|
|
|
|
|
def inc(self):
|
|
|
|
self.current += 1
|
|
|
|
|
|
|
|
count = Progress(self)
|
|
|
|
|
|
|
|
for key, value in self.structures.items():
|
2019-07-01 14:21:45 +02:00
|
|
|
for element in value.references:
|
|
|
|
if isinstance(element.structure, str):
|
2019-07-01 13:08:48 +02:00
|
|
|
# try to resolve link
|
|
|
|
try:
|
|
|
|
ref = self.structures[element.structure]
|
|
|
|
element.structure = ref
|
|
|
|
except KeyError:
|
|
|
|
err = LinkError("dangeling sref (structure {} is not defined in library)".format(element.structure))
|
|
|
|
err.element = element
|
|
|
|
|
|
|
|
raise err
|
|
|
|
|
|
|
|
count.inc()
|
|
|
|
|
|
|
|
if progress_callback:
|
|
|
|
progress_callback(count)
|
|
|
|
|
|
|
|
|