Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BaseModel.py « src « DigitalLibrary « plugins - github.com/Ultimaker/Cura.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5bfd14febae7f53d37334b938ee758aa590ecc34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Copyright (c) 2021 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.

from datetime import datetime, timezone
from typing import TypeVar, Dict, List, Any, Type, Union


# Type variable used in the parse methods below, which should be a subclass of BaseModel.
T = TypeVar("T", bound="BaseModel")


class BaseModel:

    def __init__(self, **kwargs) -> None:
        self.__dict__.update(kwargs)
        self.validate()

    # Validates the model, raising an exception if the model is invalid.
    def validate(self) -> None:
        pass

    def __eq__(self, other):
        """Checks whether the two models are equal.

        :param other: The other model.
        :return: True if they are equal, False if they are different.
        """
        return type(self) == type(other) and self.toDict() == other.toDict()

    def __ne__(self, other) -> bool:
        """Checks whether the two models are different.

        :param other: The other model.
        :return: True if they are different, False if they are the same.
        """
        return type(self) != type(other) or self.toDict() != other.toDict()

    def toDict(self) -> Dict[str, Any]:
        """Converts the model into a serializable dictionary"""

        return self.__dict__

    @staticmethod
    def parseModel(model_class: Type[T], values: Union[T, Dict[str, Any]]) -> T:
        """Parses a single model.

        :param model_class: The model class.
        :param values: The value of the model, which is usually a dictionary, but may also be already parsed.
        :return: An instance of the model_class given.
        """
        if isinstance(values, dict):
            return model_class(**values)
        return values

    @classmethod
    def parseModels(cls, model_class: Type[T], values: List[Union[T, Dict[str, Any]]]) -> List[T]:
        """Parses a list of models.

        :param model_class: The model class.
        :param values: The value of the list. Each value is usually a dictionary, but may also be already parsed.
        :return: A list of instances of the model_class given.
        """
        return [cls.parseModel(model_class, value) for value in values]

    @staticmethod
    def parseDate(date: Union[str, datetime]) -> datetime:
        """Parses the given date string.

        :param date: The date to parse.
        :return: The parsed date.
        """
        if isinstance(date, datetime):
            return date
        return datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=timezone.utc)