1 | import re
|
---|
2 |
|
---|
3 | import time
|
---|
4 | import datetime
|
---|
5 | import pytz
|
---|
6 |
|
---|
7 | from ....builtins import *
|
---|
8 |
|
---|
9 | from ...exceptions import *
|
---|
10 | from ...expressions import *
|
---|
11 | from ...autotype import *
|
---|
12 | from ...streams import *
|
---|
13 |
|
---|
14 | ##
|
---|
15 | ## nbstlutil -U records begin with the string "Image:" on a line by itself
|
---|
16 | ##
|
---|
17 | def stream(stream, format='nbstlutil list -U'):
|
---|
18 |
|
---|
19 | if format in ['nbstlutil list -U']:
|
---|
20 | return LineSeparatorStream(stream, separator='Image:', header=1)
|
---|
21 | else:
|
---|
22 | raise ParseError, 'Unknown format %s' % (format)
|
---|
23 |
|
---|
24 |
|
---|
25 | ##
|
---|
26 | ## Parse a nbstlutil record
|
---|
27 | ##
|
---|
28 | def parse(record, format='nbstlutil list -U', version=None, tz=None):
|
---|
29 |
|
---|
30 | re_pair = re.compile('^\s*([^:]+):\s*(.*)\s*$')
|
---|
31 | re_type_id = re.compile('^([0-9a-zA-Z\-_ ]+)\s+\((\d+)\)$')
|
---|
32 | re_id_type = re.compile('^(\d+)\s+\(([0-9a-zA-Z\-_ ]+)\)$')
|
---|
33 | re_in_parens = re.compile('^\((.*)\)$')
|
---|
34 | re_starts_with_time = re.compile('^time_.*$')
|
---|
35 | re_ends_with_time = re.compile('^.*_time$')
|
---|
36 | re_time_plus_epoch_seconds_in_parens = re.compile('^.*\d:\d\d:\d\d.*\((\d+)\)$')
|
---|
37 | re_epoch_seconds_plus_time_in_parens = re.compile('^(\d+)\s+\(.*\d:\d\d:\d\d.*\)$')
|
---|
38 |
|
---|
39 | image = ExtendedDict()
|
---|
40 |
|
---|
41 | if format == 'nbstlutil list -U':
|
---|
42 |
|
---|
43 | header = True
|
---|
44 |
|
---|
45 | try:
|
---|
46 |
|
---|
47 | i = 0
|
---|
48 |
|
---|
49 | #
|
---|
50 | # process image header
|
---|
51 | #
|
---|
52 | while header and i < len(record):
|
---|
53 |
|
---|
54 | line = record[i]
|
---|
55 |
|
---|
56 | i += 1
|
---|
57 |
|
---|
58 | #
|
---|
59 | # skip blank lines
|
---|
60 | #
|
---|
61 | if line == '':
|
---|
62 | continue
|
---|
63 |
|
---|
64 | match = re_pair.match(line)
|
---|
65 |
|
---|
66 | key = match.group(1)
|
---|
67 | value = match.group(2)
|
---|
68 | key = key.lower()
|
---|
69 | key = key.rstrip()
|
---|
70 | key = key.replace('-', '_')
|
---|
71 | key = key.replace(' ', '_')
|
---|
72 |
|
---|
73 | if key == 'copy':
|
---|
74 | header = False
|
---|
75 | else:
|
---|
76 |
|
---|
77 | #
|
---|
78 | # convert text values to python datatypes
|
---|
79 | #
|
---|
80 | while True:
|
---|
81 |
|
---|
82 | #
|
---|
83 | # key name starts with time_ or ends with _time
|
---|
84 | #
|
---|
85 | if re_ends_with_time.match(key) or re_starts_with_time.match(key):
|
---|
86 |
|
---|
87 | match = re_epoch_seconds_plus_time_in_parens.match(value)
|
---|
88 | if match:
|
---|
89 | value = datetime.datetime.fromtimestamp(float(match.group(1)), tz)
|
---|
90 | break
|
---|
91 |
|
---|
92 | match = re_time_plus_epoch_seconds_in_parens.match(value)
|
---|
93 | if match:
|
---|
94 | value = datetime.datetime.fromtimestamp(float(match.group(1)), tz)
|
---|
95 | break
|
---|
96 |
|
---|
97 | #
|
---|
98 | # separate id and type
|
---|
99 | #
|
---|
100 | match = re_id_type.match(value)
|
---|
101 | if match:
|
---|
102 | idkey = '%s_id' % (key)
|
---|
103 | value = match.group(2)
|
---|
104 | image[idkey] = int(match.group(1))
|
---|
105 | break
|
---|
106 |
|
---|
107 | #
|
---|
108 | # strip enclosing parens
|
---|
109 | #
|
---|
110 | match = re_in_parens.match(value)
|
---|
111 | if match:
|
---|
112 | value = match.group(1)
|
---|
113 | break
|
---|
114 |
|
---|
115 | #
|
---|
116 | # separate type and id
|
---|
117 | #
|
---|
118 | match = re_type_id.match(value)
|
---|
119 | if match:
|
---|
120 | idkey = '%s_id' % (key)
|
---|
121 | value = match.group(1)
|
---|
122 | image[idkey] = int(match.group(2))
|
---|
123 | break
|
---|
124 |
|
---|
125 | #
|
---|
126 | # ..otherwise autodetect and break if no datatype conversion was possible
|
---|
127 | #
|
---|
128 | value = autotype(value)
|
---|
129 | break
|
---|
130 |
|
---|
131 | #
|
---|
132 | # store value
|
---|
133 | #
|
---|
134 | image[key] = value
|
---|
135 |
|
---|
136 | #
|
---|
137 | # parse image copies
|
---|
138 | #
|
---|
139 | copies = ExtendedDict()
|
---|
140 | fragment = False
|
---|
141 |
|
---|
142 | while i < len(record):
|
---|
143 |
|
---|
144 | line = record[i]
|
---|
145 |
|
---|
146 | i += 1
|
---|
147 |
|
---|
148 | #
|
---|
149 | # skip blank lines
|
---|
150 | #
|
---|
151 | if line == '':
|
---|
152 | continue
|
---|
153 |
|
---|
154 | match = re_pair.match(line)
|
---|
155 |
|
---|
156 | key = match.group(1)
|
---|
157 | value = match.group(2)
|
---|
158 | key = key.lower()
|
---|
159 | key = key.rstrip()
|
---|
160 | key = key.replace('-', '_')
|
---|
161 | key = key.replace(' ', '_')
|
---|
162 |
|
---|
163 | if key == 'copy':
|
---|
164 | fragment = False
|
---|
165 |
|
---|
166 | if key == 'fragment':
|
---|
167 | fragment = True
|
---|
168 | #
|
---|
169 | # #
|
---|
170 | # # identify current copy
|
---|
171 | # #
|
---|
172 | # if key == 'copy_number':
|
---|
173 | #
|
---|
174 | # copy_number = int(value)
|
---|
175 | #
|
---|
176 | # if copy_number not in copies:
|
---|
177 | # copies[copy_number] = ExtendedDict()
|
---|
178 | # copies[copy_number]['copy_number'] = copy_number
|
---|
179 | # copies[copy_number]['fragments'] = ExtendedDict()
|
---|
180 | #
|
---|
181 | # #
|
---|
182 | # # identify current fragment
|
---|
183 | # #
|
---|
184 | # elif key == 'fragment':
|
---|
185 | #
|
---|
186 | # if value == 'TIR (-1)':
|
---|
187 | # fragment = -1
|
---|
188 | # elif value == 'TIR (-2)':
|
---|
189 | # fragment = -2
|
---|
190 | # else:
|
---|
191 | # fragment = int(value)
|
---|
192 | #
|
---|
193 | # if fragment not in copies[copy_number].fragments:
|
---|
194 | # copies[copy_number].fragments[fragment] = ExtendedDict()
|
---|
195 | # copies[copy_number].fragments[fragment]['fragment'] = fragment
|
---|
196 | #
|
---|
197 | # #
|
---|
198 | # # save the key and value using the copy and fragment
|
---|
199 | # #
|
---|
200 | # else:
|
---|
201 | #
|
---|
202 | # #
|
---|
203 | # # match timestamp with Unix time in parentheses
|
---|
204 | # #
|
---|
205 | # match = re_nbu_datetime.match(line)
|
---|
206 | # if match:
|
---|
207 | # value = datetime.datetime.fromtimestamp(float(match.group(1)), tz)
|
---|
208 | #
|
---|
209 | # #
|
---|
210 | # # separate type and id
|
---|
211 | # #
|
---|
212 | # elif re_type_id.match(value):
|
---|
213 | # match = re_type_id.match(value)
|
---|
214 | # idkey = '%s_id' % (key)
|
---|
215 | # value = match.group(1)
|
---|
216 | # copies[copy_number].fragments[fragment][idkey] = int(match.group(2))
|
---|
217 | #
|
---|
218 | # #
|
---|
219 | # # ..otherwise autodetect
|
---|
220 | # #
|
---|
221 | # else:
|
---|
222 | # value = autotype(value)
|
---|
223 | #
|
---|
224 | # copies[copy_number].fragments[fragment][key] = value
|
---|
225 | #
|
---|
226 | # image['copies'] = copies
|
---|
227 | #
|
---|
228 |
|
---|
229 | return image
|
---|
230 |
|
---|
231 | except Exception, e:
|
---|
232 |
|
---|
233 | # for line in record:
|
---|
234 | # print line
|
---|
235 |
|
---|
236 | raise ParseError, e
|
---|
237 |
|
---|
238 | else:
|
---|
239 |
|
---|
240 | raise ParseError, 'Unknown format %s' % (format)
|
---|
241 |
|
---|