2016-11-05 07:54:36 +01:00
|
|
|
import os
|
|
|
|
import argparse
|
|
|
|
import textwrap
|
|
|
|
|
|
|
|
import bumblebee.theme
|
|
|
|
import bumblebee.module
|
|
|
|
|
2016-11-05 12:07:54 +01:00
|
|
|
class print_usage(argparse.Action):
|
|
|
|
def __init__(self, option_strings, dest, nargs=None, **kwargs):
|
|
|
|
argparse.Action.__init__(self, option_strings, dest, nargs, **kwargs)
|
|
|
|
self._indent = " "*4
|
|
|
|
|
|
|
|
def __call__(self, parser, namespace, value, option_string=None):
|
|
|
|
if value == "modules":
|
|
|
|
self.print_modules()
|
|
|
|
elif value == "themes":
|
|
|
|
self.print_themes()
|
|
|
|
else:
|
|
|
|
parser.print_help()
|
|
|
|
parser.exit()
|
|
|
|
|
|
|
|
def print_themes(self):
|
|
|
|
print(textwrap.fill(", ".join(bumblebee.theme.themes()),
|
|
|
|
80, initial_indent = self._indent, subsequent_indent = self._indent
|
|
|
|
))
|
|
|
|
|
|
|
|
def print_modules(self):
|
|
|
|
for m in bumblebee.module.modules():
|
2016-11-05 16:33:35 +01:00
|
|
|
print(textwrap.fill("{}: {}".format(m.name(), m.description()),
|
|
|
|
80, initial_indent=self._indent*2, subsequent_indent=self._indent*3))
|
|
|
|
print("{}Parameters:".format(self._indent*2))
|
2016-11-05 16:18:53 +01:00
|
|
|
for p in m.parameters():
|
2016-11-05 16:33:35 +01:00
|
|
|
print(textwrap.fill("* {}".format(p),
|
|
|
|
80, initial_indent=self._indent*3, subsequent_indent=self._indent*4))
|
|
|
|
print("")
|
2016-11-05 12:07:54 +01:00
|
|
|
|
2016-11-05 14:26:02 +01:00
|
|
|
class ModuleConfig(object):
|
2016-12-02 18:53:34 +01:00
|
|
|
def __init__(self, config, name, alias):
|
|
|
|
self._prefix = alias if alias else name
|
2016-11-05 14:26:02 +01:00
|
|
|
self._config = config
|
|
|
|
|
2016-12-02 18:53:34 +01:00
|
|
|
def prefix(self):
|
|
|
|
return self._prefix
|
|
|
|
|
2016-11-05 14:26:02 +01:00
|
|
|
def set(self, name, value):
|
|
|
|
name = self._prefix + name
|
|
|
|
return self._config.set(name, value)
|
|
|
|
|
|
|
|
def parameter(self, name, default=None):
|
|
|
|
name = self._prefix + name
|
|
|
|
return self._config.parameter(name, default)
|
|
|
|
|
|
|
|
def increase(self, name, limit, default):
|
|
|
|
name = self._prefix + name
|
|
|
|
return self._config.increase(name, limit, default)
|
|
|
|
|
2016-11-05 07:54:36 +01:00
|
|
|
class Config(object):
|
|
|
|
def __init__(self, args):
|
2016-11-05 08:11:08 +01:00
|
|
|
self._parser = self._parser()
|
2016-11-05 11:57:09 +01:00
|
|
|
self._store = {}
|
2016-11-05 07:54:36 +01:00
|
|
|
|
|
|
|
if len(args) == 0:
|
|
|
|
self._parser.print_help()
|
|
|
|
self._parser.exit()
|
|
|
|
|
|
|
|
self._args = self._parser.parse_args(args)
|
|
|
|
|
2016-11-05 12:28:05 +01:00
|
|
|
for p in self._args.parameters:
|
|
|
|
key, value = p.split("=")
|
|
|
|
self.parameter(key, value)
|
|
|
|
|
|
|
|
def set(self, name, value):
|
|
|
|
self._store[name] = value
|
|
|
|
|
2016-11-05 12:18:30 +01:00
|
|
|
def parameter(self, name, default=None):
|
2016-11-05 11:57:09 +01:00
|
|
|
if not name in self._store:
|
2016-11-05 12:28:05 +01:00
|
|
|
self.set(name, default)
|
2016-11-05 11:57:09 +01:00
|
|
|
return self._store.get(name, default)
|
|
|
|
|
|
|
|
def increase(self, name, limit, default):
|
2016-12-02 22:35:28 +01:00
|
|
|
if not name in self._store:
|
|
|
|
self._store[name] = default
|
|
|
|
return default
|
|
|
|
|
2016-11-05 11:57:09 +01:00
|
|
|
self._store[name] += 1
|
|
|
|
if self._store[name] >= limit:
|
|
|
|
self._store[name] = default
|
|
|
|
return self._store[name]
|
|
|
|
|
2016-11-05 08:11:08 +01:00
|
|
|
def theme(self):
|
|
|
|
return self._args.theme
|
|
|
|
|
|
|
|
def modules(self):
|
2016-11-05 14:26:02 +01:00
|
|
|
result = []
|
|
|
|
for m in self._args.modules:
|
|
|
|
items = m.split(":")
|
|
|
|
result.append({ "name": items[0], "alias": items[1] if len(items) > 1 else None })
|
|
|
|
return result
|
2016-11-05 07:54:36 +01:00
|
|
|
|
2016-11-05 08:11:08 +01:00
|
|
|
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" ],
|
2016-11-05 12:07:54 +01:00
|
|
|
action=print_usage,
|
2016-11-05 08:11:08 +01:00
|
|
|
)
|
2016-11-05 12:28:05 +01:00
|
|
|
parser.add_argument("-p", "--parameters", nargs="+",
|
|
|
|
help="Provide configuration parameters to individual modules.",
|
|
|
|
default=[]
|
|
|
|
)
|
2016-11-05 08:11:08 +01:00
|
|
|
parser.add_argument("-t", "--theme", help="Specify which theme to use for "
|
|
|
|
"drawing the modules",
|
|
|
|
default="default",
|
|
|
|
)
|
|
|
|
|
|
|
|
return parser
|
|
|
|
|
2016-11-05 07:54:36 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|