2016-12-04 08:02:40 +01:00
""" Configuration handling
This module provides configuration information ( loaded modules ,
module parameters , etc . ) to all other components
"""
2017-04-02 08:31:23 +02:00
import os
2016-12-17 08:42:23 +01:00
import sys
2017-04-02 08:31:23 +02:00
import logging
2016-11-05 07:54:36 +01:00
import argparse
2016-12-17 07:05:23 +01:00
import textwrap
import importlib
2016-12-09 07:57:21 +01:00
import bumblebee . store
2016-11-05 07:54:36 +01:00
2016-12-15 19:44:49 +01:00
MODULE_HELP = " Specify a space-separated list of modules to load. The order of the list determines their order in the i3bar (from left to right). Use <module>:<alias> to provide an alias in case you want to load the same module multiple times, but specify different parameters. "
THEME_HELP = " Specify the theme to use for drawing modules "
PARAMETER_HELP = " Provide configuration parameters in the form of <module>.<key>=<value> "
2016-12-17 07:05:23 +01:00
LIST_HELP = " Display a list of either available themes or available modules along with their parameters. "
2017-04-02 08:31:23 +02:00
DEBUG_HELP = " Enable debug log ( ' debug.log ' located in the same directory as the bumblebee-status executable) "
2016-12-17 07:05:23 +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 = " " * 2
def __call__ ( self , parser , namespace , value , option_string = None ) :
if value == " modules " :
self . print_modules ( )
elif value == " themes " :
self . print_themes ( )
2016-12-17 08:42:23 +01:00
sys . exit ( 0 )
2016-12-17 07:05:23 +01:00
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 . engine . all_modules ( ) :
mod = importlib . import_module ( " bumblebee.modules. {} " . format ( m [ " name " ] ) )
print ( textwrap . fill ( " {} : " . format ( m [ " name " ] ) , 80 ,
initial_indent = self . _indent * 2 , subsequent_indent = self . _indent * 2 ) )
for line in mod . __doc__ . split ( " \n " ) :
print ( textwrap . fill ( line , 80 ,
initial_indent = self . _indent * 3 , subsequent_indent = self . _indent * 6 ) )
2016-11-05 14:26:02 +01:00
2016-12-04 08:02:40 +01:00
def create_parser ( ) :
""" Create the argument parser """
parser = argparse . ArgumentParser ( description = " display system data in the i3bar " )
parser . add_argument ( " -m " , " --modules " , nargs = " + " , default = [ ] ,
2016-12-08 11:31:20 +01:00
help = MODULE_HELP )
parser . add_argument ( " -t " , " --theme " , default = " default " , help = THEME_HELP )
2016-12-09 07:57:21 +01:00
parser . add_argument ( " -p " , " --parameters " , nargs = " + " , default = [ ] ,
help = PARAMETER_HELP )
2016-12-17 07:05:23 +01:00
parser . add_argument ( " -l " , " --list " , choices = [ " modules " , " themes " ] , action = print_usage ,
help = LIST_HELP )
2017-04-02 08:31:23 +02:00
parser . add_argument ( " -d " , " --debug " , action = " store_true " ,
help = DEBUG_HELP )
2016-12-04 08:02:40 +01:00
return parser
2016-12-09 07:57:21 +01:00
class Config ( bumblebee . store . Store ) :
2016-12-04 08:02:40 +01:00
""" Top-level configuration class
Parses commandline arguments and provides non - module
specific configuration information .
"""
def __init__ ( self , args = None ) :
2016-12-09 07:57:21 +01:00
super ( Config , self ) . __init__ ( )
2016-12-04 08:02:40 +01:00
parser = create_parser ( )
self . _args = parser . parse_args ( args if args else [ ] )
2016-11-05 08:11:08 +01:00
2017-04-02 08:31:23 +02:00
if not self . _args . debug :
logger = logging . getLogger ( ) . disabled = True
2016-12-09 07:57:21 +01:00
for param in self . _args . parameters :
key , value = param . split ( " = " )
self . set ( key , value )
2016-11-05 08:11:08 +01:00
def modules ( self ) :
2016-12-04 08:02:40 +01:00
""" Return a list of all activated modules """
return [ {
2016-12-03 20:54:57 +01:00
" module " : x . split ( " : " ) [ 0 ] ,
2016-12-04 08:02:40 +01:00
" name " : x if not " : " in x else x . split ( " : " ) [ 1 ] ,
} for x in self . _args . modules ]
2016-11-05 08:11:08 +01:00
2016-12-08 11:31:20 +01:00
def theme ( self ) :
""" Return the name of the selected theme """
return self . _args . theme
2016-11-05 07:54:36 +01:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4