CMS 3D CMS Logo

helperFunctions.py
Go to the documentation of this file.
1 from __future__ import print_function
2 from __future__ import absolute_import
3 from builtins import range
4 import os
5 import re
6 import ROOT
7 import sys
8 from .TkAlExceptions import AllInOneError
9 import CondCore.Utilities.conddblib as conddblib
10 import six
11 
12 
13 def replaceByMap(target, the_map):
14  """This function replaces `.oO[key]Oo.` by `the_map[key]` in target.
15 
16  Arguments:
17  - `target`: String which contains symbolic tags of the form `.oO[key]Oo.`
18  - `the_map`: Dictionary which has to contain the `key`s in `target` as keys
19  """
20 
21  result = target
22  for key in the_map:
23  lifeSaver = 10e3
24  iteration = 0
25  while ".oO[" in result and "]Oo." in result:
26  for key in the_map:
27  try:
28  result = result.replace(".oO["+key+"]Oo.",the_map[key])
29  except TypeError: #try a dict
30  try:
31  for keykey, value in six.iteritems(the_map[key]):
32  result = result.replace(".oO[" + key + "['" + keykey + "']]Oo.", value)
33  result = result.replace(".oO[" + key + '["' + keykey + '"]]Oo.', value)
34  except AttributeError: #try a list
35  try:
36  for index, value in enumerate(the_map[key]):
37  result = result.replace(".oO[" + key + "[" + str(index) + "]]Oo.", value)
38  except TypeError:
39  raise TypeError("Something is wrong in replaceByMap! Need a string, dict, or list, but the_map(%s)=%s!"%(repr(key), repr(the_map[key])))
40  iteration += 1
41  if iteration > lifeSaver:
42  problematicLines = ""
43  for line in result.splitlines():
44  if ".oO[" in result and "]Oo." in line:
45  problematicLines += "%s\n"%line
46  msg = ("Oh Dear, there seems to be an endless loop in "
47  "replaceByMap!!\n%s\n%s"%(problematicLines, the_map))
48  raise AllInOneError(msg)
49  return result
50 
51 
52 def getCommandOutput2(command):
53  """This function executes `command` and returns it output.
54 
55  Arguments:
56  - `command`: Shell command to be invoked by this function.
57  """
58 
59  child = os.popen(command)
60  data = child.read()
61  err = child.close()
62  if err:
63  raise RuntimeError('%s failed w/ exit code %d' % (command, err))
64  return data
65 
66 
67 def castorDirExists(path):
68  """This function checks if the directory given by `path` exists.
69 
70  Arguments:
71  - `path`: Path to castor directory
72  """
73 
74  if path[-1] == "/":
75  path = path[:-1]
76  containingPath = os.path.join( *path.split("/")[:-1] )
77  dirInQuestion = path.split("/")[-1]
78  try:
79  rawLines = getCommandOutput2("rfdir /"+containingPath).splitlines()
80  except RuntimeError:
81  return False
82  for line in rawLines:
83  if line.split()[0][0] == "d":
84  if line.split()[8] == dirInQuestion:
85  return True
86  return False
87 
88 def replacelast(string, old, new, count = 1):
89  """Replace the last occurances of a string"""
90  return new.join(string.rsplit(old,count))
91 
92 fileExtensions = ["_cfg.py", ".sh", ".root"]
93 
94 def addIndex(filename, njobs, index = None):
95  if index is None:
96  return [addIndex(filename, njobs, i) for i in range(njobs)]
97  if njobs == 1:
98  return filename
99 
100  fileExtension = None
101  for extension in fileExtensions:
102  if filename.endswith(extension):
103  fileExtension = extension
104  if fileExtension is None:
105  raise AllInOneError(fileName + " does not end with any of the extensions "
106  + str(fileExtensions))
107  return replacelast(filename, fileExtension, "_" + str(index) + fileExtension)
108 
109 def parsecolor(color):
110  try: #simplest case: it's an int
111  return int(color)
112  except ValueError:
113  pass
114 
115  try: #kRed, kBlue, ...
116  color = str(getattr(ROOT, color))
117  return int(color)
118  except (AttributeError, ValueError):
119  pass
120 
121  if color.count("+") + color.count("-") == 1: #kRed+5, kGreen-2
122  if "+" in color: #don't want to deal with nonassociativity of -
123  split = color.split("+")
124  color1 = parsecolor(split[0])
125  color2 = parsecolor(split[1])
126  return color1 + color2
127 
128  if "-" in color:
129  split = color.split("-")
130  color1 = parsecolor(split[0])
131  color2 = parsecolor(split[1])
132  return color1 - color2
133 
134  raise AllInOneError("color has to be an integer, a ROOT constant (kRed, kBlue, ...), or a two-term sum or difference (kGreen-5)!")
135 
136 def parsestyle(style):
137  try: #simplest case: it's an int
138  return int(style)
139  except ValueError:
140  pass
141 
142  try: #kStar, kDot, ...
143  style = str(getattr(ROOT,style))
144  return int(style)
145  except (AttributeError, ValueError):
146  pass
147 
148  raise AllInOneError("style has to be an integer or a ROOT constant (kDashed, kStar, ...)!")
149 
151  result = [cls]
152  for subcls in cls.__subclasses__():
153  result += recursivesubclasses(subcls)
154  return result
155 
156 def cache(function):
157  cache = {}
158  def newfunction(*args, **kwargs):
159  try:
160  return cache[args, tuple(sorted(six.iteritems(kwargs)))]
161  except TypeError:
162  print(args, tuple(sorted(six.iteritems(kwargs))))
163  raise
164  except KeyError:
165  cache[args, tuple(sorted(six.iteritems(kwargs)))] = function(*args, **kwargs)
166  return newfunction(*args, **kwargs)
167  newfunction.__name__ = function.__name__
168  return newfunction
169 
170 def boolfromstring(string, name):
171  """
172  Takes a string from the configuration file
173  and makes it into a bool
174  """
175  #try as a string, not case sensitive
176  if string.lower() == "true": return True
177  if string.lower() == "false": return False
178  #try as a number
179  try:
180  return str(bool(int(string)))
181  except ValueError:
182  pass
183  #out of options
184  raise ValueError("{} has to be true or false!".format(name))
185 
186 
187 def pythonboolstring(string, name):
188  """
189  Takes a string from the configuration file
190  and makes it into a bool string for a python template
191  """
192  return str(boolfromstring(string, name))
193 
194 def cppboolstring(string, name):
195  """
196  Takes a string from the configuration file
197  and makes it into a bool string for a C++ template
198  """
199  return pythonboolstring(string, name).lower()
200 
201 def getTagsMap(db):
202  con = conddblib.connect(url = conddblib.make_url(db))
203  session = con.session()
204  TAG = session.get_dbtype(conddblib.Tag)
205  dictionary = {}
206  for i in range(0,len(session.query(TAG.object_type).order_by(TAG.name).all())):
207  q1 = session.query(TAG.object_type).order_by(TAG.name).all()[i][0]
208  q2 = session.query(TAG.name).order_by(TAG.name).all()[i][0]
209  dictionary[q1]=q2
210 
211  return dictionary
212 
213 def clean_name(s):
214  """Transforms a string into a valid variable or method name.
215 
216  Arguments:
217  - `s`: input string
218  """
219 
220  # Remove invalid characters
221  s = re.sub(r"[^0-9a-zA-Z_]", "", s)
222 
223  # Remove leading characters until we find a letter or underscore
224  s = re.sub(r"^[^a-zA-Z_]+", "", s)
225 
226  return s
helperFunctions.castorDirExists
def castorDirExists(path)
Definition: helperFunctions.py:67
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
electrons_cff.bool
bool
Definition: electrons_cff.py:393
conddblib.Tag
Definition: conddblib.py:227
helperFunctions.replacelast
def replacelast(string, old, new, count=1)
Definition: helperFunctions.py:88
helperFunctions.boolfromstring
def boolfromstring(string, name)
Definition: helperFunctions.py:170
helperFunctions.replaceByMap
def replaceByMap(target, the_map)
— Helpers —############################
Definition: helperFunctions.py:13
helperFunctions.recursivesubclasses
def recursivesubclasses(cls)
Definition: helperFunctions.py:150
python.cmstools.all
def all(container)
workaround iterator generators for ROOT classes
Definition: cmstools.py:26
helperFunctions.cache
def cache(function)
Definition: helperFunctions.py:156
helperFunctions.clean_name
def clean_name(s)
Definition: helperFunctions.py:213
str
#define str(s)
Definition: TestProcessor.cc:51
helperFunctions.parsestyle
def parsestyle(style)
Definition: helperFunctions.py:136
helperFunctions.getCommandOutput2
def getCommandOutput2(command)
Definition: helperFunctions.py:52
helperFunctions.parsecolor
def parsecolor(color)
Definition: helperFunctions.py:109
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
helperFunctions.cppboolstring
def cppboolstring(string, name)
Definition: helperFunctions.py:194
createfilelist.int
int
Definition: createfilelist.py:10
helperFunctions.getTagsMap
def getTagsMap(db)
Definition: helperFunctions.py:201
conddblib.connect
def connect(url, authPath=None, verbose=0)
Definition: conddblib.py:516
helperFunctions.pythonboolstring
def pythonboolstring(string, name)
Definition: helperFunctions.py:187
helperFunctions.addIndex
def addIndex(filename, njobs, index=None)
Definition: helperFunctions.py:94
format
HiBiasedCentrality_cfi.function
function
Definition: HiBiasedCentrality_cfi.py:4
TkAlExceptions.AllInOneError
Definition: TkAlExceptions.py:1
conddblib.make_url
def make_url(database='pro', read_only=True)
Definition: conddblib.py:470