vQc@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z yddl Z ddl Z Wne k reZ nXddlmZmZdZejdkrdZndZeaeedZd Zd Zd Zd Zd ZdZejdejZ dZ!de"fdYZ#de$fdYZ%de&fdYZ'de(fdYZ)de)fdYZ*e*Z+dZ,edZ-dZ.dS(s Configuration functions for the logging package for Python. The core package is based on PEP 282 and comments thereto in comp.lang.python, and influenced by Apache's log4j system. Copyright (C) 2001-2013 Vinay Sajip. All Rights Reserved. To use, simply 'import logging' and log away! iN(tThreadingTCPServertStreamRequestHandleriF#twin32iF'ihcCsddl}|j|}t|dr:|j|n |j|t|}tjz7tjjtj 2t ||}t |||Wdtj XdS(sD Read the logging configuration from a ConfigParser-format file. This can be called several times from an application, allowing an end user the ability to select from various pre-canned configurations (if the developer provides a mechanism to present the choices and load the chosen configuration). iNtreadline( t ConfigParserthasattrtreadfptreadt_create_formatterstloggingt _acquireLockt _handlerstcleart _handlerListt_install_handlerst_install_loggerst _releaseLock(tfnametdefaultstdisable_existing_loggersRtcpt formattersthandlers((s$/sys/lib/python2.7/logging/config.pyt fileConfig5s     cCs|jd}|jd}t|}x\|D]T}|d|}yt||}Wq1tk rt|t||}q1Xq1W|S(s)Resolve a dotted name to a global object.t.i(tsplittpopt __import__tgetattrtAttributeError(tnametusedtfoundtn((s$/sys/lib/python2.7/logging/config.pyt_resolveTs    cCstd|S(NcSs |jS(N(tstrip(tx((s$/sys/lib/python2.7/logging/config.pytcs(tmap(talist((s$/sys/lib/python2.7/logging/config.pyt _strip_spacesbscCs t|tr|S|jdS(Nsutf-8(t isinstancetstrtencode(ts((s$/sys/lib/python2.7/logging/config.pyt_encodedesc Cs|jdd}t|s"iS|jd}t|}i}x|D]}d|}|j|}d|kr|j|dd}nd }d|kr|j|dd}nd }tj}d|kr|j|d} | rt| }qn|||} | ||[a-z]+)://(?P.*)$s ^\s*(\w+)\s*s^\.\s*(\w+)\s*s^\[\s*(\w+)\s*\]\s*s^\d+$t ext_converttextt cfg_converttcfgcCs@t||_||j_tttjkr<t|_ndS(N(RxtconfigR{R}Rttypest FunctionTypetimporter(RR((s$/sys/lib/python2.7/logging/config.pyt__init__{s c Cs|jd}|jd}yy|j|}x_|D]W}|d|7}yt||}Wq7tk r|j|t||}q7Xq7W|SWnVtk rtjd\}}td||f}|||_ |_ |nXdS(s` Resolve strings to objects using standard import and attribute syntax. RiisCannot resolve %r: %sN( RRRRRt ImportErrortsystexc_infoRtt __cause__t __traceback__( RR,RRR tfragtettbtv((s$/sys/lib/python2.7/logging/config.pytresolves"    cCs |j|S(s*Default converter for the ext:// protocol.(R(RR((s$/sys/lib/python2.7/logging/config.pyRscCsO|}|jj|}|dkr7td|n||j}|j|jd}x|rJ|jj|}|r||jd}n|jj|}|r|jd}|j j|s||}qyt |}||}Wqt k r||}qXn|r1||j}qatd||fqaW|S(s*Default converter for the cfg:// protocol.sUnable to convert %risUnable to convert %r at %rN( t WORD_PATTERNRsR6RttendRtgroupst DOT_PATTERNt INDEX_PATTERNt DIGIT_PATTERNtintt TypeError(RRtrestRvtdRR!((s$/sys/lib/python2.7/logging/config.pyRs2     cCs/t|t r7t|tr7t|}||_nt|t rnt|trnt|}||_nt|t rt|trt|}||_nt|tr+|j j |}|r+|j }|d}|j j |d}|r(|d}t||}||}q(q+n|S(s Convert values to an appropriate type. dicts, lists and tuples are replaced by their converting alternatives. Strings are checked to see if they have a conversion format and are converted if they do. tprefixtsuffixN(R)RxRyR{R~R\RRt basestringtCONVERT_PATTERNRst groupdicttvalue_convertersR3R6R(RRRvRRt converterR((s$/sys/lib/python2.7/logging/config.pyR|s*         c Cs|jd}t|d rUttdrUt|tjkrU|j|}n|jdd}tg|D]"}t|rq|||f^qq}||}|rx-|j D]\}}t |||qWn|S(s1Configure an object with a user-supplied factory.s()t__call__t ClassTypeRN( RRRR}RRR6RyRwtitemstsetattr( RRR>tpropstktkwargsRRR((s$/sys/lib/python2.7/logging/config.pytconfigure_customs45 cCs"t|trt|}n|S(s0Utility function which converts lists to tuples.(R)R\R(RR((s$/sys/lib/python2.7/logging/config.pytas_tuples(RRRtretcompileRRRRRRRRRRRRR|RR(((s$/sys/lib/python2.7/logging/config.pyRgs"    "  tDictConfiguratorcBsheZdZdZdZdZdZdZdZe dZ e dZ e d Z RS( s] Configure logging using a dictionary-like object to describe the configuration. cCs|j}d|kr$tdn|ddkrKtd|dn|jdt}i}tjzz|r|jd|}x|D]}|tjkrtd|qyLtj|}||}|jdd}|r|j tj |nWqt k r.} td || fqXqW|jd |} xU| D]M}y|j || |t WqLt k r} td || fqLXqLW|jd d} | ry|j| t Wqt k r} td | qXqn|jdt } tjjtj2|jd|} xU| D]M}y|j| || |RtcnameRStthRRRR((s$/sys/lib/python2.7/logging/config.pyRsb 4     5 cCs]xV|D]N}y|j|jd|Wqtk rT}td||fqXqWdS(s.Add handlers to a logger from a list of names.RsUnable to add handler %r: %sN(R_RRRt(RRmRRUR((s$/sys/lib/python2.7/logging/config.pyt add_handlerss  cCs|jdd}|dk r7|jtj|n|sx|jD]}|j|qHW|jdd}|r|j||n|jdd}|r|j||qndS(sU Perform configuration which is common to root and non-root loggers. RDRRN( R3R6RIR RRR^RR(RRmRRRDRURR((s$/sys/lib/python2.7/logging/config.pytcommon_logger_configs cCsPtj|}|j||||jdd}|dk rL||_ndS(s.Configure a non-root logger from a dictionary.R[N(R RdRR3R6R[(RRRRRmR[((s$/sys/lib/python2.7/logging/config.pyRs  cCs#tj}|j|||dS(s*Configure a root logger from a dictionary.N(R RdR(RRRRX((s$/sys/lib/python2.7/logging/config.pyRs ( RRRRRRRRRRRRR(((s$/sys/lib/python2.7/logging/config.pyRs   :   cCst|jdS(s%Configure logging using a dictionary.N(tdictConfigClassR(R((s$/sys/lib/python2.7/logging/config.pyt dictConfig!scsptstdndtfdY}dtfdY}dtjffdY|||S(sW Start up a socket server on the specified port, and listen for new configurations. These will be sent as a file suitable for processing by fileConfig(). Returns a Thread object on which you can call start() to start the server, and which you can join() when appropriate. To stop the server, call stopListening(). s listen() needs threading to worktConfigStreamHandlercBseZdZdZRS(s Handler for a logging configuration request. It expects a completely new logging configuration and uses fileConfig to install it. c Ssddl}y+|j}|jd}t|dkr6tjd|d}|jj|}x3t||kr||j|t|}qdWy)ddl}|j|}t|WnQt j |}yt |Wqt t fk rqtjqXnX|jjr6|jjjq6nWnMtjk r}t|jtsdq|jd} | tkrqnXdS(s Handle a request. Each request is expected to be a 4-byte length, packed using struct.pack(">L", n), followed by the config file. Uses fileConfig() to do the grunt work. iNis>Li(ttempfilet connectiontrecvR4tstructtunpacktjsontloadsRt cStringIOtStringIORtKeyboardInterruptt SystemExitt tracebackt print_exctservertreadytsettsocketterrorR)RCRt RESET_ERROR( RRtconntchunktslenRRtfileRterrcode((s$/sys/lib/python2.7/logging/config.pythandle:s:  !    (RRRR(((s$/sys/lib/python2.7/logging/config.pyR3stConfigSocketReceivercBs2eZdZdZdedddZdZRS(sD A simple TCP socket-based logging config receiver. it localhostcSsLtj|||f|tjd|_tjd|_||_dS(Nii(RRR R tabortRttimeoutR(RthosttportRR((s$/sys/lib/python2.7/logging/config.pyRks     cSsddl}d}xj|s~|j|jjggg|j\}}}|r^|jntj|j}tjqW|jj dS(Nii( tselectRtfilenoRthandle_requestR R RRtclose(RRRtrdtwrtex((s$/sys/lib/python2.7/logging/config.pytserve_until_stoppedts     N(RRRtallow_reuse_addresstDEFAULT_LOGGING_CONFIG_PORTR6RR (((s$/sys/lib/python2.7/logging/config.pyRds tServercs eZfdZdZRS(csAt|j||_||_||_tj|_dS(N(tsuperRtrcvrthdlrRt threadingtEventR(RRRR(R (s$/sys/lib/python2.7/logging/config.pyRs    cSs~|jd|jd|jd|j}|jdkrI|jd|_n|jjtj|atj |j dS(NRRRii( RRRRtserver_addressRR R t _listenerRR (RR((s$/sys/lib/python2.7/logging/config.pytruns    (RRRR((R (s$/sys/lib/python2.7/logging/config.pyR s(tthreadtNotImplementedErrorRRRtThread(RRR((R s$/sys/lib/python2.7/logging/config.pytlisten&s 1cCs8tjztr%dt_danWdtjXdS(sN Stop the listening server which was created with a call to listen(). iN(R R RRR6R(((s$/sys/lib/python2.7/logging/config.pyt stopListenings    (/RRR tlogging.handlersRRtosRRRRRRRR6t SocketServerRRR tplatformRRRuRR"R(R-RRRRtIRrRwRyRxR\R~RRtobjectRRRRRR(((s$/sys/lib/python2.7/logging/config.pyts@`         + \ % .  s