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-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
|
|
|
symbol: str
|
2023-06-12 08:51:01 +00:00
|
|
|
store: object
|
2023-06-09 11:19:47 +00:00
|
|
|
|
|
|
|
def __init__(self, symbol, store):
|
|
|
|
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-09 20:20:46 +00:00
|
|
|
|
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):
|
|
|
|
return self.symbol == other.symbol and type(self) == type(other)
|
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
|
|
|
|
|
|
|
def setlst(self, attr, d, name, member):
|
|
|
|
val = sg(d, name)
|
|
|
|
if val is not None:
|
|
|
|
lst = [sg(x, member) for x in val]
|
|
|
|
setattr(self, attr, lst)
|
|
|
|
|
2023-06-10 17:39:32 +00:00
|
|
|
def __setattr__(self, name, value):
|
2023-06-13 05:15:02 +00:00
|
|
|
if name not in ['symbol','store','__dict__']:
|
2023-06-12 08:51:01 +00:00
|
|
|
self.store.dirty(self)
|
2023-06-10 17:39:32 +00:00
|
|
|
super().__setattr__(name, value)
|
|
|
|
|
|
|
|
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):
|
|
|
|
self.__dict__ = d
|
2023-06-10 17:39:32 +00:00
|
|
|
|
2023-06-09 20:20:46 +00:00
|
|
|
def dict(self):
|
2023-06-12 08:51:01 +00:00
|
|
|
r = {}
|
|
|
|
for k,v in self.__dict__.items():
|
2023-06-13 20:29:04 +00:00
|
|
|
if k in ['store']:
|
2023-06-12 08:51:01 +00:00
|
|
|
continue
|
|
|
|
r[k] = deepcopy(v)
|
2023-06-09 20:20:46 +00:00
|
|
|
return r
|
2023-06-09 11:19:47 +00:00
|
|
|
|
|
|
|
def path(self):
|
|
|
|
raise NotImplementedError('path')
|
2023-06-10 17:39:32 +00:00
|
|
|
|
|
|
|
@classmethod
|
2023-06-09 11:19:47 +00:00
|
|
|
def ext(self):
|
|
|
|
raise NotImplementedError('extension')
|
|
|
|
|
|
|
|
def type(self):
|
|
|
|
return self.__class__.__name__
|
|
|
|
|
|
|
|
def __str__(self):
|
2023-06-10 17:39:32 +00:00
|
|
|
return self.f()
|
|
|
|
|
|
|
|
def f(self, detail=1):
|
|
|
|
r = self.symbol
|
|
|
|
if detail > 1:
|
|
|
|
r += '.' + self.ext()
|
|
|
|
return r
|