128f950f026c0c8e1e1b8848f957b4b93d940759
[pti/o2.git] / o2common / domain / base.py
1 # Copyright (C) 2021 Wind River Systems, Inc.
2 #
3 #  Licensed under the Apache License, Version 2.0 (the "License");
4 #  you may not use this file except in compliance with the License.
5 #  You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #  Unless required by applicable law or agreed to in writing, software
10 #  distributed under the License is distributed on an "AS IS" BASIS,
11 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 #  See the License for the specific language governing permissions and
13 #  limitations under the License.
14
15 from datetime import datetime
16 from typing import List
17 from sqlalchemy.inspection import inspect
18 from sqlalchemy.exc import NoInspectionAvailable
19 # from sqlalchemy.orm.exc import DetachedInstanceError
20 from .events import Event
21
22
23 class AgRoot:
24
25     events = []
26
27     def __init__(self) -> None:
28         self.hash = ""
29         self.updatetime = datetime.now()
30         self.createtime = datetime.now()
31         self.events = []  # type: List[Event]
32         # self.id = ""
33
34     # def append_event(self, event: Event):
35     #     self.events = self.events.append(event)
36
37
38 class Serializer(object):
39
40     def serialize(self):
41         try:
42             d = {c: getattr(self, c) for c in inspect(self).attrs.keys()}
43             # if 'createtime' in d:
44             #     d['createtime'] = d['createtime'].isoformat()
45             # if 'updatetime' in d:
46             #     d['updatetime'] = d['updatetime'].isoformat()
47             return d
48             # return {c: getattr(self, c) for c in inspect(self).attrs.keys()}
49         except NoInspectionAvailable:
50             return self.__dict__
51
52     @staticmethod
53     def serialize_list(li):
54         return [m.serialize() for m in li]