#969: generate txt files during publication
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Mon, 17 Jan 2011 16:22:53 +0000 (17:22 +0100)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Mon, 17 Jan 2011 16:22:53 +0000 (17:22 +0100)
apps/catalogue/management/commands/importbooks.py
apps/catalogue/models.py
lib/librarian
wolnelektury/settings.py

index 3daca8a..71a7eb2 100644 (file)
@@ -22,6 +22,8 @@ class Command(BaseCommand):
             help='Print status messages to stdout'),
         make_option('-E', '--no-build-epub', action='store_false', dest='build_epub', default=True,
             help='Don\'t build EPUB file'),
+        make_option('-T', '--no-build-txt', action='store_false', dest='build_txt', default=True,
+            help='Don\'t build TXT file'),
         make_option('-w', '--wait-until', dest='wait_until', metavar='TIME',
             help='Wait until specified time (Y-M-D h:m:s)'),
     )
@@ -76,7 +78,9 @@ class Command(BaseCommand):
 
                     # Import book files
                     try:
-                        book = Book.from_xml_file(file_path, overwrite=force, build_epub=options.get('build_epub'))
+                        book = Book.from_xml_file(file_path, overwrite=force, 
+                                                  build_epub=options.get('build_epub'),
+                                                  build_txt=options.get('build_txt'))
                         files_imported += 1
 
                         if os.path.isfile(file_base + '.pdf'):
index 7e3ccfe..9c943ca 100644 (file)
@@ -453,8 +453,7 @@ class Book(models.Model):
         epub_file = StringIO()
         try:
             epub.transform(BookImportDocProvider(self), self.slug, output_file=epub_file)
-            self.epub_file.save('%s.epub' % self.slug, ContentFile(epub_file.getvalue()), save=False)
-            self.save()
+            self.epub_file.save('%s.epub' % self.slug, ContentFile(epub_file.getvalue()))
             FileRecord(slug=self.slug, type='epub', sha1=sha1(epub_file.getvalue()).hexdigest()).save()
         except NoDublinCore:
             pass
@@ -468,6 +467,16 @@ class Book(models.Model):
             child_book.save()
             book_descendants += list(child_book.children.all())
 
+    def build_txt(self):
+        from StringIO import StringIO
+        from django.core.files.base import ContentFile
+        from librarian import text
+
+        out = StringIO()
+        text.transform(open(self.xml_file.path), out)
+        self.txt_file.save('%s.txt' % self.slug, ContentFile(out.getvalue()))
+        self.save()
+
 
     @classmethod
     def from_xml_file(cls, xml_file, overwrite=False, build_epub=True):
@@ -483,7 +492,7 @@ class Book(models.Model):
             xml_file.close()
 
     @classmethod
-    def from_text_and_meta(cls, raw_file, book_info, overwrite=False, build_epub=True):
+    def from_text_and_meta(cls, raw_file, book_info, overwrite=False, build_epub=True, build_txt=True):
         from tempfile import NamedTemporaryFile
         from slughifi import slughifi
         from markupstring import MarkupString
@@ -592,6 +601,9 @@ class Book(models.Model):
                 new_fragment.save()
                 new_fragment.tags = set(book_tags + themes + [book_tag] + ancestor_tags)
 
+        if not settings.NO_BUILD_TXT and build_txt:
+            book.build_txt()
+
         if not settings.NO_BUILD_EPUB and build_epub:
             book.root_ancestor.build_epub()
 
index 5329a22..f0c23b2 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 5329a22cd6643da657dd24546b382ada9e048b68
+Subproject commit f0c23b2e3a1a1d7f37d9f00118632d116b3c8582
index 186f4b6..a50763d 100644 (file)
@@ -199,6 +199,7 @@ TRANSLATION_REGISTRY = "wolnelektury.translation"
 MAX_TAG_LIST = 6
 
 NO_BUILD_EPUB = False
+NO_BUILD_TXT = False
 
 # Load localsettings, if they exist
 try: