def load_attr( self, cls: Any, item: dict, init_args: Optional[dict] = None, load_args: Optional[dict] = None, ) -> Any: if load_args: _kwargs = {"load_args": load_args} _load_args = load_args else: _kwargs = {} _load_args = {} if init_args: _kwargs["init_args"] = init_args if cls in [None, 0, "", [], {}, bool, b'']: if cls == b'': val = as_bytes(item) else: val = item elif cls == object: val = importer(item) elif isinstance(cls, list): if isinstance(cls[0], str): _cls = importer(cls[0]) else: _cls = cls[0] if issubclass(_cls, ImpExp) and init_args: _args = { k: v for k, v in init_args.items() if k in _cls.init_args } else: _args = {} val = [_cls(**_args).load(v, **_kwargs) for v in item] elif issubclass(cls, Message): _cls_name = list(item.keys())[0] _cls = importer(_cls_name) val = _cls().from_dict(item[_cls_name]) else: if issubclass(cls, ImpExp) and init_args: _args = { k: v for k, v in init_args.items() if k in cls.init_args } else: _args = {} val = cls(**_args).load(item, **_kwargs) return val
def load(self, spec: dict, init_args: Optional[dict] = None, load_args: Optional[dict] = None) -> "DLDict": if load_args: _kwargs = {"load_args": load_args} _load_args = {} else: _load_args = {} _kwargs = {} if init_args: _kwargs["init_args"] = init_args for attr, (_item_cls, _item) in spec.items(): _cls = importer(_item_cls) if issubclass(_cls, ImpExp) and init_args: _args = { k: v for k, v in init_args.items() if k in _cls.init_args } else: _args = {} _x = _cls(**_args) _x.load(_item, **_kwargs) self.db[attr] = _x self.local_load_adjustments(**_load_args) return self
def load_class_map(spec: dict, init_args: Optional[dict] = None, load_args: Optional[dict] = None) -> dict: _item = {} for key, val in spec.items(): _item[key] = importer(val) return _item
def build_signing_service(self, klass, **kwargs): if isinstance(klass, str): _instance = importer(klass)(**kwargs) else: _instance = klass(**kwargs) return _instance
def __init__(self, base_class=Message): if isinstance(base_class, str): self.base_class = importer(base_class)() elif isinstance(base_class, Message): self.base_class = base_class elif type(base_class) == abc.ABCMeta: self.base_class = base_class() self.info = {}
def __init__(self, fdir: Optional[str] = '', key_conv: Optional[str] = '', value_conv: Optional[str] = ''): """ items = FileSystem( { 'fdir': fdir, 'key_conv':{'to': quote_plus, 'from': unquote_plus}, 'value_conv':{'to': keyjar_to_jwks, 'from': jwks_to_keyjar} }) :param fdir: The root of the directory :param key_conv: Converts to/from the key displayed by this class to users of it to something that can be used as a file name. The value of key_conv is a class that has the methods 'serialize'/'deserialize'. :param value_conv: As with key_conv you can convert/translate the value bound to a key in the database to something that can easily be stored in a file. Like with key_conv the value of this parameter is a class that has the methods 'serialize'/'deserialize'. """ super(AbstractFileSystem, self).__init__(fdir=fdir, key_conv=key_conv, value_conv=value_conv) self.fdir = fdir self.fmtime = {} self.storage = {} if key_conv: self.key_conv = importer(key_conv)() else: self.key_conv = QPKey() if value_conv: self.value_conv = importer(value_conv)() else: self.value_conv = PassThru() if not os.path.isdir(self.fdir): os.makedirs(self.fdir) self.synch()
def load_dldict(spec: dict, init_args: Optional[dict] = None, load_args: Optional[dict] = None) -> dict: db = {} for attr, (_item_cls, _item) in spec.items(): _class = importer(_item_cls) if issubclass(_class, Message): db[attr] = _class().from_dict(_item) else: if issubclass(_class, ImpExp) and init_args: _args = { k: v for k, v in init_args.items() if k in _class.init_args } else: _args = {} db[attr] = _class(**_args).load(_item) return db