|
| 1 | +#!/usr/bin/env python |
| 2 | +#------------------------------------------------------------------------------------------------- |
| 3 | +# takes fully expanded .FCL produced in --debug-config mode, generates an .org version of it |
| 4 | +# |
| 5 | +# call format: |
| 6 | +# fcl_to_org.py --file=aaa.fcl [--verbose=level] |
| 7 | +# |
| 8 | +# output : aaa.org - org representation of the expanded fcl file |
| 9 | +# |
| 10 | +# verbose mode used only for internal debugging |
| 11 | +#------------------------------------------------------------------------------------------------- |
| 12 | +import argparse, sys, os, time |
| 13 | + |
| 14 | +class Table: |
| 15 | + def __init__(self,name): |
| 16 | + self.fName = name |
| 17 | + self.fDict = {} |
| 18 | + |
| 19 | + def name(self): |
| 20 | + return self.fName; |
| 21 | + |
| 22 | +class Sequence: |
| 23 | + def __init__(self,name): |
| 24 | + self.fName = name |
| 25 | + self.fList = [] |
| 26 | + |
| 27 | + def name(self): |
| 28 | + return self.fName |
| 29 | + |
| 30 | +#------------------------------------------------------------------------------ |
| 31 | +class Tool: |
| 32 | + |
| 33 | + def __init__(self): |
| 34 | + self.fFilename = None |
| 35 | + self.fVerbose = 0 |
| 36 | + self.fTable = Table('main') |
| 37 | +# --------------------------------------------------------------------- |
| 38 | + def Print(self,Name,level,Message): |
| 39 | + if (level > self.fVerbose): return 0; |
| 40 | + now = time.strftime('%Y/%m/%d %H:%M:%S',time.localtime(time.time())) |
| 41 | + message = now+' [ GridTool::'+Name+' ] '+Message |
| 42 | + print(message) |
| 43 | + |
| 44 | +#------------------------------------------------------------------------------ |
| 45 | + def ParseParameters(self): |
| 46 | + name = 'ParseParameters' |
| 47 | + |
| 48 | + self.Print(name,2,'Starting') |
| 49 | + self.Print(name,2, '%s' % sys.argv) |
| 50 | + |
| 51 | + desc = 'fcl to org format converter: \n creates an ORG representation of the expanded FCL file.' + \ |
| 52 | + ' The output .ORG file has the same name as the input .FCL file, just a different extension.' |
| 53 | + |
| 54 | + parser = argparse.ArgumentParser(description=desc) |
| 55 | + |
| 56 | + if (len(sys.argv) == 1): |
| 57 | + parser.print_help() |
| 58 | + sys.exit() |
| 59 | + |
| 60 | + parser.add_argument('--file' , type=str, help='expanded .fcl file name', default = None) |
| 61 | + parser.add_argument('--verbose' , type=int, help='verbose' , default = 0 ) |
| 62 | + |
| 63 | + args = parser.parse_args() |
| 64 | + |
| 65 | + self.fFilename = args.file |
| 66 | + self.fVerbose = args.verbose |
| 67 | + |
| 68 | + return |
| 69 | + |
| 70 | +#------------------------------------------------------------------------------ |
| 71 | + def parse_sequence(self,lines,ifirst,seq): |
| 72 | + if (self.fVerbose > 0): print('# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> parse_sequence:',seq.name()) |
| 73 | + |
| 74 | + iend = -1; |
| 75 | + nlines=len(lines) |
| 76 | + |
| 77 | + i = ifirst |
| 78 | + |
| 79 | + while (i < nlines): |
| 80 | + iend = i |
| 81 | + line = lines[i].lstrip().rstrip() |
| 82 | + if (self.fVerbose > 0): print ('# parse_sequence line # %5i :'%i, line) |
| 83 | + if (line[0] == '#'): |
| 84 | + i = iend+1 |
| 85 | + continue; |
| 86 | + # assume that ']' or '],' is a separate line |
| 87 | + if (line[0] == ']'): |
| 88 | + # done |
| 89 | + if (self.fVerbose > 0): print('# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> end parse_sequence, i=',i); |
| 90 | + return i |
| 91 | + elif (line == '['): |
| 92 | + new_seq = Sequence('') |
| 93 | + seq.fList.append(new_seq); |
| 94 | + iend = self.parse_sequence(lines,i+1,new_seq) |
| 95 | + if (self.fVerbose > 0): print('# >>>>>>>>>>>>>>>>>>>>>>>>> back from parse_sequence, iend=',iend); |
| 96 | + |
| 97 | + elif (line == '{'): |
| 98 | + # unnamed table |
| 99 | + new_table = Table('') |
| 100 | + seq.fList.append(new_table); |
| 101 | + iend = self.parse_table(lines,i+1,new_table) |
| 102 | + if (self.fVerbose > 0): print('# >>>>>>>>>>>>>>>>>>>>>>>>> back from parse_table, iend=',iend); |
| 103 | + |
| 104 | + else: |
| 105 | + if (self.fVerbose > 0): print('# >>>>>>>>>>>> adding: ',line); |
| 106 | + seq.fList.append(line) |
| 107 | + |
| 108 | + i = iend+1 |
| 109 | + |
| 110 | + return iend |
| 111 | + |
| 112 | +#------------------------------------------------------------------------------ |
| 113 | + def parse_table(self,lines,ifirst,table): |
| 114 | + if (self.fVerbose > 0): print('# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> parse_table:',table.name()) |
| 115 | + |
| 116 | + iend = ifirst; |
| 117 | + nlines = len(lines) |
| 118 | + seq = None |
| 119 | + |
| 120 | + i = ifirst |
| 121 | + |
| 122 | + while (i < nlines): |
| 123 | + iend = i |
| 124 | + |
| 125 | + line = lines[i].lstrip().rstrip() |
| 126 | + if (self.fVerbose > 0): print ('# line # %5i :'%i, line) |
| 127 | + if ( len(line) == 0 or line[0] == '#'): |
| 128 | + i = i+1 |
| 129 | + continue; |
| 130 | + s = line.split(); |
| 131 | + nw = len(s) |
| 132 | + if (nw > 0) and (s[nw-1] == '{'): |
| 133 | + #------------------------------------------------------------------------------ |
| 134 | + # new table |
| 135 | + name = s[0].split(':')[0] |
| 136 | + if (self.fVerbose > 0): print( '# new table name:',name) |
| 137 | + new_table = Table(name) |
| 138 | + table.fDict[name] = new_table |
| 139 | + iend = self.parse_table(lines,i+1,new_table) |
| 140 | + # break |
| 141 | + elif (line[0] == '}'): |
| 142 | + #------------------------------------------------------------------------------ |
| 143 | + # end table |
| 144 | + iend = i; |
| 145 | + break |
| 146 | + else: |
| 147 | + # just add something to the table |
| 148 | + words = line.split(':'); |
| 149 | + nww = len(words); |
| 150 | + if (nww > 1) : |
| 151 | + nam = words[0]; |
| 152 | + x = words[1].lstrip().rstrip(); |
| 153 | + if (x == '['): |
| 154 | + #---------- |
| 155 | + # sequence |
| 156 | + seq = Sequence(nam) |
| 157 | + table.fDict[nam] = seq |
| 158 | + iend = self.parse_sequence(lines,i+1,seq) |
| 159 | + else: |
| 160 | + #-------------- |
| 161 | + # a simple line |
| 162 | + table.fDict[nam] = words[1] |
| 163 | + else: |
| 164 | + print ('>> ERROR: ',words) |
| 165 | + i = iend+1; |
| 166 | + |
| 167 | + if (self.fVerbose > 0): print('# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> end table:',table.name(),'line:',iend) |
| 168 | + return iend |
| 169 | + |
| 170 | + |
| 171 | +#------------------------------------------------------------------------------ |
| 172 | + def print_list_to_org(self,sequence,prefix,fout): |
| 173 | + |
| 174 | + for e in sequence.fList: |
| 175 | + if isinstance(e,Table) : |
| 176 | + fout.write(prefix+' table : %-40s\n'%e.name()) |
| 177 | + self.print_table_to_org(e,prefix+'*',fout) |
| 178 | + elif isinstance(e,Sequence): |
| 179 | + fout.write(prefix+' sequence: %-40s\n'%e.name()) |
| 180 | + self.print_list_to_org(e,prefix+'*',fout) |
| 181 | + else: |
| 182 | + fout.write (' %s\n'%e) |
| 183 | + |
| 184 | +#------------------------------------------------------------------------------ |
| 185 | + def print_table_to_org(self,table,prefix,fout): |
| 186 | + |
| 187 | + for k in table.fDict.keys(): |
| 188 | + element = table.fDict[k] |
| 189 | + # print(prefix+'* ',k, 'type:',type(element)) |
| 190 | + # print (element) |
| 191 | + |
| 192 | + if isinstance(element,Table) : |
| 193 | + fout.write(prefix+' table : %-40s\n'%k) |
| 194 | + self.print_table_to_org(element,prefix+'*',fout) |
| 195 | + elif isinstance(element,Sequence): |
| 196 | + fout.write(prefix+' sequence: %-40s\n'%k) |
| 197 | + self.print_list_to_org(element,prefix+'*',fout) |
| 198 | + else: |
| 199 | + # print ('>> element: ',element,type(element)) |
| 200 | + fout.write('%s %s : %s\n'%(prefix,k,element)); |
| 201 | + |
| 202 | +#------------------------------------------------------------------------------ |
| 203 | +# check log files. asume they are copied into the output area |
| 204 | +#------------------------------------------------------------------------------ |
| 205 | + def parse_fcl(self,fn): |
| 206 | + name = 'parse_fcl' |
| 207 | + |
| 208 | + lines = open(fn).readlines(); |
| 209 | + |
| 210 | + iend = -1; |
| 211 | + nlines = len(lines); |
| 212 | + ifirst = 0; |
| 213 | + |
| 214 | + if (self.fVerbose > 0): print('# fn = %s nlines: %7i'%(fn,nlines)); |
| 215 | + |
| 216 | + fn_out = fn.replace('.fcl','.org') |
| 217 | + |
| 218 | + fout = open(fn_out,'w') |
| 219 | + |
| 220 | + while (ifirst < nlines): |
| 221 | + iend = self.parse_table(lines,ifirst,self.fTable) |
| 222 | + ifirst = iend+1; |
| 223 | + |
| 224 | + # at this point the table is parsed, we can print it in a form of .org file |
| 225 | + |
| 226 | + fout.write('# -*- mode:org -*-\n') |
| 227 | + fout.write('# printing output in org format:\n') |
| 228 | + |
| 229 | + self.print_table_to_org(self.fTable,'*',fout); |
| 230 | + |
| 231 | + fout.close() |
| 232 | + |
| 233 | +#------------------------------------------------------------------------------ |
| 234 | +# main program, just make a GridSubmit instance and call its methods |
| 235 | +#------------------------------------------------------------------------------ |
| 236 | +if (__name__ == '__main__'): |
| 237 | + |
| 238 | + x = Tool() |
| 239 | + x.ParseParameters() |
| 240 | + |
| 241 | + x.parse_fcl(x.fFilename) |
| 242 | + |
| 243 | + sys.exit(0); |
0 commit comments