CMS 3D CMS Logo

findAndChange.py
Go to the documentation of this file.
1 import os
2 from FWCore.ParameterSet.pfnInPath import pfnInPath
3 import ROOT
4 
5 
6 def digest_path(path):
7 
8  """ Ensure that everything is done for path to exist
9  Arguments:
10  - path: String that can be both directory and file
11  Return:
12  - general: environmental variables are expanded
13  - directory: is checked to exist
14  - file: is checked to exist with backup directory being searched in cms-data
15  """
16  # sanity check for string argument
17  if not isinstance(path, str):
18  return path
19 
20  path_expanded = os.path.expandvars(path)
21 
22  # split path in folders
23  protocol = ""
24  if "://" in path_expanded:
25  protocol = path_expanded.split("://")[0]+"://"
26  path_d = path_expanded.split("://")[1]
27  elif ":" in path_expanded:
28  protocol = path_expanded.split(":")[0]+':'
29  path_d = ":".join(path_expanded.split(":")[1:])
30  # Similar to just `split(':')[1]`, but handles the case in which the rest of the path contains one or more ':'
31  else:
32  path_d = path_expanded
33 
34  path_s = path_d.split(os.sep)
35 
36  placeholderIdx = []
37  for ipart,part in enumerate(path_s):
38  # Look for {} placeholder to be replaced internally
39  if "{}" in part:
40  placeholderIdx.append(ipart)
41 
42  # re-join folders into full path
43  # only check path up to first placeholder occurence
44  if len(placeholderIdx) > 0:
45  path_to_check = os.path.join(*path_s[:placeholderIdx[0]])
46  # re add front / if needed
47  if path_d.startswith(os.sep):
48  path_to_check = os.sep + path_to_check
49  else:
50  path_to_check = path_d
51 
52  # check for path to exist
53  if not os.path.exists(path_to_check) and "." in os.path.splitext(path_to_check)[-1]:
54  # in case of directory pointing to file try backup
55  _file = pfnInPath(path_to_check)
56  if "file:" in _file:
57  return _file.split(":")[-1]
58 
59  # re add protocol declaration
60  if protocol != "": path_d = protocol + path_d
61 
62  # if all is OK return path to directory or file
63  return path_d
64 
65 
66 def get_root_color(value):
67 
68  """
69  Returns an integer correspondig to the ROOT color
70  """
71  if(isinstance(value, str)):
72  if(value.isdigit()):
73  return int(value)
74  elif('-' in value):
75  pre, op, post = value.partition('-')
76  return get_root_color(pre.strip()) - get_root_color(post.strip())
77  elif('+' in value):
78  pre, op, post = value.partition('+')
79  return get_root_color(pre.strip()) + get_root_color(post.strip())
80  else:
81  return getattr(ROOT.EColor, value)
82  else:
83  return int(value)
84 
85 
86 def get_all_keys(var):
87 
88  """
89  Generate all keys for nested dictionary
90  - reserved keywords are not picked up
91  """
92  reserved_keys = ["customrighttitle","title","Rlabel"]
93  if hasattr(var,'items'):
94  for k, v in var.items():
95  if k in reserved_keys: continue
96  if isinstance(v, dict):
97  for result in get_all_keys(v):
98  yield result
99  elif isinstance(v, list):
100  for d in v:
101  for result in get_all_keys(d):
102  yield result
103  else:
104  yield k
105 
106 
107 def find_and_change(keys, var, alt=digest_path):
108 
109  """Perform effective search for keys in nested dictionary
110  - if key is found, corresponding value is "digested"
111  - generator is returned for printout purpose only
112  - original var is overwritten
113  """
114  if hasattr(var,'items'):
115  if len(keys) == 0:
116  for key in get_all_keys(var):
117  keys.append(key)
118  for key in keys:
119  for k, v in var.items():
120  if k == key:
121  if "color" in key:
122  if isinstance(v,list):
123  var[k] = [get_root_color(_v) for _v in v]
124  else:
125  var[k] = get_root_color(v)
126  else:
127  if isinstance(v,list):
128  var[k] = [alt(_v) for _v in v]
129  else:
130  var[k] = alt(v)
131  yield alt(v)
132  if isinstance(v, dict):
133  for result in find_and_change([key], v):
134  yield result
135  elif isinstance(v, list):
136  for d in v:
137  for result in find_and_change([key], d):
138  yield result
def find_and_change(keys, var, alt=digest_path)
def digest_path(path)
Definition: findAndChange.py:6
def get_root_color(value)
static std::string join(char **cmd)
Definition: RemoteFile.cc:19
def get_all_keys(var)
if(threadIdxLocalY==0 &&threadIdxLocalX==0)