[config] Start refactoring by creating separate config class

Add a class that will hold all configuration and argument information
and serve as central repository for this kind of information.
This commit is contained in:
Tobias Witek 2016-11-05 07:54:36 +01:00
parent df27355977
commit a58610f3ee
4 changed files with 100 additions and 2 deletions

View file

@ -1,11 +1,13 @@
#!/usr/bin/env python #!/usr/bin/env python
import sys
import bumblebee.config
import bumblebee.engine import bumblebee.engine
def main(): def main():
args = bumblebee.engine.Arguments() config = bumblebee.config.Config(sys.argv[1:])
engine = bumblebee.engine.Engine(args.args()) engine = bumblebee.engine.Engine(config)
engine.load_modules() engine.load_modules()
engine.register_events() engine.register_events()

59
bumblebee/config.py Normal file
View file

@ -0,0 +1,59 @@
import os
import argparse
import textwrap
import bumblebee.theme
import bumblebee.module
class Config(object):
def __init__(self, args):
self._raw = args
self._parser = self.parser()
if len(args) == 0:
self._parser.print_help()
self._parser.exit()
self._args = self._parser.parse_args(args)
if self._args.list == "modules":
self.print_modules()
if self._args.list == "themes":
self.print_themes()
if self._args.list:
self._parser.exit()
def parser(self):
parser = argparse.ArgumentParser(description="display system data in the i3bar")
parser.add_argument("-m", "--modules", nargs="+",
help="List of modules to load. The order of the list determines "
"their order in the i3bar (from left to right)",
default=[])
parser.add_argument("-l", "--list",
help="List: 'modules', 'themes' ",
choices = [ "modules", "themes" ],
default="modules")
parser.add_argument("-t", "--theme", help="Specify which theme to use for "
"drawing the modules",
default="default")
return parser
def print_themes(self):
print(textwrap.fill(", ".join(bumblebee.theme.themes()),
80, initial_indent = " ", subsequent_indent = " "
))
def print_modules(self):
for m in bumblebee.module.modules():
print " {}: ".format(m.name())
print textwrap.fill("Description: {}".format(m.description()),
80, initial_indent=" ", subsequent_indent=" ")
print textwrap.fill("Usage : {}".format(m.usage()),
80, initial_indent=" ", subsequent_indent=" ")
print textwrap.fill("Notes : {}".format(m.notes()),
80, initial_indent=" ", subsequent_indent=" ")
print ""
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -1,3 +1,35 @@
import os
import pkgutil
import importlib
import bumblebee.modules
def modules():
result = []
path = os.path.dirname(bumblebee.modules.__file__)
for mod in [ name for _, name, _ in pkgutil.iter_modules([path])]:
result.append(ModuleDescription(mod))
return result
class ModuleDescription(object):
def __init__(self, name):
self._name = name
self._mod =importlib.import_module("bumblebee.modules.{}".format(name))
def name(self):
return str(self._name)
def description(self):
return getattr(self._mod, "description", self.na)()
def usage(self):
return getattr(self._mod, "usage", self.na)()
def notes(self):
return getattr(self._mod, "notes", self.na)()
def na(self):
return "n/a"
class Module(object): class Module(object):
def __init__(self, args): def __init__(self, args):

View file

@ -1,9 +1,14 @@
import os import os
import json import json
import glob
def getpath(): def getpath():
return os.path.dirname("{}/themes/".format(os.path.dirname(os.path.realpath(__file__)))) return os.path.dirname("{}/themes/".format(os.path.dirname(os.path.realpath(__file__))))
def themes():
d = getpath()
return [ os.path.basename(f).replace(".json", "") for f in glob.iglob("{}/*.json".format(d)) ]
class Theme: class Theme:
_cycle_index = 0 _cycle_index = 0
_cycle = None _cycle = None