Code Duplication    Length = 65-78 lines in 2 locations

groundwork/patterns/gw_shared_objects_pattern.py 1 location

@@ 34-111 (lines=78) @@
31
        pass
32
33
34
class SharedObjectsListPlugin:
35
    """
36
    Cares about plugin function for shared objects on plugin level.
37
38
    The class mainly directs most function calls to the ShareObjectApplication class, which is initiated on
39
    application level.
40
    """
41
    def __init__(self, plugin):
42
        """
43
        :param plugin: initiated plugin object
44
        """
45
        self.plugin = plugin
46
        self.app = plugin.app
47
        self.log = plugin.log
48
        # Let's register a receiver, which cares about the deactivation process of shared objects for this plugin.
49
        # We do it after the original plugin deactivation, so we can be sure that the registered function is the last
50
        # one which cares about shared objects for this plugin.
51
        self.plugin.signals.connect(receiver="%s_shared_objects_deactivation" % self.plugin.name,
52
                                    signal="plugin_deactivate_post",
53
                                    function=self.__deactivate_shared_objects,
54
                                    description="Deactivate documents for %s" % self.plugin.name,
55
                                    sender=self.plugin)
56
57
        self.log.debug("Shared objects for plugin %s initialised" % self.plugin.name)
58
59
    def __deactivate_shared_objects(self, plugin, *args, **kwargs):
60
        """
61
        Callback, which gets executed, if the signal "plugin_deactivate_post" was send by the plugin.
62
        """
63
        shared_objects = self.get()
64
        for shared_object in shared_objects.keys():
65
            self.unregister(shared_object)
66
67
    def register(self, name, description, obj):
68
        """
69
        Registers a new shared object.
70
71
        :param name: Unique name for shared object
72
        :type name: str
73
        :param description: Description of shared object
74
        :type description: str
75
        :param obj: The object, which shall be shared
76
        :type obj: any type
77
        """
78
        return self.app.shared_objects.register(name, description, obj, self.plugin)
79
80
    def unregister(self, shared_object):
81
        """
82
        Unregisters an already registered shared object.
83
84
        :param shared_object: name of the shared object
85
        :type shared_object: str
86
        """
87
        return self.app.shared_objects.unregister(shared_object)
88
89
    def get(self, name=None):
90
        """
91
        Returns requested shared objects, which were registered by the current plugin.
92
93
        If access to objects of other plugins are needed, use :func:`access` or perform get on application level::
94
95
            my_app.shared_objects.get(name="...")
96
97
        :param name: Name of a request shared object
98
        :type name: str or None
99
        """
100
        return self.app.shared_objects.get(name, self.plugin)
101
102
    def access(self, name):
103
        """
104
        Returns the object of the shared_object, if the given name has been registered.
105
        The search is done on application level, so registered shared objects form other plugins
106
        can be access.
107
108
        :param name: Name of the shared object
109
        :return: object, whatever it may be...
110
        """
111
        return self.app.shared_objects.access(name)
112
113
114
class SharedObjectsListApplication:

groundwork/patterns/gw_recipes_pattern.py 1 location

@@ 33-97 (lines=65) @@
30
    # get recipes
31
32
33
class RecipesListPlugin:
34
    """
35
    Cares about the recipe management on plugin level.
36
    Allows to register, get and build recipes in the context of the current plugin.
37
38
    :param plugin: plugin, which shall be used as contxt.
39
    """
40
    def __init__(self, plugin):
41
        self._plugin = plugin
42
        self.__app = plugin.app
43
        self.__log = plugin.log
44
45
        # Let's register a receiver, which cares about the deactivation process of recipes for this plugin.
46
        # We do it after the original plugin deactivation, so we can be sure that the registered function is the last
47
        # one which cares about recipes for this plugin.
48
        self._plugin.signals.connect(receiver="%s_recipes_deactivation" % self._plugin.name,
49
                                     signal="plugin_deactivate_post",
50
                                     function=self.__deactivate_recipes,
51
                                     description="Deactivate recipes for %s" % self._plugin.name,
52
                                     sender=self._plugin)
53
        self.__log.debug("Plugin recipes initialised")
54
55
    def __deactivate_recipes(self, plugin, *args, **kwargs):
56
        """
57
        Deactivates/unregisters all recipes of the current plugin, if this plugin gets deactivated.
58
        """
59
        recipes = self.get()
60
        for recipe in recipes.keys():
61
            self.unregister(recipe)
62
63
    def register(self, name, path, description, final_words=None):
64
        """
65
        Registers a new recipe in the context of the current plugin.
66
67
        :param name: Name of the recipe
68
        :param path: Absolute path of the recipe folder
69
        :param description: A meaningful description of the recipe
70
        :param final_words: A string, which gets printed after the recipe was build.
71
        """
72
        return self.__app.recipes.register(name, path, self._plugin, description, final_words)
73
74
    def unregister(self, recipe):
75
        """
76
        Unregister a recipe of the current plugin.
77
78
        :param recipe: Name of the recipe.
79
        """
80
        return self.__app.recipes.unregister(recipe)
81
82
    def get(self, name=None):
83
        """
84
        Gets a list of all recipes, which are registered by the current plugin.
85
        If a name is provided, only the requested recipe is returned or None.
86
87
        :param: name: Name of the recipe
88
        """
89
        return self.__app.recipes.get(name, self._plugin)
90
91
    def build(self, recipe):
92
        """
93
        Builds a recipe
94
95
        :param recipe: Name of the recipe to build.
96
        """
97
        return self.__app.recipes.build(recipe, self._plugin)
98
99
100
class RecipesListApplication: