2 This module provides helper classes to deal with metadata flatbuffers.
4 from enum
import IntEnum
7 from comm.datalayer
import (AllowedOperations, DisplayFormat, Extension, Metadata, NodeClass, Reference, LocaleText)
13 """ List of reference types as strings. """
17 """ Type when reading a value (absolute node address). """
22 """ Input type when reading a value. """
27 """ Output type when reading a value. """
32 """ Type when writing a value (absolute node address). Input/Output type are the same. """
37 """ Input type when writing a value. """
42 """ Output type when writing a value. """
47 """ Type when creating a value (absolute node address). """
52 """ Referenced (list of) absolute node addresses. """
58 Reference to a save node address which needs to be called after
59 node change to persist the new value (must be <technology>/admin/cfg/save).
66 Allowed Operation Flags
74 ALL = READ | WRITE | CREATE | DELETE | BROWSE
78 """ Builds a flatbuffer provided with the metadata information for a Data Layer node. """
81 def create_metadata(name: str, description: str, unit: str, description_url: str, node_class: NodeClass,
82 read_allowed: bool, write_allowed: bool, create_allowed: bool, delete_allowed: bool,
83 browse_allowed: bool, type_path: str, references: dict =
None) -> Variant:
85 Creates a metadata Variant object from the given arguments.
86 `type_path` is used in conjunction with the allowed operations of the object.
87 `references` allow the user to set own references in the metadata object. Set `type_path` to ""
88 for full control over references.
93 if references
is None:
95 builder =
MetadataBuilder(allowed=AllowedOperation.NONE, description=description,
96 description_url=description_url)
97 allowed = AllowedOperation.NONE
99 allowed = allowed | AllowedOperation.READ
100 if len(type_path) != 0:
101 builder.add_reference(ReferenceType.read(), type_path)
103 allowed = allowed | AllowedOperation.WRITE
104 if len(type_path) != 0:
105 builder.add_reference(ReferenceType.write(), type_path)
107 allowed = allowed | AllowedOperation.CREATE
108 if len(type_path) != 0:
109 builder.add_reference(ReferenceType.create(), type_path)
111 allowed = allowed | AllowedOperation.DELETE
113 allowed = allowed | AllowedOperation.BROWSE
115 builder.set_operations(allowed)
116 builder.set_display_name(name)
117 builder.set_node_class(node_class)
118 builder.set_unit(unit)
121 for key, val
in references.items():
122 builder.add_reference(key, val)
124 return builder.build()
126 def __init__(self, allowed: AllowedOperation = AllowedOperation.BROWSE, description: str =
"",
127 description_url: str =
""):
129 generate MetadataBuilder
135 self.
__node_class__node_class = NodeClass.NodeClass.Node
136 self.
__allowed__allowed = AllowedOperation.ALL
144 def build(self) -> Variant:
145 """Build Metadata as Variant
153 def __buildV2(self) -> Variant:
155 build function flatbuffers 2.x
157 builder = flatbuffers.Builder(1024)
170 meta = Metadata.MetadataT()
173 meta.displayName = self.
__name__name
174 meta.unit = self.
__unit__unit
178 meta.operations = operations
179 meta.references = references
180 meta.extensions = extensions
181 meta.descriptions = descriptions
182 meta.displayNames = displaynames
185 metadata_internal = meta.Pack(builder)
188 builder.Finish(metadata_internal)
191 metadata.set_flatbuffers(builder.Output())
215 def set_operations(self, allowed: AllowedOperation = AllowedOperation.NONE):
217 set allowed operations
243 add localization of descriptions
249 add localization of display names
253 def __is_allowed(self, allowed: AllowedOperation) -> bool:
254 return (self.
__allowed__allowed & allowed) == allowed
256 def __build_operations(self):
257 op = AllowedOperations.AllowedOperationsT()
258 op.read = self.
__is_allowed__is_allowed(AllowedOperation.READ)
259 op.write = self.
__is_allowed__is_allowed(AllowedOperation.WRITE)
260 op.create = self.
__is_allowed__is_allowed(AllowedOperation.CREATE)
261 op.delete = self.
__is_allowed__is_allowed(AllowedOperation.DELETE)
262 op.browse = self.
__is_allowed__is_allowed(AllowedOperation.BROWSE)
265 def __add_reference(self, t: ReferenceType, addr: str):
266 ref = Reference.ReferenceT()
268 ref.targetAddress = addr
271 def __build_references(self):
279 def __add_extension(self, key: str, val: str):
280 ex = Extension.ExtensionT()
285 def __build_extensions(self):
293 def __add_local_text(self, ident: str, txt: str):
294 lt = LocaleText.LocaleTextT()
299 def __build_local_texts(self, lst):
301 for i
in sorted(lst):
307 def __build_descriptions(self):
310 def __build_display_names(self):
Variant is a container for a many types of data.