0ptr/nullptr/models/base.py

78 lines
1.6 KiB
Python
Raw Normal View History

2023-06-09 20:20:46 +00:00
from copy import deepcopy
from nullptr.util import sg
2023-06-09 11:19:47 +00:00
class Base:
identifier = 'symbol'
2023-06-09 11:19:47 +00:00
symbol: str
store: object
2023-06-09 11:19:47 +00:00
def __init__(self, symbol, store):
self.store = store
self.symbol = symbol
self.define()
2023-06-09 20:20:46 +00:00
def define(self):
pass
def __hash__(self):
return hash((str(type(self)), self.symbol))
def __eq__(self, other):
return self.symbol == other.symbol and type(self) == type(other)
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)
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__']:
self.store.dirty(self)
2023-06-10 17:39:32 +00:00
super().__setattr__(name, value)
def update(self, d):
pass
2023-06-18 05:06:32 +00:00
def is_expired(self):
return False
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):
r = {}
for k,v in self.__dict__.items():
if k in ['store']:
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