File: indexhandler.py

package info (click to toggle)
chemical-structures 2.2.dfsg.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,808 kB
  • sloc: xml: 7,224; javascript: 1,194; python: 993; sh: 51; makefile: 42
file content (193 lines) | stat: -rwxr-xr-x 5,682 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""Index file handler for Python.

Exported classes:
    Entry - a entry class.
    IndexHandler - Index file handler.
"""

import xml.sax.handler

class Entry:
    """Class for common index entry.

    It contains each feature of an entry element.
    """
    def __init__(self):
        """Creates an instance of the Entry class.

        Set the object attributes with default values.
        """
        self.id = ""
        self.name = {}
        self.path = ""
        self.synDict = {}
        self.abbreviation = []
        self.authors = ""
        self.date = ""

class IndexHandler(xml.sax.handler.ContentHandler):
    """Class for receiving logical Index content events.

    It supports Index entities as defined in the project's DTD.
    For more details, see xml/index.dtd
    """
    def __init__(self):
        """Creates an instance of the IndexHandler class.

        Set the object attributes with default values.
        """
        self.title = {}
        self.titleLang = ""
        self.inTitle = False
        self.entry = None 
        self.entryList = {'dir': [], 'file': []}
        self.inEntry = False
        self.nameLang = ""
        self.inName = False
        self.inDirname = False
        self.inFilename = False
        self.type = None
        self.synonym = ""
        self.synLang = ""
        self.inSynonym = False
        self.abbreviation = ""
        self.inAbbreviation = False
        self.inAuthors = False
        self.inDate = False

    def startElement(self, name, attributes):
        """Signals the start of an element.

        The function set a variable depending on the element and the attribut.

        Parameters:
            name - contains the element name as a string.
            attributes -  contains an instance of the Attributes class.
        """
        if name == "title":
            self.inTitle = True
            if "xml:lang" in attributes:
                self.titleLang = attributes["xml:lang"]
                if self.titleLang == "":
                    self.titleLang = "en"
            else:
                self.titleLang = "en"
            self.title[self.titleLang] = ""

        if name == "entry":
            self.inEntry = True
            self.entry = Entry()

        if name == "name":
            self.inName = True
            if "xml:lang" in attributes:
                self.nameLang = attributes["xml:lang"]
                if self.nameLang == "":
                    self.nameLang = "en"
            else:
                self.nameLang = "en"
            self.entry.name[self.nameLang] = ""

        if name == "dirname":
            self.inDirname = True
            self.type = "dir"

        if name == "filename":
            self.inFilename = True
            self.type = "file"

        if name == "synonym":
            self.inSynonym = True
            if "xml:lang" in attributes:
                self.synLang = attributes["xml:lang"]
                if self.synLang == "":
                    self.synLang = "en"
            else:
                self.synLang ="en"
            if not self.synLang in self.entry.synDict:
                self.entry.synDict[self.synLang] = []

        if name == "abbrev":
            self.inAbbreviation = True

        if name == "authors":
            self.inAuthors = True

        if name == "date":
            self.inDate = True

    def characters(self, data):
        """Receives notification of character data.

        The parser will call this method to report each chunk of character
        data.

        Parameters:
            data - contains the chunk of character data.
        """
        if self.inTitle:
            self.title[self.titleLang] += data
        elif self.inName:
            self.entry.name[self.nameLang] += data
        elif self.inDirname:
            self.entry.path += data
        elif self.inFilename:
            self.entry.path += data
        elif self.inSynonym:
            self.synonym += data
        elif self.inAbbreviation:
            self.abbreviation += data
        elif self.inAuthors:
            self.entry.authors += data
        elif self.inDate:
            self.entry.date += data

    def endElement(self,name):
        """Signals the end of an element.

        Parameters:
            name - contains the name of the element type.
        """
        if name == "title":
            self.inTitle = False
            self.titleLang = ""

        elif name == "entry":
            if self.type == "file":
                self.entryList["file"].append(self.entry)
            else:
                self.entryList["dir"].append(self.entry)
            self.entry = None
            self.inEntry = False 

        elif name == "name":
            self.inName = False
            self.nameLang = ""

        elif name == "dirname":
            self.inDirname = False

        elif name == "filename":
            self.inFilename = False

        elif name == "synonym":
            try:
                self.entry.synDict[self.synLang].append(self.synonym)
            except:
                sys.stderr.write("[ERROR] Could not append the " \
                               + "synonym to " % (self.synonym) \
                               + "the %s dictionary.\n" % (self.synLang))
            self.synonym = ""
            self.inSynonym = False
            self.synLang = ""

        elif name == "abbrev":
            self.inAbbreviation = False
            self.entry.abbreviation.append(self.abbreviation)
            self.abbreviation = ""

        elif name == "authors":
            self.inAuthors = False

        elif name == "date":
            self.inDate = False