Browse Source

new objects

master
Burathar 4 years ago
parent
commit
62ee1acc4f
  1. 17
      biscd/biscd/models/user.py
  2. 82
      biscd/biscd/models/yaml_serializable.py
  3. 5
      installation-files/users_example.yaml

17
biscd/biscd/models/user.py

@ -0,0 +1,17 @@ @@ -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

82
biscd/biscd/models/yaml_serializable.py

@ -0,0 +1,82 @@ @@ -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)

5
installation-files/users_example.yaml

@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
users:
- henk:
email: mymail@mail.com
password: hash
Loading…
Cancel
Save