def __doStuffModule(moduleId, module_dir, printStyle): pdfgen = _find_pdfgen() if not pdfgen: print >> sys.stderr, "No valid pdfgen script found. Specify one via the command line" return 1 temp_dir = mkdtemp(suffix='-module-xhtml2pdf') cnxml, files = loadModule(module_dir) _, newFiles = module2dbk.convert( moduleId, cnxml, files, {}, temp_dir, svg2png=False, math2svg=True, reduce_quality=False) # Last arg is coll params dbkFile = open(os.path.join(temp_dir, 'index.standalone.dbk')) dbk = etree.parse(dbkFile) allFiles = {} allFiles.update(files) allFiles.update(newFiles) p = util.Progress() stdErr = convert(p, dbk, allFiles, printStyle, temp_dir, '/dev/stdout', pdfgen) return stdErr
def test(self, short=False, max_length=None): self.Parse, self.Weight = [], 0.0 #n = str(len(self.S)) #m = len(n) #o = "%"+str(m)+"d of "+n #i = 0 #print "Parsed", o % i, #sys.stdout.flush() #o = ("\b"*(2*m+5)) + o p = util.Progress('Parsed', 0, len(self.S)) for s in self.S: if max_length is None or len(s) <= max_length: (parse, weight) = self.parse(s) else: (parse, weight) = (None, 0.0) self.Parse += [parse] self.Weight += weight #i += 1 #print o % i, #sys.stdout.flush() p.next() print "\nFinished parsing." self.eval(short=short, max_length=max_length) self.tested = True
def collection2pdf(collection_dir, print_style, output_pdf, pdfgen, temp_dir, verbose=False,reduce_quality=False): p = util.Progress() collxml = etree.parse(os.path.join(collection_dir, 'collection.xml')) moduleIds = MODULES_XPATH(collxml) modules = {} # {'m1000': (etree.Element, {'file.jpg':'23947239874'})} allFiles = {} for moduleId in moduleIds: moduleDir = os.path.join(collection_dir, moduleId) if os.path.isdir(moduleDir): cnxml, files = loadModule(moduleDir) for f in files: allFiles[os.path.join(moduleId, f)] = files[f] modules[moduleId] = (cnxml, files) p.start(1, 'Converting collection to Docbook') dbk, newFiles = collection2dbk.convert(p, collxml, modules, temp_dir, svg2png=False, math2svg=True, reduce_quality=reduce_quality) allFiles.update(newFiles) p.tick('Converting Docbook to PDF') stdErr = convert(p, dbk, allFiles, print_style, temp_dir, output_pdf, pdfgen, verbose) p.finish() return stdErr
def main(): try: import argparse except ImportError: print "argparse is needed for commandline" return 1 parser = argparse.ArgumentParser(description='Converts a a collection directory to an xhtml file and additional images') parser.add_argument('directory') parser.add_argument('-r', dest='reduce_quality', help='Reduce image quality', action='store_true') # parser.add_argument('-t', dest='temp_dir', help='Path to store temporary files to (default is a temp dir that will be removed)', nargs='?') parser.add_argument('-o', dest='output', nargs='?', type=argparse.FileType('w'), default=sys.stdout) args = parser.parse_args() temp_dir = args.directory p = util.Progress() collxml, modules, allFiles = util.loadCollection(args.directory) dbk, newFiles = collection2dbk.convert(p, collxml, modules, temp_dir, svg2png=True, math2svg=True, reduce_quality=args.reduce_quality) allFiles.update(newFiles) dbk, files = convert(dbk, allFiles) args.output.write(etree.tostring(dbk)) # Write out all the added files for name in newFiles: f = open(os.path.join(args.directory, name), 'w') f.write(newFiles[name]) f.close()
def main(): try: import argparse except ImportError: print "argparse is needed for commandline" return 1 parser = argparse.ArgumentParser(description='Converts a module directory to an xhtml file and additional images') parser.add_argument('directory') parser.add_argument('-i', dest='module_id', help='Published Module id') parser.add_argument('-c', dest='css_file', help='CSS File to include')# , type=argparse.FileType('r')) parser.add_argument('-e', dest='epub_script', help='Path to XSL file that generates an epub from a dbk file') parser.add_argument('-r', dest='reduce_quality', help='Reduce image quality', action='store_true') parser.add_argument('-t', dest='content_type', help='The type of content being converted. One of ["module", "collection"]') # parser.add_argument('-t', dest='temp_dir', help='Path to store temporary files to (default is a temp dir that will be removed)', nargs='?') parser.add_argument('-o', dest='output', nargs='?') # , type=argparse.FileType('w'), default=sys.stdout) args = parser.parse_args() temp_dir = args.directory p = util.Progress() if args.content_type == 'module': cnxml, allFiles = util.loadModule(args.directory) dbk, newFiles = module2dbk.convert(args.module_id, cnxml, allFiles, {}, temp_dir, svg2png=True, math2svg=True, reduce_quality=args.reduce_quality) allFiles.update(newFiles) elif args.content_type == 'collection': p = util.Progress() collxml, modulesDict, allFiles = util.loadCollection(args.directory) dbk, newFiles = collection2dbk.convert(p, collxml, modulesDict, temp_dir, svg2png=True, math2svg=True, reduce_quality=args.reduce_quality) allFiles.update(newFiles) else: print "Invalid content type. Must be one of ['module', 'collection']" return 1 nothing = convert(etree.parse(StringIO(dbk)), temp_dir, args.css_file, args.output) # Write out all the added files for name in newFiles: f = open(os.path.join(temp_dir, name), 'w') f.write(newFiles[name]) f.close()
def downloadWithProgress(url, output, msg='Downloading...'): with open(output, 'wb') as handle: response = requests.get(url, stream=True) total = float(response.headers.get('content-length', 1)) sofar = 0 blockSize = 4096 if not response.ok: return False with util.Progress('Download', msg) as p: for block in response.iter_content(blockSize): if p.iscanceled(): raise DownloadCanceledException() sofar += blockSize pct = int((sofar / total) * 100) p.update(pct) handle.write(block)