File: martel_support.py

package info (click to toggle)
python-biopython 1.45-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 18,192 kB
  • ctags: 12,310
  • sloc: python: 83,505; xml: 13,834; ansic: 7,015; cpp: 1,855; sql: 1,144; makefile: 179
file content (102 lines) | stat: -rw-r--r-- 3,027 bytes parent folder | download | duplicates (3)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import string, sys, cgi

from xml.sax import handler

class CheckGood(handler.ContentHandler, handler.ErrorHandler):
    def __init__(self):
        handler.ContentHandler.__init__(self)
        self.good_parse = 0
    def startDocument(self):
        self.good_parse = 0
    def endDocument(self):
        self.good_parse = 1


class Storage:
    def __init__(self):
        self.data = []
    def __getitem__(self, want_name):
        for name, exp, s in self.data:
            if name == want_name:
                x = Storage()
                x.add_test(name, exp, s)
                return x
        raise KeyError, want_name
    def add_test(self, name, exp, s):
        self.data.append( (name, exp, s) )

    def add_test_lines(self, name, exp, s):
        if s[-1:] == "\n":
            s = s[:-1]
        lines = string.split(s, "\n")
        for i in range(len(lines)):
            self.add_test(name + " %d" % (i+1), exp, lines[i] + "\n")

    def test(self):
        good = CheckGood()
        for name, exp, s in self.data:
            parser = exp.make_parser()
            parser.setContentHandler(good)
            try:
                parser.parseString(s)
            except KeyboardInterrupt:
                raise
            except:
                pass
            if not good.good_parse:
                print "Cannot parse", name
                print s
                print repr(str(exp))
                print "-" * 70
                raise AssertionError, "cannot parse"
            else:
                print "Good parse", name

    def dump(self):
        h = Dump(sys.stdout)
        for name, exp, s in self.data:
            print "****************************", name, "********************"
            parser = exp.make_parser()
            parser.setContentHandler(h)
            parser.setErrorHandler(h)
            parser.parseString(s)


class Dump(handler.ContentHandler, handler.ErrorHandler):
    def __init__(self, outfile = None):
        handler.ContentHandler.__init__(self)
        if outfile is None:
            outfile = sys.stdout
        self.write = outfile.write
    def startElement(self, name, attrs):
        self.write('<%s>' % name)
    def characters(self, content):
        self.write(cgi.escape(content))
    def endElement(self, name):
        self.write('</%s>' % name)

    def startDocument(self):
        self.write("-------> Start\n")
    def endDocument(self):
        self.write("\n-------> End\n")

    def error(self, exc):
        self.write("\n-------> error " + str(exc) + "\n")
    def fatalError(self, exc):
        self.write("\n-------> fatal error - " + str(exc) + "\n")

def test_file(format, infile):
    h = Dump(sys.stdout)
    
    parser = format.make_parser()
    parser.setContentHandler(h)
    parser.setErrorHandler(h)
    parser.parseFile(infile)

def test_string(format, str):
    h = Dump(sys.stdout)
    
    parser = format.make_parser()
    parser.setContentHandler(h)
    parser.setErrorHandler(h)
    parser.parseString(str)