Browse Source

Make yaml_serializable a recursive_property

master
Burathar 4 years ago
parent
commit
35c8325159
  1. 37
      biscd/biscd/models/recursive_property.py
  2. 10
      biscd/biscd/models/user.py
  3. 41
      biscd/biscd/models/yaml_serializable.py
  4. 4
      installation-files/projects_example.yaml
  5. 5
      installation-files/users_example.yaml

37
biscd/biscd/models/recursive_property.py

@ -0,0 +1,37 @@ @@ -0,0 +1,37 @@
class RecursiveProperty:
def __init__(self, property_key):
self.property_key = property_key
def config_dict(self, values_only=False):
# pylint: disable=no-member
property_key_value = getattr(self, self.property_key)
if property_key_value is None:
raise TypeError(f"{self.property_key} cannot be None")
property_dict = {k: v for k, v in self.__dict__.items() if v is not None}
property_dict.pop(self.property_key)
property_dict.pop('property_key')
if values_only:
return property_dict
return {property_key_value : property_dict}
@classmethod
def _from_dict(cls, property_dict, property_key):
if property_dict is None:
return None
# Extract the name
property_name = [*property_dict][0]
# Step into object
property_dict = property_dict.get(property_name)
# Add name to dict
property_dict[property_key] = property_name
# Create empty instance
recursive_property = cls(property_key)
# Fill instance with dict
recursive_property.__dict__ = property_dict
return recursive_property

10
biscd/biscd/models/user.py

@ -5,6 +5,7 @@ from flask_login import UserMixin @@ -5,6 +5,7 @@ from flask_login import UserMixin
from biscd import config
from biscd import login
from .yaml_serializable import YamlSerializable
from .project import Project
class User(UserMixin, YamlSerializable):
@ -23,10 +24,19 @@ class User(UserMixin, YamlSerializable): @@ -23,10 +24,19 @@ class User(UserMixin, YamlSerializable):
return self.name
def __init__(self, name=None, email=None):
super().__init__()
self.name = name
self.email = email
self.password_hash = None
@property
def projects(self):
pass #all_projects = Project.get()
@projects.setter
def projects(self, projects):
pass
def set_password(self, password):
self.password_hash = generate_password_hash(password)

41
biscd/biscd/models/yaml_serializable.py

@ -2,6 +2,8 @@ from abc import ABCMeta, abstractmethod @@ -2,6 +2,8 @@ from abc import ABCMeta, abstractmethod
from flask import abort
import yaml
from .recursive_property import RecursiveProperty
class MyMeta(metaclass=ABCMeta):
required_attributes = []
@ -13,10 +15,13 @@ class MyMeta(metaclass=ABCMeta): @@ -13,10 +15,13 @@ class MyMeta(metaclass=ABCMeta):
raise ValueError('required attribute (%s) not set' % attr_name)
return obj
class YamlSerializable(object):
class YamlSerializable(RecursiveProperty):
__metaclass__ = MyMeta
required_attributes = ['name']
def __init__(self):
self.property_key='name'
@classmethod
#@property
@abstractmethod
@ -29,16 +34,6 @@ class YamlSerializable(object): @@ -29,16 +34,6 @@ class YamlSerializable(object):
def _yaml_object_name(cls):
pass
def config_dict(self, properties_only=False):
# pylint: disable=no-member
if self.name is None:
raise TypeError("Name cannot be None")
ymlserializable_dict = {k: v for k, v in self.__dict__.items() if v is not None}
ymlserializable_dict.pop('name')
if properties_only:
return ymlserializable_dict
return {self.name: ymlserializable_dict}
def save(self, overwrite=True):
# pylint: disable=no-member
if self.name is None:
@ -49,7 +44,7 @@ class YamlSerializable(object): @@ -49,7 +44,7 @@ class YamlSerializable(object):
if self.name == [*ymlsl][0]:
found_match = True
if overwrite:
ymlsl[self.name] = self.config_dict(properties_only=True)
ymlsl[self.name] = self.config_dict(values_only=True)
else:
raise ValueError(
f"A {type(self).__name__} with name {self.name} already exists!")
@ -102,7 +97,7 @@ class YamlSerializable(object): @@ -102,7 +97,7 @@ class YamlSerializable(object):
ymlsls = []
for ymlsl_dict in ymlsl_dicts:
ymlsls.append(cls._from_dict(ymlsl_dict))
ymlsls.append(cls._from_dict(ymlsl_dict, 'name'))
return ymlsls
@classmethod
@ -114,26 +109,6 @@ class YamlSerializable(object): @@ -114,26 +109,6 @@ class YamlSerializable(object):
ymlserializables_list.append(name)
return ymlserializables_list
@classmethod
def _from_dict(cls, ymldict):
if ymldict is None:
return None
# Extract the name
ymlsl_name = [*ymldict][0]
# Step into object
ymldict = ymldict.get(ymlsl_name)
# Add name to dict
ymldict['name'] = ymlsl_name
# Create empty instance
ymlsl = cls()
# Fill instance with dict
ymlsl.__dict__ = ymldict
return ymlsl
@classmethod
def _get_all_from_file(cls):

4
installation-files/projects_example.yaml

@ -5,6 +5,10 @@ projects: @@ -5,6 +5,10 @@ projects:
requirements_file: requirements.txt
secret: thisissecret
tests: tests.py
access:
public: true
owners:
- Carl
- Foo:
branch: master
git_repo: https://git.sciuro.org/Burathar/The-Hunt

5
installation-files/users_example.yaml

@ -4,4 +4,7 @@ users: @@ -4,4 +4,7 @@ users:
password_hash: pbkdf2:sha256:150000$uuFRyvLs$ee9863f169db786e82b9e2abe0c2cf3434e925479d0919f3b4046ebbfa0aeb28
- Carl:
email: carl@mail.com
password_hash: pbkdf2:sha256:150000$v4UyXVie$898ba71cdf3adefd7c1b6ed611897a3c8ca3151bd2628b490a052f554bae113e
password_hash: pbkdf2:sha256:150000$v4UyXVie$898ba71cdf3adefd7c1b6ed611897a3c8ca3151bd2628b490a052f554bae113e
- Marijn:
email: e2@mail.com
password_hash: pbkdf2:sha256:150000$10ZT4FFC$f96185d4279df8fdef73ad459d7f109d18e92f517ed463d9a0b8de6cc208be6a

Loading…
Cancel
Save