pvl/args.py
changeset 28 69e1b91cd83f
parent 27 62159e5b6685
child 29 8fcb140f1ee0
equal deleted inserted replaced
27:62159e5b6685 28:69e1b91cd83f
    29     general.add_option('-D', '--debug',     dest='loglevel', action='store_const', const=logging.DEBUG, help="Even more output")
    29     general.add_option('-D', '--debug',     dest='loglevel', action='store_const', const=logging.DEBUG, help="Even more output")
    30     general.add_option('--log-file',                                                                    help="Log to file")
    30     general.add_option('--log-file',                                                                    help="Log to file")
    31     general.add_option('--debug-module',    action='append', metavar='MODULE', 
    31     general.add_option('--debug-module',    action='append', metavar='MODULE', 
    32             help="Enable logging for the given logger/module name")
    32             help="Enable logging for the given logger/module name")
    33 
    33 
    34     parser.add_option('-c', '--config',    metavar='PATH',
    34     parser.add_option('-c', '--config',    metavar='PATH',      action='append',
    35             help="Read option defaults from config")
    35             help="Read option defaults from config")
    36     parser.add_option('--config-encoding',  metavar='CHARSET',  default='utf-8',
    36     parser.add_option('--config-encoding',  metavar='CHARSET',  default='utf-8',
    37             help="Unicode decoding for config file")
    37             help="Unicode decoding for config file")
    38 
    38 
    39    
    39    
    45     parser.set_defaults(
    45     parser.set_defaults(
    46         setuid              = setuid,
    46         setuid              = setuid,
    47         logname             = parser.prog,
    47         logname             = parser.prog,
    48         loglevel            = logging.WARN,
    48         loglevel            = logging.WARN,
    49         debug_module        = [],
    49         debug_module        = [],
       
    50         config              = [],
    50     )
    51     )
    51  
    52  
    52     return general
    53     return general
    53 
    54 
    54 def options (**options) :
    55 def options (**options) :
   144         Passed to OptionParser.parse_args(), called by Option.take_action():
   145         Passed to OptionParser.parse_args(), called by Option.take_action():
   145             setattr(values, dest, ...)
   146             setattr(values, dest, ...)
   146             values.ensure_value(dest, ...)
   147             values.ensure_value(dest, ...)
   147     """
   148     """
   148 
   149 
   149     def __init__ (self, defaults={ }) :
   150     def __init__ (self, parser, defaults={ }) :
       
   151         self._parser    = parser
   150         self._defaults  = defaults
   152         self._defaults  = defaults
   151         self._options   = { }
   153         self._options   = { }
   152 
   154 
   153     def __setattr__ (self, name, value) :
   155     def __setattr__ (self, name, value) :
   154         if name.startswith('_') :
   156         if name.startswith('_') :
   183         elif name in self._defaults :
   185         elif name in self._defaults :
   184             return self._defaults[name]
   186             return self._defaults[name]
   185         else :
   187         else :
   186             raise AttributeError(name)
   188             raise AttributeError(name)
   187 
   189 
       
   190     def error (self, msg) :
       
   191         """
       
   192             Raises an optparse error.
       
   193         """
       
   194 
       
   195         self._parser.error(msg)
       
   196 
   188 def apply_config (options, parser, config, encoding=None) :
   197 def apply_config (options, parser, config, encoding=None) :
   189     """
   198     """
   190         Load options from config.
   199         Load options from config.
   191     """
   200     """
   192 
   201 
   193     import configobj
   202     import configobj
   194         
   203         
   195     config = configobj.ConfigObj(config,
   204     config = configobj.ConfigObj(config,
   196             encoding        = options.config_encoding if encoding is None else encoding,
   205             encoding        = options.config_encoding if encoding is None else encoding,
   197     )
   206     )
   198     config_options = Options(options._defaults)
   207     config_options = Options(parser, options._defaults)
   199 
   208 
   200     # load scalars
   209     # load scalars
   201     for scalar in config.scalars :
   210     for scalar in config.scalars :
   202         # option from config
   211         # option from config
   203         option = parser._long_opt.get('--' + scalar)
   212         option = parser._long_opt.get('--' + scalar)
   224 def parse (parser, argv) :
   233 def parse (parser, argv) :
   225     """
   234     """
   226         Parse options, args from argv.
   235         Parse options, args from argv.
   227     """
   236     """
   228 
   237 
   229     options, args = parser.parse_args(argv[1:], values=Options(parser.defaults))
   238     options, args = parser.parse_args(argv[1:], values=Options(parser, parser.defaults))
   230 
   239 
   231     if options.config :
   240     for config in options.config :
   232         options = apply_config(options, parser, options.config)
   241         options = apply_config(options, parser, config)
   233     
   242     
   234     return options, args
   243     return options, args
   235 
   244 
   236 def apply (options, logname=None, rootok=True) :
   245 def apply (options, logname=None, rootok=True) :
   237     """
   246     """