<s> import copy <EOL> import os . path <EOL> import re <EOL> from ConfigParser import ConfigParser , ParsingError <EOL> from genshi . builder import tag <EOL> from trac . admin import AdminCommandError , IAdminCommandProvider <EOL> from trac . core import Component , ExtensionPoint , TracError , implements <EOL> from trac . util import AtomicFile , as_bool <EOL> from trac . util . compat import OrderedDict , wait_for_file_mtime_change <EOL> from trac . util . text import cleandoc , printout , to_unicode , to_utf8 <EOL> from trac . util . translation import N_ , _ , dgettext , tag_ <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> _use_default = object ( ) <EOL> def _getint ( value ) : <EOL> return int ( value or <NUM_LIT:0> ) <EOL> def _getfloat ( value ) : <EOL> return float ( value or <NUM_LIT:0.0> ) <EOL> def _getlist ( value , sep , keep_empty ) : <EOL> if not value : <EOL> return [ ] <EOL> if isinstance ( value , basestring ) : <EOL> if isinstance ( sep , ( list , tuple ) ) : <EOL> splitted = re . split ( '<STR_LIT:|>' . join ( map ( re . escape , sep ) ) , value ) <EOL> else : <EOL> splitted = value . split ( sep ) <EOL> items = [ item . strip ( ) for item in splitted ] <EOL> else : <EOL> items = list ( value ) <EOL> if not keep_empty : <EOL> items = [ item for item in items if item not in ( None , '<STR_LIT>' ) ] <EOL> return items <EOL> def _getdoc ( option_or_section ) : <EOL> doc = to_unicode ( option_or_section . __doc__ ) <EOL> if doc : <EOL> doc = dgettext ( option_or_section . doc_domain , doc , <EOL> ** ( option_or_section . doc_args or { } ) ) <EOL> return doc <EOL> class ConfigurationError ( TracError ) : <EOL> """<STR_LIT>""" <EOL> title = N_ ( "<STR_LIT>" ) <EOL> def __init__ ( self , message = None , title = None , show_traceback = False ) : <EOL> if message is None : <EOL> message = _ ( "<STR_LIT>" ) <EOL> super ( ConfigurationError , self ) . __init__ ( message , title , <EOL> show_traceback ) <EOL> class UnicodeConfigParser ( ConfigParser ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> dict_type = kwargs . pop ( '<STR_LIT>' , None ) or OrderedDict <EOL> ConfigParser . __init__ ( self , dict_type = dict_type , ** kwargs ) <EOL> def sections ( self ) : <EOL> return map ( to_unicode , ConfigParser . sections ( self ) ) <EOL> def add_section ( self , section ) : <EOL> section_str = to_utf8 ( section ) <EOL> ConfigParser . add_section ( self , section_str ) <EOL> def has_section ( self , section ) : <EOL> section_str = to_utf8 ( section ) <EOL> return ConfigParser . has_section ( self , section_str ) <EOL> def options ( self , section ) : <EOL> section_str = to_utf8 ( section ) <EOL> return map ( to_unicode , ConfigParser . options ( self , section_str ) ) <EOL> def get ( self , section , option , raw = False , vars = None ) : <EOL> section_str = to_utf8 ( section ) <EOL> option_str = to_utf8 ( option ) <EOL> return to_unicode ( ConfigParser . get ( self , section_str , <EOL> option_str , raw , vars ) ) <EOL> def items ( self , section , raw = False , vars = None ) : <EOL> section_str = to_utf8 ( section ) <EOL> return [ ( to_unicode ( k ) , to_unicode ( v ) ) <EOL> for k , v in ConfigParser . items ( self , section_str , raw , vars ) ] <EOL> def has_option ( self , section , option ) : <EOL> section_str = to_utf8 ( section ) <EOL> option_str = to_utf8 ( option ) <EOL> return ConfigParser . has_option ( self , section_str , option_str ) <EOL> def set ( self , section , option , value = None ) : <EOL> section_str = to_utf8 ( section ) <EOL> option_str = to_utf8 ( option ) <EOL> value_str = to_utf8 ( value ) if value is not None else '<STR_LIT>' <EOL> ConfigParser . set ( self , section_str , option_str , value_str ) <EOL> def remove_option ( self , section , option ) : <EOL> section_str = to_utf8 ( section ) <EOL> option_str = to_utf8 ( option ) <EOL> ConfigParser . remove_option ( self , section_str , option_str ) <EOL> def remove_section ( self , section ) : <EOL> section_str = to_utf8 ( section ) <EOL> ConfigParser . remove_section ( self , section_str ) <EOL> def __copy__ ( self ) : <EOL> parser = self . __class__ ( ) <EOL> parser . _sections = copy . copy ( self . _sections ) <EOL> return parser <EOL> def __deepcopy__ ( self , memo ) : <EOL> parser = self . __class__ ( ) <EOL> parser . _sections = copy . deepcopy ( self . _sections ) <EOL> return parser <EOL> class Configuration ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filename , params = { } ) : <EOL> self . filename = filename <EOL> self . parser = UnicodeConfigParser ( ) <EOL> self . _pristine_parser = None <EOL> self . parents = [ ] <EOL> self . _lastmtime = <NUM_LIT:0> <EOL> self . _sections = { } <EOL> self . parse_if_needed ( force = True ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . filename ) <EOL> def __contains__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return name in self . sections ( ) <EOL> def __getitem__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name not in self . _sections : <EOL> self . _sections [ name ] = Section ( self , name ) <EOL> return self . _sections [ name ] <EOL> @ property <EOL> def exists ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . path . isfile ( self . filename ) <EOL> def get ( self , section , key , default = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return self [ section ] . get ( key , default ) <EOL> def getbool ( self , section , key , default = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return self [ section ] . getbool ( key , default ) <EOL> def getint ( self , section , key , default = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return self [ section ] . getint ( key , default ) <EOL> def getfloat ( self , section , key , default = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return self [ section ] . getfloat ( key , default ) <EOL> def getlist ( self , section , key , default = '<STR_LIT>' , sep = '<STR_LIT:U+002C>' , keep_empty = False ) : <EOL> """<STR_LIT>""" <EOL> return self [ section ] . getlist ( key , default , sep , keep_empty ) <EOL> def getpath ( self , section , key , default = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return self [ section ] . getpath ( key , default ) <EOL> def set ( self , section , key , value ) : <EOL> """<STR_LIT>""" <EOL> self [ section ] . set ( key , value ) <EOL> def defaults ( self , compmgr = None ) : <EOL> """<STR_LIT>""" <EOL> defaults = { } <EOL> for ( section , key ) , option in Option . get_registry ( compmgr ) . iteritems ( ) : <EOL> defaults . setdefault ( section , { } ) [ key ] = option . dumps ( option . default ) <EOL> return defaults <EOL> def options ( self , section , compmgr = None ) : <EOL> """<STR_LIT>""" <EOL> return self [ section ] . options ( compmgr ) <EOL> def remove ( self , section , key ) : <EOL> """<STR_LIT>""" <EOL> self [ section ] . remove ( key ) <EOL> def sections ( self , compmgr = None , defaults = True ) : <EOL> """<STR_LIT>""" <EOL> sections = set ( self . parser . sections ( ) ) <EOL> for parent in self . parents : <EOL> sections . update ( parent . sections ( compmgr , defaults = False ) ) <EOL> if defaults : <EOL> sections . update ( self . defaults ( compmgr ) ) <EOL> return sorted ( sections ) <EOL> def has_option ( self , section , option , defaults = True ) : <EOL> """<STR_LIT>""" <EOL> return self [ section ] . contains ( option , defaults ) <EOL> def save ( self ) : <EOL> """<STR_LIT>""" <EOL> all_options = { } <EOL> for ( section , name ) , option in Option . get_registry ( ) . iteritems ( ) : <EOL> all_options . setdefault ( section , { } ) [ name ] = option <EOL> def normalize ( section , name , value ) : <EOL> option = all_options . get ( section , { } ) . get ( name ) <EOL> return option . normalize ( value ) if option else value <EOL> sections = [ ] <EOL> for section in self . sections ( ) : <EOL> options = [ ] <EOL> for option in self [ section ] : <EOL> default = None <EOL> for parent in self . parents : <EOL> if parent . has_option ( section , option , defaults = False ) : <EOL> default = normalize ( section , option , <EOL> parent . get ( section , option ) ) <EOL> break <EOL> if self . parser . has_option ( section , option ) : <EOL> current = normalize ( section , option , <EOL> self . parser . get ( section , option ) ) <EOL> if current != default : <EOL> options . append ( ( option , current ) ) <EOL> if options : <EOL> sections . append ( ( section , sorted ( options ) ) ) <EOL> parser = UnicodeConfigParser ( ) <EOL> for section , options in sections : <EOL> parser . add_section ( section ) <EOL> for key , val in options : <EOL> parser . set ( section , key , val ) <EOL> try : <EOL> self . _write ( parser ) <EOL> except Exception : <EOL> self . parser = copy . deepcopy ( self . _pristine_parser ) <EOL> raise <EOL> else : <EOL> self . _pristine_parser = copy . deepcopy ( self . parser ) <EOL> def parse_if_needed ( self , force = False ) : <EOL> if not self . filename or not self . exists : <EOL> return False <EOL> changed = False <EOL> modtime = os . path . getmtime ( self . filename ) <EOL> if force or modtime != self . _lastmtime : <EOL> self . parser = UnicodeConfigParser ( ) <EOL> try : <EOL> if not self . parser . read ( self . filename ) : <EOL> raise TracError ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" , file = self . filename ) ) <EOL> except ParsingError as e : <EOL> raise TracError ( e ) <EOL> self . _lastmtime = modtime <EOL> self . _pristine_parser = copy . deepcopy ( self . parser ) <EOL> changed = True <EOL> if changed : <EOL> self . parents = self . _get_parents ( ) <EOL> else : <EOL> for parent in self . parents : <EOL> changed |= parent . parse_if_needed ( force = force ) <EOL> if changed : <EOL> self . _sections = { } <EOL> return changed <EOL> def touch ( self ) : <EOL> if self . filename and self . exists and os . access ( self . filename , os . W_OK ) : <EOL> wait_for_file_mtime_change ( self . filename ) <EOL> def set_defaults ( self , compmgr = None , component = None ) : <EOL> """<STR_LIT>""" <EOL> def set_option_default ( option ) : <EOL> section = option . section <EOL> name = option . name <EOL> if not self . has_option ( section , name , defaults = False ) : <EOL> value = option . dumps ( option . default ) <EOL> self . set ( section , name , value ) <EOL> if component : <EOL> if component . endswith ( '<STR_LIT>' ) : <EOL> component = component [ : - <NUM_LIT:2> ] <EOL> component = component . lower ( ) . split ( '<STR_LIT:.>' ) <EOL> from trac . core import ComponentMeta <EOL> for cls in ComponentMeta . _components : <EOL> clsname = ( cls . __module__ + '<STR_LIT:.>' + cls . __name__ ) . lower ( ) . split ( '<STR_LIT:.>' ) <EOL> if clsname [ : len ( component ) ] == component : <EOL> for option in cls . __dict__ . itervalues ( ) : <EOL> if isinstance ( option , Option ) : <EOL> set_option_default ( option ) <EOL> else : <EOL> for option in Option . get_registry ( compmgr ) . itervalues ( ) : <EOL> set_option_default ( option ) <EOL> def _get_parents ( self ) : <EOL> _parents = [ ] <EOL> if self . parser . has_option ( '<STR_LIT>' , '<STR_LIT:file>' ) : <EOL> for filename in self . parser . get ( '<STR_LIT>' , '<STR_LIT:file>' ) . split ( '<STR_LIT:U+002C>' ) : <EOL> filename = filename . strip ( ) <EOL> if not os . path . isabs ( filename ) : <EOL> filename = os . path . join ( os . path . dirname ( self . filename ) , <EOL> filename ) <EOL> _parents . append ( Configuration ( filename ) ) <EOL> return _parents <EOL> def _write ( self , parser ) : <EOL> if not self . filename : <EOL> return <EOL> wait_for_file_mtime_change ( self . filename ) <EOL> with AtomicFile ( self . filename , '<STR_LIT:w>' ) as fd : <EOL> fd . writelines ( [ '<STR_LIT>' , '<STR_LIT:\n>' ] ) <EOL> parser . write ( fd ) <EOL> class Section ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT>' ] <EOL> def __init__ ( self , config , name ) : <EOL> self . config = config <EOL> self . name = name <EOL> self . _cache = { } <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . name ) <EOL> def contains ( self , key , defaults = True ) : <EOL> if self . config . parser . has_option ( self . name , key ) : <EOL> return True <EOL> for parent in self . config . parents : <EOL> if parent [ self . name ] . contains ( key , defaults = False ) : <EOL> return True <EOL> return defaults and ( self . name , key ) in Option . registry <EOL> __contains__ = contains <EOL> def iterate ( self , compmgr = None , defaults = True ) : <EOL> """<STR_LIT>""" <EOL> options = set ( ) <EOL> if self . config . parser . has_section ( self . name ) : <EOL> for option in self . config . parser . options ( self . name ) : <EOL> options . add ( option . lower ( ) ) <EOL> yield option <EOL> for parent in self . config . parents : <EOL> for option in parent [ self . name ] . iterate ( defaults = False ) : <EOL> loption = option . lower ( ) <EOL> if loption not in options : <EOL> options . add ( loption ) <EOL> yield option <EOL> if defaults : <EOL> for section , option in Option . get_registry ( compmgr ) . iterkeys ( ) : <EOL> if section == self . name and option . lower ( ) not in options : <EOL> yield option <EOL> __iter__ = iterate <EOL> def get ( self , key , default = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> cached = self . _cache . get ( key , _use_default ) <EOL> if cached is not _use_default : <EOL> return cached <EOL> if self . config . parser . has_option ( self . name , key ) : <EOL> value = self . config . parser . get ( self . name , key ) <EOL> else : <EOL> for parent in self . config . parents : <EOL> value = parent [ self . name ] . get ( key , _use_default ) <EOL> if value is not _use_default : <EOL> break <EOL> else : <EOL> if default is not _use_default : <EOL> option = Option . registry . get ( ( self . name , key ) ) <EOL> value = option . dumps ( option . default ) if option else _use_default <EOL> else : <EOL> value = _use_default <EOL> if value is _use_default : <EOL> return default <EOL> self . _cache [ key ] = value <EOL> return value <EOL> def getbool ( self , key , default = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return as_bool ( self . get ( key , default ) ) <EOL> def getint ( self , key , default = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> value = self . get ( key , default ) <EOL> try : <EOL> return _getint ( value ) <EOL> except ValueError : <EOL> raise ConfigurationError ( <EOL> _ ( '<STR_LIT>' <EOL> '<STR_LIT>' , section = self . name , entry = key , <EOL> value = repr ( value ) ) ) <EOL> def getfloat ( self , key , default = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> value = self . get ( key , default ) <EOL> try : <EOL> return _getfloat ( value ) <EOL> except ValueError : <EOL> raise ConfigurationError ( <EOL> _ ( '<STR_LIT>' <EOL> '<STR_LIT>' , section = self . name , entry = key , <EOL> value = repr ( value ) ) ) <EOL> def getlist ( self , key , default = '<STR_LIT>' , sep = '<STR_LIT:U+002C>' , keep_empty = True ) : <EOL> """<STR_LIT>""" <EOL> return _getlist ( self . get ( key , default ) , sep , keep_empty ) <EOL> def getpath ( self , key , default = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> path = self . get ( key , default ) <EOL> if not path : <EOL> return default <EOL> if not os . path . isabs ( path ) : <EOL> path = os . path . join ( os . path . dirname ( self . config . filename ) , path ) <EOL> return os . path . normcase ( os . path . realpath ( path ) ) <EOL> def options ( self , compmgr = None ) : <EOL> """<STR_LIT>""" <EOL> for key in self . iterate ( compmgr ) : <EOL> yield key , self . get ( key ) <EOL> def set ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> self . _cache . pop ( key , None ) <EOL> if not self . config . parser . has_section ( self . name ) : <EOL> self . config . parser . add_section ( self . name ) <EOL> return self . config . parser . set ( self . name , key , value ) <EOL> def remove ( self , key ) : <EOL> """<STR_LIT>""" <EOL> if self . config . parser . has_section ( self . name ) : <EOL> self . _cache . pop ( key , None ) <EOL> self . config . parser . remove_option ( self . name , key ) <EOL> def _get_registry ( cls , compmgr = None ) : <EOL> """<STR_LIT>""" <EOL> if compmgr is None : <EOL> return cls . registry <EOL> from trac . core import ComponentMeta <EOL> components = { } <EOL> for comp in ComponentMeta . _components : <EOL> for attr in comp . __dict__ . itervalues ( ) : <EOL> if isinstance ( attr , cls ) : <EOL> components [ attr ] = comp <EOL> return dict ( each for each in cls . registry . iteritems ( ) <EOL> if each [ <NUM_LIT:1> ] not in components <EOL> or compmgr . is_enabled ( components [ each [ <NUM_LIT:1> ] ] ) ) <EOL> class ConfigSection ( object ) : <EOL> """<STR_LIT>""" <EOL> registry = { } <EOL> @ staticmethod <EOL> def get_registry ( compmgr = None ) : <EOL> """<STR_LIT>""" <EOL> return _get_registry ( ConfigSection , compmgr ) <EOL> def __init__ ( self , name , doc , doc_domain = '<STR_LIT>' , doc_args = None ) : <EOL> """<STR_LIT>""" <EOL> self . name = name <EOL> self . registry [ self . name ] = self <EOL> self . __doc__ = cleandoc ( doc ) <EOL> self . doc_domain = doc_domain <EOL> self . doc_args = doc_args <EOL> def __get__ ( self , instance , owner ) : <EOL> if instance is None : <EOL> return self <EOL> config = getattr ( instance , '<STR_LIT>' , None ) <EOL> if config and isinstance ( config , Configuration ) : <EOL> return config [ self . name ] <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . name ) <EOL> @ property <EOL> def doc ( self ) : <EOL> """<STR_LIT>""" <EOL> return _getdoc ( self ) <EOL> class Option ( object ) : <EOL> """<STR_LIT>""" <EOL> registry = { } <EOL> def accessor ( self , section , name , default ) : <EOL> return section . get ( name , default ) <EOL> @ staticmethod <EOL> def get_registry ( compmgr = None ) : <EOL> """<STR_LIT>""" <EOL> return _get_registry ( Option , compmgr ) <EOL> def __init__ ( self , section , name , default = None , doc = '<STR_LIT>' , <EOL> doc_domain = '<STR_LIT>' , doc_args = None ) : <EOL> """<STR_LIT>""" <EOL> self . section = section <EOL> self . name = name <EOL> self . default = self . normalize ( default ) <EOL> self . registry [ ( self . section , self . name ) ] = self <EOL> self . __doc__ = cleandoc ( doc ) <EOL> self . doc_domain = doc_domain <EOL> self . doc_args = doc_args <EOL> def __get__ ( self , instance , owner ) : <EOL> if instance is None : <EOL> return self <EOL> config = getattr ( instance , '<STR_LIT>' , None ) <EOL> if config and isinstance ( config , Configuration ) : <EOL> section = config [ self . section ] <EOL> value = self . accessor ( section , self . name , self . default ) <EOL> return value <EOL> def __set__ ( self , instance , value ) : <EOL> raise AttributeError ( _ ( "<STR_LIT>" ) ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . section , <EOL> self . name ) <EOL> @ property <EOL> def doc ( self ) : <EOL> """<STR_LIT>""" <EOL> return _getdoc ( self ) <EOL> def dumps ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> return '<STR_LIT>' <EOL> if value is True : <EOL> return '<STR_LIT>' <EOL> if value is False : <EOL> return '<STR_LIT>' <EOL> if isinstance ( value , unicode ) : <EOL> return value <EOL> return to_unicode ( value ) <EOL> def normalize ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return self . dumps ( value ) <EOL> class BoolOption ( Option ) : <EOL> """<STR_LIT>""" <EOL> def accessor ( self , section , name , default ) : <EOL> return section . getbool ( name , default ) <EOL> def normalize ( self , value ) : <EOL> if value not in ( True , False ) : <EOL> value = as_bool ( value ) <EOL> return self . dumps ( value ) <EOL> class IntOption ( Option ) : <EOL> """<STR_LIT>""" <EOL> def accessor ( self , section , name , default ) : <EOL> return section . getint ( name , default ) <EOL> def normalize ( self , value ) : <EOL> try : <EOL> value = _getint ( value ) <EOL> except ValueError : <EOL> pass <EOL> return self . dumps ( value ) <EOL> class FloatOption ( Option ) : <EOL> """<STR_LIT>""" <EOL> def accessor ( self , section , name , default ) : <EOL> return section . getfloat ( name , default ) <EOL> def normalize ( self , value ) : <EOL> try : <EOL> value = _getfloat ( value ) <EOL> except ValueError : <EOL> pass <EOL> return self . dumps ( value ) <EOL> class ListOption ( Option ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , section , name , default = None , sep = '<STR_LIT:U+002C>' , keep_empty = False , <EOL> doc = '<STR_LIT>' , doc_domain = '<STR_LIT>' , doc_args = None ) : <EOL> self . sep = sep <EOL> self . keep_empty = keep_empty <EOL> Option . __init__ ( self , section , name , default , doc , doc_domain , <EOL> doc_args ) <EOL> def accessor ( self , section , name , default ) : <EOL> return section . getlist ( name , default , self . sep , self . keep_empty ) <EOL> def dumps ( self , value ) : <EOL> if isinstance ( value , ( list , tuple ) ) : <EOL> sep = self . sep <EOL> if isinstance ( sep , ( list , tuple ) ) : <EOL> sep = sep [ <NUM_LIT:0> ] <EOL> return sep . join ( Option . dumps ( self , v ) or '<STR_LIT>' for v in value ) <EOL> return Option . dumps ( self , value ) <EOL> def normalize ( self , value ) : <EOL> return self . dumps ( _getlist ( value , self . sep , self . keep_empty ) ) <EOL> class ChoiceOption ( Option ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , section , name , choices , doc = '<STR_LIT>' , doc_domain = '<STR_LIT>' , <EOL> doc_args = None ) : <EOL> Option . __init__ ( self , section , name , to_unicode ( choices [ <NUM_LIT:0> ] ) , doc , <EOL> doc_domain , doc_args ) <EOL> self . choices = set ( to_unicode ( c ) . strip ( ) for c in choices ) <EOL> def accessor ( self , section , name , default ) : <EOL> value = section . get ( name , default ) <EOL> if value not in self . choices : <EOL> raise ConfigurationError ( <EOL> _ ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> section = section . name , entry = name , value = repr ( value ) , <EOL> choices = '<STR_LIT:U+002CU+0020>' . join ( '<STR_LIT>' % c <EOL> for c in sorted ( self . choices ) ) ) ) <EOL> return value <EOL> class PathOption ( Option ) : <EOL> """<STR_LIT>""" <EOL> def accessor ( self , section , name , default ) : <EOL> return section . getpath ( name , default ) <EOL> class ExtensionOption ( Option ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , section , name , interface , default = None , doc = '<STR_LIT>' , <EOL> doc_domain = '<STR_LIT>' , doc_args = None ) : <EOL> Option . __init__ ( self , section , name , default , doc , doc_domain , <EOL> doc_args ) <EOL> self . xtnpt = ExtensionPoint ( interface ) <EOL> def __get__ ( self , instance , owner ) : <EOL> if instance is None : <EOL> return self <EOL> value = Option . __get__ ( self , instance , owner ) <EOL> for impl in self . xtnpt . extensions ( instance ) : <EOL> if impl . __class__ . __name__ == value : <EOL> return impl <EOL> raise ConfigurationError ( <EOL> tag_ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> interface = tag . code ( self . xtnpt . interface . __name__ ) , <EOL> implementation = tag . code ( value ) , <EOL> option = tag . code ( "<STR_LIT>" % ( self . section , self . name ) ) ) ) <EOL> class OrderedExtensionsOption ( ListOption ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , section , name , interface , default = None , <EOL> include_missing = True , doc = '<STR_LIT>' , doc_domain = '<STR_LIT>' , <EOL> doc_args = None ) : <EOL> ListOption . __init__ ( self , section , name , default , doc = doc , <EOL> doc_domain = doc_domain , doc_args = doc_args ) <EOL> self . xtnpt = ExtensionPoint ( interface ) <EOL> self . include_missing = include_missing <EOL> def __get__ ( self , instance , owner ) : <EOL> if instance is None : <EOL> return self <EOL> order = ListOption . __get__ ( self , instance , owner ) <EOL> components = [ ] <EOL> implementing_classes = [ ] <EOL> for impl in self . xtnpt . extensions ( instance ) : <EOL> implementing_classes . append ( impl . __class__ . __name__ ) <EOL> if self . include_missing or impl . __class__ . __name__ in order : <EOL> components . append ( impl ) <EOL> not_found = sorted ( set ( order ) - set ( implementing_classes ) ) <EOL> if not_found : <EOL> raise ConfigurationError ( <EOL> tag_ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> interface = tag . code ( self . xtnpt . interface . __name__ ) , <EOL> implementation = tag ( <EOL> ( '<STR_LIT:U+002CU+0020>' if idx != <NUM_LIT:0> else None , tag . code ( impl ) ) <EOL> for idx , impl in enumerate ( not_found ) ) , <EOL> option = tag . code ( "<STR_LIT>" % ( self . section , self . name ) ) ) ) <EOL> def compare ( x , y ) : <EOL> x , y = x . __class__ . __name__ , y . __class__ . __name__ <EOL> if x not in order : <EOL> return int ( y in order ) <EOL> if y not in order : <EOL> return - int ( x in order ) <EOL> return cmp ( order . index ( x ) , order . index ( y ) ) <EOL> components . sort ( compare ) <EOL> return components <EOL> class ConfigurationAdmin ( Component ) : <EOL> """<STR_LIT>""" <EOL> implements ( IAdminCommandProvider ) <EOL> def get_admin_commands ( self ) : <EOL> yield ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> self . _complete_config , self . _do_get ) <EOL> yield ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> self . _complete_config , self . _do_remove ) <EOL> yield ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> self . _complete_config , self . _do_set ) <EOL> def _complete_config ( self , args ) : <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> return self . config . sections ( ) <EOL> elif len ( args ) == <NUM_LIT:2> : <EOL> return [ name for ( name , value ) in self . config [ args [ <NUM_LIT:0> ] ] . options ( ) ] <EOL> def _do_get ( self , section , option ) : <EOL> if not self . config . has_option ( section , option ) : <EOL> raise AdminCommandError ( <EOL> _ ( "<STR_LIT>" <EOL> "<STR_LIT>" , option = option , section = section ) ) <EOL> printout ( self . config . get ( section , option ) ) <EOL> def _do_set ( self , section , option , value ) : <EOL> self . config . set ( section , option , value ) <EOL> if section == '<STR_LIT>' and as_bool ( value ) : <EOL> self . config . set_defaults ( component = option ) <EOL> self . config . save ( ) <EOL> if section == '<STR_LIT>' and option == '<STR_LIT:file>' : <EOL> self . config . parse_if_needed ( force = True ) <EOL> def _do_remove ( self , section , option ) : <EOL> if not self . config . has_option ( section , option ) : <EOL> raise AdminCommandError ( <EOL> _ ( "<STR_LIT>" <EOL> "<STR_LIT>" , option = option , section = section ) ) <EOL> self . config . remove ( section , option ) <EOL> self . config . save ( ) <EOL> if section == '<STR_LIT>' and option == '<STR_LIT:file>' : <EOL> self . config . parse_if_needed ( force = True ) <EOL> def get_configinfo ( env ) : <EOL> """<STR_LIT>""" <EOL> all_options = { } <EOL> for ( section , name ) , option in Option . get_registry ( env . compmgr ) . iteritems ( ) : <EOL> all_options . setdefault ( section , { } ) [ name ] = option <EOL> sections = [ ] <EOL> for section in env . config . sections ( env . compmgr ) : <EOL> options = [ ] <EOL> for name , value in env . config . options ( section , env . compmgr ) : <EOL> registered = all_options . get ( section , { } ) . get ( name ) <EOL> if registered : <EOL> default = registered . default <EOL> normalized = registered . normalize ( value ) <EOL> else : <EOL> default = u'<STR_LIT>' <EOL> normalized = unicode ( value ) <EOL> options . append ( { '<STR_LIT:name>' : name , '<STR_LIT:value>' : value , <EOL> '<STR_LIT>' : normalized != default } ) <EOL> options . sort ( key = lambda o : o [ '<STR_LIT:name>' ] ) <EOL> sections . append ( { '<STR_LIT:name>' : section , '<STR_LIT>' : options } ) <EOL> sections . sort ( key = lambda s : s [ '<STR_LIT:name>' ] ) <EOL> return sections </s>
