1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """
18
19 This module provides dictionaries of default values and string lengths
20 for AIPS adverbs generated from the POPSDAT.HLP help file. These
21 dictionaries are pickled to avoid parsing POPSDAT.HLP again and again.
22
23 """
24
25
26 import os, pickle
27
30 """Determine default values and string lengths for all AIPS
31 tasks by parsing POPSDAT.HLP."""
32
33
34
35
36 self.__parse_statements()
37
38 input = open(self.path)
39
40 for line in input:
41
42 if line.startswith('--------'):
43 break
44 continue
45
46 for line in input:
47
48 if line.startswith(';') or line.startswith('C-'):
49 continue
50
51 split_line = line.split()
52 name = split_line[0].lower()
53 type = int(split_line[2])
54
55 if type == 1:
56 self.default_dict[name] = float(split_line[3])
57 elif type == 2:
58 if name in self.default_dict:
59 value = [self.default_dict[name]]
60 else:
61 value = [0.0]
62 pass
63
64 dimensions = int(split_line[3])
65 if dimensions == 1:
66 length = int(float(split_line[4]))
67 self.default_dict[name] = [None] + length * value
68 elif dimensions == 2:
69 dimy = int(float(split_line[4]))
70 dimx = int(float(split_line[5]))
71 self.default_dict[name] = [None] \
72 + dimx * [[None] + dimy * value]
73 else:
74 msg = "Cannot handle float arrays of dimension %d" \
75 % dimension
76 raise AssertionError, msg
77 elif type == 4:
78 self.verb_dict[name] = int(split_line[2])
79 elif type == 6:
80 break
81 elif type == 7:
82 if name in self.default_dict:
83 value = self.default_dict[name]
84 else:
85 value = ''
86 pass
87
88 dimensions = int(split_line[3])
89 self.strlen_dict[name] = int(float(split_line[4]))
90 if dimensions == 1:
91 self.default_dict[name] = value
92 elif dimensions == 2:
93 length = int(float(split_line[5]))
94 self.default_dict[name] = [None] + length * [value]
95 else:
96 msg = "Cannot handle character arrays of dimension %d" \
97 % dimension
98 raise AssertionError, msg
99 else:
100 continue
101
102 for line in input:
103
104
105 if line.startswith('PROC DEFADV'):
106 break
107 continue
108
109 for line in input:
110
111 if line.startswith('FINISH'):
112 break
113
114 split_line = line.split()
115 type = split_line[0]
116
117 if type == 'ARRAY':
118 for array in split_line[1:]:
119 lparen = array.find('(')
120 rparen = array.find(')')
121 if lparen == -1 or rparen == -1:
122 continue
123 name = array[:lparen].lower()
124 dim = array[lparen+1:rparen]
125 dim = dim.split(',')
126 if len(dim) == 1:
127 length = int(dim[0])
128 self.default_dict[name] = [None] + length * [0.0]
129 elif len(dim) == 2:
130 dimx = int(dim[0])
131 dimy = int(dim[1])
132 self.default_dict[name] = [None] \
133 + dimx * [[None] \
134 + dimy *[0.0]]
135 else:
136 continue
137 continue
138 pass
139 elif type == 'SCALAR':
140 for scalar in split_line[1:]:
141 name = scalar.rstrip(',').lower()
142 if name:
143 self.default_dict[name] = 0.0
144 pass
145 continue
146 pass
147 continue
148
150 """Parse statements in POPSDAT.HLP."""
151
152 input = open(self.path)
153
154 for line in input:
155
156 if line.startswith('QUITPOPS'):
157 break;
158 continue
159
160 for line in input:
161 if line.startswith('*'):
162 break
163 continue
164
165 for line in input:
166 if line.startswith('*'):
167 break
168
169
170 split_line = line.strip().split(';')
171 for statement in split_line:
172 if not statement:
173 continue
174
175
176
177 words = statement.strip().split('=')
178 name = words[0].strip().lower()
179 value = words[1].strip()
180 if value.startswith("'"):
181 value = value.strip("'")
182 else:
183 value = float(value)
184 pass
185
186
187
188 self.default_dict[name] = value
189 continue
190
192 self.default_dict = {}
193 self.strlen_dict = {}
194 self.verb_dict = {}
195
196 self.version = version
197 self.path = self.version + '/HELP/POPSDAT.HLP'
198 if not os.path.exists(self.path):
199 self.version = os.environ['AIPS_VERSION']
200 self.path = self.version + '/HELP/POPSDAT.HLP'
201
202 path = os.environ['HOME'] + '/.ParselTongue/' \
203 + os.path.basename(self.version) + '/' + 'popsdat.pickle'
204
205 try:
206 unpickler = pickle.Unpickler(open(path))
207 self.default_dict = unpickler.load()
208 self.strlen_dict = unpickler.load()
209 self.verb_dict = unpickler.load()
210 except IOError, EOFError:
211 self.__parse()
212
213
214 if not os.path.exists(os.path.dirname(path)):
215 os.makedirs(os.path.dirname(path))
216
217 pickler = pickle.Pickler(open(path, mode='w'))
218 pickler.dump(self.default_dict)
219 pickler.dump(self.strlen_dict)
220 pickler.dump(self.verb_dict)
221