def importDocument(self, content=None, at=None, format=None, anchor='as-char', wrapInPara=True, size=None, sizeUnit='cm', style=None): '''If p_at is not None, it represents a path or url allowing to find the document. If p_at is None, the content of the document is supposed to be in binary format in p_content. The document p_format may be: odt or any format in imageFormats. p_anchor, p_wrapInPara and p_size are only relevant for images: * p_anchor defines the way the image is anchored into the document; Valid values are 'page','paragraph', 'char' and 'as-char'; * p_wrapInPara, if true, wraps the resulting 'image' tag into a 'p' tag; * p_size, if specified, is a tuple of float or integers (width, height) expressing size in p_sizeUnit (see below). If not specified, size will be computed from image info. * p_sizeUnit is the unit for p_size elements, it can be "cm" (centimeters) or "px" (pixels). * If p_style is given, it is the content of a "style" attribute, containing CSS attributes. If "width" and "heigth" attributes are found there, they will override p_size and p_sizeUnit. ''' importer = None # Is there someting to import? if not content and not at: raise PodError(DOC_NOT_SPECIFIED) # Guess document format if isinstance(content, FileWrapper): format = content.mimeType elif hasattr(content, 'filename') and content.filename: format = os.path.splitext(content.filename)[1][1:] content = content.data if not format: # It should be deduced from p_at if not at: raise PodError(DOC_FORMAT_ERROR) format = os.path.splitext(at)[1][1:] else: # If format is a mimeType, convert it to an extension if mimeTypesExts.has_key(format): format = mimeTypesExts[format] isImage = False if format in self.ooFormats: importer = OdtImporter self.forceOoCall = True elif (format in self.imageFormats) or not format: # If the format can't be guessed, we suppose it is an image. importer = ImageImporter isImage = True elif format == 'pdf': importer = PdfImporter else: raise PodError(DOC_WRONG_FORMAT % format) imp = importer(content, at, format, self) # Initialise image-specific parameters if isImage: imp.setImageInfo(anchor, wrapInPara, size, sizeUnit, style) res = imp.run() return res
def importDocument(self, content=None, at=None, format=None, anchor='as-char', wrapInPara=True, size=None): '''If p_at is not None, it represents a path or url allowing to find the document. If p_at is None, the content of the document is supposed to be in binary format in p_content. The document p_format may be: odt or any format in imageFormats. p_anchor, p_wrapInPara and p_size are only relevant for images: * p_anchor defines the way the image is anchored into the document; Valid values are 'page','paragraph', 'char' and 'as-char'; * p_wrapInPara, if true, wraps the resulting 'image' tag into a 'p' tag; * p_size, if specified, is a tuple of float or integers (width, height) expressing size in centimeters. If not specified, size will be computed from image info.''' ns = self.currentParser.env.namespaces importer = None # Is there someting to import? if not content and not at: raise PodError(DOC_NOT_SPECIFIED) # Guess document format if isinstance(content, FileWrapper): format = content.mimeType if not format: # It should be deduced from p_at if not at: raise PodError(DOC_FORMAT_ERROR) format = os.path.splitext(at)[1][1:] else: # If format is a mimeType, convert it to an extension if mimeTypesExts.has_key(format): format = mimeTypesExts[format] isImage = False if format in self.ooFormats: importer = OdtImporter self.forceOoCall = True elif format in self.imageFormats: importer = ImageImporter isImage = True elif format == 'pdf': importer = PdfImporter else: raise PodError(DOC_WRONG_FORMAT % format) imp = importer(content, at, format, self.tempFolder, ns, self.fileNames) # Initialise image-specific parameters if isImage: imp.setImageInfo(anchor, wrapInPara, size) res = imp.run() return res
def importDocument(self, content=None, at=None, format=None, anchor='as-char'): '''If p_at is not None, it represents a path or url allowing to find the document. If p_at is None, the content of the document is supposed to be in binary format in p_content. The document p_format may be: odt or any format in imageFormats. p_anchor is only relevant for images.''' ns = self.currentParser.env.namespaces importer = None # Is there someting to import? if not content and not at: raise PodError(DOC_NOT_SPECIFIED) # Guess document format if not format: # It should be deduced from p_at if not at: raise PodError(DOC_FORMAT_ERROR) format = os.path.splitext(at)[1][1:] else: # If format is a mimeType, convert it to an extension if mimeTypesExts.has_key(format): format = mimeTypesExts[format] isImage = False if format in self.ooFormats: importer = OdtImporter self.forceOoCall = True elif format in self.imageFormats: importer = ImageImporter isImage = True elif format == 'pdf': importer = PdfImporter else: raise PodError(DOC_WRONG_FORMAT % format) imp = importer(content, at, format, self.tempFolder, ns) if isImage: imp.setAnchor(anchor) res = imp.run() if imp.fileNames: self.fileNames += imp.fileNames return res
def importDocument(self, content=None, at=None, format=None, anchor='as-char', wrapInPara=True, size=None, sizeUnit='cm', style=None, keepRatio=True, pageBreakBefore=False, pageBreakAfter=False, convertOptions=None): '''If p_at is not None, it represents a path or url allowing to find the document. If p_at is None, the content of the document is supposed to be in binary format in p_content. The document p_format may be: odt or any format in imageFormats. p_anchor, p_wrapInPara, p_size, p_sizeUnit, p_style and p_keepRatio are only relevant for images: * p_anchor defines the way the image is anchored into the document; Valid values are 'page','paragraph', 'char' and 'as-char'; * p_wrapInPara, if true, wraps the resulting 'image' tag into a 'p' tag; * p_size, if specified, is a tuple of float or integers (width, height) expressing size in p_sizeUnit (see below). If not specified, size will be computed from image info; * p_sizeUnit is the unit for p_size elements, it can be "cm" (centimeters), "px" (pixels) or "pc" (percentage). Percentages, in p_size, must be expressed as integers from 1 to 100. * if p_style is given, it is the content of a "style" attribute, containing CSS attributes. If "width" and "heigth" attributes are found there, they will override p_size and p_sizeUnit. * If p_keepRatio is True, the image width/height ratio will be kept when p_size is specified. p_pageBreakBefore and p_pageBreakAfter are only relevant for import of external odt documents, and allows to insert a page break before/after the inserted document. If p_convertOptions are given (for images only), imagemagick will be called with these options to perform some transformation on the image. For example, if you specify convertOptions="-rotate 90" pod will perform this command before importing the file into the result: convert your.image -rotate 90 your.image You can also specify a function in convertOptions. This function will receive a single arg, "image", an instance of appy.pod.doc_importers.Image giving some characteristics of the image to convert, like image.width and image.height in pixels (integers). If your function does not return a string containing the convert options, no conversion will occur. ''' importer = None # Is there someting to import ? if not content and not at: raise PodError(DOC_NOT_SPECIFIED) # Convert Zope files into Appy wrappers if content.__class__.__name__ in ('File', 'Image'): content = FileWrapper(content) # Guess document format if isinstance(content, FileWrapper): format = content.mimeType if not format: # It should be deduced from p_at if not at: raise PodError(DOC_FORMAT_ERROR) format = os.path.splitext(at)[1][1:] else: # If format is a mimeType, convert it to an extension if mimeTypesExts.has_key(format): format = mimeTypesExts[format] isImage = False isOdt = False if format in self.ooFormats: importer = OdtImporter self.forceOoCall = True isOdt = True elif (format in self.imageFormats) or not format: # If the format can't be guessed, we suppose it is an image importer = ImageImporter isImage = True elif format == 'pdf': importer = PdfImporter elif format in self.convertibleFormats: importer = ConvertImporter else: raise PodError(DOC_WRONG_FORMAT % format) imp = importer(content, at, format, self) # Initialise image-specific parameters if isImage: imp.init(anchor, wrapInPara, size, sizeUnit, style, keepRatio, convertOptions) elif isOdt: imp.init(pageBreakBefore, pageBreakAfter) return imp.run()