''' We are doing stuff ''' import glob import json import re import os # pseudocode # load all files # json parse # recursively descend all json struct # Find expr and add a second expr after the or with a re.replace of pod_name & # container_name # write all new files def import_file(f): ''' Read the file as json ''' with open(f, 'r') as r: return json.load(r) def export_file(f, content): ''' Write the file as json ''' with open(f, 'w') as w: json.dump(content, w, indent=4) def process_items(item): ''' Recursive parse a JSON object, find the expr attributes and mangle them ''' if isinstance(item, (int, float)): return item if isinstance(item, bool): return item if isinstance(item, str): return item if isinstance(item, dict): for k, v in item.items(): if k == 'expr': item[k] = process_expression(v) elif k == 'legendFormat': item[k] = process_legendFormat(v) elif k == 'definition' or k == 'query' and isinstance(v, str): item[k] = process_def_query(v) elif k == 'version': item[k] = v + 1 else: item[k] = process_items(v) return item if isinstance(item, list): for i in range(len(item)): item[i] = process_items(item[i]) return item return item def process_expression(expression): ''' if the expr has pod_name, container_name, add an or with switch to pod and container ''' if re.search('pod_name', expression) or \ re.search('container_name', expression): new_term = expression new_term = re.sub('pod_name', 'pod', new_term) new_term = re.sub('(?