2023-06-09 20:20:46 +00:00
|
|
|
from copy import deepcopy
|
2023-06-11 18:51:03 +00:00
|
|
|
from nullptr.util import sg
|
2023-06-12 08:51:01 +00:00
|
|
|
|
2023-07-10 17:25:01 +00:00
|
|
|
class Reference:
|
|
|
|
def __init__(self, typ, symbol, store):
|
|
|
|
self.typ = typ
|
|
|
|
self.symbol = symbol
|
|
|
|
self.store = store
|
2023-07-11 05:17:13 +00:00
|
|
|
|
2023-07-10 17:25:01 +00:00
|
|
|
@classmethod
|
|
|
|
def create(cls, obj):
|
|
|
|
o = cls(type(obj), obj.symbol, obj.store)
|
|
|
|
return o
|
|
|
|
|
|
|
|
def resolve(self):
|
2023-07-11 05:17:13 +00:00
|
|
|
return self.store.get(self.typ, self.symbol)
|
2023-07-10 17:25:01 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f'*REF*{self.symbol}.{self.typ.ext()}'
|
|
|
|
|
2023-06-09 11:19:47 +00:00
|
|
|
class Base:
|
2023-06-16 11:03:19 +00:00
|
|
|
identifier = 'symbol'
|
2023-06-09 11:19:47 +00:00
|
|
|
|
|
|
|
def __init__(self, symbol, store):
|
2023-06-20 19:46:05 +00:00
|
|
|
self.disable_dirty = True
|
2023-07-11 15:48:51 +00:00
|
|
|
self.file_offset = None
|
2023-06-09 11:19:47 +00:00
|
|
|
self.store = store
|
2023-06-12 08:51:01 +00:00
|
|
|
self.symbol = symbol
|
2023-06-18 17:15:51 +00:00
|
|
|
self.define()
|
2023-06-20 19:46:05 +00:00
|
|
|
self.disable_dirty = False
|
|
|
|
|
2023-07-10 17:25:01 +00:00
|
|
|
@classmethod
|
|
|
|
def ext(cls):
|
|
|
|
raise NotImplementedError('no ext')
|
|
|
|
|
2023-06-18 17:15:51 +00:00
|
|
|
def define(self):
|
|
|
|
pass
|
|
|
|
|
2023-06-12 08:51:01 +00:00
|
|
|
def __hash__(self):
|
2023-06-14 13:37:48 +00:00
|
|
|
return hash((str(type(self)), self.symbol))
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
2023-07-11 20:09:57 +00:00
|
|
|
return type(self) == type(other) and self.symbol == other.symbol
|
2023-07-12 20:26:25 +00:00
|
|
|
|
|
|
|
def get_system(self):
|
|
|
|
parts = self.symbol.split('-')
|
|
|
|
system_str = f'{parts[0]}-{parts[1]}'
|
|
|
|
system = self.store.get('System', system_str, create=True)
|
|
|
|
return system
|
2023-06-12 08:51:01 +00:00
|
|
|
|
2023-06-16 11:03:19 +00:00
|
|
|
def seta(self, attr, d, name=None, interp=None):
|
2023-06-10 18:49:50 +00:00
|
|
|
if name is None:
|
|
|
|
name = attr
|
|
|
|
val = sg(d, name)
|
|
|
|
if val is not None:
|
2023-06-16 11:03:19 +00:00
|
|
|
if interp is not None:
|
|
|
|
val = interp(val)
|
2023-06-10 18:49:50 +00:00
|
|
|
setattr(self, attr, val)
|
2023-06-11 18:51:03 +00:00
|
|
|
|
2023-07-12 20:26:25 +00:00
|
|
|
def setlst(self, attr, d, name, member, interp=None):
|
2023-06-11 18:51:03 +00:00
|
|
|
val = sg(d, name)
|
|
|
|
if val is not None:
|
2023-07-12 20:26:25 +00:00
|
|
|
lst = []
|
|
|
|
for x in val:
|
|
|
|
val = sg(x, member)
|
|
|
|
if interp is not None:
|
|
|
|
val = interp(val)
|
|
|
|
lst.append(val)
|
2023-06-11 18:51:03 +00:00
|
|
|
setattr(self, attr, lst)
|
|
|
|
|
2023-06-10 17:39:32 +00:00
|
|
|
def __setattr__(self, name, value):
|
2023-07-10 17:25:01 +00:00
|
|
|
if name not in ['symbol','store','disable_dirty', 'file_offset'] and not self.disable_dirty:
|
2023-06-12 08:51:01 +00:00
|
|
|
self.store.dirty(self)
|
2023-07-10 17:25:01 +00:00
|
|
|
if issubclass(type(value), Base):
|
|
|
|
value = Reference.create(value)
|
2023-06-10 17:39:32 +00:00
|
|
|
super().__setattr__(name, value)
|
|
|
|
|
2023-07-10 17:25:01 +00:00
|
|
|
def __getattribute__(self, nm):
|
|
|
|
val = super().__getattribute__(nm)
|
|
|
|
if type(val) == Reference:
|
|
|
|
val = val.resolve()
|
|
|
|
return val
|
|
|
|
|
2023-06-10 17:39:32 +00:00
|
|
|
def update(self, d):
|
|
|
|
pass
|
2023-06-16 11:03:19 +00:00
|
|
|
|
2023-06-18 05:06:32 +00:00
|
|
|
def is_expired(self):
|
|
|
|
return False
|
|
|
|
|
2023-06-16 11:03:19 +00:00
|
|
|
def load(self, d):
|
2023-06-18 18:42:30 +00:00
|
|
|
self.disable_dirty = True
|
|
|
|
self.__dict__.update(d)
|
|
|
|
self.disable_dirty = False
|
2023-06-09 11:19:47 +00:00
|
|
|
|
|
|
|
def type(self):
|
|
|
|
return self.__class__.__name__
|
|
|
|
|
|
|
|
def __str__(self):
|
2023-06-10 17:39:32 +00:00
|
|
|
return self.f()
|
|
|
|
|
2023-07-12 20:26:25 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return self.f()
|
|
|
|
|
2023-06-10 17:39:32 +00:00
|
|
|
def f(self, detail=1):
|
|
|
|
r = self.symbol
|
|
|
|
if detail > 1:
|
|
|
|
r += '.' + self.ext()
|
|
|
|
return r
|