<s> import importlib <EOL> from docutils import nodes <EOL> from docutils . statemachine import ViewList <EOL> from jinja2 import Template <EOL> from libqtile import command , configurable , widget <EOL> from six import class_types <EOL> from six . moves import builtins , reduce <EOL> from sphinx . util . compat import Directive <EOL> from sphinx . util . nodes import nested_parse_with_titles <EOL> qtile_module_template = Template ( '''<STR_LIT>''' ) <EOL> qtile_class_template = Template ( '''<STR_LIT>''' ) <EOL> qtile_hooks_template = Template ( '''<STR_LIT>''' ) <EOL> def import_object ( module_name , expr ) : <EOL> mod = __import__ ( module_name ) <EOL> mod = reduce ( getattr , module_name . split ( '<STR_LIT:.>' ) [ <NUM_LIT:1> : ] , mod ) <EOL> globals = builtins <EOL> if not isinstance ( globals , dict ) : <EOL> globals = globals . __dict__ <EOL> return eval ( expr , globals , mod . __dict__ ) <EOL> class SimpleDirectiveMixin ( object ) : <EOL> has_content = True <EOL> required_arguments = <NUM_LIT:1> <EOL> def make_rst ( self ) : <EOL> raise NotImplementedError <EOL> def run ( self ) : <EOL> node = nodes . section ( ) <EOL> node . document = self . state . document <EOL> result = ViewList ( ) <EOL> for line in self . make_rst ( ) : <EOL> result . append ( line , '<STR_LIT>' . format ( self . __class__ . __name__ ) ) <EOL> nested_parse_with_titles ( self . state , result , node ) <EOL> return node . children <EOL> class QtileClass ( SimpleDirectiveMixin , Directive ) : <EOL> optional_arguments = <NUM_LIT:2> <EOL> def make_rst ( self ) : <EOL> module , class_name = self . arguments [ <NUM_LIT:0> ] . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> arguments = self . arguments [ <NUM_LIT:1> : ] <EOL> obj = import_object ( module , class_name ) <EOL> is_configurable = '<STR_LIT>' not in arguments <EOL> is_commandable = '<STR_LIT>' not in arguments <EOL> arguments = [ i for i in arguments if i not in ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> defaults = { } <EOL> for klass in reversed ( obj . mro ( ) ) : <EOL> if not issubclass ( klass , configurable . Configurable ) : <EOL> continue <EOL> if not hasattr ( klass , "<STR_LIT>" ) : <EOL> continue <EOL> klass_defaults = getattr ( klass , "<STR_LIT>" ) <EOL> defaults . update ( { <EOL> d [ <NUM_LIT:0> ] : d [ <NUM_LIT:1> : ] for d in klass_defaults <EOL> } ) <EOL> defaults = [ <EOL> ( k , v [ <NUM_LIT:0> ] , v [ <NUM_LIT:1> ] ) for k , v in sorted ( defaults . items ( ) ) <EOL> ] <EOL> context = { <EOL> '<STR_LIT>' : module , <EOL> '<STR_LIT>' : class_name , <EOL> '<STR_LIT>' : "<STR_LIT:=>" * len ( class_name ) , <EOL> '<STR_LIT>' : obj , <EOL> '<STR_LIT>' : defaults , <EOL> '<STR_LIT>' : is_configurable and issubclass ( obj , configurable . Configurable ) , <EOL> '<STR_LIT>' : is_commandable and issubclass ( obj , command . CommandObject ) , <EOL> '<STR_LIT>' : issubclass ( obj , widget . base . _Widget ) , <EOL> '<STR_LIT>' : arguments , <EOL> } <EOL> if context [ '<STR_LIT>' ] : <EOL> context [ '<STR_LIT>' ] = [ attr for attr in dir ( obj ) <EOL> if attr . startswith ( '<STR_LIT>' ) ] <EOL> rst = qtile_class_template . render ( ** context ) <EOL> for line in rst . splitlines ( ) : <EOL> yield line <EOL> class QtileHooks ( SimpleDirectiveMixin , Directive ) : <EOL> def make_rst ( self ) : <EOL> module , class_name = self . arguments [ <NUM_LIT:0> ] . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> obj = import_object ( module , class_name ) <EOL> for method in obj . hooks : <EOL> rst = qtile_hooks_template . render ( method = method ) <EOL> for line in rst . splitlines ( ) : <EOL> yield line <EOL> class QtileModule ( SimpleDirectiveMixin , Directive ) : <EOL> optional_arguments = <NUM_LIT:4> <EOL> def make_rst ( self ) : <EOL> module = importlib . import_module ( self . arguments [ <NUM_LIT:0> ] ) <EOL> BaseClass = None <EOL> if '<STR_LIT>' in self . arguments : <EOL> BaseClass = import_object ( * self . arguments [ <EOL> self . arguments . index ( '<STR_LIT>' ) + <NUM_LIT:1> ] . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) ) <EOL> for item in dir ( module ) : <EOL> obj = import_object ( self . arguments [ <NUM_LIT:0> ] , item ) <EOL> if not isinstance ( obj , class_types ) and ( BaseClass and <EOL> not isinstance ( obj , BaseClass ) ) : <EOL> continue <EOL> context = { <EOL> '<STR_LIT>' : self . arguments [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : item , <EOL> '<STR_LIT>' : '<STR_LIT>' in self . arguments , <EOL> '<STR_LIT>' : '<STR_LIT>' in self . arguments , <EOL> } <EOL> rst = qtile_module_template . render ( ** context ) <EOL> for line in rst . splitlines ( ) : <EOL> if not line . strip ( ) : <EOL> continue <EOL> yield line <EOL> def setup ( app ) : <EOL> app . add_directive ( '<STR_LIT>' , QtileClass ) <EOL> app . add_directive ( '<STR_LIT>' , QtileHooks ) <EOL> app . add_directive ( '<STR_LIT>' , QtileModule ) </s>
