X-Git-Url: https://git.mdrn.pl/librarian.git/blobdiff_plain/5938bc78f11ebad067eae86f51b4f5243003f946..4c03e8c6d362890f31ea8227e5ab5e0cb3f4df30:/librarian/__init__.py diff --git a/librarian/__init__.py b/librarian/__init__.py index 8977c30..619650d 100644 --- a/librarian/__init__.py +++ b/librarian/__init__.py @@ -3,8 +3,16 @@ # This file is part of Librarian, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # +import os + class ParseError(Exception): - pass + def __str__(self): + """ Dirty workaround for Python Unicode handling problems. """ + return self.message.message + + def __unicode__(self): + """ Dirty workaround for Python Unicode handling problems. """ + return self.message.message class ValidationError(Exception): pass @@ -42,9 +50,38 @@ RDFNS = XMLNamespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#') DCNS = XMLNamespace('http://purl.org/dc/elements/1.1/') XINS = XMLNamespace("http://www.w3.org/2001/XInclude") XHTMLNS = XMLNamespace("http://www.w3.org/1999/xhtml") +NCXNS = XMLNamespace("http://www.daisy.org/z3986/2005/ncx/") +OPFNS = XMLNamespace("http://www.idpf.org/2007/opf") WLNS = EmptyNamespace() + +class DocProvider(object): + """ Base class for a repository of XML files. + Used for generating joined files, like EPUBs + """ + + def by_slug(self, slug): + raise NotImplemented + + def __getitem__(self, slug): + return self.by_slug(slug) + + def by_uri(self, uri): + return self.by_slug(uri.rsplit('/', 1)[1]) + + +class DirDocProvider(DocProvider): + """ Serve docs from a directory of files in form .xml """ + + def __init__(self, dir): + self.dir = dir + self.files = {} + + def by_slug(self, slug): + return open(os.path.join(self.dir, '%s.xml' % slug)) + + import lxml.etree as etree import dcparser @@ -56,6 +93,7 @@ DEFAULT_BOOKINFO = dcparser.BookInfo( DCNS('subject.type'): [u'Unknown'], DCNS('subject.genre'): [u'Unknown'], DCNS('date'): ['1970-01-01'], + DCNS('language'): [u'pol'], # DCNS('date'): [creation_date], DCNS('publisher'): [u"Fundacja Nowoczesna Polska"], DCNS('description'): @@ -99,3 +137,7 @@ SERIALIZERS = { def serialize_children(element, format='raw'): return SERIALIZERS[format](element) + +def get_resource(path): + return os.path.join(os.path.dirname(__file__), path) +