summaryrefslogtreecommitdiff
path: root/cad/src/experimental/xml-mmp/MmpFile.py
blob: 5530ef260b3a0e770c2649442eb805306f565356 (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/python

# Copyright 2006-2007 Nanorex, Inc.  See LICENSE file for details.
"""This class should represent an MMP file as an abstract data object.
We can read it in from a conventional *.mmp text file, and if we wish,
write it out in some kind of XML format.

XML has many flaws, but it's got some big benefits. It's popular and
there are parsers and generators in every language. XML files are
unambiguous; once you have a DTD or schema their validity is never in
question. We might be smart to define a DTD or schema for the XML
version of the MMP file, and keep it under CVS control.
"""

import types

DEBUG = False

RecordTypes = ( )

class Mismatch(Exception):
    pass

class Record:

    def __init__(self):
        self.children = [ ]

    def __repr__(self, depth=0):
        r = self.__class__.__name__
        if hasattr(self, "name"):
            r += " " + self.name
        for c in self.children:
            r += "\n" + ((depth + 1) * "    ") + c.__repr__(depth+1)
        return r

    def matches(self, line, pattern):
        if not line.startswith(pattern):
            raise Mismatch, pattern + ":::" + line
        return line[len(pattern):]

    def processFirstLine(self, line):
        pass

    def read(self, lines, depth=0):
        lines = self.readFirstLine(lines, depth)
        if DEBUG:
            print (depth * "    ") + self.keyword
        if hasattr(self, "bairns"):
            return self.readRemainingLines(lines, depth)
        else:
            return lines

    def readFirstLine(self, lines, depth):
        #if DEBUG: print self, "FIRST LINE", lines[0][:-1]
        if len(lines) < 1:
            raise Mismatch, "no lines left"
        firstline = lines[0]
        firstline = self.matches(firstline,
                                 self.keyword + " ")
        self.processFirstLine(firstline)
        return lines[1:]

    def readRemainingLines(self, lines, depth):
        while lines:
            #if DEBUG: print self, "NEXT LINE", lines[0][:-1]
            if hasattr(self, "endline"):
                if lines[0].startswith(getattr(self, "endline")):
                    return lines[1:]
            found = False
            for rtype in self.bairns:
                try:
                    newRecord = rtype()
                    lines = newRecord.read(lines, depth+1)
                    self.children.append(newRecord)
                    found = True
                    break
                except Mismatch:
                    pass
            if not found:
                if depth == 0:
                    print "?", lines[0]
                    lines = lines[1:]
                else:
                    return lines

class TemperatureRecord(Record):
    keyword = "kelvin"
    pass

class GroupRecord(Record):
    keyword = "group"
    def processFirstLine(self, line):
        self.name = line.strip()
    pass

class InformationRecord(Record):
    keyword = "info"
    pass

class CoordinateSystemRecord(Record):
    keyword = "csys"
    pass

class MoleculeRecord(Record):
    keyword = "mol"
    def processFirstLine(self, line):
        self.name = line.strip()
    pass

class AtomRecord(Record):
    keyword = "atom"
    pass

class Bond1Record(Record):
    keyword = "bond1"
    pass


class MmpFile(Record):

    keyword = "mmpformat"


MmpFile.bairns = (TemperatureRecord,
                  GroupRecord)
MmpFile.endline = "end molecular machine part"
GroupRecord.bairns = (InformationRecord,
                      CoordinateSystemRecord,
                      MoleculeRecord)
GroupRecord.endline = "egroup"
MoleculeRecord.bairns = (AtomRecord,)
AtomRecord.bairns = (Bond1Record,)


examples = [
    "../../../partlib/bearings/BigBearing.mmp",
    "../../../partlib/bearings/BuckyBearing.mmp",
    "../../../partlib/bearings/HCBearing.mmp",
    "../../../partlib/bearings/SmallBearing.mmp",
    "../../../partlib/bearings/ThrustBearing.mmp"
    ]

for e in examples:
    print "#############"
    print e
    lines = open(e).readlines()
    m = MmpFile()
    m.read(lines)
    print m