Code Duplication    Length = 47-54 lines in 2 locations

groundwork/patterns/gw_documents_pattern.py 1 location

@@ 28-81 (lines=54) @@
25
        self.documents = DocumentsListPlugin(self)
26
27
28
class DocumentsListPlugin:
29
    """
30
    Stores and handles documents.
31
32
    These documents are used for real-time and offline documentation of a groundwork application.
33
34
    The content of a document must be string, which is can contain jinja and rst syntax.
35
36
    Plugins, which want to generate a documentation out of all documents, must render this content
37
    (jinja render_template) and transform the rst by the own (e.g. by using rst2html).
38
39
    Please see :ref:`documents` for more details.
40
    """
41
42
    def __init__(self, plugin):
43
        """
44
        :param plugin: The plugin, which wants to use documents
45
        :type plugin: GwBasePattern
46
        """
47
        self._plugin = plugin
48
        self.__app = plugin.app
49
        self.__log = plugin.log
50
51
        # Let's register a receiver, which cares about the deactivation process of documents for this plugin.
52
        # We do it after the original plugin deactivation, so we can be sure that the registered function is the last
53
        # one which cares about documents for this plugin.
54
        self._plugin.signals.connect(receiver="%s_documents_deactivation" % self._plugin.name,
55
                                     signal="plugin_deactivate_post",
56
                                     function=self.__deactivate_documents,
57
                                     description="Deactivate documents for %s" % self._plugin.name,
58
                                     sender=self._plugin)
59
        self.__log.debug("Plugin documents initialised")
60
61
    def __deactivate_documents(self, plugin, *args, **kwargs):
62
        documents = self.get()
63
        for document in documents.keys():
64
            self.unregister(document)
65
66
    def register(self, name, content, description=None):
67
        """
68
        Register a new document.
69
70
        :param content: Content of this document. Jinja and rst are supported.
71
        :type content: str
72
        :param name: Unique name of the document for documentation purposes.
73
        :param description: Short description of this document
74
        """
75
        return self.__app.documents.register(name, content, self._plugin, description)
76
77
    def unregister(self, document):
78
        return self.__app.documents.unregister(document)
79
80
    def get(self, name=None):
81
        return self.__app.documents.get(name, self._plugin)
82
83
84
class DocumentsListApplication:

groundwork/patterns/gw_threads_pattern.py 1 location

@@ 29-75 (lines=47) @@
26
        self.threads = ThreadsListPlugin(self)
27
28
29
class ThreadsListPlugin:
30
    """
31
    Stores and handles threads.
32
33
    Please see :ref:`threads` for more details.
34
    """
35
36
    def __init__(self, plugin):
37
        """
38
        :param plugin: The plugin, which wants to use threads
39
        :type plugin: GwBasePattern
40
        """
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 threads 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 threads for this plugin.
48
        self._plugin.signals.connect(receiver="%s_threads_deactivation" % self._plugin.name,
49
                                     signal="plugin_deactivate_post",
50
                                     function=self.__deactivate_threads,
51
                                     description="Deactivate threads for %s" % self._plugin.name,
52
                                     sender=self._plugin)
53
        self.__log.debug("Plugin threads initialised")
54
55
    def __deactivate_threads(self, plugin, *args, **kwargs):
56
        threads = self.get()
57
        for thread in threads.keys():
58
            self.unregister(thread)
59
60
    def register(self, name, function, description=None):
61
        """
62
        Register a new thread.
63
64
        :param function: Function, which gets called for the new thread
65
        :type function: function
66
        :param name: Unique name of the thread for documentation purposes.
67
        :param description: Short description of the thread
68
        """
69
        return self.__app.threads.register(name, function, self._plugin, description)
70
71
    def unregister(self, thread):
72
        return self.__app.threads.unregister(thread)
73
74
    def get(self, name=None):
75
        return self.__app.threads.get(name, self._plugin)
76
77
78
class ThreadsListApplication: