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