Browse Source

start making a proper module from vdstatus.py, also properly use configparser

master
Von Random 2 years ago
parent
commit
e2ca550f37
4 changed files with 19 additions and 15 deletions
  1. 1
    1
      conf.ini
  2. 7
    3
      plugins/date.py
  3. 10
    11
      vdstatus.py
  4. 1
    0
      vdstatus_i3

+ 1
- 1
conf.ini View File

@@ -1,8 +1,8 @@
1 1
 [main]
2
-plugins = day,time
3 2
 
4 3
 [day]
5 4
 plugin = date
5
+color = red
6 6
 format = %%A %%d
7 7
 
8 8
 [time]

+ 7
- 3
plugins/date.py View File

@@ -1,6 +1,10 @@
1 1
 import time
2 2
 
3 3
 
4
-def run(conf):
5
-    fmt = conf['format']
6
-    return time.strftime(fmt)
4
+def run(config, section):
5
+    fmt = config.get(section, 'format')
6
+    result = dict()
7
+    if config.has_option(section, 'color'):
8
+        result['color'] = config.get(section, 'color')
9
+    result['full_text'] = time.strftime(fmt)
10
+    return result

+ 10
- 11
vdstatus.py View File

@@ -1,23 +1,22 @@
1
+import json
1 2
 import os
2 3
 import configparser
3 4
 import importlib
4 5
 import plugins
5 6
 
6 7
 
7
-def get_plugins(config):
8
-    result = list()
9
-    for element in config['main']['plugins'].split(','):
10
-        result.append(config[element])
11
-    return result
12
-
13 8
 DEFAULT_CONFIG = os.path.join(os.environ['HOME'], 'IdeaProjects/vdstatus/conf.ini')
14 9
 
15 10
 configuration = configparser.ConfigParser()
16 11
 configuration.read(DEFAULT_CONFIG)
17 12
 
18
-plugin_list = get_plugins(configuration)
19 13
 
20
-for plugin in plugin_list:
21
-    plugin_name = '.' + plugin['plugin']
22
-    plugin_module = importlib.import_module(plugin_name, 'plugins')
23
-    print(plugin_module.run(plugin))
14
+def run_plugins():
15
+    outputs = list()
16
+    for section in configuration.sections():
17
+        if section == 'main':
18
+            continue
19
+        plugin_name = '.' + configuration.get(section, 'plugin')
20
+        plugin_module = importlib.import_module(plugin_name, 'plugins')
21
+        outputs.append(plugin_module.run(configuration, section))
22
+    print(json.dumps(outputs) + ',')

+ 1
- 0
vdstatus_i3 View File

@@ -0,0 +1 @@
1
+#!/usr/bin/env python3 -u

Loading…
Cancel
Save