B @`S@sZdZddlZddlZddlZddlZddlZddlmZmZm Z m Z m Z m Z m Z yeWneZYnXGdddeZGdddeZGdd d eZGd d d eZGd d d e ZGddde ZGdddeZGdddeZefddZGdddeZGdddeZGdddeZGdddeZGdddeZGd d!d!eZ dS)"a~ The Option class provides a mechanism for application classes to transparently load configuration data. For example, the consider the following class declaration: class FOO(Option): declare_option("x", local_name="bar") def __init__(self): self.x = FOO.bar The FOO class contains a declaration 'bar', which is initialized by loading a Configuration object. N) PluginError Interface implementsPlugin PluginGlobalsExtensionPointIOptionDataProviderc@seZdZdZdS) OptionErrorz=Exception raised when there is an error processing an option.N)__name__ __module__ __qualname____doc__rrE/tmp/pip-unpacked-wheel-bvjqrktv/pyutilib/component/config/options.pyr %sr c@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)IOptionz`An interface that supports the initialization of Option objects from a Configuration object.cCsdS)z5Returns True if this option matches the section name.Nr)selfsectionrrrmatches_section-szIOption.matches_sectioncCsdS)z4Returns True if this option matches the option name.Nr)rrrrr matches_name0szIOption.matches_namecCsdS)zuLoads this option, if it matches the option name. Returns True if the option is loaded, and False otherwise.Nr)roptionvaluerrrload3sz IOption.loadcCsdS)zReturn the value of this optionNr)rrrr get_value7szIOption.get_valuecCsdS)z=Return a string value that describes the default option valueNr)rrrr default_str:szIOption.default_strcCsdS)z*Reset all options to their default values.Nr)rrrrreset=sz IOption.resetN) r r r r rrrrrrrrrrr)src@seZdZdZddZdS)IUpdatedOptionsActionz@Define actions that are applied after options have been updated.cCsdS)z-Perform actions that use updated option data.Nr)rrrrreset_after_updatesDsz)IUpdatedOptionsAction.reset_after_updatesN)r r r r rrrrrrAsrc@seZdZdZddZdS) IFileOptionzAn interface that supports the initialization of the directory for options that specify files. This is needed to correctly initialize relative paths for files.cCsdS)z2Sets the directory of the configuration data file.Nr)rpathrrrset_dirMszIFileOption.set_dirN)r r r r rrrrrrHsrc@sFeZdZdZeeddddZddZdd Zd d Z dd dZ d S) OptionDataae A class that is used to share option data between Option objects. This data in this class represents option data as section -> option -> data Note: this class does not currentl support the situation where an option is removed. This would require a backwards map from option names to the Option classes that declare them. T)servicecCsd|_i|_dS)Z ConstructorFN)ignore_missingdata)rrrr__init___szOptionData.__init__cCs|jS)zGet the class data)r#)rrrrget_datadszOptionData.get_datacCs&||jkri|j|<||j||<dS)z1Set the value of an option in a specified sectionN)r#)rrnamerrrrseths  zOptionData.setc Csjy|j||Stk rd|jr(dS||jkrPtd|||j|fntd||fYnXdS)z1Get the value of an option in a specified sectionNzLProblem retrieving the value of option %r from section %r. Valid keys are %szMProblem retrieving the value of option %r from section %r. Undefined section.)r# Exceptionr"r keys)rrr&rrrgetns zOptionData.getNcCs6|dkri|_n"x |D]}||jkr&q|j|=qWdS)zClears the dataN)r#)rr)keyrrrclear~s   zOptionData.clear)N) r r r r rrr$r%r'r*r,rrrrr Qs  r c@sfeZdZdZeeddddZddZdd Zd d Z d d Z dddZ ddZ ddZ ddZdS) OptionPluginz(Manages the initialization of an Option.T)r!cCsttt|_tjdkrtt_t|jdkrTtjttjj t tjtj tjj <t|jdkrpt d|jdS)z Declare an extension point for a data provider, and construct one if one hasn't already been provided. NTrz2Problem constructing a global OptionData object %s)rrr#rZ_default_OptionDatar lenZinterface_servicesadd_idweakrefrefZplugin_instancesrr&)rrrrr$s    zOptionPlugin.__init__cCs$||jkp"|jdko"|j|dk S)z This method returns true if the section name matches the option section, or if the option's section regular expression matches the section name. N)r section_re section_pmatch)rrrrrrszOptionPlugin.matches_sectioncCs|jdkp||jkS)zQ This method returns true if the name matches the options' name. )r&)rr&rrrrszOptionPlugin.matches_namecCs|dS)zConvert a value into a specific type. The default behavior is to take the list of values, and simply return the last one defined in the configuration.r)rrdefaultrrrconvertszOptionPlugin.convertcCs|j|j|jS)z' Get the option value. )r#r!r*rr&)rrrrrszOptionPlugin.get_valueFcCsb|r|j|j|j|n@t|tk s6t|dkr<|g}|j|j|j|||j dS)z Set the option value. By default, the option is converted using the option-specific `convert` method, but the `raw` option can be specified to force the raw value to be inserted. rN) r#r!r'rr&typelistr.r9r8)r_value_rawrrr set_values zOptionPlugin.set_valuecCsrt|tkr&t|dkr&td|jy||Wn8tk rltd}tdt|t|fYnXdS)z Load an option value. This method assumes that the option value is provided in a list, which is the format used when interfacing with the Configure class. rz,Attempting to load option %r with empty datazError loading option %r: %sT) r:r;r.r r&r>sysexc_infostr)r_option_r<errrrrrs  zOptionPlugin.loadcCs|j|jdddS)zSet option to its default valueT)r=N)r>r8)rrrrrszOptionPlugin.resetcCs t|jS)z=Return a string value that describes the default option value)rBr8)rrrrrszOptionPlugin.default_strN)F)r r r r rrr$rrr9rr>rrrrrrrr-s +  r-csDeZdZdZdfdd ZddZddZd d Zdd d ZZ S)OptionaDescriptor for configuration options. This class uses an OptionPlugin instance to support initialization of the option. However, the OptionPlugin instance is given a handle on the Option instance to perform this initialization. This allows for the explicit referencing of this object's data, as well as referencing the conversion routines that are specified in subclasses of Option. Nc sjd|_tt|d|_||_d|_d|_x |D]\}}| ||q6W|dkr^t d| dS)aCreate the extension point. @param name: the name of the option @param section: the name of the configuration section this option belongs to @param default: the default value for the option @param doc: documentation of the option globalsNr6z The Option class requires a name) rsuperrEr$r3r&r8r items _parse_optionr r)rr&kwdskv) __class__rrr$s zOption.__init__cCsd|dkr||_nP|dkr,||_t||_n4|dkr<||_n$|dkrL||_n|dkr`td|dS)z Parse optionrr3r8doc local_namezUnknown keyword: N)rr3recompiler4r8r r )rrKrLrrrrIszOption._parse_optioncCs tddS)Nz4The Option class cannot be used as a class decorator)r)rinstanceownerrrr__get__-szOption.__get__cCs tddS)Nz4The Option class cannot be used as a class decorator)r)rrRrrrr__set__1szOption.__set__FcCs"d|jd}d|jj||jfS)z2Returns a string representation of the option namez []z <%s%s %r>)rrMr r&)rsimplesecrrr__repr__5szOption.__repr__)N)F) r r r r r$rIrTrUrY __classcell__rr)rMrrEs rEc@s<eZdZdZdddZddZddZd d Zdd d ZdS) VirtualOptionz This class is the decorator that is used to declare an option. To use an option, each instance must declare an Option object. NcCs||_||_dS)N)r&r)rr&rrrrr$AszVirtualOption.__init__cCsH|jdk r|jSy t||jStk rBtd|j|fYnXdS)NzeExpected attribute %r in instance %r. The declare_option() method was not called in the constructor!)rgetattrr&AttributeErrorr)rrRrrr _get_optionEs  zVirtualOption._get_optioncCs|dkr |S||S)zEReturns the value of the option, accessed through the local instance.N)r^r)rrRrSrrrrTOszVirtualOption.__get__cCs|||dS)zSets the value of the optionN)r^r>)rrRrrrrrUUszVirtualOption.__set__FcCs d|jS)z2Returns a string representation of the option namez)r&)rrWrrrrYYszVirtualOption.__repr__)N)F) r r r r r$r^rTrUrYrrrrr[;s   r[cKst|tstd||d|}td}|j}||jk r||d<|f|}d|krjtd||||<q||dj j krt |dj |td|t |dd||ntddS) Nz8The 'cls' argument must specify an Option class type: %srOr?r&r _rz7declare_option() can only be used in a class definition) issubclassrErr*r@ _getframef_locals f_globalsr[rM__dict__setattr)r&clsrJrOframeZlocals_Zoption_rrrdeclare_option^s"       rhcsReZdZdZGdddeZfddZddZdd d Zd d Z d dZ Z S) DictOptiona An option that supports an interface to _all_ options in a section. For example, consider the following class: class FOO(object): options = DictOption(section="bar") The `options` object will be populated by all data in the `bar` section. For example, if the values `a` and `b` are in this section, then they can be referenced as `foo.a` and `foo.b`. Similarly, data can be inserted into the `bar` section by specifying the value of attributes of options: options.c = 1 c@sBeZdZdZdddZddZddZd d Zd d Zd dZ dS)zDictOption.SectionWrapperz A class that is used to manage attribute access to a DictOption instance. This class uses IOptionDataProvider extension points to directly access shared Option data. FcCs&||_tt}||_||jd<dS)Nr#) _section_rrr!r"rd)rrr"eprrrr$s z"DictOption.SectionWrapper.__init__cCs0|j|jjkriS|jj|jS)N)rjr#r!__iter__)rrrrrlsz"DictOption.SectionWrapper.__iter__cCs|j|j|S)N)r#r!r*rj)rr&rrr __getitem__sz%DictOption.SectionWrapper.__getitem__cCs,|j|jjkrgS|jj|jS)N)rjr#r!r))rrrrr)szDictOption.SectionWrapper.keyscCs*y |j|S|j|j|SdS)N)rdr#r!r*rj)rr&rrr __getattr__s z%DictOption.SectionWrapper.__getattr__cCs2|ddkr||j|<n|j|j||dS)Nrr_)rdr#r!r'rj)rr&rrrr __setattr__s  z%DictOption.SectionWrapper.__setattr__N)F) r r r r r$rlrmr)rnrorrrrSectionWrappers rpc sLd|d<d|kr |d}|d=nd}tt|jf|tj|j|d|_dS)z Constructor.r6r&r"F)r"N)rGrir$rprr8)rrJr")rMrrr$szDictOption.__init__cCs|jS)zReturns the default value.)r8)rrrrrszDictOption.get_valueFcCs4t|tk rdSx|D]}|j|||qWdS)z If the value is a dictionary, then it is evaluated to populate corresponding data in the options data. NOTE: the raw option is ignored, but maintained for compatibility with the parent class. N)r:dictr8ro)rr<r=r+rrrr>s  zDictOption.set_valuecCs|j|j||ddS)z Data is loaded one option/value pair at a time, so this method circumvents the core logic of this class. r7T)r#r!r'r)rrCr<rrrrszDictOption.loadcCsdS)z=Return a string value that describes the default option valuez=None (this dictionary consists of all values in this section)r)rrrrrszDictOption.default_str)F) r r r r objectrpr$rr>rrrZrr)rMrri~s&  ric@seZdZdZdZddZdS) BoolOptionz6A class that converts option data into boolean values.)yestrueZenabledonZaye1r?TcCs(|d}t|tr |tjk}t|S)zConversion routine.r7) isinstance basestringlowerrs _TRUE_VALUESbool)rrr8valrrrr9s zBoolOption.convertN)r r r r r{r9rrrrrssrsc@seZdZdZddZdS) IntOptionz6A class that converts option data into integer values.cCsd|d}|sdSyt|Stk r<tdt|Yn$tk r^tdt|YnXdS)zConversion routine.r7rzExpected integer, got %sz'Expected string or integer type, got %sN)int ValueErrorr repr TypeError)rrr8r}rrrr9szIntOption.convertN)r r r r r9rrrrr~sr~c@seZdZdZddZdS) FloatOptionz4A class that converts option data into float values.cCsd|d}|sdSyt|Stk r<tdt|Yn$tk r^tdt|YnXdS)zConversion routine.r7rzExpected float, got %sz%Expected string or float type, got %sN)floatrr rr)rrr8r}rrrr9szFloatOption.convertN)r r r r r9rrrrrsrc@sPeZdZdZeeddeeddddZddZdd Z d d Z d d Z dS) FileOptionz{A class that converts option data into a path. Relative paths are converted using the path for the configuration file.T)r!cKsd|_tj||f|dS)zp Constructor. By default, the current working directory is the path used in this data. N)dirrEr$)rr&rJrrrr$szFileOption.__init__cCs"|dkr||_nt|||dS)z7Parse options that are specific to the FileOption class directoryN)rrErI)rrKrLrrrrIszFileOption._parse_optioncCst|d}|s,|dkrdStjtj|Stj|s\tj|j|}tjtj|Stjtj|SdS)zConversion routine.r7N)osrnormcaserealpathisabsjoinr)rrr8r}rrrr9%s zFileOption.convertcCs ||_dS)z-Sets the path of the configuration data file.N)r)rrrrrr2szFileOption.set_dircCs|jdkrtddS)Nz+FileOption must have a directory specified.)rr )rrrrr6s zFileOption.reset_after_updatesN) r r r r rrrr$rIr9rrrrrrrs   rc@s$eZdZddZddZddZdS)ExecutableOptioncCs4t|||}|dks$t|tjs0td||S)zConversion routine.Nz}tj tj||rtjtjtj||}d}PqW|sdS|dkst |tj std|dS)zConversion routine.r7NFPATHTzs2$  8s?# b ,