K iedZddlZddlZddlZddlZddlZddlZddlmZm Z ddl m Z ddl m Z mZddlmZmZmZmZmZmZmZmZmZmZGddeZGd d ZGd d ZGd dZeZ d!de dedee!dee dee de"dee deeegdfddfdZ# d"deee de"dee fdZ$d#de de"ddfdZ%d$deeddfdZ&degdfddfd Z'e ey)%a A command line parsing module that lets modules define their own options. This module is inspired by Google's `gflags `_. The primary difference with libraries such as `argparse` is that a global registry is used so that options may be defined in any module (it also enables `tornado.log` by default). The rest of Tornado does not depend on this module, so feel free to use `argparse` or other configuration libraries if you prefer them. Options must be defined with `tornado.options.define` before use, generally at the top level of a module. The options are then accessible as attributes of `tornado.options.options`:: # myapp/db.py 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) ... # myapp/server.py from tornado.options import define, options define("port", default=8080, help="port to listen on") def start_server(): app = make_app() app.listen(options.port) The ``main()`` method of your application does not need to be aware of all of the options used throughout your program; they are all automatically loaded when the modules are loaded. However, all modules that define options must have been imported before the command line is parsed. Your ``main()`` method can parse the command line or parse a config file with either `parse_command_line` or `parse_config_file`:: import myapp.db, myapp.server import tornado if __name__ == '__main__': tornado.options.parse_command_line() # or tornado.options.parse_config_file("/etc/server.conf") .. note:: When using multiple ``parse_*`` functions, pass ``final=False`` to all but the last one, or side effects may occur twice (in particular, this can result in log messages being doubled). `tornado.options.options` is a singleton instance of `OptionParser`, and the 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() .. note:: `parse_command_line` or `parse_config_file` function should called after logging configuration and user-defined command line flags using the ``callback`` option definition, or these configurations will not take effect. .. 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. N)_unicode native_str)define_logging_options)basestring_typeexec_in) AnyIteratorIterableTupleSetDictCallableListTextIOOptionalceZdZdZy)Errorz1Exception raised by errors in the options module.N)__name__ __module__ __qualname____doc__U/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/tornado/options.pyrrs;rrceZdZdZd%dZdedefdZdedefdZdededdfd Z de fd Z dede fd Z dedefd Zdededdfd ZdeeeeffdZdeefdZdedeeeffdZdeeeffdZ d&dededeedeedeede deedeeegdfddfdZ d'deeede deefdZd(dede ddfdZd)deeddfd Z de ddfd!Z!degdfddfd"Z"d%d#Z#d*d$Z$y)+ OptionParserzA collection of options, a dictionary with object-like access. Normally accessed via static functions in the `tornado.options` module, which reference a global instance. returnNci|jd<g|jd<|jdtd|jy)N_options_parse_callbackshelpzshow this help information)typer!callback)__dict__definebool_help_callbackselfs r__init__zOptionParser.__init__sB$& j!,. () -((  rnamec&|jddS)N_-)replacer)r+s r_normalize_namezOptionParser._normalize_names||C%%rc|j|}t|jj|tr|j|j St d|zNzUnrecognized option %r)r1 isinstancerget_OptionvalueAttributeErrorr0s r __getattr__zOptionParser.__getattr__sT##D) dmm''-w 7==&,,. .5<==rr7c|j|}t|jj|tr|j|j |St d|zr3)r1r4rr5r6setr8r)r+r7s r __setattr__zOptionParser.__setattr__sV##D) dmm''-w 7==&**51 15<==rcDd|jjDS)Nc34K|]}|jywNr+).0opts r z(OptionParser.__iter__..s;S;s)rvaluesr(s r__iter__zOptionParser.__iter__s;DMM$8$8$:;;rc@|j|}||jvSr@)r1rr0s r __contains__zOptionParser.__contains__s!##D)t}}$$rc$|j|Sr@)r9r0s r __getitem__zOptionParser.__getitem__s%%rc&|j||Sr@)r=r<s r __setitem__zOptionParser.__setitem__se,,rc|jjDcgc]!\}}|j|jf#c}}Scc}}w)zKAn iterable of (name, value) pairs. .. versionadded:: 3.1 ritemsr+r7r)r+rCs rrOzOptionParser.itemss8 :>9L9L9NOID#399;'OOOs&Acp|jjDchc]}|jc}Scc}w)zWThe set of option-groups created by ``define``. .. versionadded:: 3.1 )rrE group_name)r)rCs rgroupszOptionParser.groupss) +/--*>*>*@A3AAAs3groupc|jjDcic]1\}}|r||jk(r|j|j 3c}}Scc}}w)aThe names and values of options in a group. Useful for copying options into Application settings:: from tornado.options import define, parse_command_line, options define('template_path', group='application') define('static_path', group='application') parse_command_line() application = Application( handlers, **options.group_dict('application')) .. versionadded:: 3.1 )rrOrRr+r7)r)rTr+rCs r group_dictzOptionParser.group_dictsR&"]]002 cES^^3 HHciik !   s6Ac|jjDcic] \}}|j|j"c}}Scc}}w)zLThe names and values of all options. .. versionadded:: 3.1 rNrPs ras_dictzOptionParser.as_dicts8 8<}}7J7J7LM)$#))+%MMMs%Adefaultr"r!metavarmultipler#c |j|} | |jvr(td| d|j| jt j d} | | j j} | jR| jj j| k(r/| jj jdk(r | j} | jJ| jj j} nd} |  k(rd} ||s| |j}nt}|r|} n| } t|| |||||| | }||j| <y) acDefines a new command line option. ``type`` can be any of `str`, `int`, `float`, `bool`, `~datetime.datetime`, or `~datetime.timedelta`. If no ``type`` is given but a ``default`` is, ``type`` is the type of ``default``. Otherwise, ``type`` defaults to `str`. If ``multiple`` is True, the option value is a list of ``type`` instead of an instance of ``type``. ``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. 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. Option z already defined in rNr%z ) file_namerYr"r!rZr[rRr#) r1rrr_sys _getframef_code co_filenamef_backco_name __class__strr6)r)r+rYr"r!rZr[rTr# normalizedframe options_filer_rRoptions rr%zOptionParser.definesHZ))$/  &t}}Z8BBD  a    <<33L  (LL''33|CLL''//8; <<+ ++ ++77I#I  $I < 3(( J"J !  %+ j!rargsfinalc8|tj}g}tdt|D]}||j ds||d}n||dk(r ||dzd}n||j d}|j d\}}}|j|}||jvr|jtd|z|j|} |s$| jtk(rd}ntd|z| j||r|j|S) a+Parses all options given on the command line (defaults to `sys.argv`). Options look like ``--option=value`` and are parsed according to their ``type``. For boolean options, ``--option`` is equivalent to ``--option=true`` If the option has ``multiple=True``, comma-separated values are accepted. For multi-value integer options, the syntax ``x:y`` is also accepted and equivalent to ``range(x, y)``. 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. Nr.z--=z$Unrecognized command line option: %rtruezOption %r requires a value)r`argvrangelen startswithlstrip partitionr1r print_helprr"r&parserun_parse_callbacks) r)rlrm remainingiargr+equalsr7rks rparse_command_linezOptionParser.parse_command_line;s"0 <88D q#d)$ A7%%c* H Aw$ QM q'..%C"%--"4 D&%''-D4==(!BTIJJ]]4(F;;$&"E z)OptionParser.print_help..s fkkr)keyrpr^z (default %s),z --30 ro34)r`stderrprintrrrrE setdefaultrRappendsortedrOrrnormpathsortr1r+rZr!rYtextwrapwraprtinsert) r)rby_grouprkfilenameoprefix descriptionlineslines rrxzOptionParser.print_helps <::D #chhqk1= n4(mm**, FF    1 12 6 = =f E F"(.."23 ?KHa'"''*:*:8*DD4P FF1F 2 ?--fkk:>>cFNN22F$kk/R >>-&..B2F?V^^#CCK k7;v;#s5zQLLB'a9E!!"I?DT2>? ?  ?$ 4rcT|r&|jtjdyy)Nr)rxr`exitr)r7s rr'zOptionParser._help_callbacks  OO  HHQK rc:|jj|y)zAAdds a parse callback, to be invoked when option parsing is done.N)r rr)r#s radd_parse_callbackzOptionParser.add_parse_callbacks $$X.rc4|jD] }| yr@)r rs rrzz OptionParser.run_parse_callbackss-- H J rct|S)aReturns a wrapper around self that is compatible with `unittest.mock.patch`. The `unittest.mock.patch` function is incompatible with objects like ``options`` that override ``__getattr__`` and ``__setattr__``. This function returns an object that can be used with `mock.patch.object ` to modify option values:: with mock.patch.object(options.mockable(), 'name', value): assert options.name == value ) _Mockabler(s rmockablezOptionParser.mockablesr)rNNNNNFNNNTTr@)rr)%rrrrr*rgr1rr9r=r rFr&rHrJrLr r rOr rSr rVrXrr"rr%rrrrrxr'rrzrrrrrrs6  &C&C&>>> >>C>D> <(<%%%&&&--C-D-Pxc3h0PBCB  S#X .Nc3hN#"!%#48Z+Z+Z+tn Z+ sm Z+ # Z+Z+}Z+8SE4K01Z+ Z+z?C4T#Y'47;4 c4lE'cE'$E'$E'Nx/4<DT /8BH+=/$/ rrcTeZdZdZdeddfdZdedefdZdededdfd Z deddfd Z y) ra`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 capture ``__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. optionsrNc@||jd<i|jd<y)Nr _originals)r$)r)rs rr*z_Mockable.__init__s$+ j!&( l#rr+c.t|j|Sr@)getattrrr0s rr9z_Mockable.__getattr__st}}d++rr7c||jvsJdt|j||j|<t|j||y)Nzdon't reuse mockable objects)rrrsetattrr<s rr=z_Mockable.__setattr__sC4??*J,JJ* ' t < tU+rcdt|j||jj|yr@)rrrpopr0s r __delattr__z_Mockable.__delattr__ s! tT__%8%8%>?r) rrrrrr*rgrr9r=rrrrrrs] ) )) ,,,,,C,D, @@@rrceZdZeZ d dededeedeedeede deed eed ee egdfd dfd Z d efd Z ded efdZ ded dfdZgdZded ej fdZdddddddddd ZdZej*dezej,Zded ej0fdZded e fdZded efdZy)!r6Nr+rYr"r!rZr[r_rRr#rc ||rg}||_| td||_||_||_||_||_||_| |_||_ tj|_ y)Nztype must not be None) r+ ValueErrorr"r!rZr[r_rRr#rYr6UNSET_value) r)r+rYr"r!rZr[r_rRr#s rr*z_Option.__init__sk ?xG <45 5     "$   mm rcj|jtjur |jS|jSr@)rr6rrYr(s rr7z _Option.value0s$#{{gmm;t||LLrr7ctj|jtj|jt|j t |jij|j|j}|jrg|_ |jdD]}t|jtjrR|j!d\}}}||}|r||n|}|jj#t%||dzy|jj'||n |||_ |j(|j)|j|j+S)N,:ro)datetime_parse_datetime timedelta_parse_timedeltar& _parse_boolr _parse_stringr5r"r[rsplit issubclassnumbersIntegralrwextendrsrr#r7) r)r7_parsepartlo_strr-hi_strlohis rryz _Option.parse3s*   t33    5 5 $"" T//  # IItyy  ==DK C( 5dii)9)9:(,s(;%FAvB+1rBKK&&uRa'89KK&&vd|4 5!-DK == $ MM$++ &zz|rc @|jrt|ts/td|jd|j j |D]J}|t||j rtd|jd|j j nT|Rt||j s->A     FN#. ch.@.@$88833rr6r+rYr"r!rZr[rTr#rc <tj||||||||S)zODefines an option in the global namespace. See `OptionParser.define`. )rYr"r!rZr[rTr#)rr%)r+rYr"r!rZr[rTr#s rr%r%s2 >>      rrlrmc0tj||S)z]Parses global options from the command line. See `OptionParser.parse_command_line`. rm)rr)rlrms rrrs  % %d% % 88rrc0tj||S)zYParses global options from a config file. See `OptionParser.parse_config_file`. r )rr)rrms rrrs  $ $T $ 77rrc,tj|S)ziPrints all the command line options to stderr (or another file). See `OptionParser.print_help`. )rrxrs rrxrxs   d ##rc.tj|y)zqAdds a parse callback, to be invoked when option parsing is done. See `OptionParser.add_parse_callback` N)rr)r#s rrrs  x(rrrrr@)(rrrrr`rrtornado.escaperr tornado.logr tornado.utilrrtypingrr r r r r rrrrrrrrr6rrgr"r&r%rrrxrrrrrs Tl /.1    I iiX @@