def _logException(self, instance): if instance and hasattr(instance, 'getPhysicalPath'): path = '/'.join(instance.getPhysicalPath()) filename = self.getFilename(instance) msg = 'EXCEPTION object: %s, file: %s: \n' % (path, filename) LOG.warning(msg, exc_info=True) else: LOG.warning('Exception occured', exc_info=True)
def convertStringToPreview(self, content, content_type, instance): """ convertStringToPreview(self, content) => Utility to convert a string to HTML using the converter stuff. """ LOG.debug("convertStringToPreview...") cnv = oo_to_html() return self._convertOutput(cnv.convert_(content,), "html")
def __init__(self): self.flex_storage = {} for storage_method, storage_name in SUPPORTED_FLEX_STORAGE: try: exec storage_method self.flex_storage[storage_name] = eval("%s()" % storage_name) except ImportError, e: LOG.info("%s is not installed", storage_name)
def convertStringToIndex(self, content, content_type, instance): """ convertStringToIndex(self, content, content_type, instance) => Utility to convert a string to HTML using the converter stuff. """ LOG.debug("convertStringToIndex...") cnv = oo_to_html() return self._html_to_text(cnv.convert_(content,), )
def getIndexableValue(self, field, instance): """ getIndexableValue(self, field, instance) => (possibliy big) string Return the ZCatalog-indexable string for that type. """ LOG.debug("getIndexableValue") content = field.get(instance) content_type = field.getContentType(instance) return self.convertStringToIndex(content, content_type, instance)
def fakeUpload(self): current_user = getSecurityManager().getUser() newSecurityManager(None, UnrestrictedUser('manager', '', ['Manager'], [])) dummy = self._dummy request = FakeRequest() request.form.update(field_values) dummy.REQUEST = request dummy.processForm(data=1) LOG.debug("dummy size after processForm: %d" % ( dummy.Schema()['attach_doc'].get(dummy).getSize())) self._content = dummy newSecurityManager(None, current_user)
def getPreview(self, field, instance): """ getPreview(self, field, instance) => string or None Return the HTML preview (generating it if it's not already done) for this attachement. If the attachment is not previewable, or if there's a problem in the preview, return None. """ LOG.debug("getPreview") content = field.get(instance) content_type = field.getContentType(instance) return self.convertStringToPreview(content, content_type, instance)
def uninstall(self): out = StringIO.StringIO() # Uninstall configlets try: cptool = getToolByName(self, 'portal_controlpanel') cptool.unregisterApplication(PROJECTNAME) except: LOG.info("Error at uninstall", exc_info=True) print >> out, "Successfully uninstalled %s." % PROJECTNAME return out.getvalue()
def getIcon(self, instance): """ getIcon(self, instance) => return the underlying file class icon (object) """ name = self.getName() icon = getattr(instance, _icon_ % name, None) if not icon: handler = self._getHandler(instance) LOG.debug("getIcon for %s / %s" % (self.getFilename(instance), handler.converter_type, )) icon = handler.getIconFile(self, instance) setattr(instance, _icon_ % name, icon) return getattr(instance, icon, None)
def test_get(self): current_user = getSecurityManager().getUser() newSecurityManager(None, UnrestrictedUser('manager', '', ['Manager'], [])) dummy = self.makeDummy() request = FakeRequest() request.form.update(field_values) dummy.REQUEST = request dummy.processForm(data=1) for k, v in expected_values.items(): LOG.debug(k) got = dummy.Schema()[k].get(dummy) if isinstance(got, File): got = str(got) self.assertEquals(got, v, 'field "%s"\n got:\n %r\n\n\n expected:\n %r' % (k, got, v)) newSecurityManager(None, current_user)
def test_set(self): """Same as previous but with fieldsets""" current_user = getSecurityManager().getUser() newSecurityManager(None, UnrestrictedUser('manager', '', ['Manager'], [])) dummy = self.makeDummy() request = FakeRequest() request.form.update(field_values) request.form['fieldset'] = 'default' dummy.REQUEST = request dummy.processForm() for k, v in test_fields.items(): LOG.debug(k) got = dummy.Schema()[k].get(dummy) if isinstance(got, (File, ScalableImage, Image)): got = str(got) self.assertEquals(got, v['expected'], 'field "%s"\ngot:\n %r\n\n\n expected:\n %r' % (k, got, v['expected'])) newSecurityManager(None, current_user)
def _getTransformPath(self, input, output): """ _getTransformPath(self, input, output) => chain or None Try to build a transform chain from 'input' mime type to 'output' mime type. If it's not possible to build such a chain, return None. Nota: this code is taken from TransformEngine.py """ ## get a path to output mime type transform = self.getPortalTransforms() requirements = transform._policies.get(target_mt, []) path = transform._findPath(orig_mt, target_mt, list(requirements)) if not path and requirements: LOG.debug("Unable to satisfy requirements %s" % (", ".join(requirements),)) path = transform._findPath(orig_mt, target_mt) if not path: LOG.debug("NO PATH FROM %s TO %s : %s" % (orig_mt, target_mimetype, path)) return None return path
def _convertStringToMime(self, content, content_type, instance, output_mime): # Check if a transform is available trans = self.getTransforms(None, instance) ct = content_type if not trans["html_paths"].get(ct, None): raise ValueError, "No converter found for content type '%s'" % (ct,) # Convert it to plain text pt = self.getPortalTransforms(None, instance) out = pt.convertTo(target_mimetype=output_mime, orig=content, data=None, mimetype=content_type) output = out.getData() # Try to use / guess the encoding out_encoding = out.getMetadata().get("encoding", None) if out_encoding: LOG.debug("Have encoding: '%s'" % out_encoding) output = unicode(output, encoding=out_encoding) else: # Convert from encoded string to unicode for enc in AVAILABLE_ENCODINGS: try: LOG.debug("Trying encoding: '%s'" % enc) output = output.decode(enc) break except UnicodeError: LOG.debug("Encoding '%s' failed" % enc) pass # Return an encoded output return self.unicode2string(output, instance)
## # Try to use / guess the encoding ## out_encoding = out.getMetadata().get('encoding', None) ## if out_encoding: ## Log(LOG_DEBUG, "Have encoding", out_encoding) ## output = unicode(output, encoding = out_encoding, ) ## else: ## # Convert from encoded string to unicode ## for enc in AVAILABLE_ENCODINGS: ## try: ## Log(LOG_DEBUG, "Trying encoding", enc) ## output = output.decode(enc, ) ## break ## except UnicodeError: ## Log(LOG_DEBUG, "Encoding", enc, "failed.") ## pass ## # Return an encoded output ## return self.unicode2string(output, instance) try: import libxml2 import libxslt except: LOG.warning("""libxml2 or libxslt not available. Under windows, download it at http://users.skynet.be/sbi/libxml-python/ Open-Office indexing will be disabled.""") else: from ooconverter import oo_to_html AttachmentHandler.registerHandler(OOAttachment)