Sfoglia il codice sorgente

vdstatus, plugins: some refactoring and better usage of dict() methods; rename fga to fortune and make uri configurable

master
Von Random 1 mese fa
parent
commit
151c94c5f3
10 ha cambiato i file con 56 aggiunte e 47 eliminazioni
  1. 7
    11
      plugins/__init__.py
  2. 6
    6
      plugins/batt.py
  3. 5
    3
      plugins/date.py
  4. 4
    2
      plugins/disk.py
  5. 8
    6
      plugins/fortune.py
  6. 4
    2
      plugins/load.py
  7. 4
    2
      plugins/mem.py
  8. 7
    7
      plugins/pacman.py
  9. 8
    4
      plugins/ping.py
  10. 3
    4
      vdstatus

+ 7
- 11
plugins/__init__.py Vedi File

@@ -2,21 +2,17 @@ import threading
2 2
 import time
3 3
 
4 4
 
5
-def parse_config(config, defaults):
6
-    result = dict()
7
-    for key in defaults:
8
-        result[key] = config[key] if key in config else defaults[key]
9
-    return result
5
+PLUGIN_DEFAULTS = {'freq': 1, 'hide_ok': True}
10 6
 
11 7
 
12 8
 class PluginThreadCommon:
13
-    def __init__(self, config, defaults=dict()):
14
-        if 'freq' not in defaults:
15
-            defaults['freq'] = 1
16
-        if 'hide_ok' not in defaults:
17
-            defaults['hide_ok'] = True
18
-        self.conf = parse_config(config, defaults)
9
+    def __init__(self, config, defaults=None):
19 10
         self.status = dict()
11
+        self.conf = dict()
12
+        self.conf.update(PLUGIN_DEFAULTS)
13
+        if defaults:
14
+            self.conf.update(defaults)
15
+        self.conf.update(config)
20 16
         self.hide = False
21 17
         self.thread = threading.Thread(target=self.run)
22 18
         self.thread.daemon = True

+ 6
- 6
plugins/batt.py Vedi File

@@ -2,16 +2,16 @@ import plugins
2 2
 
3 3
 
4 4
 BATTERY_DIR = '/sys/class/power_supply/BAT0/'
5
+BATT_DEFAULTS = {
6
+    'problem': 15,
7
+    'symbol_charging': '\u2191',
8
+    'symbol_discharging': '\u2193'
9
+}
5 10
 
6 11
 
7 12
 class PluginThread(plugins.PluginThreadCommon):
8 13
     def __init__(self, config):
9
-        defaults = {
10
-            'problem': 15,
11
-            'symbol_charging': '\u2191',
12
-            'symbol_discharging': '\u2193'
13
-        }
14
-        super(PluginThread, self).__init__(config, defaults)
14
+        super(PluginThread, self).__init__(config, BATT_DEFAULTS)
15 15
 
16 16
     def main(self):
17 17
         with \

+ 5
- 3
plugins/date.py Vedi File

@@ -2,12 +2,14 @@ import datetime
2 2
 import plugins
3 3
 
4 4
 
5
+DATE_DEFAULTS = {'format': '%c'}
6
+
7
+
5 8
 class PluginThread(plugins.PluginThreadCommon):
6 9
     def __init__(self, config):
7
-        defaults = {'format': '%c', 'tz': None}
8
-        super(PluginThread, self).__init__(config, defaults)
10
+        super(PluginThread, self).__init__(config, DATE_DEFAULTS)
9 11
         self.timezone = None
10
-        if self.conf['tz']:
12
+        if 'tz' in self.conf:
11 13
             import pytz
12 14
             self.timezone = pytz.timezone(self.conf['tz'])
13 15
 

+ 4
- 2
plugins/disk.py Vedi File

@@ -2,10 +2,12 @@ import plugins
2 2
 import psutil
3 3
 
4 4
 
5
+DISK_DEFAULTS = {'partition': '/', 'problem': 80, 'freq': 15}
6
+
7
+
5 8
 class PluginThread(plugins.PluginThreadCommon):
6 9
     def __init__(self, config):
7
-        defaults = {'partition': '/', 'problem': 80, 'freq': 15}
8
-        super(PluginThread, self).__init__(config, defaults)
10
+        super(PluginThread, self).__init__(config, DISK_DEFAULTS)
9 11
 
10 12
     def main(self):
11 13
         du_stat = psutil.disk_usage(self.conf['partition'])

plugins/fga.py → plugins/fortune.py Vedi File

@@ -3,17 +3,20 @@ import requests
3 3
 import time
4 4
 
5 5
 
6
-URI = 'http://fucking-great-advice.ru/api/random'
6
+FGA_DEFAULTS = {
7
+    'uri': 'http://fucking-great-advice.ru/api/random',
8
+    'freq': 120, 'retry': 3
9
+}
10
+
11
+
7 12
 class PluginThread(plugins.PluginThreadCommon):
8 13
     def __init__(self, config):
9
-        defaults = {'freq': 120, 'retry': 3}
10
-        super(PluginThread, self).__init__(config, defaults)
14
+        super(PluginThread, self).__init__(config, FGA_DEFAULTS)
11 15
         self.retry = False
12 16
 
13
-
14 17
     def main(self):
15 18
         try:
16
-            req = requests.get(URI, timeout=2)
19
+            req = requests.get(self.conf['uri'], timeout=2)
17 20
             advice = req.json()['text'] if req.status_code == 200 else 'N/A'
18 21
             self.retry = False
19 22
         except requests.exceptions.Timeout:
@@ -24,7 +27,6 @@ class PluginThread(plugins.PluginThreadCommon):
24 27
             self.retry = True
25 28
         self.status['full_text'] = advice
26 29
 
27
-
28 30
     def run(self):
29 31
         while True:
30 32
             self.main()

+ 4
- 2
plugins/load.py Vedi File

@@ -2,10 +2,12 @@ import os
2 2
 import plugins
3 3
 
4 4
 
5
+LOAD_DEFAULTS = {'freq': 20, 'problem': 1}
6
+
7
+
5 8
 class PluginThread(plugins.PluginThreadCommon):
6 9
     def __init__(self, config):
7
-        defaults = {'freq': 20, 'problem': 1}
8
-        super(PluginThread, self).__init__(config, defaults)
10
+        super(PluginThread, self).__init__(config, LOAD_DEFAULTS)
9 11
 
10 12
     def main(self):
11 13
         loads = os.getloadavg()

+ 4
- 2
plugins/mem.py Vedi File

@@ -2,10 +2,12 @@ import psutil
2 2
 import plugins
3 3
 
4 4
 
5
+MEM_DEFAULTS = {'problem': 85}
6
+
7
+
5 8
 class PluginThread(plugins.PluginThreadCommon):
6 9
     def __init__(self, config):
7
-        defaults = {'problem': 85}
8
-        super(PluginThread, self).__init__(config, defaults)
10
+        super(PluginThread, self).__init__(config, MEM_DEFAULTS)
9 11
 
10 12
     def main(self):
11 13
         mem_stat = psutil.virtual_memory()

+ 7
- 7
plugins/pacman.py Vedi File

@@ -2,15 +2,15 @@ import plugins
2 2
 import subprocess
3 3
 
4 4
 
5
+PACMAN_DEFAULTS = {
6
+    'cmd': ('/usr/bin/pacman', '-Qu'),
7
+    'title': 'UPD', 'freq': 15, 'problem': 10
8
+}
9
+
10
+
5 11
 class PluginThread(plugins.PluginThreadCommon):
6 12
     def __init__(self, config):
7
-        defaults = {
8
-            'cmd': ('/usr/bin/pacman', '-Qu'),
9
-            'title': 'UPD',
10
-            'freq': 15,
11
-            'problem': 10
12
-        }
13
-        super(PluginThread, self).__init__(config, defaults)
13
+        super(PluginThread, self).__init__(config, PACMAN_DEFAULTS)
14 14
         self.format_status(0)
15 15
 
16 16
     def format_status(self, count):

+ 8
- 4
plugins/ping.py Vedi File

@@ -3,15 +3,19 @@ import random
3 3
 import plugins
4 4
 
5 5
 
6
+PING_DEFAULTS = {
7
+    'hosts': list(), 'title': 'PING', 'timeout': 150
8
+}
9
+
10
+
6 11
 class PluginThread(plugins.PluginThreadCommon):
7 12
     def __init__(self, config):
8
-        defaults = {'hosts': list(), 'title': 'PING', 'timeout': 150}
9
-        super(PluginThread, self).__init__(config, defaults)
13
+        super(PluginThread, self).__init__(config, PING_DEFAULTS)
10 14
         self.format_status('n/a')
11 15
 
12 16
     def format_status(self, state):
13 17
         self.status['full_text'] = self.conf['title'] + ': ' + state
14
-        if state == 'on':
18
+        if state == 'up':
15 19
             self.status['urgent'] = False
16 20
             self.hide = True
17 21
         else:
@@ -26,4 +30,4 @@ class PluginThread(plugins.PluginThreadCommon):
26 30
             if response == 0:
27 31
                 self.format_status('on')
28 32
                 break
29
-            self.format_status('off')
33
+            self.format_status('down')

+ 3
- 4
vdstatus Vedi File

@@ -6,7 +6,6 @@ import os
6 6
 import sys
7 7
 import time
8 8
 import yaml
9
-import plugins
10 9
 
11 10
 
12 11
 DEFAULT_CONFIG = os.path.join(os.environ['HOME'], '.config/vdstatus/conf.yaml')
@@ -28,10 +27,10 @@ def parse_arguments():
28 27
 
29 28
 class PluginRunner:
30 29
     def __init__(self, config_file=DEFAULT_CONFIG):
31
-        config = dict()
30
+        self.conf = dict()
31
+        self.conf.update(DEFAULTS)
32 32
         with open(config_file) as config_data:
33
-            config = yaml.load(config_data)
34
-        self.conf = plugins.parse_config(config, DEFAULTS)
33
+            self.conf.update(yaml.load(config_data))
35 34
         self.plugins_loaded = list()
36 35
         self.format_output = self.format_term
37 36
         for plugin in self.conf['plugins']:

Loading…
Annulla
Salva