U g@s4dZddddddgZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZddlZdd lmZdd lmZdd l mZdd l Tdd lmZddlmZddlmZmZdd lmZddlmZddlmZeZ GdddeZ!e!Z"GdddeZ#ddZ$ddddddddddddd d d d!d!d"Z%ddddddddddddd d d d!d!d#Z&ddddddddddddd d d d!d!d$Z'ddddddddddddd d d d!d!d%Z(e)d&d'd(gZ*eZ+Gd)d*d*e#Z,d+dZ-d1d-dZ.d2d.d/Z/Gd0ddeZ0dS)3a!parses UNIX conf files and makes the result readily available The hafs.config module reads configuration information for the HAFS system from one or more *.conf files, via the Python ConfigParser module. This module also automatically fills in certain information, such as fields calculated from the tcvitals or date. The result is accessible via the HAFSConfig class, which provides many ways of automatically accessing configuration options. from_filez from-string confwalker HAFSConfigZ fordriver ENVIRONMENTN) ConfigParser)StringIO) Datastore)*) to_datetime) Formatter) NoOptionErrorNoSectionError)find_tcvitals_for) HAFSErrorc@s eZdZdZddZddZdS) Environmenta!returns environment variables, allowing substitutions This class is used to read (but not write) environment variables and provide default values if an environment variable is unset or blank. It is only meant to be used in string formats, by passing ENV=ENVIRONMENT. There is a global constant in this module, ENVIRONMENT, which is an instance of this class. You should never need to instantiate another one.cCs|ddkp|tjkS)z!Determines if __getitem__ will return something (True) or raise KeyError (False). Same as "s in os.environ" unless s contains "|-", in which case, the result is True.|-r)findosenviron)selfsr8/lfs/h1/ops/prod/packages/hafs.v2.0.7/ush/hafs/config.py __contains__3szEnvironment.__contains__cCs^|sdS|d}|dkr$tj|S|d|}||dd}tj|d}|dkrZ|S|S)z!Same as os.environ[s] unless s contains "|-". ENVIRONMENT["VARNAME|-substitute"] will return os.environ[VARNAME] if VARNAME is defined and non-empty in os.environ. Otherwise, it will return "substitute".rrN)rrrget)rrivarsubvalrrr __getitem__8s   zEnvironment.__getitem__N)__name__ __module__ __qualname____doc__rr!rrrrr*srcsFeZdZdZd fdd ZeddZddZd d Zd d Z Z S) ConfFormatteraw!Internal class that implements HAFSConfig.strinterp() This class is part of the implementation of HAFSConfig: it is used to interpolate strings using a syntax similar to string.format(), but it allows recursion in the config sections, and it also is able to use the [config] and [dir] sections as defaults for variables not found in the current section.Fcs,tt||r(|j|_|j|_t|_dS)z!Constructor for ConfFormatterN) superr&__init__ slow_formatformat slow_vformatvformatqparseparserquoted_literals __class__rrr(Ss zConfFormatter.__init__cCs |jtkSN)r.r-rrrrr0[szConfFormatter.quoted_literalscOs||||Sr3)r,)r format_stringargskwargsrrrr)_szConfFormatter.slow_formatc Cst}||D]\}}}}|r*|||r||||\} } | dkrZ| rZ|| ||} | } |dkrpt| } n(|dkrt| } n|rtdt|f|r| |} || q| } | | dk st t | tst | S)NrrzUnknown conversion %s) rr.write get_field get_valuestrrepr ValueError __format__getvaluecloseAssertionError isinstance) rr5r6r7out literal_text field_name format_spec conversionobjZused_keyvalueretrrrr+as0       zConfFormatter.slow_vformatc CsB|dd7<|dtjkr4t|d|d|zt|trP||WS|dd}||krp||}ndd|kr|dr||dkr|d|}n8|d}|d kr|d |}||dd}|s|dd}|r|}n |dd}|dd}t}|dk r|dk r|||r4|j||d d }nF||d rz||d  d D]$} || |rT|j| |d d }qT|tkr|d|r|jd|d d }n|d|r|jd|d d }|tkrt |t|t r$|dd ks|dd kr$| |||} | dk st | W S|WS|dd8<XdS)a!Return the value of variable, or a substitution. Never call this function. It is called automatically by str.format. It provides the value of an variable, or a string substitution. @param key the string key being analyzed by str.format() @param args the indexed arguments to str.format() @param kwargs the keyword arguments to str.format()__depth__key __section__confN __taskvars/rTraw@inc,configdir{%) configparserMAX_INTERPOLATION_DEPTHInterpolationDepthErrorrCintrrNOTFOUND has_optionsplitKeyErrorr<r,rB) rkeyr6r7confvisecsectionnkeyosecvnewrrrr;{sd               zConfFormatter.get_value)F) r"r#r$r%r(propertyr0r)r+r; __classcell__rrr1rr&Ks r&c Csr|sgSt|ts,tdt|jt|ft}d}d}d}d}td|D]}| drn|| d7}qN| dr|| d7}qN| dr|d7}qN| d r|d 7}qN| d r|| d 7}qN| d r| || d | d| dfd}qN| drN| ddkr"t dqN| dd kr (?: \' (?! \} ) | [^'] )* ) \' \} | \{ \" (?P (?: \" (?! \} ) | [^"] )* ) \" \} | (?P \{ (?P [^\}:!\['"\{] [^\}:!\[]* (?: \. [a-zA-Z_][a-zA-Z_0-9]+ | \[ [^\]]+ \] )* ) (?: ! (?P[rs]) )? (?: : (?P (?: [^\{\}]+ | \{[^\}]*\} )* ) )? \} ) | (?P \{\{ ) | (?P \}\} ) | (?P [^\{\}]+ ) | (?P . ) ) ZqescapeZdqescapeZleft_setrYZ right_set}rEZreplacement_fieldrFrGrHerrorz'Single '{' encountered in format stringz'Single '}' encountered in format stringzUnexpected %s in format string) rCr< TypeErrortyper"r=listrefinditergroupappendr>)r5resultrErFrGrHmrrrr-sV              r- %Y%m%d%H%M%Y%m%d%H%Y%m%d%Y%y%C%m%d%H%M)ZfYMDHMZfYMDHZfYMDZfyearZfYYYYZfYYZfCCZfcenZfmonthZfMMZfdayZfDDZfhourZfcycZfHHZfminutefmin)ZaYMDHMZaYMDHZaYMDZayearZaYYYYZaYYZaCCZacenZamonthZaMMZadayZaDDZahourZacycZaHHZaminuteamin)Zam6YMDHMZam6YMDHZam6YMDZam6yearZam6YYYYZam6YYZam6CCZam6cenZam6monthZam6MMZam6dayZam6DDZam6hourZam6cycZam6HHZ am6minuteZam6min)Zap6YMDHMZap6YMDHZap6YMDZap6yearZap6YYYYZap6YYZap6CCZap6cenZap6monthZap6MMZap6dayZap6DDZap6hourZap6cycZap6HHZ ap6minuteZap6minfahrfaminfahrmincs*eZdZdZdfdd ZddZZS)ConfTimeFormattera~!internal function that implements time formatting Like its superclass, ConfFormatter, this class is part of the implementation of HAFSConfig, and is used to interpolate strings in a way similar to string.format(). It works the same way as ConfFormatter, but accepts additional keys generated based on the forecast and analysis times: fYMDHM - 201409171200 = forecast time September 17, 2014 at 12:00 UTC fYMDH - 2014091712 fYMD - 20140917 fyear - 2014 fYYYY - 2014 fYY - 14 (year % 100) fCC - 20 (century) fcen - 20 fmonth - 09 fMM - 09 fday - 17 fDD - 17 fhour - 12 fcyc - 12 fHH - 12 fminute - 00 fmin - 00 Replace the initial "f" with "a" for analysis times. In addition, the following are available for the time difference between forecast and analysis time. Suppose the forecast is twenty-three hours and nineteen minutes (23:19) after the analysis time: fahr - 23 famin - 1399 ( = 23*60+19) fahrmin - 19 Fcstt|jt|ddS)z"!constructor for ConfTimeFormatterr0N)r'rr(boolr/r1rrr(Ys zConfTimeFormatter.__init__c Cst}|dd7<|dtjkr8t|d|d|zvt|trT||W`S||krh||}nd|kr|dr||dkr|d|}nd|kr|tkr|dt|}n\d|kr|tkr|dt|}n6d|kr|t kr|dt dd }|t |}nd|krT|t krT|dt dd }|t |}nd|krd|kr|t krtj|d|d\}}|d krt|}n@|d krt|d |}n$|d krt|}nt|d |}n2|d} | dkr2|d| } || dd} | s&|dd} | r>| }n |dd} |dd} | r| r| | |rr| | |}nB| | dr| | ddD] } | | |r| | |}q|tkr| d|r| d|}n| d|r| d|}|tkrtdt|t| ft|tr|ddksB|ddkrz&||||}|dk s`t|WWJStk r}z t|d|d|t|W5d}~XYnX|WS|dd8<XdS)a!return the value of a variable, or a substitution Never call this function. It is called automatically by str.format. It provides the value of an variable, or a string substitution. @param key the string key being analyzed by str.format() @param args the indexed arguments to str.format() @param kwargs the keyword arguments to str.format()rLrMrNrOrQ__ftime__atimeri`Trr<rrRNrPrUrVrWrXz Cannot find key %s in section %srYrZ)r_r[r\r]rCr^ FCST_KEYSstrftimeANL_KEYS ANL_M6_KEYSdatetime timedelta ANL_P6_KEYSTIME_DIFF_KEYStcutilnumerics fcst_hr_minrrr`rarbr=r<r,rBrZInterpolationMissingOptionError)rrcr6r7reZam6Zap6ihoursiminutesrfrgrhrdrirjerrrr;]s                     zConfTimeFormatter.get_value)F)r"r#r$r%r(r;rlrrr1rr6s"rc Cst}t|g}t|dkr|d}||kr0q||||D]`\}} |||rb|||| ||krDt| dD]*} | } t| dkrx| |krx| | qxqDqdS)a!walks through a ConfigParser-like object performing some action Recurses through a ConfigParser-like object "conf" starting at section "start", performing a specified action. The special variable whose name is in recursevar specifies a list of additional sections to recurse into. No section will be processed more than once, and sections are processed in breadth-first order. For each variable seen in each section (including recursevar), this will call selector(sectionname, varname) to see if the variable should be processed. If selector returns True, then acceptor(section, varname, value) will be called. @param conf the ConfigParser-like object @param start the starting section @param selector a function selector(section,option) that decides if an option needs processing (True) or not (False) @param acceptor a function acceptor(section,option,value) run on all options for which the selector returns True @param recursevar an option in each section that lists more sections the confwalker should touch. If the selector returns True for the recursevar, then the recursevar will be sent to the acceptor. However, it will be scanned for sections to recurse into even if the selector rejects it.rrVN) setr<lenpopadditemsreversedrastripru) rdstartselectorZacceptorZ recursevartouchedZ requestedsecrcr Zsec2trimrrrrs      FcCs.t|tstdtt|d}|||S)z!Reads the specified conf file into an HAFSConfig object. Creates a new HAFSConfig object and instructs it to read the specified file. @param filename the path to the file that is to be read @return a new HAFSConfig objectz6First input to hafs.config.from_file must be a string.r)rCr<rorrread)filenamer0rdrrrrs   cCs.t|tstdtt|d}|||S)z!Reads the given string as if it was a conf file into an HAFSConfig object Creates a new HAFSConfig object and reads the string data into it as if it was a config file @param confstr the config data @return a new HAFSConfig objectz8First input to hafs.config.from_string must be a string.r)rCr<rorrreadstr)confstrr0rdrrr from_strings   rc@seZdZdZdeddZeddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZ eddZddZddZddZd d!Zdfd"d#Zd$d%Zeeddd&Zd'd(Zd)d*Zeeedd+Zd,d-Zd.d/Zd0d1Zd2d3Zdgd4d5Zdhd6d7Zdid8d9Zd:d;Z dd?Z"d@dAZ#djdBdCZ$dDdEZ%dkdFdGZ&dHdIZ'dJdKZ(dLdMZ)ee'e(e)dNZ*dOdPZ+dldQdRZ,dmdSdTZ-dndUdVZ.dodWdXZ/dpdYdZZ0dqd[d\Z1drd]d^Z2d_d`Z3dsdadbZ4dtdcddZ5dS)uraB!a class that contains configuration information This class keeps track of configuration information for all tasks in a running HAFS model. It can be used in a read-only manner as if it was a ConfigParser object. All HAFSTask objects require an HAFSConfig object to keep track of registered task names via the register_task_name method, the current forecast cycle (cycle property) and the Datastore object (datastore property). This class should never be instantiated directly. Instead, you should use the hafs.config.from_string or hafs.config.from_file to read configuration information from an in-memory string or a file. Also note that this class should not be used to create a new config file for the first HAFS job in a workflow. The hafs.launcher module does that for you.NF;cCstd|_|j}t|_tt||_t t||_ d|_ t |_ |dkrZt||dn||_t|j_|jd|jdt|_dS)a!HAFSConfig constructor Creates a new HAFSConfig object. @param conf the underlying configparser.ConfigParser object that stores the actual config data. This was a SafeConfigParser in Python 2 but in Python 3 the SafeConfigParser is now ConfigParser. @param quoted_literals if True, then {'...'} and {"..."} will be interpreted as quoting the contained ... text. Otherwise, those blocks will be considered errors. @param strict set default to False so it will not raise DuplicateOptionError or DuplicateSectionError, This param was added when ported to Python 3.6, to maintain the previous python 2 behavior. @param inline_comment_prefixes, defaults set to ;. This param was added when ported to Python 3.6, to maintain the previous python 2 behavior. Note: In Python 2, conf was ConfigParser.SafeConfigParser. In Python 3.2, the old ConfigParser class was removed in favor of SafeConfigParser which has in turn been renamed to ConfigParser. Support for inline comments is now turned off by default and section or option duplicates are not allowed in a single configuration source.Z prodconfigN)strictinline_comment_prefixesrWrX)logging getLogger_logger threadingRLock_lockr&r _formatterr_time_formatter _datastorer _tasknamesr_confr<Z optionxform add_sectionrq_fallback_callbacks)rrdr0rrloggerrrrr( s    zHAFSConfig.__init__cCs|jjo|jjSr3)rr0rr4rrrr05szHAFSConfig.quoted_literalscCs.|ddd}|jD]}t||||}q|S)a;!Asks whether the specified fallback is allowed. May perform other tasks, such as alerting the operator. Calls the list of functions sent to add_fallback_callback. Each one receives the result of the last, and the final result at the end is returned. Note that ALL of the callbacks are called, even if one returns False; this is not a short-circuit operation. This is done to allow all reporting methods report to their operator and decide whether the fallback is allowed. Each function called is f(allow,name,details) where: - allow = True or False, whether the callbacks called thus far have allowed the fallback. - name = The short name of the fallback. - details = A long, human-readable description. May be several lines long. @param name the name of the emergency situation @warning This function may take seconds or minutes to return. It could perform cpu- or time-intensive operations such as emailing an operator. rWZallow_fallbacksF)getboolrr)rnamedetailsZallowfcrrrfallback:s zHAFSConfig.fallbackcCs|j|dS)aM!Appends a function to the list of fallback callback functions called by fallback() Appends the given function to the list that fallback() searches while determining if a workflow emergency fallback option is allowed. @param function a function f(allow,name,details) @see fallbacks()N)rru)rfunctionrrradd_fallback_callback[s z HAFSConfig.add_fallback_callbackcCs$tt|}|j|||S)z!read config data and add it to this object Given a string with conf data in it, parses the data. @param source the data to parse @return self)rr<rreadfprA)rsourcefprrrrgs  zHAFSConfig.readstrcCs|j||S)a\!reads and parses a config file Opens the specified config file and reads it, adding its contents to the configuration. This is used to implement the from_file module-scope function. You can use it again on an HAFSConfig object to read additional files. @param source the file to read @return self)rrrrrrrrss zHAFSConfig.readcCs|j||S)z!read config data from an open file Reads a config file from the specified file-like object. This is used to implement the readstr. @param source the opened file to read @return self)rrrrrrrs zHAFSConfig.readfpcCst|}|j||S)a:!reads config data from an in-memory string Reads the given string as a config file. This is used to implement the from_string module-scope function. You can use it again to read more config data into an existing HAFSConfig. @param string the string to parse @return self)rrr)rstringsiorrrrs cKs.|D] \}}t|}|j|||qdS)aq!set values of several options in a section Sets the value of several options in one section. The keywords arguments are the names of the options to set and the keyword values are the option values. @param section the section being modified @param kwargs additional keyword arguments are the option names and valuesN)rr<rr)rrgr7krerJrrr set_optionss zHAFSConfig.set_optionsc CsJ|}|d|tjj|d}t|d}||gW5QRX|S)a!reads tcvitals WARNING: This is presently unused and may be removed. It does not belong in HAFSConfig. Reads tcvitals from the specified file if specified, or from the current cycle's vitals storage area if not. Does not parse, clean or otherwise modify the vitals: they are assumed to contain output for only one storm, with no duplicate cycles. Ideally, the file should have been created by the hafs.launcher module. Returns an tcutil.revital.Revital object.zread vitals from: rrt)loginforrevitalRevitalopen readfiles)rvitfilerrfrrrread_precleaned_vitfiles  z"HAFSConfig.read_precleaned_vitfilecCs|dddS)z!is this a real-time simulation? Is this configuration for a real-time simulation? Defaults to True if unknown. This is the same as doing getbool('config','realtime',True).rWrealtimeTrr4rrrrszHAFSConfig.realtimecCs |jt|t|t|dS)z!set a config option Sets the specified config option (key) in the specified section, to the specified value. All three are converted to strings via str() before setting the value.N)rrr<)rrgrcrJrrrrszHAFSConfig.setcCs|jdS)z!grab the thread lock Grabs this HAFSConfig's thread lock. This is only for future compatibility and is never used.N)racquirer4rrr __enter__szHAFSConfig.__enter__cCs|jdS)z!release the thread lock Releases this HAFSConfig's thread lock. This is only for future compatibility and is never used. @param a,b,c unusedN)rrelease)rabcrrr__exit__szHAFSConfig.__exit__c Cs<|.||jkr"tjd|f|j|W5QRXdS)a!add an hafs.hafstask.HAFSTask to the database Checks to ensure that there is no other task by this name, and records the fact that there is now a task. This is used by the hafs.hafstask.HAFSTask to ensure only one task is made by any name.z)%s: attempted to use this task name twiceN)rhafs exceptionsZDuplicateTaskNamer)rrrrrregister_hafs_tasks  zHAFSConfig.register_hafs_taskc Cs4|dk r.|td|W5QRSQRX|jS)a}!returns a logging.Logger object Returns a logging.Logger object. If the sublog argument is provided, then the logger will be under that subdomain of the "hafs" logging domain. Otherwise, this HAFSConfig's logger (usually the "hafs" domain) is returned. @param sublog the logging subdomain, or None @return a logging.Logger objectNzhafs.)rrr)rZsublogrrrrs  zHAFSConfig.logc Csb|j}|dk r|S|B|jdkrF|dd}tjj||dd|_|jW5QRSQRXdS)zl!returns the Datastore Returns the produtil.datastore.Datastore object for this HAFSConfig.NrW datastorer)rgetstrprodutilrrr)rdZdsfilerrr getdatastores  zHAFSConfig.getdatastorezReturns the Datastore object for this HAFS simulation, creating it if necessary. If the underlying datastore file did not already exist, it will be opened in create=True mode.cCs$|jdkrt|jdd|_|jS)zo!get the analysis time Returns the analysis time of this HAFS workflow as a datetime.datetime.NrWcycle)_cycler rrr4rrrgetcycles zHAFSConfig.getcyclecCs4t|}|d}|jdd|||_|dS)z!set the analysis time Sets the analysis time of this HAFS workflow. Also sets the [config] section's "cycle" option. Accepts anything that tcutil.numerics.to_datetime recognizes.ryrWrN)r rrrr set_time_vars)rrstrcyclerrrsetcycles  zHAFSConfig.setcyclezBThe cycle this HAFS simulation should run, as a datetime.datetime.c Cs:|,dD] \}}|jd||j|q W5QRXdS)a!internal function that sets time-related variables Sets many config options in the [config] section based on this HAFS workflow's analysis time. This is called automatically when the cycle property is assigned. You never need to call this function directly. YMDHM - 201409171200 = forecast time September 17, 2014 at 12:00 UTC YMDH - 2014091712 YMD - 20140917 year - 2014 YYYY - 2014 YY - 14 (year % 100) CC - 20 (century) cen - 20 month - 09 MM - 09 day - 17 DD - 17 hour - 12 cyc - 12 HH - 12 minute - 00 min - 00))ZYMDHMrx)YMDHry)ZYMDrz)yearr{)ZYYYYr{)ZYYr|)ZCCr})cenr})monthr~)ZMMr~)dayr)ZDDr)hourr)cycr)ZHHr)minuter)minrrWN)rrrr)rrfmtrrrrs zHAFSConfig.set_time_varsc Cs,||j||W5QRSQRXdS)z!add a new config section Adds a section to this HAFSConfig. If the section did not already exist, it will be initialized empty. Otherwise, this function has no effect. @param sec the new section's nameN)rrrrrrrr:s zHAFSConfig.add_sectionc Cs(||j|W5QRSQRXdS)z!does this section exist? Determines if a config section exists (even if it is empty) @return True if this HAFSConfig has the given section and False otherwise. @param sec the section to check forN)r has_sectionrrrrrDszHAFSConfig.has_sectionc Cs*||j||W5QRSQRXdS)a#! is this option set? Determines if an option is set in the specified section @return True if this HAFSConfig has the given option in the specified section, and False otherwise. @param sec the section @param opt the name of the option in that sectionN)rr`)rroptrrrr`MszHAFSConfig.has_optionc Cs0|"|jd||||dW5QRSQRXdS)a! query the "dir" section Search the "dir" section. @return the specified key (name) from the "dir" section. Other options are passed to self.getstr. @param default the default value if the option is unset @param morevars more variables for string substitution @param taskvars even more variables for string substitution @param name the option name to search forrXdefaultmorevarstaskvarsNrrrrrrrrrgetdirWs  zHAFSConfig.getdirc Cs|~|d|r2|jd||||dW5QRS|d|r^|jd||||dW5QRS|jd||||dW5QRSW5QRXdS)a!search the config, exe and dir sections in that order Find the location of a file in the named option. Searches the [config], [exe] and [dir] sections in order for an option by that name, returning the first one found. @param default the default value if the option is unset @param morevars more variables for string substitution @param taskvars even more variables for string substitution @param name the option name to search for @returns the resulting valuerWrexerXN)r`rrrrrgetloces      zHAFSConfig.getlocc Cs0|"|jd||||dW5QRSQRXdS)a! query the "exe" section Search the "exe" section. @return the specified key (name) from the "exe" section. Other options are passed to self.getstr. @param default the default value if the option is unset @param morevars more variables for string substitution @param taskvars even more variables for string substitution @param name the option name to search forrrNrrrrrgetexezs  zHAFSConfig.getexec Cs|t|tr*t||W5QRSt|ts>t|trt|dkrdt||W5QRSt|dkr|t|dt|dW5QRSt|dkr|jt|dt|d|ddW5QRSW5QRXtS)a!convenience function; replaces self.items and self.get This is a convenience function that provides access to the self.items or self.get functions. * conf["section"] -- returns a dict containing the results of self.items(arg) * conf[a,b,c] -- returns self.get(a,b,c) (b and c are optional) @param arg the arguments: a list or stringrMrr)r) rCr<dictrrqtuplerrNotImplemented)rargrrrr!s    ( zHAFSConfig.__getitem__c s<fdd|D}W5QRX|D]}tj|q&dS)a.!calls produtil.fileop.makedirs() on directories in the [dir] section This is a simple utility function that calls produtil.fileop.makedirs() on some of the directories in the [dir] section. @param args the keys in the [dir] section for the directories to make.csg|]}d|qS)rXr).0r r4rr sz'HAFSConfig.makedirs..N)rfileopmakedirs)rr6dirsZmakemerr4rrszHAFSConfig.makedirsc Cs2|$dd|j|DW5QRSQRXdS)z!get options in a section Returns a list containing the config options in the given section. @param sec the string name of the sectioncSsg|]}|qSrr)r rrrrr sz#HAFSConfig.keys..NroptionsrrrrkeysszHAFSConfig.keyscCs |jS)z:!gets the list of all sections from a configuration object)rsectionsr4rrrrszHAFSConfig.sectionsc CsFg}|4|j|D] }|||j||||dfqW5QRX|S)a8!get the list of (option,value) tuples for a section Returns a section's options as a list of two-element tuples. Each tuple contains a config option, and the value of the config option after string interpolation. Note that the special config section inclusion option "@inc" is also returned. @param sec the section @param morevars variables for string substitution @param taskvars yet more variables @return a list of (option,value) tuples, where the value is after string expansionr)rrru_interp)rrrrrDrrrrrs zHAFSConfig.itemsc Cs ||j|W5QRXdS)a!write the contents of this HAFSConfig to a file Writes the contents of an HAFSConfig to the specified file, without interpolating (expanding) any strings. The file will be suitable for reading in to a new HAFSConfig object in a later job. This is used by the hafs.launcher module to create the initial config file. @param fileobject an opened file to write toN)rr9)rZ fileobjectrrrr9s zHAFSConfig.writecCs@z|jj||ddWStk r:|dk r4|YSYnXdS)a!return the raw value of an option Returns the raw value for the specified section and option, without string interpolation. That is, any {...} will be returned unmodified. Raises an exception if no value is set. Will not search other sections, unlike other accessors. @param sec the section @param opt the option name @param default the value to return if the option is unset. If unspecified or None, NoOptionError is raisedTrSN)rrr )rrrrrrrgetraws zHAFSConfig.getrawcCsd|jkr|jSdSNsyndat__dict__rr4rrr getvitalss zHAFSConfig.getvitalscCst|tjjst||_dSr3)rCr storminfo StormInforBr)rvitrrr setvitalsszHAFSConfig.setvitalscCsd|jkr|`dSrrr4rrr delvitalss zHAFSConfig.delvitalszzThe tcutil.storminfo.StormInfo describing the storm to be run, or a fake storm representing the basin in multi-storm mode.c Kst|tstt|tst|ld|kr@d|jkr@|jj|d<d|kr^d|jkr^|jj|d<|jj|f|dd|jt d|W5QRSQRXdS) a8!perform string expansion Performs this HAFSConfig's string interpolation on the specified string, as if it was a value from the specified section. @param sec the section name @param string the string to expand @param kwargs more variables for string substitutionrroldvit oldsyndat __string__r)rOrNrLrPENVN) rCr<rBrrr#rr*rr)rrrr7rrr strinterps    zHAFSConfig.strinterpc Ks|dk rtj|}n|j}|dkr*|}ntj||}d|krVd|jkrV|jj|d<d|krtd|jkrt|jj|d<|4|jj |f|dd|j t ||d|W5QRSQRXdS) a!performs string expansion, including time variables Performs this HAFSConfig's string interpolation on the specified string, as self.strinterp would, but adds in additional keys based on the given analysis and forecast times. The keys are the same as the keys added to [config] for the cycle, except with "a" prepended for the analysis time, or "f" for the forecast time. There are three more keys for the difference between the forecast an analysis time. The famin is the forecast time in minutes, rounded down. The fahr and fahrmin are the forecast hour, rounded down, and the remainder in minutes, rounded down to the next nearest minute. If the analysis time is None or unspecified, then self.cycle is used. The atime can be anything understood by tcutil.numerics.to_datetime and the ftime can be anything understood by tcutil.numerics.to_datetime_rel, given the atime (or, absent atime, self.cycle) as the second argument. This is implemented as a wrapper around the self._time_formatter object, which knows how to expand the a* and f* variables without having to generate all of them. @param sec the section name @param string the string to expand @param ftime the forecast time or None @param atime the analysis time or None @param kwargs more variables for string expansionNrrr"r#r$r)rOrNrLrPr%rr) rrr rto_datetime_relrrr#rr*rr)rrrftimeatimer7rrr timestrinterps*   zHAFSConfig.timestrinterpc Csi}i}d|jkr|jj}d|jkr,|jj}|dddf}d}|D].} | dkr"z|j|d} Wntk rzd} YnX| rt|ddf} | dD]x} | } t | d kr| | kr| | z"|jj| |d d }d }WqWqt t tfk r}zW5d }~XYqXq|rpqrq@z"|jj| |d d }d }WqrWq@t t tfk rn}zW5d }~XYq@Xq@|st|||d kr|j j|||d |j|t||d S|j j|f||d |j|t||d |Sd S)a!implementation of data-getting routines This is the underlying implementation of the various self.get* routines, and lies below the self._get. It reads a config option opt from the config section sec. If the string contains a {...} expansion, the _interp will perform string interpolation, expanding {...} strings according to ConfigParser rules. If the section contains an @inc, and any variables requested are not found, then the sections listed in @inc are searched. Failing that, the config and dir sections are searched. @param sec the section name @param opt the option name @param morevars a dict containing variables whose values will override anything in this HAFSConfig when performing string interpolation. @param taskvars serves the same purpose as morevars, but provides a second scope. @return the result of the string expansionrr#rWrXrUFrrVrTrSN)rOrNrLrPrr%r"rQ)rrr#rrr rrarrrrbr rr*r)rrrrrZvitdictZolddictrZgottedrgincrZ incsectionrgotrrrrr6sv            zHAFSConfig._interpc Csz(|j||||d}|dk s t||WStk rN|dk rH|YSYn0tk r||rv|dk rp|YSYdSYnXdS)a! high-level implemention of get routines This is the implementation of all of the self.get* routines. It obtains option opt from section sec via the self._interp, providing the optional list of additional variables for string interpolation in the morevars. It then converts to the given type via typeobj (for example, typeobj=int for int conversion). If default is not None, and the variable cannot be found, then the default is returned. @param sec the section name @param opt the option name in that section @param default the default value to return if the variable cannot be found, or None if no default is provided. @param badtypeok if True and default is not None, and the type conversion failed, then default is returned. Otherwise, the TypeError resulting from the failed type conversion is passed to the caller. @param morevars a dict containing variables whose values will override anything in this HAFSConfig when performing string interpolation. @param taskvars serves the same purpose as morevars, but provides a second scope. rrN)rrBr ro) rrrtypeobjr badtypeokrrrrrr_getzs  zHAFSConfig._getc Cs4|&|j||t||||dW5QRSQRXdS)a!get an integer value Gets option opt from section sec and expands it; see "get" for details. Attempts to convert it to an int. @param sec,opt the section and option @param default if specified and not None, then the default is returned if an option has no value or the section does not exist @param badtypeok is True, and the conversion fails, and a default is specified, the default will be returned. @param morevars,taskvars dicts of more variables for string expansionrN)r0r^rrrrr/rrrrrgetints zHAFSConfig.getintc Cs4|&|j||t||||dW5QRSQRXdS)a!get a float value Gets option opt from section sec and expands it; see "get" for details. Attempts to convert it to a float @param sec,opt the section and option @param default if specified and not None, then the default is returned if an option has no value or the section does not exist @param badtypeok is True, and the conversion fails, and a default is specified, the default will be returned. @param morevars,taskvars dicts of more variables for string expansionrN)r0floatr1rrrgetfloats zHAFSConfig.getfloatc Cs4|&|j||t||||dW5QRSQRXdS)a!get a string value Gets option opt from section sec and expands it; see "get" for details. Attempts to convert it to a str @param sec,opt the section and option @param default if specified and not None, then the default is returned if an option has no value or the section does not exist @param badtypeok is True, and the conversion fails, and a default is specified, the default will be returned. @param morevars,taskvars dicts of more variables for string expansionrN)r0r<r1rrrrs zHAFSConfig.getstrc Csh|Zz |j||||dWW5QRStk rX|dk rR|YW5QRSYnXW5QRXdS)a!get the value of an option from a section Gets option opt from section sec, expands it and converts to a string. If the option is not found and default is specified, returns default. If badtypeok, returns default if the option is found, but cannot be converted. The morevars is used during string expansion: if {abc} is in the value of the given option, and morevars contains a key abc, then {abc} will be expanded using that value. The morevars is a dict that allows the caller to override the list of variables for string extrapolation. @param sec,opt the section and option @param default if specified and not None, then the default is returned if an option has no value or the section does not exist @param badtypeok is True, and the conversion fails, and a default is specified, the default will be returned. @param morevars,taskvars dicts of more variables for string expansionrN)rr r1rrrrs zHAFSConfig.getc Cs(||j|W5QRSQRXdS)zy!what options are in this section? Returns a list of options in the given section @param sec the sectionNrrrrrrszHAFSConfig.optionscCs|j||||||dS)av!alias for getbool: get a bool value This is an alias for getbool for code expecting a ConfigParser. Gets option opt from section sec and expands it; see "get" for details. Attempts to convert it to a bool @param sec,opt the section and option @param default if specified and not None, then the default is returned if an option has no value or the section does not exist @param badtypeok is True, and the conversion fails, and a default is specified, the default will be returned. @param morevars,taskvars dicts of more variables for string expansion)rr/rrrr1rrr getbooleans zHAFSConfig.getbooleanc Csz&||j||||d}W5QRXWn*tk rP|dk rJt|YSYnXtd|rbdStd|rrdSzt|dkWStk r}zW5d}~XYnX|r|dk rt|Std||t|fdS) a!get a bool value Gets option opt from section sec and expands it; see "get" for details. Attempts to convert it to a bool @param sec,opt the section and option @param default if specified and not None, then the default is returned if an option has no value or the section does not exist @param badtypeok is True, and the conversion fails, and a default is specified, the default will be returned. @param morevars,taskvars dicts of more variables for string expansionr-Nz$(?i)\A(?:T|\.true\.|true|yes|on|1)\ZTz&(?i)\A(?:F|\.false\.|false|no|off|0)\ZFrz3%s.%s: invalid value for HAFS conf file boolean: %s)rr rrrmatchr^r>r=) rrrrr/rrrrrrrrs(       zHAFSConfig.getbool)NFFr)N)NNN)NNN)NNN)NN)N)NN)NN)NN)NFNN)NFNN)NFNN)NFNN)NFNN)NFNN)6r"r#r$r%r(rkr0rrrrrrrrrrrrrrrrrrrrrr`rrrr!rrrrr9rrr r!vitalsr&r*rr0r2r4rrrr5rrrrrrsv ) !        !          . D %     )F)F)1r%__all__ collectionsrrrrrros.pathsysrprodutil.fileoprZprodutil.datastoretcutil.numericsrtcutil.storminfotcutil.revitalhafs.exceptionsrr[riorrr r r r rrobject UNSPECIFIEDrrr&r-rrrrrrr_rrrrrrrrrs 0        lC     *