some cleanup
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Fri, 20 Aug 2010 12:32:04 +0000 (14:32 +0200)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Fri, 20 Aug 2010 12:32:04 +0000 (14:32 +0200)
librarian/__init__.py
librarian/epub.py

index 8977c30..0074ee9 100644 (file)
@@ -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 <slug>.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
 
index cdb5aae..c1e3f4e 100644 (file)
@@ -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 <input file>'