U ›2Óg¡ã@s”ddlZddlZddlZddlmZmZddlmZdd„Zddd„Zdd d „Z d d „Z ddd„Z ddd„Z dd„Z dd„Zddd„Zddd„ZdS) éN)ÚdatetimeÚdate)Ú ModuleTypecCs|d}zbt|ƒ}|dkr$t |d¡}n@|dkr:t |d¡}n*|dkrPt |d¡}n|dkrdt |d ¡}Wnd}YnX|S) zxGet python datetime object from string. Args: s: a string Returns: datetime object or None Néz%Y%m%dé z%Y%m%d%Hé ú %Y%m%d%H%Méz %Y%m%d%H%M%S)ÚlenrÚstrptime)ÚsÚvÚl©rúE/lfs/h1/ops/prod/packages/aqm.v7.0.11/ush/python_utils/environment.pyÚ str_to_date s rrcCs| |¡}|S)aGet string from python datetime object. By default it converts to YYYYMMDDHHMM format unless told otherwise by passing a different format Args: d: datetime object Returns: string in YYYYMMDDHHMM or shorter version of it )Ústrftime)ÚdÚformatr rrrÚ date_to_str"s rcCsŽ| d¡}|dkrŠ| ¡dkr"dS| ¡dkr2dSt|ƒ}|dk rR|dkrN|S|Szt|ƒ}|WSYnXzt|ƒ}|WSYnX|S) a*Check if the string contains a float, int, boolean, datetime, or just regular string. This will be used to automatically convert environment variables to data types that are more convenient to work with. If you don't want this functionality, pass return_string = 1 Args: s: a string return_string: Set to 1 to return the string itself Set to 2 to return the string itself only for a datetime object Returns: a float, int, boolean, datetime, or the string itself when all else fails z"'é)ÚtrueZyesZyeahT)ZfalseZnoZnopeFNé)ÚstripÚlowerrÚintÚfloat)r Ú return_stringr rrrÚ str_to_type0s,   rcCsLt|tƒr|rdSdSt|ttfƒr&nt|tƒr8t|ƒS|dkrDdSt|ƒS)z¹Given a float/int/boolean/date or list of these types, gets a string representing their values Args: v: a variable of the above types Returns: a string ZTRUEZFALSENÚ)Ú isinstanceÚboolrrrrÚstr)r rrrÚ type_to_strWs   r#FcCslt|tƒr|St|tƒr^dd„|Dƒ}|s6t|ƒdkrJdd |¡d}qhdd |¡d }n t|ƒ›}|S) zìGiven a string or list of string, construct a string to be used on right hand side of shell environement variables Args: v: a string/number, list of strings/numbers, or null string('') Returns: A string cSsg|] }t|ƒ‘qSr)r#)Ú.0ÚirrrÚ wszlist_to_str..éz( "z" "z" )z( \ "z" \ "z" \ ))r r"Úlistr Újoinr#)r ZonelineZ shell_strrrrÚ list_to_strks   r*cCsÐt|tƒs|S| ¡}|sdS|ddkr6|ddksN|ddkrÆ|ddkrÆ|dd…}| d d ¡}t |¡}g}|D]H}| ¡}|d krŽqxd |kr°| d ¡}||dd…}| t||ƒ¡qx|St||ƒS) zÚGiven a string, construct a string or list of strings. Basically does the reverse operation of `list_to_string`. Args: v: a string Returns: a string, list of strings or null string('') Nrú(éÿÿÿÿú)ú[ú]rú,ú rú=) r r"rÚreplaceÚshlexÚsplitÚfindÚappendr)r rÚtokensZlstZitmÚidxrrrÚ str_to_list‚s( 0    r:cCst|ƒtj|<dS)z Set an environment variable Args: param: the variable to set value: either a string, list of strings or None Returns: None N)r*ÚosÚenviron©ÚparamÚvaluerrrÚ set_env_var£s r@cCs |tjkrdStj|}t|ƒS)z¨Get the value of an environement variable Args: param: the environement variable Returns: Returns either a string, list of strings or None N)r;r<r:r=rrrÚ get_env_var°s  rAcs‚ˆdkrtj‰|dkr(t ¡ddj}|dkr6ˆ}n‡fdd„|Dƒ}| ¡D],\}}||krp|dksP|dksPt|ƒ||<qPdS)a'Import all (or select few) environment/dictionary variables as python global variables of the caller module. Call this function at the beginning of a function that uses environment variables. Note that for read-only environmental variables, calling this function once at the beginning should be enough. However, if the variable is modified in the module it is called from, the variable should be explicitly tagged as `global`, and then its value should be exported back to the environment with a call to export_vars() import_vars() # import all environment variables global MY_VAR, MY_LIST_VAR MY_PATH = "/path/to/somewhere" MY_LIST_VAR.append("Hello") export_vars() # these exports all global variables There doesn't seem to an easier way of imitating the shell script doing way of things, which assumes that everything is global unless specifically tagged local, while the opposite is true for python. Args: dictionary: source dictionary (default=os.environ) target_dict: target dictionary (default=caller module's globals()) env_vars: list of selected environement/dictionary variables to import, or None, in which case all environment/dictionary variables are imported Returns: None Nrrcs"i|]}||ˆkrˆ|nd“qS©Nr©r$Úk©Ú dictionaryrrÚ äszimport_vars..r)r;r<ÚinspectÚstackÚ f_globalsÚitemsr:)rFZ target_dictÚenv_varsrDr rrErÚ import_vars¿srMcs’|dkrtj}ˆdkr(t ¡ddj‰|dkr6ˆ}n‡fdd„|Dƒ}| ¡D]<\}}t|ƒrbqPt|tƒrnqP|rP|ddkr€qPt |ƒ||<qPdS)aExport all (or select few) global variables of the caller module's to either the environement/dictionary. Call this function at the end of a function that updates environment variables. Args: dictionary: target dictionary to set (default=os.environ) source_dict: source dictionary (default=caller modules globals()) env_vars: list of selected environement/dictionary variables to export, or None, in which case all environment/dictionary variables are exported Returns: None Nrrcs"i|]}||ˆkrˆ|nd“qSrBrrC©Ú source_dictrrrGszexport_vars..Ú_) r;r<rHrIrJrKÚcallabler rr*)rFrOrLrDr rrNrÚ export_varsìs  rR)r)r)F)r)NNN)NNN)r;rHr4rrÚtypesrrrrr#r*r:r@rArMrRrrrrÚs   '  !  -