3 2g@sddlZddlZddlZddlmZmZddlmZddZdddZdd d Z d d Z dddZ dddZ ddZ ddZdddZdddZdS) N)datetimedate) ModuleTypec Cs|d}ybt|}|dkr$tj|d}n@|dkr:tj|d}n*|dkrPtj|d}n|dkrdtj|d }Wnd}YnX|S) zxGet python datetime object from string. Args: s: a string Returns: datetime object or None Nz%Y%m%d z%Y%m%d%H z %Y%m%d%H%Mz %Y%m%d%H%M%S)lenrstrptime)svlrE/lfs/h1/ops/prod/packages/aqm.v7.0.11/ush/python_utils/environment.py str_to_date s r %Y%m%d%H%McCs|j|}|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)dformatr rrr date_to_str"s rcCs|jd}|dkr|jd kr"dS|jdkr2d St|}|d k rR|d krN|S|Sy t|}|SYnXy t|}|SYnX|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"'trueyesyeahTfalsenonopeFN)rrr)rrr)striplowerrintfloat)r return_stringr rrr str_to_type0s,   r#cCsLt|tr|rdSdSt|ttfr&nt|tr8t|S|dkrDdSt|S)zGiven 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) isinstanceboolr r!rrstr)r rrr type_to_strWs   r(FcCslt|tr|St|tr^dd|D}|s6t|dkrJddj|d}qhddj|d }n t|}|S) zGiven 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().0irrr wszlist_to_str..z( "z" "z" )z( \ "z" \ "z" \ ))r%r'listr joinr()r ZonelineZ shell_strrrr list_to_strks   r/cCst|ts|S|j}|sdS|ddkr6|d dksN|ddkr|d dkr|dd}|jdd }tj|}g}xP|D]H}|j}|d krqzd |kr|jd }||dd}|jt||qzW|St||S)zGiven 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$=r7r7) r%r'rreplaceshlexsplitfindappendr#)r r"tokensZlstZitmidxrrr str_to_lists( 0     r?cCst|tj|<dS)zSet an environment variable Args: param: the variable to set value: either a string, list of strings or None Returns: None N)r/osenviron)paramvaluerrr set_env_vars rDcCs |tjkrdStj|}t|S)zGet the value of an environement variable Args: param: the environement variable Returns: Returns either a string, list of strings or None N)r@rAr?)rBrCrrr get_env_vars  rEcsdkrtj|dkr(tjddj}|dkr6}nfdd|D}x8|jD],\}}||kop|dkpp|dksRt|||<qRWdS)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@rAinspectstack f_globalsitemsr?)rG target_dictenv_varsrFr r)rGr import_varssrOcs|dkrtj}dkr(tjddj|dkr6}nfdd|D}xJ|jD]>\}}t|rdqRt|trpqR| sR|ddkrqRt |||<qRWdS)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|qS)Nr)r)rF) source_dictrrrHszexport_vars.._) r@rArIrJrKrLcallabler%rr/)rGrPrNrFr r)rPr export_varss  rS)r)r)F)r)NNN)NNN)r@rIr9rrtypesrrrr#r(r/r?rDrErOrSrrrrs   '  !  -