From ae0e673a17c3edcdca910fafb84eeff9dfe7b588 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Fri, 20 Aug 2010 14:32:04 +0200 Subject: [PATCH] some cleanup --- librarian/__init__.py | 31 +++++++++++++++++++++++++++++++ librarian/epub.py | 33 +++++---------------------------- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/librarian/__init__.py b/librarian/__init__.py index 8977c30..0074ee9 100644 --- a/librarian/__init__.py +++ b/librarian/__init__.py @@ -3,6 +3,8 @@ # 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 @@ -42,9 +44,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 diff --git a/librarian/epub.py b/librarian/epub.py index cdb5aae..c1e3f4e 100644 --- a/librarian/epub.py +++ b/librarian/epub.py @@ -11,34 +11,11 @@ from copy import deepcopy from lxml import etree import zipfile -from librarian import XMLNamespace, RDFNS, DCNS, WLNS, NoDublinCore -from librarian.dcparser import BookInfo - -NCXNS = XMLNamespace("http://www.daisy.org/z3986/2005/ncx/") -OPFNS = XMLNamespace("http://www.idpf.org/2007/opf") - - -class DocProvider(object): - class DoesNotExist(Exception): - pass - - def by_slug(self, slug): - raise NotImplemented - - def __getitem__(self, slug): - return self.by_slug(slug) +import sys +sys.path.append('..') # for running from working copy - def by_uri(self, uri): - return self.by_slug(uri.rsplit('/', 1)[1]) - - -class DirDocProvider(DocProvider): - def __init__(self, dir): - self.dir = dir - self.files = {} - - def by_slug(self, slug): - return open(os.path.join(self.dir, '%s.xml' % slug)) +from librarian import XMLNamespace, RDFNS, DCNS, WLNS, NCXNS, OPFNS, NoDublinCore +from librarian.dcparser import BookInfo def inner_xml(node): @@ -410,7 +387,7 @@ def transform(provider, slug, output_file=None, output_dir=None): if __name__ == '__main__': - import sys + from librarian import DirDocProvider if len(sys.argv) < 2: print >> sys.stderr, 'Usage: python epub.py ' -- 2.20.1