def transform(self, data, options=None): """Returns the transform result.""" if self._validate(data) is None: return None commandline = "%s %s" % (self.command, self.args) if not self.use_stdin: tmpname = self.initialize_tmpfile(data) commandline = commandline % { 'infile' : tmpname } child_stdin, child_stdout, child_stderr = os.popen3(commandline, 'b') if self.use_stdin: self.write(child_stdin, data) status = child_stdin.close() out = self.extract_output(child_stdout) child_stdout.close() if not self.use_stdin: os.unlink(tmpname) # Add the errors to the transform result result = TransformResult(out) result.errors = child_stderr.read() child_stderr.close() return result
def prepare_transform(self, data, arguments=None): """ The method takes some data in one of the input formats and returns a TransformResult with data in the output format. """ if arguments is not None: infile_data_suffix = arguments.get('infile_data_suffix', False) del arguments['infile_data_suffix'] result = TransformResult(None) tmpfilepath = None try: tmpdirpath = tempfile.mkdtemp() tmpfilepath = self.initialize_tmpfile(data, directory=tmpdirpath) if infile_data_suffix: primaryname = os.path.basename(tmpfilepath) + infile_data_suffix commandline = 'cd "%s" && %s %s' % ( tmpdirpath, self.command, self.args) arguments['infile'] = tmpfilepath commandline = commandline % arguments if os.name=='posix': # TODO: tbenita suggests to remove 1>/dev/null as some commands # return result in flow. Maybe turn this into another subobject # commandline = commandline + ' 2>error_log' commandline = commandline + ' 2>error_log 1>/dev/null' os.system(commandline) for tmpfile in os.listdir(tmpdirpath): tmp = os.path.join(tmpdirpath, tmpfile) # Exclude the original file and the error_log from the result if tmp == tmpfilepath: continue fd = file(tmp, 'rb') # Should we use the infile as the primary output? if infile_data_suffix and primaryname == tmpfile: result.data = StringIO() result.data.writelines(fd) result.data.seek(0) elif tmp.endswith('error_log'): result.errors = fd.read() else: sub = cStringIO() sub.writelines(fd) sub.seek(0) result.subobjects[tmpfile] = sub fd.close() os.unlink(tmp) finally: if tmpfilepath is not None and os.path.isdir(tmpdirpath): shutil.rmtree(tmpdirpath) return result
def transform(self, data, options=None): if self._validate(data) is None: return None if self.format is None: return None width = self.width height = self.height # Allow to override the size settings via the options dict if options is not None: width = options.get("width", width) height = options.get("height", height) result = TransformResult(None) try: # If we already got a file-like iterator use it if isinstance(data, file): orig = data else: orig = StringIO() orig.writelines(data) orig.seek(0) try: pil_image = Image.open(orig) except IOError, e: result.errors = str(e) log_debug("Error %s while transforming an Image in %s." % (str(e), self.name)) return result transparency = pil_image.info.get("transparency", False) if self.format in ["jpeg", "ppm"]: pil_image.draft("RGB", pil_image.size) pil_image = pil_image.convert("RGB") if width is None: width = pil_image.size[0] if height is None: height = pil_image.size[1] if width and height: pil_image.thumbnail((width, height), Image.ANTIALIAS) transformed = StringIO() # Only use transparency in the supported modes if self.format == "png" and pil_image.mode not in ("P", "L"): pil_image.save(transformed, self.format) else: pil_image.save(transformed, self.format, transparency=transparency) transformed.seek(0)
def transform(self, data, options={}): if self._validate(data) is None: return None # parse as HTML and serialize as XHTML body = "".join(data).encode('utf-8') doc = lxml.html.fromstring('<html name="Section-%s">%s</html>' % (hashlib.sha1(body).hexdigest(), body)) lxml.html.html_to_xhtml(doc) body = lxml.html.tostring(doc) # reparse as valid XML, then apply transform doc = lxml.etree.fromstring(body) result_tree = self.xslt_transform(doc) # apply styles (optional) for style in options.get('styles', ()): tag, name = style.split(':') for element in result_tree.xpath( ".//text:%s" % tag, namespaces={ 'text': "urn:oasis:names:tc:opendocument:xmlns:text:1.0" }): key = "{%s}style-name" % \ "urn:oasis:names:tc:opendocument:xmlns:text:1.0" if key not in element.attrib: element.attrib[key] = name data = unicode(result_tree) # strip XML declaration since this transform deals with # fragments. data = data.lstrip('<?xml version="1.0"?>') return TransformResult(StringIter(data))