From a345adf6d8950ffe2e46ae3db9a5be0f51065fd2 Mon Sep 17 00:00:00 2001
From: Honza Mach <honza.mach.ml@gmail.com>
Date: Thu, 3 Aug 2017 09:36:17 +0200
Subject: [PATCH] Added more convenient __init__ method for DemoZenDaemon.

---
 pyzenkit/tests/test_zendaemon.py | 16 ++------
 pyzenkit/zendaemon.py            | 65 +++++++++++++++++++++-----------
 2 files changed, 45 insertions(+), 36 deletions(-)

diff --git a/pyzenkit/tests/test_zendaemon.py b/pyzenkit/tests/test_zendaemon.py
index 938774a..ec56800 100644
--- a/pyzenkit/tests/test_zendaemon.py
+++ b/pyzenkit/tests/test_zendaemon.py
@@ -29,7 +29,7 @@ import pyzenkit.zendaemon
 #
 # Global variables
 #
-DMN_NAME       = 'test_zendaemon.py'              # Name of the daemon process
+DMN_NAME       = 'test-zendaemon.py'              # Name of the daemon process
 JSON_FILE_NAME = '/tmp/daemon-state.json'         # Name of the test JSON file
 CFG_FILE_NAME  = '/tmp/{}.conf'.format(DMN_NAME)  # Name of the daemon configuration file
 CFG_DIR_NAME   = '/tmp/{}'.format(DMN_NAME)       # Name of the daemon configuration directory
@@ -43,18 +43,8 @@ class TestPyzenkitZenDaemon(unittest.TestCase):
         except FileExistsError:
             pass
 
-        self.obj = pyzenkit.zendaemon._DemoZenDaemon(
-            name = DMN_NAME,
-            path_cfg = '/tmp',
-            path_log = '/tmp',
-            path_tmp = '/tmp',
-            path_run = '/tmp',
-            description = 'DemoZenDaemon - generic daemon (DEMO)',
-            schedule = [('default',)],
-            components = [
-                pyzenkit.zendaemon._DemoDaemonComponent()
-            ]
-        )
+        self.obj = pyzenkit.zendaemon.DemoZenDaemon(name = DMN_NAME)
+
     def tearDown(self):
         os.remove(CFG_FILE_NAME)
         shutil.rmtree(CFG_DIR_NAME)
diff --git a/pyzenkit/zendaemon.py b/pyzenkit/zendaemon.py
index 24e2813..f5e33e7 100644
--- a/pyzenkit/zendaemon.py
+++ b/pyzenkit/zendaemon.py
@@ -947,7 +947,44 @@ class DemoZenDaemon(ZenDaemon):
     """
     Minimalistic class for demonstration purposes.
     """
-    pass
+    def __init__(self, name = None, description = None):
+        """
+        Initialize demonstration script. This method overrides the base
+        implementation in :py:func:`baseapp.BaseApp.__init__` and it aims to
+        even more simplify the script object creation.
+
+        :param str name: Optional script name.
+        :param str description: Optional script description.
+        """
+        name        = 'demo-zendaemon.py' if not name else name
+        description = 'DemoZenDaemon - Demonstration daemon' if not description else description
+
+        super().__init__(
+            name        = name,
+            description = description,
+
+            #
+            # Configure required application paths to harmless locations.
+            #
+            path_bin = '/tmp',
+            path_cfg = '/tmp',
+            path_log = '/tmp',
+            path_tmp = '/tmp',
+            path_run = '/tmp',
+
+            # Force dhe demonstration daemon to stay in foreground.
+            default_no_daemon = True,
+
+            # Define internal daemon components.
+            components = [
+                DemoDaemonComponent()
+            ],
+
+            # Schedule initial daemon events.
+            schedule = [
+                ('default',)
+            ]
+        )
 
 #-------------------------------------------------------------------------------
 
@@ -957,30 +994,12 @@ class DemoZenDaemon(ZenDaemon):
 if __name__ == "__main__":
 
     # Prepare demonstration environment.
-    pyzenkit.baseapp.BaseApp.json_save('/tmp/demo-zendaemon.py.conf', {'test_a':1})
+    DMN_NAME = 'demo-zendaemon.py'
+    pyzenkit.baseapp.BaseApp.json_save('/tmp/{}.conf'.format(DMN_NAME), {'test_a':1})
     try:
-        os.mkdir('/tmp/demo-zendaemon.py')
+        os.mkdir('/tmp/{}'.format(DMN_NAME))
     except FileExistsError:
         pass
 
-    ZENDAEMON = DemoZenDaemon(
-        name        = 'demo-zenscript.py',
-        description = 'DemoZenDaemon - Demonstration daemon',
-
-        #
-        # Configure required application paths to harmless locations.
-        #
-        path_bin = '/tmp',
-        path_cfg = '/tmp',
-        path_log = '/tmp',
-        path_tmp = '/tmp',
-        path_run = '/tmp',
-
-        default_no_daemon = True,
-
-        schedule = [('default',)],
-        components = [
-            DemoDaemonComponent()
-        ]
-    )
+    ZENDAEMON = DemoZenDaemon(DMN_NAME)
     ZENDAEMON.run()
-- 
GitLab