import repository from arizona
[raven.git] / apps / importyum / yumflparse.py
1 from xml.parsers.xmlproc import xmlproc,xmlapp
2
3 def UTOA(x):
4     """ un-unicode a string """
5     return x.encode("utf8")
6
7 class YumFileListsApp(xmlproc.Application):
8     """This class parses the groups.xml file."""
9     def __init__(self):
10         #
11         # Do this the old way (instead of using super) because xmlproc is written using
12         # old-style classes that don't work with super.
13         #
14         xmlproc.Application.__init__(self)
15         self.curtag = "root"
16         self.loc = None
17         self.packages = {}
18
19     def set_locator(self, locator):
20         self.loc = locator
21
22     def doc_start(self):
23         self.group = None
24
25     def handle_start_tag(self, tag, attrs):
26         line = self.loc.get_line()
27         file = self.loc.get_current_sysid()
28         self.curtag = self.curtag + "." + tag
29         if self.curtag == "root.filelists.package":
30             self.package = {}
31             self.package["pkgid"] = UTOA(attrs.get("pkgid"))
32             self.package["name"] = UTOA(attrs.get("name"))
33             self.package["files"] = []
34         else:
35             pass
36
37     def handle_data(self, data, start, end):
38         data = data[start:end]
39         if self.curtag == "root.filelists.package.file":
40             self.package["files"].append(UTOA(data))
41         else:
42             pass
43
44     def handle_end_tag(self,tag):
45         if self.curtag == "root.filelists.package":
46             #print "package:", self.package
47             self.packages[self.package["pkgid"]] = self.package
48         else:
49             pass
50         self.curtag = ".".join(self.curtag.split(".")[:-1])
51
52
53 def YumFileListsParse(filename):
54     app = YumFileListsApp()
55     parser = xmlproc.XMLProcessor()
56     parser.set_application(app)
57     parser.parse_resource(filename)
58
59     return app.packages