Coverage for /Users/Newville/Codes/xraylarch/larch/xrmmap/configfile.py: 17%

111 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-09 10:08 -0600

1#!/usr/bin/python 

2 

3import os 

4import sys 

5import time 

6from configparser import ConfigParser 

7from io import StringIO 

8 

9 

10conf_sects = {'general': {}, 

11 'xps':{'bools':('use_ftp',)}, 

12 'fast_positioners': {'ordered':True}, 

13 'slow_positioners': {'ordered':True}, 

14 'xrf': {}, 

15 'scan': {'ints': ('dimension',), 

16 'floats':('start1','stop1', 'step1','time1', 

17 'start2','stop2', 'step2')}} 

18 

19conf_objs = {'general': ('mapdb', 'struck', 'scaler', 'xmap', 'mono', 

20 'fileplugin', 'basedir', 'scandir', 'envfile'), 

21 'xps': ('host', 'user', 'passwd', 'group', 'positioners'), 

22 'scan': ('filename', 'dimension', 'comments', 'pos1', 'start1', 

23 'stop1', 'step1', 'time1', 'pos2', 'start2', 'stop2', 

24 'step2'), 

25 'xrf': ('use', 'type', 'prefix', 'plugin'), 

26 'fast_positioners': None, 

27 'slow_positioners': None} 

28 

29conf_files = ('Scan.ini',) 

30 

31 

32default_conf = """# FastMap configuration file (default) 

33[general] 

34basedir= 

35envfile = 

36[xps] 

37type = NewportXPS 

38mode = 

39host = 

40user = 

41passwd = 

42group = 

43positioners= 

44[scan] 

45filename = scan.001 

46comments = 

47dimension = 2 

48pos1 = 

49start1 = -1.0 

50stop1 = 1.0 

51step1 = 0.01 

52time1 = 20.0 

53pos2 = 

54start2 = -1.0 

55stop2 = 1.0 

56step2 = 0.01 

57[fast_positioners] 

581 = X | X 

592 = Y | Y 

60[slow_positioners] 

611 = X | X 

622 = Y | Y 

63""" 

64 

65class FastMapConfig(object): 

66 def __init__(self, filename=None, conftext=None): 

67 self.config = {} 

68 self.cp = ConfigParser() 

69 conf_found = False 

70 if filename is not None: 

71 self.Read(fname=filename) 

72 else: 

73 for fname in conf_files: 

74 if os.path.exists(fname) and os.path.isfile(fname): 

75 self.Read(fname) 

76 conf_found = True 

77 break 

78 if not conf_found: 

79 self.cp.readfp(StringIO(default_conf)) 

80 self._process_data() 

81 

82 def Read(self,fname=None): 

83 if fname is not None: 

84 ret = self.cp.read(fname) 

85 if len(ret)==0: 

86 time.sleep(0.5) 

87 ret = self.cp.read(fname) 

88 self.filename = fname 

89 self._process_data() 

90 

91 def _process_data(self): 

92 for sect,opts in conf_sects.items(): 

93 # if sect == 'scan': print( opts) 

94 if not self.cp.has_section(sect): 

95 continue 

96 bools = opts.get('bools',[]) 

97 floats= opts.get('floats',[]) 

98 ints = opts.get('ints',[]) 

99 thissect = {} 

100 is_ordered = False 

101 if 'ordered' in opts: 

102 thissect = {} 

103 is_ordered = True 

104 for opt in self.cp.options(sect): 

105 get = self.cp.get 

106 if opt in bools: get = self.cp.getboolean 

107 elif opt in floats: get = self.cp.getfloat 

108 elif opt in ints: get = self.cp.getint 

109 val = get(sect,opt) 

110 if is_ordered and '|' in val: 

111 opt,val = val.split('|',1) 

112 opt = opt.strip() 

113 val = val.strip() 

114 thissect[opt] = val 

115 self.config[sect] = thissect 

116 

117 def Save(self,fname): 

118 o = [] 

119 cnf = self.config 

120 self.filename = fname 

121 o.append('# FastMap configuration file (saved: %s)' % (time.ctime())) 

122 for sect,optlist in conf_objs.items(): 

123 o.append('#------------------#\n[%s]'%sect) 

124 if optlist is not None: 

125 for opt in optlist: 

126 try: 

127 val = cnf[sect].get(opt,'<unknown>') 

128 if not isinstance(val, str): 

129 val = str(val) 

130 o.append("%s = %s" % (opt,val)) 

131 except: 

132 pass 

133 else: 

134 for i,x in enumerate(cnf[sect]): 

135 o.append("%i = %s | %s" % (i+1,x, 

136 cnf[sect].get(x,'<unknown>'))) 

137 o.append('#------------------#\n') 

138 f = open(fname,'w') 

139 f.write('\n'.join(o)) 

140 f.close() 

141 

142 def SaveScanParams(self,fname): 

143 "save only scan parameters to a file" 

144 o = [] 

145 o.append('# FastMap Scan Parameter file (saved: %s)' % (time.ctime())) 

146 sect = 'scan' 

147 optlist = conf_objs[sect] 

148 o.append('#------------------#\n[%s]'%sect) 

149 scan =self.config['scan'] 

150 for opt in optlist: 

151 val = scan.get(opt,None) 

152 if val is not None: 

153 if not isinstance(val, str): 

154 val = str(val) 

155 o.append("%s = %s" % (opt,val)) 

156 o.append('#------------------#\n') 

157 f = open(fname,'w') 

158 f.write('\n'.join(o)) 

159 f.close() 

160 

161 def sections(self): 

162 return self.config.keys() 

163 

164 def section(self, section): 

165 return self.config[section] 

166 

167 def get(self, section, value=None): 

168 if value is None: 

169 return self.config[section] 

170 else: 

171 return self.config[section][value] 

172 

173 

174if __name__ == "__main__": 

175 a = FastMapConfig() 

176 a.Read('default.ini') 

177 for k,v in a.config.items(): 

178 print( k,v, type(v)) 

179 a.Read('xmap.001.ini') 

180 print( a.config['scan']) 

181 a.SaveScanParams('xmap.002.ini')