Package Bio :: Module StdHandler
[hide private]
[frames] | no frames]

Source Code for Module Bio.StdHandler

  1  # Standard Content and Dispatch handlers for the Bioformat IO system 
  2   
  3  from xml.sax import handler 
  4  from Martel import Parser, Dispatch 
  5  from Bio import Std, Decode 
  6   
  7  ################################### 
  8   
  9  # Helper functions to make functions 
 10   
11 -def add_int_handler(klass, tag, attrname):
12 assert not hasattr(klass, "start_" +tag), "existing method exists" 13 assert not hasattr(klass, "end_" +tag), "existing method exists" 14 s = """if 1: 15 def start(self, tag, attrs): 16 self.save_characters() 17 def end(self, tag): 18 self.%s = int(self.get_characters()) 19 """ % attrname 20 d = {} 21 exec s in d 22 setattr(klass, "start_" + tag, d["start"]) 23 setattr(klass, "end_" + tag, d["end"])
24
25 -def add_text_handler(klass, tag, attrname):
26 assert not hasattr(klass, "start_" +tag), "existing method exists" 27 assert not hasattr(klass, "end_" +tag), "existing method exists" 28 s = """if 1: 29 def start(self, tag, attrs): 30 self.save_characters() 31 def end(self, tag): 32 self.%s = self.get_characters() 33 """ % attrname 34 d = {} 35 exec s in d 36 setattr(klass, "start_" + tag, d["start"]) 37 setattr(klass, "end_" + tag, d["end"])
38
39 -def add_text_dict_handler(klass, tag, attrname, key):
40 assert not hasattr(klass, "start_" +tag), "existing method exists" 41 assert not hasattr(klass, "end_" +tag), "existing method exists" 42 s = """if 1: 43 def start(self, tag, attrs): 44 self.save_characters() 45 def end(self, tag): 46 self.%s["%s"] = self.get_characters() 47 """ % (attrname, key) 48 d = {} 49 exec s in d 50 setattr(klass, "start_" + tag, d["start"]) 51 setattr(klass, "end_" + tag, d["end"])
52
53 -def add_text_decode_handler(klass, tag, attrname):
54 assert not hasattr(klass, "start_" +tag), "existing method exists" 55 assert not hasattr(klass, "end_" +tag), "existing method exists" 56 s = """if 1: 57 def start(self, tag, attrs): 58 self.save_characters() 59 self._decode_%s = attrs.get("bioformat:decode", None) 60 def end(self, tag): 61 if self._decode_%s is not None: 62 s = Decode.make_decoder(self._decode_%s)(s) 63 self.%s = self.get_characters() 64 """ % (tag, tag, tag, attrname) 65 d = {"Decode": Decode} 66 exec s in d 67 setattr(klass, "start_" + tag, d["start"]) 68 setattr(klass, "end_" + tag, d["end"])
69
70 -def add_first_text_handler(klass, tag, attrname):
71 assert not hasattr(klass, "start_" +tag), "existing method exists" 72 assert not hasattr(klass, "end_" +tag), "existing method exists" 73 s = """if 1: 74 def start(self, tag, attrs): 75 if self.%s is None: 76 self.save_characters() 77 def end(self, tag): 78 if self.%s is None: 79 self.%s = self.get_characters() 80 """ % (attrname, attrname, attrname) 81 d = {} 82 exec s in d 83 setattr(klass, "start_" + tag, d["start"]) 84 setattr(klass, "end_" + tag, d["end"])
85
86 -def add_text_block_handler(klass, tag, joinattr, defaultjoin, attrname):
87 assert not hasattr(klass, "start_" + tag), "existing method exists" 88 assert not hasattr(klass, "end_" + tag), "existing method exists" 89 assert not hasattr(klass, "start_"+tag+"_block"), "existing method exists" 90 assert not hasattr(klass, "end_" +tag+"_block"), "existing method exists" 91 s = """if 1: 92 def start_block(self, tag, attrs): 93 self._%(tag)s_join_func = Decode.make_decoder(attrs.get(%(joinattr)r, %(defaultjoin)r)) 94 self._%(tag)s_lines = [] 95 def end_block(self, tag): 96 self.%(attrname)s = self._%(tag)s_join_func(self._%(tag)s_lines) 97 def start(self, tag, attrs): 98 self.save_characters() 99 def end(self, tag): 100 self._%(tag)s_lines.append(self.get_characters()) 101 """ % locals() 102 d = {"Decode": Decode} 103 exec s in d 104 setattr(klass, "start_" + tag, d["start"]) 105 setattr(klass, "end_" + tag, d["end"]) 106 setattr(klass, "start_" + tag + "_block", d["start_block"]) 107 setattr(klass, "end_" + tag + "_block", d["end_block"])
108
109 -def add_value_handler(klass, tag, attrname):
110 assert not hasattr(klass, "start_" +tag), "existing method exists" 111 assert not hasattr(klass, "end_" +tag), "existing method exists" 112 s = """if 1: 113 def start(self, tag, attrs): 114 self._%(tag)s_name = attrs["name"] 115 self._%(tag)s_decode = attrs.get("bioformat:decode", None) 116 self.save_characters() 117 def end(self, tag): 118 s = self.get_characters() 119 if self._%(tag)s_decode is not None: 120 s = Decode.make_decoder(self._%(tag)s_decode)(s) 121 self.%(attrname)s[self._%(tag)s_name] = s 122 """ % locals() 123 d = {"Decode": Decode} 124 exec s in d 125 setattr(klass, "start_" + tag, d["start"]) 126 setattr(klass, "end_" + tag, d["end"])
127 128 129 ################################# 130
131 -class ConvertHandler(handler.ContentHandler):
132 """Used to read records and produce output"""
133 - def __init__(self, record_builder, writer, record_tag = "record"):
134 handler.ContentHandler.__init__(self) 135 self.record_builder = record_builder 136 self.writer = writer 137 self.record_tag = record_tag
138
139 - def startDocument(self):
140 self.inside_record = 0 141 self.characters = self.ignore_characters
142
143 - def startElement(self, tag, attrs):
144 if self.inside_record: 145 self.record_builder.startElement(tag, attrs) 146 elif tag == self.record_tag: 147 self.record_builder.startDocument() 148 self.inside_record = 1 149 self.characters = self.record_builder.characters 150 self.record_builder.startElement(tag, attrs)
151
152 - def endElement(self, tag):
153 if self.inside_record: 154 self.record_builder.endElement(tag) 155 if tag == self.record_tag: 156 self.record_builder.endDocument() 157 self.writer.write(self.record_builder.document) 158 self.inside_record = 0 159 self.characters = self.ignore_characters
160
161 - def ignore_characters(self, s):
162 pass
163
164 -class ConvertDispatchHandler(Dispatch.Dispatcher):
165 """Used to read records and produce output through a Dispatcher"""
166 - def __init__(self, record_builder, writer, record_tag = "record"):
167 setattr(self, "end_" + record_tag, self.write_record) 168 Dispatch.Dispatcher.__init__(self, 169 remap = {record_tag: "bioformat:"} 170 ) 171 self.acquire(record_builder) 172 self.record_builder = record_builder 173 self.writer = writer 174 self.record_tag = record_tag
175 - def write_record(self, tag):
176 self.writer.write(self.record_builder.document)
177 178 179
180 -class RecognizeHandler(handler.ContentHandler, handler.ErrorHandler):
181 - def __init__(self):
182 self.recognized = 1 183 self.exc = None
184
185 - def fatalError(self, exc):
186 if isinstance(exc, Parser.ParserIncompleteException): 187 pass 188 else: 189 self.recognized = 0 190 self.exc = exc 191 raise exc
192 193 error = fatalError 194
195 - def endElement(self, tag):
196 if tag == "record": 197 raise Parser.ParserException("we finished a record!")
198 199 200
201 -class Handle_dbid(Dispatch.Callback):
202 - def start_dbid(self, tag, attrs):
203 self.attrs = attrs 204 self.save_characters()
205
206 - def end_dbid(self, tag):
207 text = self.get_characters() 208 self.callback(text, self.attrs)
209 210
211 -class Handle_description(Dispatch.Callback):
212 - def start_description_block(self, tag, attrs):
213 j = attrs.get("join", None) 214 if j is None: 215 self.join_fctn = Decode.join_fixspaces 216 else: 217 self.join_fctn = Decode.make_typechecked_decoder(j, list, str) 218 self.descriptions = []
219 - def start_description(self, tag, attrs):
220 self.save_characters()
221 - def end_description(self, tag):
222 x = self.get_characters() 223 self.descriptions.append(x)
224 - def end_description_block(self, tag):
225 self.callback(self.join_fctn(self.descriptions))
226 227 #### There can be multiple dbxref_dbids in a dbxref 228 # DR EMBL; X64411; CAA45756.1; -. 229 # <dbxref><..dbname style="swiss">EMBL</..dbname> 230 # <dbid type="primary">X64411</dbid> 231 # <dbid type="accession">CAA45756.1</dbid> 232 # </dbxref> 233 ### 234 # DR P35156, YPUI_BACSU, F; 235 # <dbxref><dbid type="primary" dbname="sprot">P35156</dbid> 236 # <dbid type="accession" dbname="sprot">YPUI_BACSU</dbid> 237 # <negate/> 238 # </dbxref> 239
240 -def _fixup_sp_pattern(exp):
241 import re 242 import Martel 243 exp = Martel.select_names(exp, (Std.dbxref_dbname.tag,Std.dbxref_dbid.tag)) 244 245 e = exp._find_groups(Std.dbxref_dbname.tag) 246 assert len(e) == 1 247 e = e[0] 248 e.name = "dbname" 249 dbstyle = e.attrs["style"] 250 e.attrs = {} 251 e = exp._find_groups(Std.dbxref_dbid.tag) 252 assert len(e) == 2 253 e[0].name = "primary_dbid" 254 primary_type = e[0].attrs["type"] 255 e[0].attrs = {} 256 e[1].name = "secondary_dbid" 257 secondary_type = e[1].attrs["type"] 258 e[1].attrs = {} 259 pattern = str(exp) + "$" 260 pat = re.compile(pattern) 261 return pat, dbstyle, primary_type, secondary_type
262 263 # Turns out these 'fast' versions speed up the dbxref code by about 264 # a factor of 2. 265 266 # DR PIR; S08427; S08427. 267 _fast_dbxref_sp_general_data = None
268 -def _fast_dbxref_sp_general(s):
269 global _fast_dbxref_sp_general_data 270 if _fast_dbxref_sp_general_data is None: 271 from Bio.expressions.swissprot import sprot38 272 _fast_dbxref_sp_general_data = _fixup_sp_pattern( 273 sprot38.real_DR_general) 274 275 pat, dbstyle, primary_type, secondary_type = _fast_dbxref_sp_general_data 276 277 m = pat.match(s) 278 assert m is not None, "Ill-formated sp-general dxbref: %r" % s 279 return ( 280 (dbstyle, m.group("dbname"), primary_type, 281 m.group("primary_dbid"), 0), 282 (dbstyle, m.group("dbname"), secondary_type, 283 m.group("secondary_dbid"), 0) 284 )
285 286 # DR PFAM; PF01018; GTP1_OBG; 1. 287 # DR PROSITE; PS00905; GTP1_OBG; 1. 288 289 _fast_dbxref_sp_prosite_data = None
290 -def _fast_dbxref_sp_prosite(s):
291 global _fast_dbxref_sp_prosite_data 292 293 if _fast_dbxref_sp_prosite_data is None: 294 from Bio.expressions.swissprot import sprot38 295 _fast_dbxref_sp_prosite_data = _fixup_sp_pattern( 296 sprot38.real_DR_prosite) 297 298 pat, dbstyle, primary_type, secondary_type = _fast_dbxref_sp_prosite_data 299 m = pat.match(s) 300 assert m is not None, "Ill-formated sp-prosite dxbref: %r" % s 301 return ( 302 (dbstyle, m.group("dbname"), primary_type, 303 m.group("primary_dbid"), 0), 304 (dbstyle, m.group("dbname"), secondary_type, 305 m.group("secondary_dbid"), 0) 306 )
307 308 309 # DR EMBL; M36407; AAA33110.1; -. 310 _fast_dbxref_sp_embl_data = None
311 -def _fast_dbxref_sp_embl(s):
312 global _fast_dbxref_sp_embl_data 313 314 if _fast_dbxref_sp_embl_data is None: 315 from Bio.expressions.swissprot import sprot38 316 _fast_dbxref_sp_embl_data = _fixup_sp_pattern( 317 sprot38.real_DR_embl) 318 319 pat, dbstyle, primary_type, secondary_type = _fast_dbxref_sp_embl_data 320 m = pat.match(s) 321 assert m is not None, "Ill-formated sp-embl dxbref: %r" % s 322 return ( 323 (dbstyle, m.group("dbname"), primary_type, 324 m.group("primary_dbid"), 0), 325 (dbstyle, m.group("dbname"), secondary_type, 326 m.group("secondary_dbid"), 0) 327 )
328 329 _fast_dbxref_parser_table = { 330 "sp-general": _fast_dbxref_sp_general, 331 "sp-prosite": _fast_dbxref_sp_prosite, 332 "sp-embl": _fast_dbxref_sp_embl, 333 } 334
335 -class Handle_dbxref(Dispatch.Callback):
336 - def __init__(self, callback):
337 Dispatch.Callback.__init__(self, callback) 338 self.supported_features.append("fast-sp-dbxref") 339 self.slow_callback = self.callback
340 - def start_dbxref(self, tag, attrs):
341 self.negate = 0 342 self.dbname = None 343 self.dbids = [] 344 self.info = []
345
346 - def start_dbxref_dbname(self, tag, attrs):
347 assert self.dbname is None, "cannot set the dbname twice" 348 self.dbname_style = attrs.get("style", "unknown") 349 self.save_characters()
350 - def end_dbxref_dbname(self, tag):
351 self.dbname = self.get_characters()
352
353 - def start_dbxref_dbid(self, tag, attrs):
354 d = attrs.get("dbname", None) 355 if d is None: 356 assert self.dbname is not None, "must set the dbname" 357 self.info.append( (self.dbname_style, self.dbname, 358 attrs.get("type", "primary")) ) 359 else: 360 self.info.append( ("bioformat", d, 361 attrs.get("type", "primary")) ) 362 self.save_characters()
363
364 - def end_dbxref_dbid(self, tag):
365 self.dbids.append( self.get_characters())
366
367 - def start_dbxref_negate(self, tag, attrs):
368 self.negate = 1
369
370 - def end_dbxref(self, tag):
371 cb = self.slow_callback 372 if cb is None: 373 return 374 negate = self.negate 375 for ( (dbname_style, dbname, idtype), dbid) in zip(self.info, 376 self.dbids): 377 self.slow_callback(dbname_style, dbname, idtype, dbid, negate)
378
379 - def start_fast_dbxref(self, tag, attrs):
380 style = attrs["style"] 381 self._fast_parser = _fast_dbxref_parser_table[style] 382 self.save_characters() 383 self.slow_callback = None
384 - def end_fast_dbxref(self, tag):
385 for info in self._fast_parser(self.get_characters()): 386 self.callback(*info) 387 self.slow_callback = self.callback
388 389 ##################
390 -class Handle_sequence(Dispatch.Callback):
391 global_alphabet = None
392 - def start_(self, tag, attrs):
393 self.global_alphabet = None
394
395 - def start_sequence_block(self, tag, attrs):
396 self.local_alphabet = attrs.get("alphabet", None) 397 self.gapchar = attrs.get("gapchar", None) 398 self.stopchar = attrs.get("stopchar", None) 399 j = attrs.get("join", None) 400 if j is not None: 401 self.join_func = Decode.make_typechecked_decoder(j, list, str) 402 else: 403 self.join_func = None 404 self.sequences = []
405
406 - def end_sequence_block(self, tag):
407 f = self.join_func 408 if f is not None: 409 seq = self.f(self.sequences) 410 else: 411 seq = "".join(self.sequences).replace(" ", "") 412 alphabet = self.local_alphabet or self.global_alphabet or "unknown" 413 self.callback( (alphabet, seq, self.gapchar, self.stopchar) )
414
415 - def start_alphabet(self, tag, attrs):
416 self.global_alphabet = attrs["alphabet"]
417
418 - def start_sequence(self, tag, attrs):
419 self.save_characters()
420 - def end_sequence(self, tag):
421 self.sequences.append(self.get_characters())
422
423 -class Feature:
424 - def __init__(self, name, description, location, qualifiers):
425 self.name = name 426 self.description = description 427 self.location = location 428 self.qualifiers = qualifiers
429 - def __str__(self):
430 return "Feature %r %r %s num_qualifiers = %d" % \ 431 (self.name, self.description, self.location, 432 len(self.qualifiers))
433 434
435 -class Handle_feature_location(Dispatch.Callback):
436 - def __init__(self, callback, settings = {}):
437 Dispatch.Callback.__init__(self, callback) 438 self.settings = settings
439
440 - def start_feature(self, tag, attrs):
441 self.location_style = attrs.get("location-style", 442 self.settings["location-style"]) 443 j = attrs.get("join-feature", None) 444 if j is None: 445 self.text_join_func = "".join 446 else: 447 self.text_join_func = Decode.make_typechecked_decoder(j, list, str) 448 449 self.location_start = None 450 self.location_end = None 451 self.text_lines = []
452
453 - def end_feature(self, tag):
454 if self.location_start or self.location_end: 455 if self.text_lines: 456 raise TypeError("Cannot have both location text and start/end") 457 self.callback(self.location_style, 458 (self.location_start, self.location_end)) 459 else: 460 self.callback(self.location_style, 461 (self.text_join_func(self.text_lines), None))
462
463 - def start_feature_location(self, tag, attrs):
464 self.save_characters()
465 - def end_feature_location(self, tag):
466 self.text_lines.append(self.get_characters())
467 468 add_text_handler(Handle_feature_location, "feature_location_start", 469 "location_start") 470 add_text_handler(Handle_feature_location, "feature_location_end", 471 "location_end") 472 473 ################################## 474
475 -class Handle_feature_qualifier(Dispatch.Callback):
476 - def __init__(self, callback, settings):
477 self.settings = settings 478 Dispatch.Callback.__init__(self, callback)
479
480 - def start_feature_qualifier(self, tag, attrs):
481 self.name = None 482 self.description = [] 483 qj = attrs.get("join-qualifier", None) 484 if qj is None: 485 self.join = self.settings["qualifier_join_func"] 486 else: 487 self.join = Decode.make_typechecked_decoder(qj, list, str)
488
489 - def end_feature_qualifier(self, tag):
490 self.callback(self.name, self.join(self.description))
491
492 - def start_feature_qualifier_description(self, tag, attrs):
493 self.save_characters()
496 497 add_text_handler(Handle_feature_qualifier, "feature_qualifier_name", "name") 498 499 #################### 500
501 -class Handle_features(Dispatch.Callback):
502 - def __init__(self, callback):
503 Dispatch.Callback.__init__(self, callback) 504 self.settings = {} 505 506 self.acquire(Handle_feature_location(self.add_location, self.settings)) 507 508 self.acquire(Handle_feature_qualifier(self.add_feature_qualifier, 509 self.settings))
510
511 - def start_feature_block(self, tag, attrs):
512 jf = attrs.get("join-description", None) 513 if jf is None: 514 self.join_feature_description = Decode.join_fixspaces 515 else: 516 self.join_feature_description = Decode.make_typechecked_decoder( 517 jf, list, str) 518 519 self.settings["location-style"] = attrs.get("location-style", None) 520 521 jq = attrs.get("join-qualifier", None) 522 if jq is None: 523 self.settings["qualifier_join_func"] = Decode.join_fixspaces 524 else: 525 self.settings["qualifier_join_func"] = \ 526 Decode.make_typechecked_decoder(jq, list, str) 527 self.features = []
528
529 - def end_feature_block(self, tag):
530 self.callback(self.features) 531 self.features = None
532
533 - def start_feature(self, tag, attrs):
534 self.name = None 535 self.description = [] 536 self.location = None 537 self.qualifiers = []
538
539 - def start_feature_description(self, tag, attrs):
540 self.save_characters()
541 - def end_feature_description(self, tag):
543
544 - def end_feature(self, tag):
545 self.features.append(Feature( 546 self.name, 547 self.join_feature_description(self.description), 548 self.location, 549 self.qualifiers))
550
551 - def add_feature_qualifier(self, name, description):
552 self.qualifiers.append((name, description))
553
554 - def add_location(self, style, location_info):
555 self.location = (style, location_info)
556 557 add_text_handler(Handle_features, "feature_name", "name") 558 559 560 ############## Search handlers 561
562 -class Handle_hsp_seqalign(Dispatch.Callback):
563 - def start_hsp(self, tag, attrs):
564 self.query_name = None # "Query" 565 self.subject_name = None # "Sbjct" 566 567 self.query_seq = "" # the actual text of the sequence 568 self.homology_seq = "" 569 self.subject_seq = "" 570 571 self.query_start_loc = None 572 self.query_end_loc = None 573 574 self.subject_start_loc = None 575 self.subject_end_loc = None
576
577 - def end_hsp(self, tag):
578 self.callback(self)
579
580 - def start_hsp_seqalign(self, tag, attrs):
581 self.sub_leader = None
582
583 - def start_hsp_seqalign_query_seq(self, tag, attrs):
584 self.save_characters()
585 - def end_hsp_seqalign_query_seq(self, tag):
586 s = self.get_characters() 587 self.query_seq += s 588 self.sub_query_seq_len = len(s)
589
590 - def start_hsp_seqalign_homology_seq(self, tag, attrs):
591 self.save_characters()
592 - def end_hsp_seqalign_homology_seq(self, tag):
593 query_leader = self.leader_size 594 query_seq_len = self.sub_query_seq_len 595 line = self.get_characters() 596 s = line[query_leader:query_leader+query_seq_len] 597 assert len(s) == query_seq_len, (len(s), query_seq_len, line) 598 self.homology_seq += s
599
600 - def start_hsp_seqalign_subject_seq(self, tag, attrs):
601 self.save_characters()
602 - def end_hsp_seqalign_subject_seq(self, tag):
603 self.subject_seq += self.get_characters()
604
605 - def start_hsp_seqalign_query_leader(self, tag, attrs):
606 self.save_characters()
607 - def end_hsp_seqalign_query_leader(self, tag):
608 self.leader_size = len(self.get_characters())
609 610 add_first_text_handler(Handle_hsp_seqalign, "hsp_seqalign_query_name", 611 "query_name") 612 613 add_first_text_handler(Handle_hsp_seqalign, "hsp_seqalign_subject_name", 614 "subject_name") 615 616 add_first_text_handler(Handle_hsp_seqalign, "hsp_seqalign_query_start", 617 "query_start_loc") 618 add_text_handler(Handle_hsp_seqalign, "hsp_seqalign_query_end", 619 "query_end_loc") 620 621 add_first_text_handler(Handle_hsp_seqalign, "hsp_seqalign_subject_start", 622 "subject_start_loc") 623 add_text_handler(Handle_hsp_seqalign, "hsp_seqalign_subject_end", 624 "subject_end_loc") 625 626 627 628 629 ############################# 630
631 -class Handle_hsp(Dispatch.Callback):
632 - def __init__(self, callback):
633 Dispatch.Callback.__init__(self, callback) 634 self.acquire(Handle_hsp_seqalign(self.add_hsp_seqs))
635
636 - def start_hsp(self, tag, attrs):
637 self.hsp_values = {} # expect, p, identities, ... 638 self.strands = {} 639 self.frames = {}
640
641 - def end_hsp(self, tag):
642 self.callback(self.hsp_values, 643 self.hsp_info, 644 self.strands, self.frames, 645 )
646
647 - def start_hsp_strand(self, tag, attrs):
648 self.strands[attrs["which"]] = attrs["strand"]
649
650 - def start_hsp_frame(self, tag, attrs):
651 self.getting_frame = attrs["which"] 652 self.save_characters()
653
654 - def end_hsp_frame(self, tag):
655 self.frames[self.getting_frame] = self.get_characters() 656 self.getting_frame = None
657
658 - def add_hsp_seqs(self, hsp_info):
659 self.hsp_info = hsp_info
660
661 - def start_hsp_value(self, tag, attrs):
662 self.value_convert = attrs.get("bioformat:decode", None) 663 self.value_name = attrs["name"] 664 self.save_characters()
665
666 - def end_hsp_value(self, tag):
667 s = self.get_characters() 668 if self.value_name is not None: 669 if self.value_name == "float": 670 s = float(s) 671 else: 672 s = Decode.make_decoder(self.value_convert)(s) 673 self.hsp_values[self.value_name] = s
674 675 ############################# 676 677
678 -class Handle_search_table(Dispatch.Callback):
679 - def start_search_table_value(self, tag, attrs):
680 self.value_name = attrs["name"] 681 self.value_decode = attrs.get("bioformat:decode", None) 682 self.save_characters()
683 - def end_search_table_value(self, tag):
684 s = self.get_characters() 685 if self.value_decode is not None: 686 x = self.value_decode 687 if x == "int": 688 s = int(s) 689 elif x == "float": 690 s = float(s) 691 else: 692 s = Decode.make_decoder(x)(s) 693 self.values[self.value_name] = s
694
695 - def start_search_table(self, tag, attrs):
696 self.data = []
697 - def end_search_table(self, tag):
698 self.callback(self.data) 699 self.data = None
700
701 - def start_search_table_entry(self, tag, attrs):
702 self.description = None 703 self.values = {}
704
705 - def end_search_table_entry(self, tag):
706 self.data.append( (self.description, self.values) ) 707 self.description = self.values = None
708 709 add_text_handler(Handle_search_table, "search_table_description", 710 "description") 711 712 ############################# 713
714 -class Handle_search_header(Dispatch.Callback):
715 - def start_(self, tag, attrs):
716 self.dict = {} 717 self.query_description = None
718
719 - def end_search_header(self, tag):
720 d = self.dict 721 d["query_description"] = self.query_description 722 self.callback(d)
723 724 add_text_block_handler(Handle_search_header, "query_description", 725 "join-query", "join|fixspaces", "query_description") 726 727 add_text_dict_handler(Handle_search_header, "application_name", 728 "dict", "appname") 729 add_text_dict_handler(Handle_search_header, "application_version", 730 "dict", "appversion") 731 add_text_dict_handler(Handle_search_header, "database_name", 732 "dict", "dbname") 733 add_text_dict_handler(Handle_search_header, "database_num_sequences", 734 "dict", "db_num_sequences") 735 add_text_dict_handler(Handle_search_header, "database_num_letters", 736 "dict", "db_num_letters") 737 add_text_dict_handler(Handle_search_header, "query_size", 738 "dict", "query_size") 739 740 741 ############################# 742
743 -class Handle_search_info(Dispatch.Callback):
744 - def start_(self, tag, attrs):
745 self.parameters = {} 746 self.statistics = {}
747
748 - def end_(self, tag):
749 self.callback(self.parameters, self.statistics)
750 751 add_value_handler(Handle_search_info, "search_parameter", "parameters") 752 add_value_handler(Handle_search_info, "search_statistic", "statistics") 753