File indexing completed on 2023-03-17 10:40:24
0001 from __future__ import print_function
0002 from __future__ import absolute_import
0003 from builtins import range
0004 import os
0005 import re
0006 import ROOT
0007 import sys
0008 from .TkAlExceptions import AllInOneError
0009 import CondCore.Utilities.conddblib as conddblib
0010
0011
0012 def replaceByMap(target, the_map):
0013 """This function replaces `.oO[key]Oo.` by `the_map[key]` in target.
0014
0015 Arguments:
0016 - `target`: String which contains symbolic tags of the form `.oO[key]Oo.`
0017 - `the_map`: Dictionary which has to contain the `key`s in `target` as keys
0018 """
0019
0020 result = target
0021 for key in the_map:
0022 lifeSaver = 10e3
0023 iteration = 0
0024 while ".oO[" in result and "]Oo." in result:
0025 for key in the_map:
0026 try:
0027 result = result.replace(".oO["+key+"]Oo.",the_map[key])
0028 except TypeError:
0029 try:
0030 for keykey, value in the_map[key].items():
0031 result = result.replace(".oO[" + key + "['" + keykey + "']]Oo.", value)
0032 result = result.replace(".oO[" + key + '["' + keykey + '"]]Oo.', value)
0033 except AttributeError:
0034 try:
0035 for index, value in enumerate(the_map[key]):
0036 result = result.replace(".oO[" + key + "[" + str(index) + "]]Oo.", value)
0037 except TypeError:
0038 raise TypeError("Something is wrong in replaceByMap! Need a string, dict, or list, but the_map(%s)=%s!"%(repr(key), repr(the_map[key])))
0039 iteration += 1
0040 if iteration > lifeSaver:
0041 problematicLines = ""
0042 for line in result.splitlines():
0043 if ".oO[" in result and "]Oo." in line:
0044 problematicLines += "%s\n"%line
0045 msg = ("Oh Dear, there seems to be an endless loop in "
0046 "replaceByMap!!\n%s\n%s"%(problematicLines, the_map))
0047 raise AllInOneError(msg)
0048 return result
0049
0050
0051 def getCommandOutput2(command):
0052 """This function executes `command` and returns it output.
0053
0054 Arguments:
0055 - `command`: Shell command to be invoked by this function.
0056 """
0057
0058 child = os.popen(command)
0059 data = child.read()
0060 err = child.close()
0061 if err:
0062 raise RuntimeError('%s failed w/ exit code %d' % (command, err))
0063 return data
0064
0065
0066 def castorDirExists(path):
0067 """This function checks if the directory given by `path` exists.
0068
0069 Arguments:
0070 - `path`: Path to castor directory
0071 """
0072
0073 if path[-1] == "/":
0074 path = path[:-1]
0075 containingPath = os.path.join( *path.split("/")[:-1] )
0076 dirInQuestion = path.split("/")[-1]
0077 try:
0078 rawLines = getCommandOutput2("rfdir /"+containingPath).splitlines()
0079 except RuntimeError:
0080 return False
0081 for line in rawLines:
0082 if line.split()[0][0] == "d":
0083 if line.split()[8] == dirInQuestion:
0084 return True
0085 return False
0086
0087 def replacelast(string, old, new, count = 1):
0088 """Replace the last occurances of a string"""
0089 return new.join(string.rsplit(old,count))
0090
0091 fileExtensions = ["_cfg.py", ".sh", ".root"]
0092
0093 def addIndex(filename, njobs, index = None):
0094 if index is None:
0095 return [addIndex(filename, njobs, i) for i in range(njobs)]
0096 if njobs == 1:
0097 return filename
0098
0099 fileExtension = None
0100 for extension in fileExtensions:
0101 if filename.endswith(extension):
0102 fileExtension = extension
0103 if fileExtension is None:
0104 raise AllInOneError(fileName + " does not end with any of the extensions "
0105 + str(fileExtensions))
0106 return replacelast(filename, fileExtension, "_" + str(index) + fileExtension)
0107
0108 def parsecolor(color):
0109 try:
0110 return int(color)
0111 except ValueError:
0112 pass
0113
0114 try:
0115 color = str(getattr(ROOT, color))
0116 return int(color)
0117 except (AttributeError, ValueError):
0118 pass
0119
0120 if color.count("+") + color.count("-") == 1:
0121 if "+" in color:
0122 split = color.split("+")
0123 color1 = parsecolor(split[0])
0124 color2 = parsecolor(split[1])
0125 return color1 + color2
0126
0127 if "-" in color:
0128 split = color.split("-")
0129 color1 = parsecolor(split[0])
0130 color2 = parsecolor(split[1])
0131 return color1 - color2
0132
0133 raise AllInOneError("color has to be an integer, a ROOT constant (kRed, kBlue, ...), or a two-term sum or difference (kGreen-5)!")
0134
0135 def parsestyle(style):
0136 try:
0137 return int(style)
0138 except ValueError:
0139 pass
0140
0141 try:
0142 style = str(getattr(ROOT,style))
0143 return int(style)
0144 except (AttributeError, ValueError):
0145 pass
0146
0147 raise AllInOneError("style has to be an integer or a ROOT constant (kDashed, kStar, ...)!")
0148
0149 def recursivesubclasses(cls):
0150 result = [cls]
0151 for subcls in cls.__subclasses__():
0152 result += recursivesubclasses(subcls)
0153 return result
0154
0155 def cache(function):
0156 cache = {}
0157 def newfunction(*args, **kwargs):
0158 try:
0159 return cache[args, tuple(sorted(kwargs.items()))]
0160 except TypeError:
0161 print(args, tuple(sorted(kwargs.items())))
0162 raise
0163 except KeyError:
0164 cache[args, tuple(sorted(kwargs.items()))] = function(*args, **kwargs)
0165 return newfunction(*args, **kwargs)
0166 newfunction.__name__ = function.__name__
0167 return newfunction
0168
0169 def boolfromstring(string, name):
0170 """
0171 Takes a string from the configuration file
0172 and makes it into a bool
0173 """
0174
0175 if string.lower() == "true": return True
0176 if string.lower() == "false": return False
0177
0178 try:
0179 return str(bool(int(string)))
0180 except ValueError:
0181 pass
0182
0183 raise ValueError("{} has to be true or false!".format(name))
0184
0185
0186 def pythonboolstring(string, name):
0187 """
0188 Takes a string from the configuration file
0189 and makes it into a bool string for a python template
0190 """
0191 return str(boolfromstring(string, name))
0192
0193 def cppboolstring(string, name):
0194 """
0195 Takes a string from the configuration file
0196 and makes it into a bool string for a C++ template
0197 """
0198 return pythonboolstring(string, name).lower()
0199
0200 def getTagsMap(db):
0201 con = conddblib.connect(url = conddblib.make_url(db))
0202 session = con.session()
0203 TAG = session.get_dbtype(conddblib.Tag)
0204 dictionary = {}
0205 for i in range(0,len(session.query(TAG.object_type).order_by(TAG.name).all())):
0206 q1 = session.query(TAG.object_type).order_by(TAG.name).all()[i][0]
0207 q2 = session.query(TAG.name).order_by(TAG.name).all()[i][0]
0208 dictionary[q1]=q2
0209
0210 return dictionary
0211
0212 def clean_name(s):
0213 """Transforms a string into a valid variable or method name.
0214
0215 Arguments:
0216 - `s`: input string
0217 """
0218
0219
0220 s = re.sub(r"[^0-9a-zA-Z_]", "", s)
0221
0222
0223 s = re.sub(r"^[^a-zA-Z_]+", "", s)
0224
0225 return s