File: _stanzaformat.py

package info (click to toggle)
python-biopython 1.42-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 17,584 kB
  • ctags: 12,272
  • sloc: python: 80,461; xml: 13,834; ansic: 7,902; cpp: 1,855; sql: 1,144; makefile: 203
file content (46 lines) | stat: -rw-r--r-- 1,405 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""This module reads and writes (actually, write not implemented yet)
files in the OBF stanza format.

"""
class StanzaFormat:
    """Contains information from a stanza-formatted file."""
    def __init__(self, version, stanzas):
        self.version = version
        self.stanzas = stanzas

class Stanza:
    """Contains information about one stanza in a file."""
    def __init__(self, name, tag_value_pairs):
        self.name = name
        self.tag_value_pairs = tag_value_pairs
        
        dict = {}
        for tag, value in tag_value_pairs:
            dict[tag] = value
        self.tag_value_dict = dict

def load(handle):
    """load(handle) -> StanzaFormat object"""
    import ConfigParser
    parser = ConfigParser.ConfigParser()

    # Read the VERSION string.
    line = handle.readline()
    while line and not line.startswith("VERSION"):
        line = handle.readline()
    assert line, "I could not find the VERSION line"
    x, version = line.split("=")
    version = version.strip()

    try:
        parser.readfp(handle)
    except ConfigParser.Error, x:
        raise SyntaxError, x
    stanzas = []
    for section in parser.sections():
        pairs = []
        for tag in parser.options(section):
            value = parser.get(section, tag)
            pairs.append((tag, value))
        stanzas.append(Stanza(section, pairs))
    return StanzaFormat(version, stanzas)