Moved slughifi to lib folder.
[wolnelektury.git] / catalogue / lib / dcparser / dcparser.py
1 # -*- coding: utf-8 -*-
2 from xml.parsers.expat import ExpatError
3
4 # Import ElementTree from anywhere
5 try:
6     import xml.etree.ElementTree as ET # Python >= 2.5
7 except ImportError:
8     try:
9         import elementtree.ElementTree as ET # effbot's pure Python module
10     except ImportError:
11         import lxml.etree as ET # ElementTree API using libxml2
12
13 import converters
14
15
16
17 __all__ = ('parse', 'ParseError')
18
19
20
21 class ParseError(Exception):
22     def __init__(self, message):
23         super(self, Exception).__init__(message)
24
25
26
27 class XMLNamespace(object):
28     '''Represents XML namespace.'''
29     
30     def __init__(self, uri):
31         self.uri = uri
32
33     def __call__(self, tag):
34         return '{%s}%s' % (self.uri, tag)
35
36     def __contains__(self, tag):
37         return tag.startswith(str(self))
38
39     def __repr__(self):
40         return 'NS(%r)' % self.uri
41     
42     def __str__(self):
43         return '%s' % self.uri
44
45
46
47 class BookInfo(object):
48     RDF = XMLNamespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
49     DC = XMLNamespace('http://purl.org/dc/elements/1.1/')
50     
51     mapping = {
52         DC('creator')        : ('author', converters.str_to_person),
53         DC('title')          : ('title', converters.str_to_unicode),
54         DC('subject.period') : ('epoch', converters.str_to_unicode),
55         DC('subject.type')   : ('kind', converters.str_to_unicode),
56         DC('subject.genre')  : ('genre', converters.str_to_unicode),
57         DC('date')           : ('created_at', converters.str_to_date),
58         DC('date.pd')        : ('released_to_public_domain_at', converters.str_to_date),
59         DC('contributor.translator') : ('translator', converters.str_to_person),
60         DC('contributor.technical_editor') : ('technical_editor', converters.str_to_person),
61         DC('publisher')      : ('publisher', converters.str_to_unicode),
62         DC('source')         : ('source_name', converters.str_to_unicode),
63         DC('source.URL')     : ('source_url', converters.str_to_unicode),
64     }
65
66     
67     @classmethod
68     def from_string(cls, xml):
69         """docstring for from_string"""
70         from StringIO import StringIO
71         return cls.from_file(StringIO(xml))
72
73     
74     @classmethod
75     def from_file(cls, xml_file):
76         book_info = cls()
77         
78         try:
79             tree = ET.parse(xml_file)
80         except ExpatError, e:
81             raise ParseError(e)
82
83         description = tree.find('//' + book_info.RDF('Description'))
84         if description is None:
85             raise ParseError('no Description tag found in document')
86         
87         for element in description.findall('*'):
88             book_info.parse_element(element) 
89         
90         return book_info
91
92         
93     def parse_element(self, element):
94         try:
95             attribute, converter = self.mapping[element.tag]
96             setattr(self, attribute, converter(element.text))
97         except KeyError:
98             pass
99
100
101     def to_xml(self):
102         """XML representation of this object."""
103         ET._namespace_map[str(self.RDF)] = 'rdf'
104         ET._namespace_map[str(self.DC)] = 'dc'
105         
106         root = ET.Element(self.RDF('RDF'))
107         description = ET.SubElement(root, self.RDF('Description'))
108         
109         for tag, (attribute, converter) in self.mapping.iteritems():
110             if hasattr(self, attribute):
111                 e = ET.Element(tag)
112                 e.text = unicode(getattr(self, attribute))
113                 description.append(e)
114         
115         return unicode(ET.tostring(root, 'utf-8'), 'utf-8')
116
117
118 def parse(file_name):
119     return BookInfo.from_file(file_name)
120
121