1 # Copyright (C) 2021 Wind River Systems, Inc.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
23 class InfrastructureInventoryObject:
26 def __init__(self) -> None:
27 # self.ObjectState = {}
30 def get_fields_as_dict(self, fields):
32 if hasattr(self, field):
33 self.ObjectState[field] = getattr(self, field)
34 return self.ObjectState
41 def __init__(self) -> None:
43 self.updatetime = datetime.now()
44 self.createtime = datetime.now()
45 self.events = [] # type: List[Event]
48 # def append_event(self, event: Event):
49 # self.events = self.events.append(event)
52 class Serializer(object):
56 d = {c: getattr(self, c) for c in inspect(self).attrs.keys()}
57 # if 'createtime' in d:
58 # d['createtime'] = d['createtime'].isoformat()
59 # if 'updatetime' in d:
60 # d['updatetime'] = d['updatetime'].isoformat()
62 # return {c: getattr(self, c) for c in inspect(self).attrs.keys()}
63 except NoInspectionAvailable:
67 def serialize_list(li):
68 return [m.serialize() for m in li]