-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse_idd.py
executable file
·336 lines (279 loc) · 7.59 KB
/
parse_idd.py
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/usr/bin/env python
## EPlusInterface (EPI) - An interface for EnergyPlus
## Copyright (C) 2004 Santosh Philip
# This file is part of EPlusInterface.
# =======================================================================
# Distributed under the MIT License.
# (See accompanying file LICENSE or copy at
# http://opensource.org/licenses/MIT)
# =======================================================================
# VERSION: 0.2
## Last updated 21 Apr 2004
import sys
import getopt
import os
import string
import user
sys.path.append(user.home+'/Library/PythonFiles')
import mylib1,mylib2,mylib3
import eplus2,time
import cPickle
def nocomment(st,com):
"""
just like the comment in python.
removes any text after the phrase 'com'
"""
ls=st.splitlines()
for i in range(len(ls)):
el=ls[i]
pt=el.find(com)
if pt!=-1:
ls[i]=el[:pt]
return '\n'.join(ls)
def get_nocom_vars(st):
"""
input 'st' which is the Energy+.idd file as a string
returns (st1,st2,lss)
st1= with all the ! comments striped
st2= strips all comments - both the '!' and '\\'
lss= nested list of all the variables in Energy+.idd file
"""
nocom=nocomment(st,'!')# remove '!' comments
st1=nocom
nocom1=nocomment(st1,'\\')# remove '\' comments
st1=nocom
st2=nocom1
ls=string.split(st2,';')
lss=[]
# break the .idd file into a nested list
#=======================================
for el in ls:
item=string.split(el,',')
lss.append(item)
for i in range(0,len(lss)):
for j in range(0,len(lss[i])):
lss[i][j]=lss[i][j].strip()
if len(lss)>1:lss.pop(-1)
#=======================================
#st1 has the '\' comments --- looks like I don't use this
#lss is the .idd file as a nested list
return (st1,st2,lss)
def removeblanklines(st):
"""
removeblanklines(st)
returns the string after
remove blank lines in 'st'
"""
linesep=mylib3.getlinesep(st)
ls=st.split(linesep)
lss=[]
for el in ls:
ell=el.strip()
if ell!='':
lss.append(el)
st1=linesep.join(lss)
return st1
def extractidddata(fname,debug=False):
"""
extracts all the needed information out of the idd file
if debug is True, it generates a series of text files.
Each text file is incrementally different. You can do a diff
see what the change is
"""
st=mylib2.readfile(fname)
(nocom,nocom1,blocklst)=get_nocom_vars(st)
st=nocom
st1=removeblanklines(st)
if debug:
mylib1.writeStr2File('nocom2.txt',st1)
#find the groups and the start object of the group
#find all the group strings
groupls=[]
ls=st1.splitlines()
for el in ls:
lss=el.split()
if lss[0].upper()=='\\group'.upper():
groupls.append(el)
#find the var just after each item in groupls
groupstart=[]
for i in range(len(groupls)):
ii=ls.index(groupls[i])
groupstart.append([ls[ii],ls[ii+1]])
#remove the group commentline
for el in groupls:
ls.remove(el)
if debug:
st1='\n'.join(ls)
mylib1.writeStr2File('nocom3.txt',st1)
#strip each line
for i in range(len(ls)):
ls[i]=ls[i].strip()
if debug:
st1='\n'.join(ls)
mylib1.writeStr2File('nocom4.txt',st1)
#ensure that each line is a comment or variable
#find lines that don't start with a comment
#if this line has a comment in it
# then move the comment to a new line below
lss=[]
for i in range(len(ls)):
#find lines that don't start with a comment
if ls[i][0]!='\\':
#if this line has a comment in it
pt=ls[i].find('\\')
if pt!=-1:
#then move the comment to a new line below
lss.append(ls[i][:pt].strip())
lss.append(ls[i][pt:].strip())
else:
lss.append(ls[i])
else:
lss.append(ls[i])
ls=lss[:]
if debug:
st1='\n'.join(ls)
mylib1.writeStr2File('nocom5.txt',st1)
#need to make sure that each line has only one variable - as in WindowGlassSpectralData,
lss=[]
for el in ls:
# if the line is not a comment
if el[0]!='\\':
#test for more than one var
ll=el.split(',')
if ll[-1]=='':
tmp=ll.pop()
for elm in ll:
if elm[-1]==';':
lss.append(elm.strip())
else:
lss.append((elm+',').strip())
else:
lss.append(el)
ls_debug=ls[:] # needed for the next debug - 'nocom7.txt'
ls=lss[:]
if debug:
st1='\n'.join(ls)
mylib1.writeStr2File('nocom6.txt',st1)
if debug:
#need to make sure that each line has only one variable - as in WindowGlassSpectralData,
#this is same as above.
# but the variables are put in without the ';' and ','
#so we can do a diff between 'nocom7.txt' and 'nocom8.txt'. Should be identical
lss_debug=[]
for el in ls_debug:
# if the line is not a comment
if el[0]!='\\':
#test for more than one var
ll=el.split(',')
if ll[-1]=='':
tmp=ll.pop()
for elm in ll:
if elm[-1]==';':
lss_debug.append(elm[:-1].strip())
else:
lss_debug.append((elm).strip())
else:
lss_debug.append(el)
ls_debug=lss_debug[:]
st1='\n'.join(ls_debug)
mylib1.writeStr2File('nocom7.txt',st1)
#replace each var with '=====var======'
#join into a string,
#split using '=====var====='
for i in range(len(lss)):
#if the line is not a comment
if lss[i][0]!='\\':
lss[i]='=====var====='
st2='\n'.join(lss)
lss=st2.split('=====var=====\n')
lss.pop(0) # the above split generates an extra item at start
if debug:
fname='nocom8.txt'
f=open(fname,'wb')
k=0
for i in range(len(blocklst)):
for j in range(len(blocklst[i])):
f.write(blocklst[i][j]+'\n')
f.write(lss[k])
k=k+1
f.close()
#map the structure of the comments -(this is 'lss' now) to
#the structure of blocklst - blocklst is a nested list
#make lss a similar nested list
k=0
lst=[]
for i in range(len(blocklst)):
lst.append([])
for j in range(len(blocklst[i])):
lst[i].append(lss[k])
k=k+1
if debug:
fname='nocom9.txt'
f=open(fname,'wb')
k=0
for i in range(len(blocklst)):
for j in range(len(blocklst[i])):
f.write(blocklst[i][j]+'\n')
f.write(lst[i][j])
k=k+1
f.close()
#break up multiple line comment so that it is a list
for i in range(len(lst)):
for j in range(len(lst[i])):
lst[i][j]=lst[i][j].splitlines()
# remove the '\'
for k in range(len(lst[i][j])):
lst[i][j][k]=lst[i][j][k][1:]
commlst=lst
#copied with minor modifications from readidd2_2.py -- which has been erased ha !
c=lst
lss=[]
for i in range(0,len(c)):
ls=[]
for j in range(0,len(c[i])):
it=c[i][j]
dt={}
for el in it:
if len(el.split())==0:
break
dt[el.split()[0]]=[]
for el in it:
if len(el.split())==0:
break
dt[el.split()[0]].append(string.join(el.split()[1:]))
ls.append(dt)
lss.append(ls)
commdct=lss
return blocklst,commlst,commdct
def getobjectref(blocklst,commdct):
"""
makes a dictionary of object-lists
each item in the dictionary points to a list of tuples
the tuple is (objectname, fieldindex)
"""
objlst_dct={}
for eli in commdct:
for elj in eli:
if elj.has_key('object-list'):
objlist=elj['object-list'][0]
objlst_dct[objlist]=[]
for objlist in objlst_dct.keys():
for i in range(len(commdct)):
for j in range(len(commdct[i])):
if commdct[i][j].has_key('reference'):
for ref in commdct[i][j]['reference']:
if ref==objlist:
objlst_dct[objlist].append((blocklst[i][0],j))
return objlst_dct
# blocklst,commlst,commdct=extractidddata('Energy+.idd_old')
# dct=getobjectref(blocklst,commdct)
#
# for el in dct.keys():
# print el
# print dct[el]
# fname='./Energy+.idd'
# debug=True
# blocklst,commlst,commdct=extractidddata(fname,debug)
# blocklst,commlst,commdct=extractidddata('Energy+.idd_old')
#mylib2.cpickledump((blocklst,lss),'block_comm.pik')
#this file is identical to the one produced by readidd2_2.py