def apply_func(func, arg): """ Apply post-processing function |func| to |arg|. |func| is a string representing a list of functions (which are to be applied to |arg| in succession). Each function is either: - 'duration', 'date', 'size' for special formatting - '%...' for sprintf-style formatting - s/.../... for regular expression substitution - [a:b] for taking substrings """ FUNC_DELIM = ' | ' if isinstance(arg, tuple): # tuples are (bundle_uuid, genpath) which have not been fleshed out return arg + (func,) try: if func is None: return arg # String encoding of a function: size s/a/b for f in func.split(FUNC_DELIM): if f == 'date': arg = formatting.date_str(float(arg)) if arg is not None else None elif f == 'duration': arg = formatting.duration_str(float(arg)) if arg is not None else None elif f == 'size': arg = formatting.size_str(float(arg)) if arg is not None else None elif f.startswith('%'): arg = (f % float(arg)) if arg is not None else None elif f.startswith('s/'): # regular expression: s/<old string>/<new string> esc_slash = '_ESC_SLASH_' # Assume this doesn't occur in s # Preserve escaped characters: \/ tokens = f.replace('\\/', esc_slash).split('/') if len(tokens) != 3: return '<invalid regex: %s>' % f s = tokens[1].replace(esc_slash, '/') t = tokens[2].replace(esc_slash, '/') arg = re.sub(s, t, arg) elif f.startswith('['): # substring m = re.match('\[(.*):(.*)\]', f) if m: start = int(m.group(1) or 0) end = int(m.group(2) or len(arg)) arg = arg[start:end] else: return '<invalid function: %s>' % f elif f.startswith('add '): # 'add k v' checks if arg is a dictionary and updates it with arg[k] = v if isinstance(arg, dict): k, v = f.split(' ')[1:] arg[k] = v else: return 'arg (%s) not a dictionary' % type(arg) elif f.startswith('key '): # 'key k' converts arg into a dictionary where arg[k] = arg arg = {f.split(' ')[1]: arg} else: return '<invalid function: %s>' % f return arg except: # Applying the function failed, so just return the arg. return arg
def apply_func(func, arg): ''' Apply post-processing function |func| to |arg|. |func| is a string representing a list of functions (which are to be applied to |arg| in succession). Each function is either: - 'duration', 'date', 'size' for special formatting - '%...' for sprintf-style formatting - s/... for regular expression substitution - [a:b] for taking substrings ''' FUNC_DELIM = ' | ' if isinstance(arg, tuple): # tuples are (bundle_uuid, genpath) which have not been fleshed out return arg + (func,) try: if func == None: return arg # String encoding of a function: size s/a/b for f in func.split(FUNC_DELIM): if f == 'date': arg = formatting.date_str(arg) elif f == 'duration': arg = formatting.duration_str(float(arg)) if arg != None else '' elif f == 'size': arg = formatting.size_str(arg) elif f.startswith('%'): arg = (f % float(arg)) if arg != None else '' elif f.startswith('s/'): # regular expression _, s, t = f.split("/") arg = re.sub(s, t, arg) elif f.startswith('['): # substring m = re.match('\[(.*):(.*)\]', f) if m: start = int(m.group(1) or 0) end = int(m.group(2) or -1) arg = arg[start:end] else: return '<invalid function: %s>' % f else: return '<invalid function: %s>' % f return arg except: # Can't apply the function, so just return the arg. return arg
def apply_func(func, arg): """ Apply post-processing function |func| to |arg|. |func| is a string representing a list of functions (which are to be applied to |arg| in succession). Each function is either: - 'duration', 'date', 'size' for special formatting - '%...' for sprintf-style formatting - s/.../... for regular expression substitution - [a:b] for taking substrings """ FUNC_DELIM = ' | ' if isinstance(arg, tuple): # tuples are (bundle_uuid, genpath) which have not been fleshed out return arg + (func, ) try: if func is None: return arg # String encoding of a function: size s/a/b for f in func.split(FUNC_DELIM): if f == 'str': arg = str(arg) elif f == 'date': arg = formatting.date_str( float(arg)) if arg is not None else None elif f == 'duration': arg = formatting.duration_str( float(arg)) if arg is not None else None elif f == 'size': arg = formatting.size_str( float(arg)) if arg is not None else None elif f.startswith('%'): arg = (f % float(arg)) if arg is not None else None elif f.startswith( 's/'): # regular expression: s/<old string>/<new string> esc_slash = '_ESC_SLASH_' # Assume this doesn't occur in s # Preserve escaped characters: \/ tokens = f.replace('\\/', esc_slash).split('/') if len(tokens) != 3: return '<invalid regex: %s>' % f s = tokens[1].replace(esc_slash, '/') t = tokens[2].replace(esc_slash, '/') arg = re.sub(s, t, arg) elif f.startswith('['): # substring m = re.match('\[(.*):(.*)\]', f) if m: start = int(m.group(1) or 0) end = int(m.group(2) or len(arg)) arg = arg[start:end] else: return '<invalid function: %s>' % f elif f.startswith('add '): # 'add k v' checks if arg is a dictionary and updates it with arg[k] = v if isinstance(arg, dict): k, v = f.split(' ')[1:] arg[k] = v else: return 'arg (%s) not a dictionary' % type(arg) elif f.startswith('key '): # 'key k' converts arg into a dictionary where arg[k] = arg arg = {f.split(' ')[1]: arg} else: return '<invalid function: %s>' % f return arg except: # Applying the function failed, so just return the arg. return arg