-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmantidpythonfromconfig
executable file
·116 lines (97 loc) · 3.48 KB
/
mantidpythonfromconfig
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import os
__version__="0.0.1"
def getAlgName(information):
return information.split('=')[0].split('\\')[1]
def getAlgNames(lines):
alg_names=set()
for line in lines:
if not line.startswith("Algorithms\\"):
continue
name = getAlgName(line)
if name == "LastDirectory":
continue
alg_names.add(name)
return list(alg_names)
def algValueToStr(value):
# if it can be turned into a float, return without quotes
try:
float(value)
return value
except ValueError:
pass
# don't quote things that are already quoted
if value.startswith('"') or value.startswith("'"):
return value
# quote everything else
return '"%s"' % value
class Algorithm:
def __init__(self, name, lines):
self.name=name
self.parameters={}
prefix="Algorithms\\%s\\" % name
for line in lines:
if line.startswith(prefix):
prop_name=line.split('=')[0]
value=line.replace(prop_name+'=', '')
prop_name=prop_name.replace(prefix,'')
self.parameters[prop_name]=value
def __str__(self):
result=[]
for key in self.parameters.keys():
result.append("%s=%s" % (key, algValueToStr(self.parameters[key])))
return "%s(%s)" % (self.name, ','.join(result))
class Config:
def __init__(self, filename):
handle = open(filename, 'r')
sections = {}
for line in handle.readlines():
line = line.strip()
if len(line) <= 0:
continue
if line[0] == '[' and line[-1] == ']':
heading = line[1:-1]
sections[heading]=[]
else:
sections[heading].append(line)
handle.close()
self.__init_Mantid(sections['Mantid'])
def __init_Mantid(self, lines):
self.algorithms=[]
alg_names=getAlgNames(lines)
for name in alg_names:
alg=Algorithm(name, lines)
self.algorithms.append(alg)
def printAlg(self,name):
for alg in self.algorithms:
if name in alg.name:
print(alg)
break
def printAllAlg(self):
for alg in self.algorithms:
print(alg)
def determineConfigFile(filename, userdir):
if filename is not None:
return os.path.expanduser(filename)
filename=os.path.join(userdir, '.config/Mantid/MantidPlot.conf')
return os.path.expanduser(filename)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='Explore ~/.config/Mantid/MantidPlot.conf settings')
parser.add_argument('-f','--filename', metavar='CONFIG_FILE',
help='configuration file to get information from')
parser.add_argument('-u','--userdir', metavar='USER_HOME',
help='specify a user home directory',
default="~/")
parser.add_argument('-a','--alg', metavar='ALG_NAME',
help='print information for a specific algorithm')
args=parser.parse_args()
filename = determineConfigFile(args.filename, args.userdir)
print("# Reading information from '%s'" % filename)
config = Config(filename)
if args.alg is not None:
config.printAlg(args.alg)
else:
config.printAllAlg()