0ptr/nullptr/models/base.py

109 lines
2.5 KiB
Python
Raw Normal View History

2023-06-09 20:20:46 +00:00
from copy import deepcopy
from nullptr.util import sg
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-10 17:25:01 +00:00
@classmethod
def create(cls, obj):
o = cls(type(obj), obj.symbol, obj.store)
return o
def resolve(self):
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:
identifier = 'symbol'
2023-06-09 11:19:47 +00:00
def __init__(self, symbol, store):
self.disable_dirty = True
self.file_offset = None
2023-06-09 11:19:47 +00:00
self.store = store
self.symbol = symbol
self.define()
self.disable_dirty = False
2023-07-10 17:25:01 +00:00
@classmethod
def ext(cls):
raise NotImplementedError('no ext')
def define(self):
pass
def __hash__(self):
return hash((str(type(self)), self.symbol))
def __eq__(self, other):
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
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:
if interp is not None:
val = interp(val)
2023-06-10 18:49:50 +00:00
setattr(self, attr, val)
2023-07-12 20:26:25 +00:00
def setlst(self, attr, d, name, member, interp=None):
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)
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:
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-18 05:06:32 +00:00
def is_expired(self):
return False
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