Package Bio :: Package config :: Module _stanzaformat
[hide private]
[frames] | no frames]

Source Code for Module Bio.config._stanzaformat

 1  """This module reads and writes (actually, write not implemented yet) 
 2  files in the OBF stanza format. 
 3   
 4  """ 
5 -class StanzaFormat:
6 """Contains information from a stanza-formatted file."""
7 - def __init__(self, version, stanzas):
8 self.version = version 9 self.stanzas = stanzas
10
11 -class Stanza:
12 """Contains information about one stanza in a file."""
13 - def __init__(self, name, tag_value_pairs):
14 self.name = name 15 self.tag_value_pairs = tag_value_pairs 16 17 dict = {} 18 for tag, value in tag_value_pairs: 19 dict[tag] = value 20 self.tag_value_dict = dict
21
22 -def load(handle):
23 """load(handle) -> StanzaFormat object""" 24 import ConfigParser 25 parser = ConfigParser.ConfigParser() 26 27 # Read the VERSION string. 28 line = handle.readline() 29 while line and not line.startswith("VERSION"): 30 line = handle.readline() 31 assert line, "I could not find the VERSION line" 32 x, version = line.split("=") 33 version = version.strip() 34 35 try: 36 parser.readfp(handle) 37 except ConfigParser.Error, x: 38 raise SyntaxError, x 39 stanzas = [] 40 for section in parser.sections(): 41 pairs = [] 42 for tag in parser.options(section): 43 value = parser.get(section, tag) 44 pairs.append((tag, value)) 45 stanzas.append(Stanza(section, pairs)) 46 return StanzaFormat(version, stanzas)
47