def __init__(self, *args, **kwargs):
if type(self) is BaseProvider:
return
if self.debug:
print(f'{self._typ.upper()} Provider .ctor')
if not self._base_instance:
self._base_instance = self.__class__.__bases__[0]()
base_attributes = inspect.getmembers(self._base_instance, lambda a:not(inspect.isroutine(a)))
base_keys = [a[0] for a in base_attributes if not(a[0].startswith('_'))]
# base_keys = ['debug', 'default_mc_version']
for attribute_key in base_keys:
if attribute_key in kwargs:
value = kwargs.get(attribute_key)
setattr(self, attribute_key, kwargs[attribute_key])
provider_settings = kwargs.get('provider_settings', {})
provider_settings = provider_settings.get(self._typ, {})
if self.debug:
print(f'{self._typ} settings: {provider_settings}')
attributes = inspect.getmembers(self, lambda a:not(inspect.isroutine(a)))
attribute_keys = [a[0] for a in attributes if not(a[0].startswith('_'))]
attribute_keys = list(set(attribute_keys) - set(base_keys))
path = Path(kwargs['data_path'], 'defaults.yaml')
if path.is_dir():
path.rmdir()
global_defaults = {}
if path.exists():
with open(path, 'r') as stream:
try:
global_defaults = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
global_defaults = {}
# get all default values
global_defaults[self._typ] = {k: getattr(self, k) for k in attribute_keys}
with open(path, 'w') as outfile:
yaml.dump(global_defaults, outfile, default_flow_style=False)
# write provider settings overrides to self
for attribute_key in attribute_keys:
if attribute_key in provider_settings:
value = provider_settings.get(attribute_key)
if self.debug:
print(f'setting {attribute_key}, value={value}')
setattr(self, attribute_key, provider_settings[attribute_key])
评论列表
文章目录