o nÇgú„ã @sLdZddlZddlZddlZddlmZddlZdZdZdZ dZ dZ d Z d Z d Zd Zd ZdZdZddddddddddœ ZdZdd„Zdd„Zdd„Zdd„Zdd„Zdd „Zd!d"„Zd#d$„Zd%d&„Zd'd'efd(d)„ZdDd*d+„ZdEd,d-„Z dEd.d/„Z!d0d1„Z"dEd2d3„Z#dEd4d5„Z$d6d7„Z%d8d9„Z&d:d;„Z'dFdd?„Z)d@dA„Z*dBdC„Z+dS)Ga Program Name: string_template_substitution.py Contact(s): Julie Prestopnik, NCAR RAL DTC, jpresto@ucar.edu, George McCabe Abstract: Supporting functions for parsing and creating filename templates History Log: Initial version for METPlus Usage: Usually imported in other Python code for individual function calls Parameters: Varies Input Files: None Output Files: None Condition codes: Varies éN)Ú relativedeltaÚ{Ú}ú?ú=ÚfmtÚshiftÚtruncateÚvalidÚleadÚinitZda_initÚoffsetééé) ÚYÚmÚdÚHÚMÚSÚjÚyÚbéc s4t dd ttjˆ ¡ƒ¡¡}| ‡fdd„|¡S)z¡Helper function for do_string_sub. Replace in 'text' all occurrences of any key in the given dictionary by its corresponding value. Returns the new string. z(%s)ú|csˆ|j| ¡| ¡…S©N)ÚstringÚstartÚend)Úmo©Ú replace_dict©úR/lfs/h1/ops/prod/packages/evs.v1.0.19/ush/analyses/string_template_substitution.pyÚ<sz"multiple_replace..)ÚreÚcompileÚjoinÚmapÚescapeÚkeysÚsub)r"ÚtextÚregexr#r!r$Úmultiple_replace4s r/cCs˜d}t|ƒ}g}||krJ||tkr/| t|¡}||d|…}| d¡d}| |¡|}n||dks;||dkrB| ||¡|d7}||ks |S)aH!Parse template and pull out all wildcard characters (* or ?) and all tags, i.e. {init?fmt=%H}. Used to pull out information from a template that contains wildcards and add that information when filled out another template Returns a list of wildcards and tag names found, i.e. [ '*', 'init', 'lead'] rérÚ*)ÚlenÚTEMPLATE_IDENTIFIER_BEGINÚfindÚTEMPLATE_IDENTIFIER_ENDÚsplitÚappend)ÚtemplateÚiZ template_lenÚtagsÚend_iÚtagÚ identifierr#r#r$Úget_tags>s   ö r>cCs–| |¡}|dkrId}t d|d|¡}|r$t| d¡ƒ}| d¡}n|}t d|d|¡}|r@| d¡}|d kr@td|ƒ}t|ƒ |¡|SdS) az!Helper function for do_string_sub. Determine precision of time offset value and format Args: @param item format to substitute, i.e. 3M or H @param time_str time value that precision will be applied, i.e. 60 @param unit currently being processed, i.e. M or H or S Returns: Padded value or empty string if unit is not found in item rÚz ^\.*(\d+)z(.*)r0rú^z+(.*)Ús)Úcountr&ÚmatchÚintÚgroupÚmaxÚstrÚzfill)ÚitemZtime_strÚunitrBÚrestÚresÚpaddingr#r#r$Úformat_one_time_itemUs    rNc Csd}| d¡dd…}|d}|d}|d}|}dd d „|Dƒvr4|dd}|dd}|dd}dd d „|DƒvrI|dd}|dd}dd d „|DƒvrX|dd}|D]*}|t||d ƒ7}|t||dƒ7}|t||dƒ7}|t||dƒ7}|t||dƒ7}qZ|S)a<!Helper function for do_string_sub. For time offset values, get hour, minute, and second values to format as necessary Args: @param fmt format to substitute, i.e. %3H or %2M or %S @param obj time value in seconds to format, i.e. 3600 Returns: Formatted time value r?ú%r0Ni€Qéé<TcSóg|]}d|v‘qS)rr#©Ú.0Úxr#r#r$Ú †ózformat_hms..cSrR)rr#rSr#r#r$rVŒrWcSrR)rr#rSr#r#r$rV‘rWrrrrAr)r6rN) rÚobjZout_strZ fmt_splitÚdaysÚhoursÚminutesÚsecondsrIr#r#r$Ú format_hmsss,      r]cCs8|ddkr|ddkrttj t|dƒd¡ d¡ƒ|d<|ddkr<|ddkrrNr]rerjrwrr‰rrŽrŸr™r£r¡r¢r¥r¬ršr¸r¹rºrÂr#r#r#r$Úsn ø  + 4 ý E B ! 0 7L "