Source code for tornado.options

#!/usr/bin/env python## Copyright 2009 Facebook## Licensed under the Apache License, Version 2.0 (the "License"); you may# not use this file except in compliance with the License. You may obtain# a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the# License for the specific language governing permissions and limitations# under the License."""A command line parsing module that lets modules define their own options.Each module defines its own options which are added to the globaloption namespace, e.g.:: from tornado.options import define, options define("mysql_host", default="127.0.0.1:3306", help="Main user DB") define("memcache_hosts", default="127.0.0.1:11011", multiple=True, help="Main user memcache servers") def connect(): db = database.Connection(options.mysql_host) ...The ``main()`` method of your application does not need to be aware of all ofthe options used throughout your program; they are all automatically loadedwhen the modules are loaded. However, all modules that define optionsmust have been imported before the command line is parsed.Your ``main()`` method can parse the command line or parse a config file witheither:: tornado.options.parse_command_line() # or tornado.options.parse_config_file("/etc/server.conf").. note: When using tornado.options.parse_command_line or tornado.options.parse_config_file, the only options that are set are ones that were previously defined with tornado.options.define.Command line formats are what you would expect (``--myoption=myvalue``).Config files are just Python files. Global names become options, e.g.:: myoption = "myvalue" myotheroption = "myothervalue"We support `datetimes <datetime.datetime>`, `timedeltas<datetime.timedelta>`, ints, and floats (just pass a ``type`` kwarg to`define`). We also accept multi-value options. See the documentation for`define()` below.`tornado.options.options` is a singleton instance of `OptionParser`, andthe top-level functions in this module (`define`, `parse_command_line`, etc)simply call methods on it. You may create additional `OptionParser`instances to define isolated sets of options, such as for subcommands... note:: By default, several options are defined that will configure the standard `logging` module when `parse_command_line` or `parse_config_file` are called. If you want Tornado to leave the logging configuration alone so you can manage it yourself, either pass ``--logging=none`` on the command line or do the following to disable it in code:: from tornado.options import options, parse_command_line options.logging = None parse_command_line().. versionchanged:: 4.3 Dashes and underscores are fully interchangeable in option names; options can be defined, set, and read with any mix of the two. Dashes are typical for command-line usage while config files require underscores."""from__future__importabsolute_import,division,print_function,with_statementimportdatetimeimportnumbersimportreimportsysimportosimporttextwrapfromtornado.escapeimport_unicode,native_strfromtornado.logimportdefine_logging_optionsfromtornadoimportstack_contextfromtornado.utilimportbasestring_type,exec_in

[docs]classError(Exception):"""Exception raised by errors in the options module."""pass

[docs]classOptionParser(object):"""A collection of options, a dictionary with object-like access. Normally accessed via static functions in the `tornado.options` module, which reference a global instance. """def__init__(self):# we have to use self.__dict__ because we override setattr.self.__dict__['_options']={}self.__dict__['_parse_callbacks']=[]self.define("help",type=bool,help="show this help information",callback=self._help_callback)def_normalize_name(self,name):returnname.replace('_','-')def__getattr__(self,name):name=self._normalize_name(name)ifisinstance(self._options.get(name),_Option):returnself._options[name].value()raiseAttributeError("Unrecognized option %r"%name)def__setattr__(self,name,value):name=self._normalize_name(name)ifisinstance(self._options.get(name),_Option):returnself._options[name].set(value)raiseAttributeError("Unrecognized option %r"%name)def__iter__(self):return(opt.nameforoptinself._options.values())def__contains__(self,name):name=self._normalize_name(name)returnnameinself._optionsdef__getitem__(self,name):returnself.__getattr__(name)def__setitem__(self,name,value):returnself.__setattr__(name,value)

[docs]defdefine(self,name,default=None,type=None,help=None,metavar=None,multiple=False,group=None,callback=None):"""Defines a new command line option. If ``type`` is given (one of str, float, int, datetime, or timedelta) or can be inferred from the ``default``, we parse the command line arguments based on the given type. If ``multiple`` is True, we accept comma-separated values, and the option value is always a list. For multi-value integers, we also accept the syntax ``x:y``, which turns into ``range(x, y)`` - very useful for long integer ranges. ``help`` and ``metavar`` are used to construct the automatically generated command line help string. The help message is formatted like:: --name=METAVAR help string ``group`` is used to group the defined options in logical groups. By default, command line options are grouped by the file in which they are defined. Command line option names must be unique globally. They can be parsed from the command line with `parse_command_line` or parsed from a config file with `parse_config_file`. If a ``callback`` is given, it will be run with the new value whenever the option is changed. This can be used to combine command-line and file-based options:: define("config", type=str, help="path to config file", callback=lambda path: parse_config_file(path, final=False)) With this definition, options in the file specified by ``--config`` will override options set earlier on the command line, but can be overridden by later flags. """ifnameinself._options:raiseError("Option %r already defined in %s"%(name,self._options[name].file_name))frame=sys._getframe(0)options_file=frame.f_code.co_filename# Can be called directly, or through top level define() fn, in which# case, step up above that frame to look for real caller.if(frame.f_back.f_code.co_filename==options_fileandframe.f_back.f_code.co_name=='define'):frame=frame.f_backfile_name=frame.f_back.f_code.co_filenameiffile_name==options_file:file_name=""iftypeisNone:ifnotmultipleanddefaultisnotNone:type=default.__class__else:type=strifgroup:group_name=groupelse:group_name=file_namenormalized=self._normalize_name(name)option=_Option(name,file_name=file_name,default=default,type=type,help=help,metavar=metavar,multiple=multiple,group_name=group_name,callback=callback)self._options[normalized]=option

[docs]defparse_command_line(self,args=None,final=True):"""Parses all options given on the command line (defaults to `sys.argv`). Note that ``args[0]`` is ignored since it is the program name in `sys.argv`. We return a list of all arguments that are not parsed as options. If ``final`` is ``False``, parse callbacks will not be run. This is useful for applications that wish to combine configurations from multiple sources. """ifargsisNone:args=sys.argvremaining=[]foriinrange(1,len(args)):# All things after the last option are command line argumentsifnotargs[i].startswith("-"):remaining=args[i:]breakifargs[i]=="--":remaining=args[i+1:]breakarg=args[i].lstrip("-")name,equals,value=arg.partition("=")name=self._normalize_name(name)ifnamenotinself._options:self.print_help()raiseError('Unrecognized command line option: %r'%name)option=self._options[name]ifnotequals:ifoption.type==bool:value="true"else:raiseError('Option %r requires a value'%name)option.parse(value)iffinal:self.run_parse_callbacks()returnremaining

[docs]defparse_config_file(self,path,final=True):"""Parses and loads the Python config file at the given path. If ``final`` is ``False``, parse callbacks will not be run. This is useful for applications that wish to combine configurations from multiple sources. .. versionchanged:: 4.1 Config files are now always interpreted as utf-8 instead of the system default encoding. .. versionchanged:: 4.4 The special variable ``__file__`` is available inside config files, specifying the absolute path to the config file itself. """config={'__file__':os.path.abspath(path)}withopen(path,'rb')asf:exec_in(native_str(f.read()),config,config)fornameinconfig:normalized=self._normalize_name(name)ifnormalizedinself._options:self._options[normalized].set(config[name])iffinal:self.run_parse_callbacks()

[docs]defmockable(self):"""Returns a wrapper around self that is compatible with `mock.patch <unittest.mock.patch>`. The `mock.patch <unittest.mock.patch>` function (included in the standard library `unittest.mock` package since Python 3.3, or in the third-party ``mock`` package for older versions of Python) is incompatible with objects like ``options`` that override ``__getattr__`` and ``__setattr__``. This function returns an object that can be used with `mock.patch.object <unittest.mock.patch.object>` to modify option values:: with mock.patch.object(options.mockable(), 'name', value): assert options.name == value """return_Mockable(self)

class_Mockable(object):"""`mock.patch` compatible wrapper for `OptionParser`. As of ``mock`` version 1.0.1, when an object uses ``__getattr__`` hooks instead of ``__dict__``, ``patch.__exit__`` tries to delete the attribute it set instead of setting a new one (assuming that the object does not catpure ``__setattr__``, so the patch created a new attribute in ``__dict__``). _Mockable's getattr and setattr pass through to the underlying OptionParser, and delattr undoes the effect of a previous setattr. """def__init__(self,options):# Modify __dict__ directly to bypass __setattr__self.__dict__['_options']=optionsself.__dict__['_originals']={}def__getattr__(self,name):returngetattr(self._options,name)def__setattr__(self,name,value):assertnamenotinself._originals,"don't reuse mockable objects"self._originals[name]=getattr(self._options,name)setattr(self._options,name,value)def__delattr__(self,name):setattr(self._options,name,self._originals.pop(name))class_Option(object):UNSET=object()def__init__(self,name,default=None,type=basestring_type,help=None,metavar=None,multiple=False,file_name=None,group_name=None,callback=None):ifdefaultisNoneandmultiple:default=[]self.name=nameself.type=typeself.help=helpself.metavar=metavarself.multiple=multipleself.file_name=file_nameself.group_name=group_nameself.callback=callbackself.default=defaultself._value=_Option.UNSETdefvalue(self):returnself.defaultifself._valueis_Option.UNSETelseself._valuedefparse(self,value):_parse={datetime.datetime:self._parse_datetime,datetime.timedelta:self._parse_timedelta,bool:self._parse_bool,basestring_type:self._parse_string,}.get(self.type,self.type)ifself.multiple:self._value=[]forpartinvalue.split(","):ifissubclass(self.type,numbers.Integral):# allow ranges of the form X:Y (inclusive at both ends)lo,_,hi=part.partition(":")lo=_parse(lo)hi=_parse(hi)ifhielseloself._value.extend(range(lo,hi+1))else:self._value.append(_parse(part))else:self._value=_parse(value)ifself.callbackisnotNone:self.callback(self._value)returnself.value()defset(self,value):ifself.multiple:ifnotisinstance(value,list):raiseError("Option %r is required to be a list of %s"%(self.name,self.type.__name__))foriteminvalue:ifitemisnotNoneandnotisinstance(item,self.type):raiseError("Option %r is required to be a list of %s"%(self.name,self.type.__name__))else:ifvalueisnotNoneandnotisinstance(value,self.type):raiseError("Option %r is required to be a %s (%s given)"%(self.name,self.type.__name__,type(value)))self._value=valueifself.callbackisnotNone:self.callback(self._value)# Supported date/time formats in our options_DATETIME_FORMATS=["%a %b %d %H:%M:%S %Y","%Y-%m-%d %H:%M:%S","%Y-%m-%d %H:%M","%Y-%m-%dT%H:%M","%Y%m%d %H:%M:%S","%Y%m%d %H:%M","%Y-%m-%d","%Y%m%d","%H:%M:%S","%H:%M",]def_parse_datetime(self,value):forformatinself._DATETIME_FORMATS:try:returndatetime.datetime.strptime(value,format)exceptValueError:passraiseError('Unrecognized date/time format: %r'%value)_TIMEDELTA_ABBREV_DICT={'h':'hours','m':'minutes','min':'minutes','s':'seconds','sec':'seconds','ms':'milliseconds','us':'microseconds','d':'days','w':'weeks',}_FLOAT_PATTERN=r'[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+)?'_TIMEDELTA_PATTERN=re.compile(r'\s*(%s)\s*(\w*)\s*'%_FLOAT_PATTERN,re.IGNORECASE)def_parse_timedelta(self,value):try:sum=datetime.timedelta()start=0whilestart<len(value):m=self._TIMEDELTA_PATTERN.match(value,start)ifnotm:raiseException()num=float(m.group(1))units=m.group(2)or'seconds'units=self._TIMEDELTA_ABBREV_DICT.get(units,units)sum+=datetime.timedelta(**{units:num})start=m.end()returnsumexceptException:raisedef_parse_bool(self,value):returnvalue.lower()notin("false","0","f")def_parse_string(self,value):return_unicode(value)options=OptionParser()"""Global options object.All defined options are available as attributes on this object."""

[docs]defdefine(name,default=None,type=None,help=None,metavar=None,multiple=False,group=None,callback=None):"""Defines an option in the global namespace. See `OptionParser.define`. """returnoptions.define(name,default=default,type=type,help=help,metavar=metavar,multiple=multiple,group=group,callback=callback)

[docs]defparse_command_line(args=None,final=True):"""Parses global options from the command line. See `OptionParser.parse_command_line`. """returnoptions.parse_command_line(args,final=final)

[docs]defparse_config_file(path,final=True):"""Parses global options from a config file. See `OptionParser.parse_config_file`. """returnoptions.parse_config_file(path,final=final)

[docs]defprint_help(file=None):"""Prints all the command line options to stderr (or another file). See `OptionParser.print_help`. """returnoptions.print_help(file)

[docs]defadd_parse_callback(callback):"""Adds a parse callback, to be invoked when option parsing is done. See `OptionParser.add_parse_callback` """options.add_parse_callback(callback)