This commit is contained in:
Kovid Goyal 2008-03-06 21:13:24 +00:00
parent ac64f8029c
commit 9728f54d15
2 changed files with 22 additions and 11 deletions

View File

@ -17,7 +17,7 @@
import sys, os, subprocess, logging import sys, os, subprocess, logging
from libprs500 import isosx, setup_cli_handlers, filename_to_utf8 from libprs500 import isosx, setup_cli_handlers, filename_to_utf8
from libprs500.ebooks import ConversionError from libprs500.ebooks import ConversionError
from libprs500.ptempfile import PersistentTemporaryFile from libprs500.ptempfile import PersistentTemporaryDirectory
from libprs500.ebooks.lrf import option_parser as lrf_option_parser from libprs500.ebooks.lrf import option_parser as lrf_option_parser
from libprs500.ebooks.lrf.html.convert_from import process_file as html_process_file from libprs500.ebooks.lrf.html.convert_from import process_file as html_process_file
@ -29,17 +29,18 @@ if isosx and hasattr(sys, 'frameworks_dir'):
def generate_html(pathtopdf, logger): def generate_html(pathtopdf, logger):
''' '''
Convert the pdf into html. Convert the pdf into html.
@return: A closed PersistentTemporaryFile. @return: Path to a temporary file containing the HTML.
''' '''
if not os.access(pathtopdf, os.R_OK): if not os.access(pathtopdf, os.R_OK):
raise ConversionError, 'Cannot read from ' + pathtopdf raise ConversionError, 'Cannot read from ' + pathtopdf
pf = PersistentTemporaryFile('.html') tdir = PersistentTemporaryDirectory('pdftohtml')
pf.close() index = os.path.join(tdir, 'index.html')
# This is neccessary as pdftohtml doesn't always (linux) respect absolute paths # This is neccessary as pdftohtml doesn't always (linux) respect absolute paths
cmd = PDFTOHTML + ' -enc UTF-8 -noframes -p -nomerge "%s" "%s"'%(pathtopdf, os.path.basename(pf.name)) cmd = PDFTOHTML + ' -enc UTF-8 -noframes -p -nomerge "%s" "%s"'%(pathtopdf, os.path.basename(index))
cwd = os.getcwd() cwd = os.getcwd()
try: try:
os.chdir(os.path.dirname(pf.name)) os.chdir(tdir)
p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE, p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE,
stdout=subprocess.PIPE) stdout=subprocess.PIPE)
logger.info(p.stdout.read()) logger.info(p.stdout.read())
@ -47,11 +48,11 @@ def generate_html(pathtopdf, logger):
if ret != 0: if ret != 0:
err = p.stderr.read() err = p.stderr.read()
raise ConversionError, err raise ConversionError, err
if os.stat(pf.name).st_size < 100: if os.stat(index).st_size < 100:
raise ConversionError(os.path.basename(pathtopdf) + ' does not allow copying of text.') raise ConversionError(os.path.basename(pathtopdf) + ' does not allow copying of text.')
finally: finally:
os.chdir(cwd) os.chdir(cwd)
return pf return index
def option_parser(): def option_parser():
return lrf_option_parser( return lrf_option_parser(
@ -74,7 +75,7 @@ def process_file(path, options, logger=None):
options.pdftohtml = True options.pdftohtml = True
if not options.title: if not options.title:
options.title = filename_to_utf8(os.path.splitext(os.path.basename(options.output))[0]) options.title = filename_to_utf8(os.path.splitext(os.path.basename(options.output))[0])
html_process_file(htmlfile.name, options, logger) html_process_file(htmlfile, options, logger)
def main(args=sys.argv, logger=None): def main(args=sys.argv, logger=None):

View File

@ -16,7 +16,7 @@
Provides platform independent temporary files that persist even after Provides platform independent temporary files that persist even after
being closed. being closed.
""" """
import tempfile, os, atexit import tempfile, os, atexit, shutil
from libprs500 import __version__, __appname__ from libprs500 import __version__, __appname__
@ -63,4 +63,14 @@ def PersistentTemporaryFile(suffix="", prefix="", dir=None):
fd, name = tempfile.mkstemp(suffix, __appname__+"_"+ __version__+"_" + prefix, fd, name = tempfile.mkstemp(suffix, __appname__+"_"+ __version__+"_" + prefix,
dir=dir) dir=dir)
_file = os.fdopen(fd, 'w+b') _file = os.fdopen(fd, 'w+b')
return _TemporaryFileWrapper(_file, name) return _TemporaryFileWrapper(_file, name)
def PersistentTemporaryDirectory(suffix='', prefix='', dir=None):
'''
Return the path to a newly created temporary directory that will
be automatically deleted on application exit.
'''
tdir = tempfile.mkdtemp(suffix, __appname__+"_"+ __version__+"_" +prefix, dir)
atexit.register(shutil.rmtree, tdir, True)
return tdir