From 62ee1acc4f76bbbd7298bd1a58f17208ff7220f6 Mon Sep 17 00:00:00 2001 From: Burathar Date: Fri, 12 Mar 2021 12:29:27 +0100 Subject: [PATCH] new objects --- biscd/biscd/models/user.py | 17 +++++ biscd/biscd/models/yaml_serializable.py | 82 +++++++++++++++++++++++++ installation-files/users_example.yaml | 5 ++ 3 files changed, 104 insertions(+) create mode 100644 biscd/biscd/models/user.py create mode 100644 biscd/biscd/models/yaml_serializable.py create mode 100644 installation-files/users_example.yaml diff --git a/biscd/biscd/models/user.py b/biscd/biscd/models/user.py new file mode 100644 index 0000000..8fc1257 --- /dev/null +++ b/biscd/biscd/models/user.py @@ -0,0 +1,17 @@ +from pathlib import Path +from biscd import config +from biscd.models import YamlSerializable + +class Project(YamlSerializable): + @property + def _storage_file(self): + return Path(config.config_dir()) / 'users.yaml' + + @property + def _yaml_object_name(self): + return 'users' + + def __init__(self, name=None, password=None, email=None): + self.name = name + self.password = password + self.email = email diff --git a/biscd/biscd/models/yaml_serializable.py b/biscd/biscd/models/yaml_serializable.py new file mode 100644 index 0000000..5e1c75f --- /dev/null +++ b/biscd/biscd/models/yaml_serializable.py @@ -0,0 +1,82 @@ +from abc import ABCMeta, abstractmethod +import yaml + +class MyMeta(metaclass=ABCMeta): + required_attributes = [] + + def __call__(self, *args, **kwargs): + obj = super(MyMeta, self).__call__(*args, **kwargs) + for attr_name in obj.required_attributes: + if not getattr(obj, attr_name): + raise ValueError('required attribute (%s) not set' % attr_name) + return obj + +class YamlSerializable(object, metaclass=MyMeta): + required_attributes = ['name'] + + @abstractmethod + def __init__(self): + pass + + @property + @abstractmethod + def _storage_file(self): + pass + + @property + @abstractmethod + def _yaml_object_name(self): + pass + + @property + def config_dict(self): + ymlserializable_dict = self.__dict__.copy() + ymlserializable_dict.pop('name') + return {self.name: ymlserializable_dict} + + def save(self): + if self.name is None: + raise TypeError("Name cannot be None") + ymlserializables = self._get_all_from_file() + if self.name in ([*ymlserializable][0] for ymlserializable in ymlserializables): + ymlserializables[self.name] = self.config_dict + else: + ymlserializables.append(self.config_dict) + print(ymlserializables) + self._save_all_to_file(ymlserializables) + + @classmethod + def get(cls, name): + ymlserializable_dict = next(ymlserializable for ymlserializable in cls._get_all_from_file() if [*ymlserializable][0] == name) + if ymlserializable_dict is None: + return None + ymlserializable_name = [*ymlserializable_dict][0] + ymlserializable_dict = ymlserializable_dict.get(ymlserializable_name) + + ymlserializable_dict['name'] = ymlserializable_name + + ymlserializable = cls() + ymlserializable.__dict__ = ymlserializable_dict + return ymlserializable + + + @classmethod + def list(cls): + ymlserializables = cls._get_all_from_file() + ymlserializables_list = [] + for ymlserializable in ymlserializables: + name = [*ymlserializable][0] + ymlserializables_list.append(name) + return ymlserializables_list + + @classmethod + def _get_all_from_file(cls): + with open(cls._storage_file) as file: + ymlserializables = yaml.load(file, yaml.FullLoader).get(_yaml_object_name) + return ymlserializables + + @classmethod + def _save_all_to_file(cls, ymlserializables): + ymlserializables_object = {_yaml_object_name : ymlserializables} + with open(cls._storage_file, 'w') as file: + yaml.dump(ymlserializables_object, file) diff --git a/installation-files/users_example.yaml b/installation-files/users_example.yaml new file mode 100644 index 0000000..f6e80ca --- /dev/null +++ b/installation-files/users_example.yaml @@ -0,0 +1,5 @@ +users: +- henk: + email: mymail@mail.com + password: hash + \ No newline at end of file