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-07-08 08:11:52 +02:00
DEBUG_HELP = " Enable debug log, This will create ' ~/bumblebee-status-debug.log ' by default, can be changed with the ' -f ' option "
2020-02-12 18:43:01 +01:00
ICONMARKUP_HELP = " A Python format string that is valid Pango markup used for low level customization of icons on top of themes. There is no validation performed, this is delegated to the user. Used together with --markup=pango. Example: \" <span foreground= ' #ffffff ' background= ' #000000 ' > {} </span> \" . WARNING: highly experimental feature "
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 " :
2019-12-27 13:49:09 +01:00
self . _args = namespace
2016-12-17 07:05:23 +01:00
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 ) :
2017-10-17 18:53:02 +02:00
print ( " , " . join ( bumblebee . theme . themes ( ) ) )
2016-12-17 07:05:23 +01:00
def print_modules ( self ) :
2019-12-27 13:49:09 +01:00
if self . _args . list_format == " markdown " :
print ( " # Table of modules " )
print ( " |Name |Description | " )
print ( " |-----|------------| " )
2016-12-17 07:05:23 +01:00
for m in bumblebee . engine . all_modules ( ) :
2018-03-24 08:04:00 +01:00
try :
mod = importlib . import_module ( " bumblebee.modules. {} " . format ( m [ " name " ] ) )
2019-12-27 13:49:09 +01:00
if self . _args . list_format == " markdown " :
print ( " | {} | {} | " . format ( m [ " name " ] , mod . __doc__ . replace ( " \n " , " <br> " ) ) )
else :
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 ) )
2018-03-24 08:04:00 +01:00
except :
pass
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 " )
2019-02-15 18:01:06 +01:00
parser . add_argument ( " -m " , " --modules " , nargs = " + " , action = ' append ' , default = [ ] ,
2016-12-08 11:31:20 +01:00
help = MODULE_HELP )
parser . add_argument ( " -t " , " --theme " , default = " default " , help = THEME_HELP )
2019-12-24 13:58:29 +01:00
parser . add_argument ( " --markup " , default = " none " , help = " Specify the markup type of the output (e.g. ' pango ' ) " )
2020-02-12 18:43:01 +01:00
parser . add_argument ( " --iconmarkup " , default = " none " , help = ICONMARKUP_HELP )
2019-02-15 18:01:06 +01:00
parser . add_argument ( " -p " , " --parameters " , nargs = " + " , action = ' append ' , default = [ ] ,
2016-12-09 07:57:21 +01:00
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 )
2019-12-27 13:49:09 +01:00
parser . add_argument ( " --list-format " , choices = [ " plain " , " markdown " ] , help = " output format of -l, *must* be specified before -l " )
2017-04-02 08:31:23 +02:00
parser . add_argument ( " -d " , " --debug " , action = " store_true " ,
help = DEBUG_HELP )
2018-09-22 14:40:32 +02:00
parser . add_argument ( " -r " , " --right-to-left " , action = " store_true " ,
help = " Draw widgets from right to left, rather than left to right (which is the default) " )
2017-04-04 15:14:54 +02:00
parser . add_argument ( " -f " , " --logfile " , default = " ~/bumblebee-status-debug.log " ,
help = " Location of the debug log file " )
2018-04-29 16:12:39 +02:00
parser . add_argument ( " -i " , " --iconset " , default = " auto " ,
help = " Specify the name of an iconset to use (overrides theme default) " )
2019-01-19 14:51:48 +01:00
parser . add_argument ( " -a " , " --autohide " , nargs = " + " , default = [ ] ,
help = " Specify a list of modules to hide when not in warning/error state " )
2017-04-04 15:14:54 +02:00
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 :
2017-10-18 08:36:07 +02:00
logging . getLogger ( ) . disabled = True
2017-04-02 08:31:23 +02:00
2019-02-15 18:01:06 +01:00
parameters = [ item for sub in self . _args . parameters for item in sub ]
for param in parameters :
2019-08-16 08:42:57 +02:00
key , value = param . split ( " = " , 1 )
2016-12-09 07:57:21 +01:00
self . set ( key , value )
2016-11-05 08:11:08 +01:00
def modules ( self ) :
2019-02-15 18:01:06 +01:00
modules = [ item for sub in self . _args . modules for item in sub ]
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 ] ,
2019-02-15 18:01:06 +01:00
} for x in 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
2017-10-13 17:09:09 +02:00
2018-04-29 16:12:39 +02:00
def iconset ( self ) :
""" Return the name of a user-specified icon-set """
return self . _args . iconset
2017-04-04 15:14:54 +02:00
def debug ( self ) :
return self . _args . debug
2018-09-22 14:40:32 +02:00
def reverse ( self ) :
return self . _args . right_to_left
2017-04-04 15:14:54 +02:00
def logfile ( self ) :
2017-08-20 10:59:39 +02:00
return os . path . expanduser ( self . _args . logfile )
2016-12-08 11:31:20 +01:00
2019-01-19 14:51:48 +01:00
def autohide ( self ) :
return self . _args . autohide
2019-12-24 13:54:41 +01:00
def markup ( self ) :
return self . _args . markup
2020-02-12 18:43:01 +01:00
def iconmarkup ( self ) :
return self . _args . iconmarkup
2016-11-05 07:54:36 +01:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4