Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: allow plugin identifier to add structs #692

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ def add_struct_to_item_template(path, struct_name, template, struct_dict, instan
#logger.debug("- add_struct_to_item_template: struct_dict = {}".format(dict(struct_dict)))
#logger.debug("- add_struct_to_item_template: struct '{}' to item '{}'".format(struct_name, path))
tmp_struct = copy.deepcopy(struct)
if '__struct_is_optional' in tmp_struct:
del tmp_struct['__struct_is_optional']
if 'name' in tmp_struct and isinstance(struct["name"], str):
from lib.smarthome import SmartHome
_sh = SmartHome.get_instance()
Expand Down
4 changes: 2 additions & 2 deletions lib/item/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ def get_instance():
# Following methods handle structs
# -----------------------------------------------------------------------------------------

def add_struct_definition(self, plugin_name, struct_name, struct, from_dir='plugins'):
self.structs.add_struct_definition(plugin_name, struct_name, struct, from_dir)
def add_struct_definition(self, plugin_name, struct_name, struct, from_dir='plugins', optional=False):
self.structs.add_struct_definition(plugin_name, struct_name, struct, from_dir, optional)


def return_struct_definitions(self, all=True):
Expand Down
13 changes: 12 additions & 1 deletion lib/item/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def load_struct_definitions_from_file(self, source_dir, filename, key_prefix):
return


def add_struct_definition(self, plugin_name, struct_name, struct, from_dir=''):
def add_struct_definition(self, plugin_name, struct_name, struct, from_dir='', optional=False):
"""
Add a single struct definition

Expand All @@ -265,6 +265,7 @@ def add_struct_definition(self, plugin_name, struct_name, struct, from_dir=''):
:param plugin_name: Name of the plugin if called from lib.plugin else an empty string
:param struct_name: Name of the struct to add
:param struct: definition of the struct to add
:param optional: only add if not yet present
:return:
"""
if plugin_name == '':
Expand All @@ -275,6 +276,12 @@ def add_struct_definition(self, plugin_name, struct_name, struct, from_dir=''):
# self.logger.debug(f"add_struct_definition: struct '{name}' = {dict(struct)}")
if self._struct_definitions.get(name, None) is None:
self._struct_definitions[name] = struct
self._struct_definitions[name]['__struct_is_optional'] = optional
elif self._struct_definitions[name].get('__struct_is_optional', False) and not optional:
# overwrite optional structs if current struct is not optional
self._struct_definitions[name] = struct
self._struct_definitions[name]['__struct_is_optional'] = optional
self.logger.info(f'overwriting optional struct {name}')
else:
if from_dir != 'plugins':
self.logger.error(f"add_struct_definition: struct '{name}' already loaded - ignoring definition from {from_dir}")
Expand Down Expand Up @@ -396,6 +403,8 @@ def merge_substruct_to_struct(self, main_struct, substruct_name, main_struct_nam
else:
# merge the sub-struct to the main-struct key by key
for key in substruct:
if key == '__struct_is_optional':
continue
if main_struct.get(key, None) is None:
self.logger.dbglow(f" - add key='{key}', value='{substruct[key]}' -> new_struct='{dict(main_struct)}'")
main_struct[key] = copy.deepcopy(substruct[key])
Expand Down Expand Up @@ -447,6 +456,8 @@ def merge(self, source, destination, source_name='', dest_name=''):
'''
# self.logger.warning("merge: source_name='{}', dest_name='{}'".format(source_name, dest_name))
for key, value in source.items():
if key == "__struct_is_optional":
continue
if isinstance(value, collections.OrderedDict):
# get node or create one
node = destination.setdefault(key, collections.OrderedDict())
Expand Down
2 changes: 2 additions & 0 deletions lib/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ def __init__(self, smarthome, configfile):
struct_keys = list(item_structs.keys())
for struct_name in struct_keys:
self._sh.items.add_struct_definition(plugin_name, struct_name, item_structs[struct_name])
# add struct again for plugin "id", marked as optional
self._sh.items.add_struct_definition(plugin, struct_name, item_structs[struct_name], optional=True)
# Test if plugin is disabled
if str(_conf[plugin].get('plugin_enabled', None)).lower() == 'false':
logger.info("Section {} (plugin_name {}) is disabled - Plugin not loaded".format(plugin, _conf[plugin].get('plugin_name', None)))
Expand Down