-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangePrettyName.py
More file actions
executable file
·96 lines (87 loc) · 3.43 KB
/
changePrettyName.py
File metadata and controls
executable file
·96 lines (87 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
__all__ = [ "change" ]
import os, sys, glob
from os import PathLike
from smodels_utils.helper.various import getSqrts, getCollaboration
def change ( anaid : str, prettyName : str, dbpath = PathLike ):
""" change all prettynames for anaid, in dbpath """
c = getCollaboration ( anaid )
s = getSqrts ( anaid )
fullpath = os.path.expanduser ( f"{dbpath}/{s}TeV/{c}" )
files = glob.glob ( f"{fullpath}/{anaid}*/convert.py" )
files += glob.glob ( f"{fullpath}/{anaid}*/globalInfo.txt" )
for f in files:
changeInFile ( prettyName, f )
def changeInFile ( prettyName : str, filename : PathLike ):
""" change the pretty name in a single file """
if not os.path.exists ( filename ):
print ( f"[changePrettyName] file {filename} does not exist" )
return
isGlobalInfo = False # true if convert.py, false if globalInfo.txt
if "globalInfo.txt" in filename:
isGlobalInfo = True
f = open ( filename )
lines = f.readlines()
f.close()
hasPrettyName = False
oldPrettyName = ""
for line in lines:
if "prettyName" in line:
hasPrettyName = True
p1 = line.find ( "=" )
if isGlobalInfo:
p1 = line.find(":" )
if p1 < 1:
print ( f"[changePrettyName] cannot find separator in {line}" )
oldPrettyName = f"{line[p1+1:]}"
oldPrettyName = oldPrettyName.strip()
if oldPrettyName.startswith ( "'" ) or oldPrettyName.startswith ( '"' ):
oldPrettyName = oldPrettyName[1:-1]
break
if not hasPrettyName:
print ( f"[changePrettyName] no prettyName defined in {filename}" )
return
if oldPrettyName == prettyName:
print ( f"[changePrettyName] old pretty name same as new: '{prettyName}'" )
return
g = open ( filename, "wt" )
print ( f'[changePrettyName] changing {filename}: "{oldPrettyName}" -> "{prettyName}"' )
for line in lines:
if not "prettyName" in line:
g.write ( line )
continue
p1 = line.find ( "=" )
if isGlobalInfo:
p1 = line.find(":" )
if p1 < 1:
print ( f"[changePrettyName] cannot find separator in {filename}" )
return
newline = f"{line[:p1+1]} '{prettyName}'"
if isGlobalInfo:
newline = f"{line[:p1+1]} {prettyName}"
g.write ( f"{newline}\n" )
g.close()
def commandline():
"""
.. code-block:: bash
>>> changePrettyName.py -a CMS-SUS-16-035 -p "2 SS l"
"""
import argparse
ap = argparse.ArgumentParser(description="Systematically change the pretty name of an analysis in a text database" )
ap.add_argument('-a', '--analysis', type=str,
help='analysis id, e.g CMS-SUS-19-006', default=None )
ap.add_argument('-p', '--prettyName', type=str,
help='the new pretty name', default=None )
ap.add_argument('-d', '--dbpath', type=str,
help='path to the database [~/git/smodels-database]',
default="~/git/smodels-database" )
args = ap.parse_args()
if args.analysis == None:
print ( "[changePrettyName] need to specify an analysis" )
sys.exit()
if args.prettyName == None:
print ( "[changePrettyName] need to specify a prettyName" )
sys.exit()
change ( args.analysis, args.prettyName, args.dbpath )
if __name__ == "__main__":
commandline()