|
|
@ -1,4 +1,5 @@ |
|
|
|
from abc import ABCMeta, abstractmethod |
|
|
|
from abc import ABCMeta, abstractmethod |
|
|
|
|
|
|
|
from flask import abort |
|
|
|
import yaml |
|
|
|
import yaml |
|
|
|
|
|
|
|
|
|
|
|
class MyMeta(metaclass=ABCMeta): |
|
|
|
class MyMeta(metaclass=ABCMeta): |
|
|
@ -15,24 +16,6 @@ class MyMeta(metaclass=ABCMeta): |
|
|
|
class YamlSerializable(object): |
|
|
|
class YamlSerializable(object): |
|
|
|
__metaclass__ = MyMeta |
|
|
|
__metaclass__ = MyMeta |
|
|
|
required_attributes = ['name'] |
|
|
|
required_attributes = ['name'] |
|
|
|
_id_counter = 0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod |
|
|
|
|
|
|
|
def initialize(cls): |
|
|
|
|
|
|
|
ymlserializables = cls._get_all_from_file() |
|
|
|
|
|
|
|
cls._id_counter = max(ymlserializable.id for ymlserializable in ymlserializables) + 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@abstractmethod |
|
|
|
|
|
|
|
def __init__(self, id = None): |
|
|
|
|
|
|
|
self.id = self.set_id(id) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod |
|
|
|
|
|
|
|
def set_id(cls, id): |
|
|
|
|
|
|
|
if id is not None: |
|
|
|
|
|
|
|
return id |
|
|
|
|
|
|
|
id = cls._id_counter |
|
|
|
|
|
|
|
cls._id_counter += 1 |
|
|
|
|
|
|
|
return id |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod |
|
|
|
@classmethod |
|
|
|
@property |
|
|
|
@property |
|
|
@ -53,37 +36,58 @@ class YamlSerializable(object): |
|
|
|
ymlserializable_dict.pop('name') |
|
|
|
ymlserializable_dict.pop('name') |
|
|
|
return {self.name: ymlserializable_dict} |
|
|
|
return {self.name: ymlserializable_dict} |
|
|
|
|
|
|
|
|
|
|
|
def save(self): |
|
|
|
def save(self, overwrite=True): |
|
|
|
# pylint: disable=no-member |
|
|
|
# pylint: disable=no-member |
|
|
|
if self.name is None: |
|
|
|
if self.name is None: |
|
|
|
raise TypeError("Name cannot be None") |
|
|
|
raise TypeError("Name cannot be None") |
|
|
|
ymlserializables = self._get_all_from_file() |
|
|
|
ymlsls = self._get_all_from_file() |
|
|
|
if self.name in ([*ymlserializable][0] for ymlserializable in ymlserializables): |
|
|
|
if self.name in ([*ymlsl][0] for ymlsl in ymlsls): |
|
|
|
ymlserializables[self.name] = self.config_dict |
|
|
|
if overwrite: |
|
|
|
|
|
|
|
ymlsls[self.name] = self.config_dict |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
raise ValueError(f"A {type(self).__name__} with name {self.name} already exists!") |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
ymlsls.append(self.config_dict) |
|
|
|
|
|
|
|
print(ymlsls) |
|
|
|
|
|
|
|
self._save_all_to_file(ymlsls) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod |
|
|
|
|
|
|
|
def first_or_404(cls, **kwargs): |
|
|
|
|
|
|
|
ymlsl = next(cls.get(**kwargs), None) |
|
|
|
|
|
|
|
if ymlsl is None: |
|
|
|
|
|
|
|
abort(404) |
|
|
|
else: |
|
|
|
else: |
|
|
|
ymlserializables.append(self.config_dict) |
|
|
|
return ymlsl |
|
|
|
print(ymlserializables) |
|
|
|
|
|
|
|
self._save_all_to_file(ymlserializables) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod |
|
|
|
@classmethod |
|
|
|
def get(cls, identifier): |
|
|
|
def get(cls, **kwargs): |
|
|
|
if isinstance(identifier, int): |
|
|
|
"""Returns any matching instances |
|
|
|
id = identifier |
|
|
|
|
|
|
|
ymlserializable_dict = next( |
|
|
|
Filters all saved instances by specified properties. |
|
|
|
ymlserializable for ymlserializable in |
|
|
|
All remaining items are returned as a list. |
|
|
|
cls._get_all_from_file() if int(ymlserializable['id']) == id |
|
|
|
""" |
|
|
|
) |
|
|
|
|
|
|
|
return cls._ymlserializable_from_dict(ymlserializable_dict) |
|
|
|
if not any(kwargs): |
|
|
|
|
|
|
|
return [] |
|
|
|
if isinstance(identifier, str): |
|
|
|
ymlsl_dicts = cls._get_all_from_file() |
|
|
|
name = identifier |
|
|
|
for key, value in kwargs.items(): |
|
|
|
ymlserializable_dict = next( |
|
|
|
# 'name' has to be evaluated separately; 'name' is the key of the entire object |
|
|
|
ymlserializable for ymlserializable in |
|
|
|
if key == 'name': |
|
|
|
cls._get_all_from_file() if [*ymlserializable][0] == name |
|
|
|
ymlsl_dicts = (ymlsl_dict for ymlsl_dict in ymlsl_dicts |
|
|
|
) |
|
|
|
if [*ymlsl_dict][0] == value) |
|
|
|
return cls._ymlserializable_from_dict(ymlserializable_dict) |
|
|
|
|
|
|
|
|
|
|
|
# For other keys, filter out any item that does not contain a key, |
|
|
|
return None |
|
|
|
# or that not match the key's value |
|
|
|
|
|
|
|
ymlsl_dicts = (ymlsl_dict for ymlsl_dict in ymlsl_dicts if ymlsl_dict.key == value) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# After each iteration: if no item is left, return None |
|
|
|
|
|
|
|
if not any(ymlsl_dicts): |
|
|
|
|
|
|
|
return [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ymlsls = [] |
|
|
|
|
|
|
|
for ymlsl_dict in ymlsl_dicts: |
|
|
|
|
|
|
|
ymlsls.append(cls._ymlserializable_from_dict(ymlsl_dict)) |
|
|
|
|
|
|
|
return ymlsls |
|
|
|
|
|
|
|
|
|
|
|
@classmethod |
|
|
|
@classmethod |
|
|
|
def _ymlserializable_from_dict(cls, ymldict): |
|
|
|
def _ymlserializable_from_dict(cls, ymldict): |
|
|
@ -100,33 +104,30 @@ class YamlSerializable(object): |
|
|
|
ymldict['name'] = ymlserializable_name |
|
|
|
ymldict['name'] = ymlserializable_name |
|
|
|
|
|
|
|
|
|
|
|
# Create empty instance |
|
|
|
# Create empty instance |
|
|
|
ymlserializable = cls() |
|
|
|
ymlsl = cls() |
|
|
|
|
|
|
|
|
|
|
|
# Fill instance with dict |
|
|
|
# Fill instance with dict |
|
|
|
ymlserializable.__dict__ = ymldict |
|
|
|
ymlsl.__dict__ = ymldict |
|
|
|
return ymlserializable |
|
|
|
return ymlsl |
|
|
|
|
|
|
|
|
|
|
|
@classmethod |
|
|
|
@classmethod |
|
|
|
def list(cls): |
|
|
|
def list(cls): |
|
|
|
ymlserializables = cls._get_all_from_file() |
|
|
|
ymlsls = cls._get_all_from_file() |
|
|
|
ymlserializables_list = [] |
|
|
|
ymlserializables_list = [] |
|
|
|
for ymlserializable in ymlserializables: |
|
|
|
for ymlsl in ymlsls: |
|
|
|
name = [*ymlserializable][0] |
|
|
|
name = [*ymlsl][0] |
|
|
|
ymlserializables_list.append(name) |
|
|
|
ymlserializables_list.append(name) |
|
|
|
return ymlserializables_list |
|
|
|
return ymlserializables_list |
|
|
|
|
|
|
|
|
|
|
|
@classmethod |
|
|
|
@classmethod |
|
|
|
def _get_all_from_file(cls): |
|
|
|
def _get_all_from_file(cls): |
|
|
|
with open(cls._storage_file) as file: |
|
|
|
with open(cls._storage_file) as file: |
|
|
|
ymlserializables = yaml.load(file, yaml.FullLoader).get(cls._yaml_object_name) |
|
|
|
ymlsls = yaml.load(file, yaml.FullLoader).get(cls._yaml_object_name) |
|
|
|
|
|
|
|
|
|
|
|
highest_id = max(ymlserializable.id for ymlserializable in ymlserializables) + 1 |
|
|
|
return ymlsls |
|
|
|
if highest_id > cls._id_counter: cls._id_counter = highest_id |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ymlserializables |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod |
|
|
|
@classmethod |
|
|
|
def _save_all_to_file(cls, ymlserializables): |
|
|
|
def _save_all_to_file(cls, ymlsls): |
|
|
|
ymlserializables_object = {cls._yaml_object_name : ymlserializables} |
|
|
|
ymlserializables_object = {cls._yaml_object_name : ymlsls} |
|
|
|
with open(cls._storage_file, 'w') as file: |
|
|
|
with open(cls._storage_file, 'w') as file: |
|
|
|
yaml.dump(ymlserializables_object, file) |
|
|
|
yaml.dump(ymlserializables_object, file) |