Some experiments with the language: html, epub, covers.
[librarian.git] / librarian / document.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 from StringIO import StringIO
7 from lxml import etree
8 from . import SSTNS
9 from .core import Section
10 from .parser import SSTParser
11
12
13 class Document(object):
14     # Do I use meta_context?
15     def __init__(self, edoc, meta_context=None):
16         self.edoc = edoc
17
18         root_elem = edoc.getroot()
19         if meta_context is not None:
20             root_elem.meta_context = meta_context
21
22         if not isinstance(root_elem, Section):
23             if root_elem.tag != SSTNS('section'):
24                 raise ValidationError("Invalid root element. Found '%s', should be '%s'" % (
25                     root_elem.tag, SSTNS('section')))
26             else:
27                 raise ValidationError("Invalid class of root element. "
28                     "Use librarian.parser.SSTParser.")
29
30     @classmethod
31     def from_string(cls, xml, *args, **kwargs):
32         return cls.from_file(StringIO(xml), *args, **kwargs)
33
34     @classmethod
35     def from_file(cls, xmlfile, *args, **kwargs):
36         # first, prepare for parsing
37         if isinstance(xmlfile, basestring):
38             file = open(xmlfile, 'rb')
39             try:
40                 data = file.read()
41             finally:
42                 file.close()
43         else:
44             data = xmlfile.read()
45
46         if not isinstance(data, unicode):
47             data = data.decode('utf-8')
48
49         data = data.replace(u'\ufeff', '')
50
51         parser = SSTParser()
52         tree = etree.parse(StringIO(data.encode('utf-8')), parser)
53         tree.xinclude()
54         return cls(tree, *args, **kwargs)
55
56     @property
57     def meta(self):
58         """ Document's metadata is root's metadata. """
59         return self.edoc.getroot().meta