1
2
3
4
5
6 """Decode elements from a Std/Martel parsed XML stream (OBSOLETE).
7
8 Andrew Dalke is no longer maintaining Martel or Bio.Mindy, and these modules
9 (and therefore Bio.Decode) have been deprecated. They are no longer used in
10 any of the current Biopython parsers, and are likely to be removed in a
11 future release."""
12
13 import warnings
14 warnings.warn("Martel and those parts of Biopython depending on it" \
15 +" directly (such as Bio.Mindy and Bio.Decode) are now" \
16 +" deprecated, and will be removed in a future release of"\
17 +" Biopython. If you want to continue to use this code,"\
18 +" please get in contact with the Biopython developers via"\
19 +" the mailing lists to avoid its permanent removal from"\
20 +" Biopython.", \
21 DeprecationWarning)
22
23 import string
24 from Bio.Parsers.spark import GenericScanner, GenericParser
25
27 result = []
28 for i in range(len(s)):
29 if s[i] != "\\":
30 result.append(s[i])
31 continue
32 c = s[i+1:i+2]
33 if c == "x":
34 x = s[i+2:i+4]
35 if len(x) != 2:
36 raise ValueError("invalid \\x escape")
37 i = int(x, 16)
38 result.append(chr(i))
39 continue
40 if c in "01234567":
41 x = s[i+1:i+4]
42
43 i = int(x, 8)
44 result.append(chr(i))
45 continue
46 result.append(c)
47 return "".join(result)
48
50 if not fields:
51 return ""
52 s = fields[0]
53 for field in fields[1:]:
54 if s[-1:] == "-" and s[-3:-2] == "-":
55 s = s + field
56 continue
57 if s.find(" ") == -1 and field.find(" ") == -1:
58 s = s + field
59 continue
60 s = s + " " + field
61 return (" ".join(s.split())).strip()
62
63
64
66 if s[-1:] == c:
67 return s[:-1]
68 return s
69
71 if s[:1] == c:
72 return s[1:]
73 return s
74
76 if s[:1] == c and s[-1:] == c:
77 return s[1:-1]
78 return s
79
85
87 return " ".join((" ".join(lines)).split()).strip()
88
90 table = string.maketrans(frm, to)
91 return s.translate(table)
92
94 """converts to int if the number is small, long if it's large"""
95 try:
96 return int(s)
97 except ValueError:
98 return long(s)
99
100 decode_functions = {
101 "chomp": (chomp, str, str),
102 "chompchomp": (chompchomp, str, str),
103 "chop": (lambda s: s[:-1], str, str),
104 "chopchop": (lambda s: s[1:-1], str, str),
105 "fixspaces": (fixspaces, str, str),
106 "lchomp": (lchomp, str, str),
107 "lchop": (lambda s: s[1:], str, str),
108 "lower": (lambda s: s.lower(), str, str),
109 "lstrip": (lambda s: s.lstrip(), str, str),
110 "replace": (lambda s, old, new: s.replace(old, new), str, str),
111 "rstrip": (lambda s: s.rstrip(), str, str),
112 "str": (str, str, str),
113 "strip": (lambda s: s.strip(), str, str),
114 "tr": (tr, str, str),
115 "unescape.c": (unescape_C, str, str),
116 "unescape.doublequote": (lambda s: s.replace('""', '"'), str, str),
117 "unescape.singlequote": (lambda s: s.replace("''", "'"), str, str),
118 "upper": (lambda s: s.upper(), str, str),
119
120
121 "join": (lambda lst, s = " ": s.join(lst), list, str),
122 "join.english": (join_english, list, str),
123
124
125 "int": (safe_int, [float, str, int], int),
126 "int.comma": (lambda s: safe_int(s.replace(",", "")),
127 [float, str, int], int),
128 "hex": (hex, str, int),
129 "oct": (oct, str, int),
130 "add": ((lambda i, j: i+j), int, int),
131
132
133 "float": (float, (float, str, int), float),
134
135 }
136
138
139 for k, v in decode_functions.items():
140 f, in_types, out_types = v
141 if isinstance(in_types, type([])):
142 in_types = tuple(in_types)
143 elif not isinstance(in_types, type( () )):
144 in_types = (in_types,)
145
146 if isinstance(out_types, type([])):
147 out_types = tuple(out_types)
148 elif not isinstance(out_types, type( () )):
149 out_types = (out_types,)
150
151 decode_functions[k] = (f, in_types, out_types)
152 _fixup_defs()
153
158 return cmp(self.type, other)
160 return "Token(%r)" % (self.type,)
161
167 return cmp(self.type, other)
169 return "%s(%r)" % (self.__class__.__name__, self.val)
172
176
180
184
188
192
197
201
205
209
213
217
221
229
231 r""" [+-]?((\d+(\.\d*)?)|\.\d+)([eE][+-]?[0-9]+)? """
232
233 try:
234 self.rv.append(Integer(safe_int(input)))
235 except ValueError:
236 self.rv.append(Float(float(input)))
237
240 self.name = name
241 self.args = args
243 args = self.args
244 if not args:
245 s = ""
246 else:
247 s = str(args)[1:-1]
248 return "%s(x, %s)" % (self.name, s)
249 __repr__ = __str__
250
252 - def __init__(self, start = "expression"):
255
257 """
258 expression ::= term
259 expression ::= term pipe expression
260 """
261 if len(args) == 1:
262 return [args[0]]
263 return [args[0]] + args[2]
264
266 """
267 term ::= functionname
268 term ::= functionname open_paren args close_paren
269 """
270 if len(args) == 1:
271 return Function(args[0].val)
272 return Function(args[0].val, tuple([x.val for x in args[2]]))
273
275 """
276 args ::= arg
277 args ::= arg comma args
278 """
279 if len(args) == 1:
280 return [args[0]]
281 return [args[0]] + args[2]
282
284 """
285 arg ::= string
286 arg ::= integer
287 arg ::= float
288 """
289 return args[0]
290
294
298
299 _decoder_cache = {}
300
303 self.f = f
304 self.args = args
306 return self.f(x, *self.args)
307
310 self.inner_f = inner_f
311 self.f = f
312 self.args = args
314 return self.f(self.inner_f(x), *self.args)
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
361
363 for x in subset:
364 if x not in total:
365 raise TypeError("%s can produce a %r value not accepted by %s" %
366 (old_name, x.__name__, new_name))
367
368 _typechecked_decoder_cache = {}
370 cache_lookup = (s, input_types, output_types)
371 try:
372 return _typechecked_decoder_cache[cache_lookup]
373 except KeyError:
374 pass
375 if input_types is not None and not isinstance(input_types, type( () )):
376 input_types = (input_types,)
377 if output_types is not None and not isinstance(output_types, type( () )):
378 output_types = (output_types,)
379
380 functions = parse(scan(s))
381
382
383 f = functions[0]
384 fc, in_types, out_types = decode_functions[f.name]
385 if input_types is not None:
386 for x in input_types:
387 if x not in in_types:
388 raise TypeError(
389 "the input type includes %r which isn't supported by %s" %
390 (x.__name__, f.name))
391
392
393 old_name = f.name
394 input_types = out_types
395 args = functions[0].args
396 if args:
397 fc = FunctionCall(fc, args)
398
399 for f in functions[1:]:
400 transform_func, in_types, out_types = decode_functions[f.name]
401 _verify_subtypes(input_types, in_types, old_name, f.name)
402 old_name = f.name
403 input_types = out_types
404 fc = FunctionCallChain(fc, transform_func, f.args)
405
406 if output_types is not None:
407 _verify_subtypes(input_types, output_types, old_name, "the output")
408 _typechecked_decoder_cache[cache_lookup] = fc
409 return fc
410
411
425
426 if __name__ == "__main__":
427 test()
428