0ptr/nullptr/models/base.py
2023-06-20 21:46:05 +02:00

82 lines
1.7 KiB
Python

from copy import deepcopy
from nullptr.util import sg
class Base:
identifier = 'symbol'
symbol: str
store: object
def __init__(self, symbol, store):
self.disable_dirty = True
self.store = store
self.symbol = symbol
self.define()
self.disable_dirty = False
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):
if name is None:
name = attr
val = sg(d, name)
if val is not None:
if interp is not None:
val = interp(val)
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)
def __setattr__(self, name, value):
if name not in ['symbol','store','disable_dirty'] and not self.disable_dirty:
self.store.dirty(self)
super().__setattr__(name, value)
def update(self, d):
pass
def is_expired(self):
return False
def load(self, d):
self.disable_dirty = True
self.__dict__.update(d)
self.disable_dirty = False
def dict(self):
r = {}
for k,v in self.__dict__.items():
if k in ['store']:
continue
r[k] = deepcopy(v)
return r
def path(self):
raise NotImplementedError('path')
@classmethod
def ext(self):
raise NotImplementedError('extension')
def type(self):
return self.__class__.__name__
def __str__(self):
return self.f()
def f(self, detail=1):
r = self.symbol
if detail > 1:
r += '.' + self.ext()
return r