Feb-06-2018, 07:26 AM
I'm working on a plugin for one program and need to implement sort of "registry". In this registry I want to store objects, created by my plugin and also allow other plugins to add/remove objects to/from this registry.
Now, I'm using following approach. In the
In the my
Now, I'm using following approach. In the
__init__.py
of my plugin I have a list variable and set of functions to add/remove items to the list. Plugins, which want to add items to my registry need to import my module and call corresponding functions. Here is simplified codeIn the my
__init__.py
registry = [] def addToRegistry(toAdd): for item in registry: if item.name == toAdd.name and item.group == toAdd.group: return registry.append(toAdd) def removeFromRegistry(toRemove): for item in registry[::-1]: if item.name == toRemove.name and item.group == toRemove.group: registry.remove(toRemove) def addDirectory(folder): for d in os.listdir(folder): obj = fromFile(os.path.join(folder, d, "object.yaml")) if obj: addToRegistry(obj) def removeDirectory(folder): for d in os.listdir(folder): obj = fromFile(os.path.join(folder, d, "object.yaml")) if obj: removeFromRegistry(obj)In other plugins I use this code to add/remove data to/from "registry"
try: from myplugin import addDirectory addDirectory(folder) except: pass ... try: from myplugin import removeDirectory removeDirectory(folder) except: passMaybe there are some other solutions? I was thinking about creating singleton registry class but not sure how flexible such solution will be in my case. Any advice is welcome.