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

This commit is contained in:
Von Random 2016-10-19 12:19:45 +03:00
parent 7edaa54a6f
commit e2ca550f37
4 changed files with 19 additions and 15 deletions

View file

@ -1,8 +1,8 @@
[main] [main]
plugins = day,time
[day] [day]
plugin = date plugin = date
color = red
format = %%A %%d format = %%A %%d
[time] [time]

View file

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

View file

@ -1,23 +1,22 @@
import json
import os import os
import configparser import configparser
import importlib import importlib
import plugins import plugins
def get_plugins(config):
result = list()
for element in config['main']['plugins'].split(','):
result.append(config[element])
return result
DEFAULT_CONFIG = os.path.join(os.environ['HOME'], 'IdeaProjects/vdstatus/conf.ini') DEFAULT_CONFIG = os.path.join(os.environ['HOME'], 'IdeaProjects/vdstatus/conf.ini')
configuration = configparser.ConfigParser() configuration = configparser.ConfigParser()
configuration.read(DEFAULT_CONFIG) configuration.read(DEFAULT_CONFIG)
plugin_list = get_plugins(configuration)
for plugin in plugin_list: def run_plugins():
plugin_name = '.' + plugin['plugin'] outputs = list()
plugin_module = importlib.import_module(plugin_name, 'plugins') for section in configuration.sections():
print(plugin_module.run(plugin)) if section == 'main':
continue
plugin_name = '.' + configuration.get(section, 'plugin')
plugin_module = importlib.import_module(plugin_name, 'plugins')
outputs.append(plugin_module.run(configuration, section))
print(json.dumps(outputs) + ',')

1
vdstatus_i3 Executable file
View file

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