Coverage for watcher/decision_engine/model/element/base.py: 100%
27 statements
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-17 12:22 +0000
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-17 12:22 +0000
1# -*- encoding: utf-8 -*-
2# Copyright (c) 2016 b<>com
3#
4# Authors: Vincent FRANCOISE <vincent.francoise@b-com.com>
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15# implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
19import abc
20import collections
22from lxml import etree # nosec: B410
23from oslo_log import log
25from watcher.objects import base
26from watcher.objects import fields as wfields
28LOG = log.getLogger(__name__)
31class Element(base.WatcherObject, base.WatcherObjectDictCompat,
32 base.WatcherComparableObject, metaclass=abc.ABCMeta):
34 # Initial version
35 VERSION = '1.0'
37 fields = {}
39 def __init__(self, context=None, **kwargs):
40 for name, field in self.fields.items():
41 # The idea here is to force the initialization of unspecified
42 # fields that have a default value
43 if (name not in kwargs and not field.nullable and
44 field.default != wfields.UnspecifiedDefault):
45 kwargs[name] = field.default
46 super(Element, self).__init__(context, **kwargs)
48 @abc.abstractmethod
49 def accept(self, visitor):
50 raise NotImplementedError()
52 def as_xml_element(self):
53 sorted_fieldmap = []
54 for field in self.fields:
55 try:
56 value = str(self[field])
57 sorted_fieldmap.append((field, value))
58 except Exception as exc:
59 LOG.exception(exc)
61 attrib = collections.OrderedDict(sorted_fieldmap)
63 element_name = self.__class__.__name__
64 instance_el = etree.Element(element_name, attrib=attrib)
66 return instance_el