def handle(self, *args, **options): if len(args) < 1: raise CommandError('No product numbers specified') output_dir = options['output_dir'] verbosity = int(options['verbosity']) products_exported = 0 for product in args: if verbosity >= 1: self.stdout.write('Exporting text only DTB for %s...\n' % product) try: document = Document.objects.get(product__identifier=product) except Document.DoesNotExist: self.stderr.write('Product %s does not exist\n' % product) continue inputFile = document.latest_version().content.path outputDir = tempfile.mkdtemp(prefix="daisyproducer-") transformationErrors = DaisyPipeline.dtbook2text_only_dtb(inputFile, outputDir) if transformationErrors: print transformationErrors zipFile = join(output_dir, product + ".zip") zipDirectory(outputDir, zipFile, document.title) shutil.rmtree(outputDir) products_exported += 1 if verbosity >= 1: self.stdout.write("%s products exported\n" % products_exported)
def as_dtb(request, document_id): form = DTBForm(request.POST) if not form.is_valid(): return HttpResponseRedirect(reverse('browse_detail', args=[document_id])) document = Document.objects.get(pk=document_id) inputFile = document.latest_version().content.path outputDir = tempfile.mkdtemp(prefix="daisyproducer-") DaisyPipeline.dtbook2dtb(inputFile, outputDir, **form.cleaned_data) zipFile = tempfile.NamedTemporaryFile(suffix='.zip', prefix=document_id, delete=False) zipFile.close() # we are only interested in a unique filename zipDirectory(outputDir, zipFile.name, document.title) shutil.rmtree(outputDir) return render_to_mimetype_response('application/zip', document.title.encode('utf-8'), zipFile.name)
def handle(self, *args, **options): outputPath = options['outputPath'] verbosity = int(options['verbosity']) fileNameMapping = options['fileNameMapping'] mapping ={} if fileNameMapping: reader = csv.reader(open(fileNameMapping)) for line in reader: source, dest = line[:2] mapping[source] = dest conversions = 0 for dtbook in options['dtbook_files']: if verbosity >= 1: self.stdout.write('Converting %s...\n' % dtbook) outputDir = tempfile.mkdtemp(prefix="daisyproducer-") result = DaisyPipeline.dtbook2text_only_dtb( dtbook, outputDir, # we assume that there are no images images=[]) if result: for error in result: self.stderr.write(error + "\n") continue if outputPath == None: outputPath = os.path.dirname(dtbook) fileName, ext = os.path.splitext(os.path.basename(dtbook)) if fileNameMapping: fileName = mapping[fileName] if verbosity >= 2: self.stdout.write('Mapping %s to %s\n' % (dtbook, fileName)) self.stdout.flush() zipFileName = os.path.join(outputPath, fileName + ".zip") zipDirectory(outputDir, zipFileName, fileName) shutil.rmtree(outputDir) conversions += 1 if verbosity >= 1: self.stdout.write('Converted %s %s of %s\n' % (conversions, "file" if conversions == 1 else "files", len(args)))
def preview_dtb(request, document_id): document = get_object_or_404(Document, pk=document_id) if request.method == 'POST': form = DTBForm(request.POST) if form.is_valid(): inputFile = document.latest_version().content.path outputDir = tempfile.mkdtemp(prefix="daisyproducer-") DaisyPipeline.dtbook2dtb(inputFile, outputDir, **form.cleaned_data) zipFile = tempfile.NamedTemporaryFile(suffix='.zip', prefix=document_id, delete=False) zipFile.close() # we are only interested in a unique filename zipDirectory(outputDir, zipFile.name, document.title) shutil.rmtree(outputDir) return render_to_mimetype_response('application/zip', document.title.encode('utf-8'), zipFile.name) else: form = DTBForm() return render(request, 'documents/todo_dtb.html', locals())
def preview_text_only_dtb(request, document_id): document = get_object_or_404(Document, pk=document_id) if request.method == 'POST': form = TextOnlyDTBForm(request.POST) if form.is_valid(): inputFile = document.latest_version().content.path outputDir = tempfile.mkdtemp(prefix="daisyproducer-") ebookNumber = form.cleaned_data.pop('ebookNumber') transformationErrors = DaisyPipeline.dtbook2text_only_dtb( inputFile, outputDir, images=document.image_set.all(), **form.cleaned_data) if transformationErrors: return render_to_response('documents/todo_text_only_dtb.html', locals(), context_instance=RequestContext(request)) zipFile = tempfile.NamedTemporaryFile(suffix='.zip', prefix=document_id, delete=False) zipFile.close() # we are only interested in a unique filename zipDirectory(outputDir, zipFile.name, ebookNumber) shutil.rmtree(outputDir) # put a copy of the ebook to a shared folder where it is fetched by another process that # puts into the distribution system. This will change, as the distribution system should # fetch the ebook directly from the archive. See fhs for a rationale about the dest # folder (http://www.pathname.com/fhs/pub/fhs-2.3.html#VARSPOOLAPPLICATIONSPOOLDATA) shutil.copy2(zipFile.name, os.path.join('/var/spool/daisyproducer', ebookNumber + '.zip')) return render_to_mimetype_response('application/zip', document.title.encode('utf-8'), zipFile.name) else: ebook = Product.objects.filter(document=document, type=2) if ebook: form = TextOnlyDTBForm({'ebookNumber': ebook[0].identifier}) else: form = TextOnlyDTBForm() return render_to_response('documents/todo_text_only_dtb.html', locals(), context_instance=RequestContext(request))