<s> import re <EOL> from collections import namedtuple <EOL> from contextlib import contextmanager <EOL> import pprint <EOL> import warnings <EOL> import sys <EOL> from six import StringIO <EOL> PY3 = ( sys . version_info [ <NUM_LIT:0> ] >= <NUM_LIT:3> ) <EOL> if PY3 : <EOL> def u ( s ) : <EOL> return s <EOL> else : <EOL> def u ( s ) : <EOL> return unicode ( s , "<STR_LIT>" ) <EOL> DeprecatedOption = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> RegisteredOption = namedtuple ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> _deprecated_options = { } <EOL> _registered_options = { } <EOL> _global_config = { } <EOL> _reserved_keys = [ '<STR_LIT:all>' ] <EOL> class OptionError ( AttributeError , KeyError ) : <EOL> """<STR_LIT>""" <EOL> def _get_single_key ( pat , silent ) : <EOL> keys = _select_options ( pat ) <EOL> if len ( keys ) == <NUM_LIT:0> : <EOL> if not silent : <EOL> _warn_if_deprecated ( pat ) <EOL> raise OptionError ( '<STR_LIT>' % pat ) <EOL> if len ( keys ) > <NUM_LIT:1> : <EOL> raise OptionError ( '<STR_LIT>' ) <EOL> key = keys [ <NUM_LIT:0> ] <EOL> if not silent : <EOL> _warn_if_deprecated ( key ) <EOL> key = _translate_key ( key ) <EOL> return key <EOL> def _get_option ( pat , silent = False ) : <EOL> key = _get_single_key ( pat , silent ) <EOL> root , k = _get_root ( key ) <EOL> return root [ k ] <EOL> def _set_option ( * args , ** kwargs ) : <EOL> nargs = len ( args ) <EOL> if not nargs or nargs % <NUM_LIT:2> != <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> silent = kwargs . get ( '<STR_LIT>' , False ) <EOL> for k , v in zip ( args [ : : <NUM_LIT:2> ] , args [ <NUM_LIT:1> : : <NUM_LIT:2> ] ) : <EOL> key = _get_single_key ( k , silent ) <EOL> o = _get_registered_option ( key ) <EOL> if o and o . validator : <EOL> o . validator ( v ) <EOL> root , k = _get_root ( key ) <EOL> root [ k ] = v <EOL> if o . cb : <EOL> o . cb ( key ) <EOL> def _describe_option ( pat = '<STR_LIT>' , _print_desc = True ) : <EOL> keys = _select_options ( pat ) <EOL> if len ( keys ) == <NUM_LIT:0> : <EOL> raise OptionError ( '<STR_LIT>' ) <EOL> s = u ( '<STR_LIT>' ) <EOL> for k in keys : <EOL> s += _build_option_description ( k ) <EOL> if _print_desc : <EOL> print ( s ) <EOL> else : <EOL> return s <EOL> def _reset_option ( pat , silent = False ) : <EOL> keys = _select_options ( pat ) <EOL> if len ( keys ) == <NUM_LIT:0> : <EOL> raise OptionError ( '<STR_LIT>' ) <EOL> if len ( keys ) > <NUM_LIT:1> and len ( pat ) < <NUM_LIT:4> and pat != '<STR_LIT:all>' : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT:value>' ) <EOL> for k in keys : <EOL> _set_option ( k , _registered_options [ k ] . defval , silent = silent ) <EOL> def get_default_val ( pat ) : <EOL> key = _get_single_key ( pat , silent = True ) <EOL> return _get_registered_option ( key ) . defval <EOL> class DictWrapper ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , d , prefix = "<STR_LIT>" ) : <EOL> object . __setattr__ ( self , "<STR_LIT:d>" , d ) <EOL> object . __setattr__ ( self , "<STR_LIT>" , prefix ) <EOL> def __repr__ ( self ) : <EOL> buf = StringIO ( ) <EOL> pprint . pprint ( self . d , stream = buf ) <EOL> return buf . getvalue ( ) <EOL> def __setattr__ ( self , key , val ) : <EOL> prefix = object . __getattribute__ ( self , "<STR_LIT>" ) <EOL> if prefix : <EOL> prefix += "<STR_LIT:.>" <EOL> prefix += key <EOL> if key in self . d and not isinstance ( self . d [ key ] , dict ) : <EOL> _set_option ( prefix , val ) <EOL> else : <EOL> raise OptionError ( "<STR_LIT>" ) <EOL> def __getattr__ ( self , key ) : <EOL> prefix = object . __getattribute__ ( self , "<STR_LIT>" ) <EOL> if prefix : <EOL> prefix += "<STR_LIT:.>" <EOL> prefix += key <EOL> v = object . __getattribute__ ( self , "<STR_LIT:d>" ) [ key ] <EOL> if isinstance ( v , dict ) : <EOL> return DictWrapper ( v , prefix ) <EOL> else : <EOL> return _get_option ( prefix ) <EOL> def __dir__ ( self ) : <EOL> return list ( self . d . keys ( ) ) <EOL> class CallableDynamicDoc ( object ) : <EOL> def __init__ ( self , func , doc_tmpl ) : <EOL> self . __doc_tmpl__ = doc_tmpl <EOL> self . __func__ = func <EOL> def __call__ ( self , * args , ** kwds ) : <EOL> return self . __func__ ( * args , ** kwds ) <EOL> @ property <EOL> def __doc__ ( self ) : <EOL> opts_desc = _describe_option ( '<STR_LIT:all>' , _print_desc = False ) <EOL> opts_list = pp_options_list ( list ( _registered_options . keys ( ) ) ) <EOL> return self . __doc_tmpl__ . format ( opts_desc = opts_desc , <EOL> opts_list = opts_list ) <EOL> _get_option_tmpl = """<STR_LIT>""" <EOL> _set_option_tmpl = """<STR_LIT>""" <EOL> _describe_option_tmpl = """<STR_LIT>""" <EOL> _reset_option_tmpl = """<STR_LIT>""" <EOL> get_option = CallableDynamicDoc ( _get_option , _get_option_tmpl ) <EOL> set_option = CallableDynamicDoc ( _set_option , _set_option_tmpl ) <EOL> reset_option = CallableDynamicDoc ( _reset_option , _reset_option_tmpl ) <EOL> describe_option = CallableDynamicDoc ( _describe_option , _describe_option_tmpl ) <EOL> options = DictWrapper ( _global_config ) <EOL> class option_context ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args ) : <EOL> if not ( len ( args ) % <NUM_LIT:2> == <NUM_LIT:0> and len ( args ) >= <NUM_LIT:2> ) : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> self . ops = list ( zip ( args [ : : <NUM_LIT:2> ] , args [ <NUM_LIT:1> : : <NUM_LIT:2> ] ) ) <EOL> def __enter__ ( self ) : <EOL> undo = [ ] <EOL> for pat , val in self . ops : <EOL> undo . append ( ( pat , _get_option ( pat , silent = True ) ) ) <EOL> self . undo = undo <EOL> for pat , val in self . ops : <EOL> _set_option ( pat , val , silent = True ) <EOL> def __exit__ ( self , * args ) : <EOL> if self . undo : <EOL> for pat , val in self . undo : <EOL> _set_option ( pat , val , silent = True ) <EOL> def register_option ( key , defval , doc = '<STR_LIT>' , validator = None , cb = None ) : <EOL> """<STR_LIT>""" <EOL> import tokenize <EOL> import keyword <EOL> key = key . lower ( ) <EOL> if key in _registered_options : <EOL> raise OptionError ( "<STR_LIT>" % key ) <EOL> if key in _reserved_keys : <EOL> raise OptionError ( "<STR_LIT>" % key ) <EOL> if validator : <EOL> validator ( defval ) <EOL> path = key . split ( '<STR_LIT:.>' ) <EOL> for k in path : <EOL> if not bool ( re . match ( '<STR_LIT>' + tokenize . Name + '<STR_LIT:$>' , k ) ) : <EOL> raise ValueError ( "<STR_LIT>" % k ) <EOL> if keyword . iskeyword ( k ) : <EOL> raise ValueError ( "<STR_LIT>" % k ) <EOL> cursor = _global_config <EOL> for i , p in enumerate ( path [ : - <NUM_LIT:1> ] ) : <EOL> if not isinstance ( cursor , dict ) : <EOL> raise OptionError ( "<STR_LIT>" <EOL> % '<STR_LIT:.>' . join ( path [ : i ] ) ) <EOL> if p not in cursor : <EOL> cursor [ p ] = { } <EOL> cursor = cursor [ p ] <EOL> if not isinstance ( cursor , dict ) : <EOL> raise OptionError ( "<STR_LIT>" <EOL> % '<STR_LIT:.>' . join ( path [ : - <NUM_LIT:1> ] ) ) <EOL> cursor [ path [ - <NUM_LIT:1> ] ] = defval <EOL> _registered_options [ key ] = RegisteredOption ( key = key , defval = defval , <EOL> doc = doc , validator = validator , <EOL> cb = cb ) <EOL> def deprecate_option ( key , msg = None , rkey = None , removal_ver = None ) : <EOL> """<STR_LIT>""" <EOL> key = key . lower ( ) <EOL> if key in _deprecated_options : <EOL> raise OptionError ( "<STR_LIT>" <EOL> % key ) <EOL> _deprecated_options [ key ] = DeprecatedOption ( key , msg , rkey , removal_ver ) <EOL> def _select_options ( pat ) : <EOL> """<STR_LIT>""" <EOL> if pat in _registered_options : <EOL> return [ pat ] <EOL> keys = sorted ( _registered_options . keys ( ) ) <EOL> if pat == '<STR_LIT:all>' : <EOL> return keys <EOL> return [ k for k in keys if re . search ( pat , k , re . I ) ] <EOL> def _get_root ( key ) : <EOL> path = key . split ( '<STR_LIT:.>' ) <EOL> cursor = _global_config <EOL> for p in path [ : - <NUM_LIT:1> ] : <EOL> cursor = cursor [ p ] <EOL> return cursor , path [ - <NUM_LIT:1> ] <EOL> def _is_deprecated ( key ) : <EOL> """<STR_LIT>""" <EOL> key = key . lower ( ) <EOL> return key in _deprecated_options <EOL> def _get_deprecated_option ( key ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> d = _deprecated_options [ key ] <EOL> except KeyError : <EOL> return None <EOL> else : <EOL> return d <EOL> def _get_registered_option ( key ) : <EOL> """<STR_LIT>""" <EOL> return _registered_options . get ( key ) <EOL> def _translate_key ( key ) : <EOL> """<STR_LIT>""" <EOL> d = _get_deprecated_option ( key ) <EOL> if d : <EOL> return d . rkey or key <EOL> else : <EOL> return key <EOL> def _warn_if_deprecated ( key ) : <EOL> """<STR_LIT>""" <EOL> d = _get_deprecated_option ( key ) <EOL> if d : <EOL> if d . msg : <EOL> print ( d . msg ) <EOL> warnings . warn ( d . msg , DeprecationWarning ) <EOL> else : <EOL> msg = "<STR_LIT>" % key <EOL> if d . removal_ver : <EOL> msg += '<STR_LIT>' % d . removal_ver <EOL> if d . rkey : <EOL> msg += "<STR_LIT>" % d . rkey <EOL> else : <EOL> msg += '<STR_LIT>' <EOL> warnings . warn ( msg , DeprecationWarning ) <EOL> return True <EOL> return False <EOL> def _build_option_description ( k ) : <EOL> """<STR_LIT>""" <EOL> o = _get_registered_option ( k ) <EOL> d = _get_deprecated_option ( k ) <EOL> s = u ( '<STR_LIT>' ) % k <EOL> if o . doc : <EOL> s += '<STR_LIT:\n>' . join ( o . doc . strip ( ) . split ( '<STR_LIT:\n>' ) ) <EOL> else : <EOL> s += '<STR_LIT>' <EOL> if o : <EOL> s += u ( '<STR_LIT>' ) % ( o . defval , <EOL> _get_option ( k , True ) ) <EOL> if d : <EOL> s += u ( '<STR_LIT>' ) <EOL> s += ( u ( '<STR_LIT>' ) % d . rkey if d . rkey else '<STR_LIT>' ) <EOL> s += u ( '<STR_LIT:)>' ) <EOL> s += '<STR_LIT>' <EOL> return s <EOL> def pp_options_list ( keys , width = <NUM_LIT> , _print = False ) : <EOL> """<STR_LIT>""" <EOL> from textwrap import wrap <EOL> from itertools import groupby <EOL> def pp ( name , ks ) : <EOL> pfx = ( '<STR_LIT>' + name + '<STR_LIT>' if name else '<STR_LIT>' ) <EOL> ls = wrap ( '<STR_LIT:U+002CU+0020>' . join ( ks ) , width , initial_indent = pfx , <EOL> subsequent_indent = '<STR_LIT:U+0020>' , break_long_words = False ) <EOL> if ls and ls [ - <NUM_LIT:1> ] and name : <EOL> ls [ - <NUM_LIT:1> ] = ls [ - <NUM_LIT:1> ] + '<STR_LIT:]>' <EOL> return ls <EOL> ls = [ ] <EOL> singles = [ x for x in sorted ( keys ) if x . find ( '<STR_LIT:.>' ) < <NUM_LIT:0> ] <EOL> if singles : <EOL> ls += pp ( '<STR_LIT>' , singles ) <EOL> keys = [ x for x in keys if x . find ( '<STR_LIT:.>' ) >= <NUM_LIT:0> ] <EOL> for k , g in groupby ( sorted ( keys ) , lambda x : x [ : x . rfind ( '<STR_LIT:.>' ) ] ) : <EOL> ks = [ x [ len ( k ) + <NUM_LIT:1> : ] for x in list ( g ) ] <EOL> ls += pp ( k , ks ) <EOL> s = '<STR_LIT:\n>' . join ( ls ) <EOL> if _print : <EOL> print ( s ) <EOL> else : <EOL> return s <EOL> @ contextmanager <EOL> def config_prefix ( prefix ) : <EOL> """<STR_LIT>""" <EOL> global register_option , get_option , set_option , reset_option <EOL> def wrap ( func ) : <EOL> def inner ( key , * args , ** kwds ) : <EOL> pkey = '<STR_LIT>' % ( prefix , key ) <EOL> return func ( pkey , * args , ** kwds ) <EOL> return inner <EOL> _register_option = register_option <EOL> _get_option = get_option <EOL> _set_option = set_option <EOL> set_option = wrap ( set_option ) <EOL> get_option = wrap ( get_option ) <EOL> register_option = wrap ( register_option ) <EOL> yield None <EOL> set_option = _set_option <EOL> get_option = _get_option <EOL> register_option = _register_option <EOL> def is_type_factory ( _type ) : <EOL> """<STR_LIT>""" <EOL> def inner ( x ) : <EOL> if type ( x ) != _type : <EOL> raise ValueError ( "<STR_LIT>" % str ( _type ) ) <EOL> return inner <EOL> def is_instance_factory ( _type ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( _type , ( tuple , list ) ) : <EOL> _type = tuple ( _type ) <EOL> type_repr = "<STR_LIT:|>" . join ( map ( str , _type ) ) <EOL> else : <EOL> type_repr = "<STR_LIT>" % _type <EOL> def inner ( x ) : <EOL> if not isinstance ( x , _type ) : <EOL> raise ValueError ( "<STR_LIT>" % type_repr ) <EOL> return inner <EOL> def is_one_of_factory ( legal_values ) : <EOL> def inner ( x ) : <EOL> if x not in legal_values : <EOL> pp_values = map ( str , legal_values ) <EOL> raise ValueError ( "<STR_LIT>" <EOL> % str ( "<STR_LIT:|>" . join ( pp_values ) ) ) <EOL> return inner <EOL> is_int = is_type_factory ( int ) <EOL> is_bool = is_type_factory ( bool ) <EOL> is_float = is_type_factory ( float ) <EOL> is_str = is_type_factory ( str ) <EOL> is_text = is_instance_factory ( ( str , bytes ) ) </s>
