Commit c0f2ddf7 authored by Bartek Górny's avatar Bartek Górny

reformatted code, added more docstrings, fixed some minor issues

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@12195 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 688ee837
...@@ -25,6 +25,13 @@ ...@@ -25,6 +25,13 @@
# #
############################################################################## ##############################################################################
import xmlrpclib
import base64
import re
import zipfile
import cStringIO
from DateTime import DateTime
from AccessControl import ClassSecurityInfo from AccessControl import ClassSecurityInfo
from OFS.Image import Pdata from OFS.Image import Pdata
from Products.CMFCore.utils import getToolByName from Products.CMFCore.utils import getToolByName
...@@ -35,15 +42,18 @@ from Products.ERP5Type.Cache import CachingMethod ...@@ -35,15 +42,18 @@ from Products.ERP5Type.Cache import CachingMethod
from Products.ERP5Type.XMLObject import XMLObject from Products.ERP5Type.XMLObject import XMLObject
from Products.ERP5.Document.File import File, stripHtml from Products.ERP5.Document.File import File, stripHtml
from Products.ERP5.Document.Document import ConversionCacheMixin from Products.ERP5.Document.Document import ConversionCacheMixin
from DateTime import DateTime
import xmlrpclib, base64, re, zipfile, cStringIO
from Products.CMFCore.utils import getToolByName from Products.CMFCore.utils import getToolByName
from Products.DCWorkflow.DCWorkflow import ValidationFailed
enc=base64.encodestring enc=base64.encodestring
dec=base64.decodestring dec=base64.decodestring
_MARKER = []
class ConversionError(Exception):pass class ConversionError(Exception):pass
class OOoDocument(File, ConversionCacheMixin): class OOoDocument(File, ConversionCacheMixin):
""" """
A file document able to convert OOo compatible files to A file document able to convert OOo compatible files to
...@@ -128,48 +138,78 @@ class OOoDocument(File, ConversionCacheMixin): ...@@ -128,48 +138,78 @@ class OOoDocument(File, ConversionCacheMixin):
) )
# regexps for stripping xml from docs # regexps for stripping xml from docs
rx_strip=re.compile('<[^>]*?>',re.DOTALL|re.MULTILINE) rx_strip = re.compile('<[^>]*?>', re.DOTALL|re.MULTILINE)
rx_compr=re.compile('\s+') rx_compr = re.compile('\s+')
searchable_property_list = File.searchable_property_list + ('text_content', ) # XXX - good idea - should'n this be made more general ? searchable_property_list = File.searchable_property_list + ('text_content', ) # XXX - good idea - should'n this be made more general ?
def index_html(self, REQUEST, RESPONSE, format=None, force=0):
"""
Standard function - gets converted version (from cache or new)
sets headers and returns converted data.
Format can be only one string (because we are OOoDocument and do not
accept more formatting arguments).
Force can force conversion.
"""
self.log(format, force)
if (not self.hasOOFile()) or force:
self.convertToBase()
if format is None:
result = self.getOOFile()
mime = self.getMimeType()
self.log(mime)
else:
try:
mime, result = self.convert(format=format, force=force)
except ConversionError, e:
raise # should we do something here?
#RESPONSE.setHeader('Last-Modified', rfc1123_date(self._p_mtime)) XXX to be implemented
RESPONSE.setHeader('Content-Type', mime)
#RESPONSE.setHeader('Content-Length', self.size) XXX to be implemented
RESPONSE.setHeader('Accept-Ranges', 'bytes')
# XXX here we should find out extension for this mime type and append to filename
RESPONSE.setBase(None)
return result
def _getServerCoordinate(self): def _getServerCoordinate(self):
""" """
Returns OOo conversion server data from Returns OOo conversion server data from
preferences preferences
""" """
pref=getToolByName(self,'portal_preferences') pref = getToolByName(self,'portal_preferences')
adr=pref.getPreferredOoodocServerAddress() adr = pref.getPreferredOoodocServerAddress()
nr=pref.getPreferredOoodocServerPortNumber() nr = pref.getPreferredOoodocServerPortNumber()
if adr is None or nr is None: if adr is None or nr is None:
raise Exception('you should set conversion server coordinates in preferences') raise Exception('you should set conversion server coordinates in preferences')
return adr,nr return adr, nr
def _mkProxy(self): def _mkProxy(self):
sp=xmlrpclib.ServerProxy('http://%s:%d' % self._getServerCoordinate(),allow_none=True) sp=xmlrpclib.ServerProxy('http://%s:%d' % self._getServerCoordinate(), allow_none=True)
return sp return sp
def returnMessage(self,msg,code=0): def returnMessage(self, msg, code=0):
""" """
code > 0 indicates a problem code > 0 indicates a problem
we distinguish data return from message by checking if it is a tuple we distinguish data return from message by checking if it is a tuple
""" """
m=Message(domain='ui',message=msg) m = Message(domain='ui', message=msg)
return (code,m) return (code, m)
security.declareProtected(Permissions.ModifyPortalContent,'convert') security.declareProtected(Permissions.View, 'convert')
def convert(self,force=0,REQUEST=None): def convertToBase(self, force=0, REQUEST=None):
""" """
Converts from the initial format to OOo format; Converts from the initial format to base format (ODF);
communicates with the conversion server communicates with the conversion server
and gets converted file as well as metadata and gets converted file as well as metadata
""" """
if force==0 and self.hasOOFile(): if force == 0 and self.hasOOFile():
return self.returnMessage('OOo file is up do date',1) return self.returnMessage('OOo file is up do date', 1)
try: try:
self._convert() self._convertToBase()
except xmlrpclib.Fault,e: except xmlrpclib.Fault, e:
return self.returnMessage('Problem: %s' % (str(e) or 'undefined'),2) return self.returnMessage('Problem: %s' % (str(e) or 'undefined'), 2)
return self.returnMessage('converted') return self.returnMessage('converted')
security.declareProtected(Permissions.AccessContentsInformation,'getTargetFormatList') security.declareProtected(Permissions.AccessContentsInformation,'getTargetFormatList')
...@@ -189,24 +229,23 @@ class OOoDocument(File, ConversionCacheMixin): ...@@ -189,24 +229,23 @@ class OOoDocument(File, ConversionCacheMixin):
cached_getTargetFormatItemList = CachingMethod(cached_getTargetFormatItemList, cached_getTargetFormatItemList = CachingMethod(cached_getTargetFormatItemList,
id = "OOoDocument_getTargetFormatItemList" ) id = "OOoDocument_getTargetFormatItemList" )
return cached_getTargetFormatItemList(self.getContentType()) return cached_getTargetFormatItemList(self.getContentType())
security.declareProtected(Permissions.AccessContentsInformation, 'getTargetFormatList')
security.declareProtected(Permissions.AccessContentsInformation,'getTargetFormatList')
def getTargetFormatList(self): def getTargetFormatList(self):
""" """
Returns a list of acceptable formats for conversion Returns a list of acceptable formats for conversion
""" """
return map(lambda x: x[0], self.getTargetFormatItemList()) return map(lambda x: x[0], self.getTargetFormatItemList())
security.declareProtected(Permissions.ModifyPortalContent, 'reset')
security.declareProtected(Permissions.ModifyPortalContent,'reset')
def reset(self): def reset(self):
'''reset''' """
make the object a non-converted one, as if it was brand new
"""
self.clearCache() self.clearCache()
self.oo_data=None self.oo_data = None
m=self.returnMessage('new') m = self.returnMessage('new')
self.setExternalProcessingStatusMessage(str(m[1])) self.setExternalProcessingStatusMessage(str(m[1]))
security.declareProtected(Permissions.ModifyPortalContent,'isAllowed') security.declareProtected(Permissions.ModifyPortalContent,'isAllowed')
...@@ -214,38 +253,36 @@ class OOoDocument(File, ConversionCacheMixin): ...@@ -214,38 +253,36 @@ class OOoDocument(File, ConversionCacheMixin):
""" """
Checks if the current document can be converted Checks if the current document can be converted
into the specified format. into the specified format.
""" """
if not self.hasOOFile(): return False allowed = self.getTargetFormatItemList()
allowed=self.getTargetFormatItemList()
if allowed is None: return False if allowed is None: return False
return (format in [x[1] for x in allowed]) return (format in [x[1] for x in allowed])
security.declareProtected(Permissions.ModifyPortalContent,'editMetadata') security.declareProtected(Permissions.ModifyPortalContent,'editMetadata')
def editMetadata(self,newmeta): def editMetadata(self, newmeta):
""" """
Updates metadata information in the converted OOo document Updates metadata information in the converted OOo document
based on the values provided by the user. This is implemented based on the values provided by the user. This is implemented
through the invocation of the conversion server. through the invocation of the conversion server.
""" """
sp=self._mkProxy() sp = self._mkProxy()
kw=sp.run_setmetadata(self.getTitle(),enc(self._unpackData(self.oo_data)),newmeta) kw = sp.run_setmetadata(self.getTitle(), enc(self._unpackData(self.oo_data)), newmeta)
self.oo_data=Pdata(dec(kw['data'])) self.oo_data = Pdata(dec(kw['data']))
self._setMetaData(kw['meta']) self._setMetaData(kw['meta'])
return True # XXX why return ? - why not? return True # XXX why return ? - why not?
security.declarePrivate('_convert') security.declarePrivate('_convert')
def _convert(self): def _convertToBase(self):
""" """
Converts the original document into OOo document Converts the original document into ODF
by invoking the conversion server. Store the result by invoking the conversion server. Store the result
on the object. Update metadata information. on the object. Update metadata information.
""" """
sp=self._mkProxy() sp = self._mkProxy()
kw=sp.run_convert(self.getSourceReference(),enc(self._unpackData(self.data))) kw = sp.run_convert(self.getSourceReference(), enc(self._unpackData(self.data)))
self.oo_data=Pdata(dec(kw['data'])) self.oo_data = Pdata(dec(kw['data']))
# now we get text content # now we get text content
text_data=self.extractTextContent() text_data = self.extractTextContent()
self.setTextContent(text_data) self.setTextContent(text_data)
self._setMetaData(kw['meta']) self._setMetaData(kw['meta'])
...@@ -254,25 +291,16 @@ class OOoDocument(File, ConversionCacheMixin): ...@@ -254,25 +291,16 @@ class OOoDocument(File, ConversionCacheMixin):
""" """
extract plain text from ooo docs - the simplest way possible, works for all ODF formats extract plain text from ooo docs - the simplest way possible, works for all ODF formats
""" """
cs=cStringIO.StringIO() cs = cStringIO.StringIO()
cs.write(self._unpackData(self.oo_data)) cs.write(self._unpackData(self.oo_data))
z=zipfile.ZipFile(cs) z = zipfile.ZipFile(cs)
s=z.read('content.xml') s = z.read('content.xml')
s=self.rx_strip.sub(" ",s) # strip xml s = self.rx_strip.sub(" ", s) # strip xml
s=self.rx_compr.sub(" ",s) # compress multiple spaces s = self.rx_compr.sub(" ", s) # compress multiple spaces
cs.close() cs.close()
z.close() z.close()
return s return s
security.declareProtected(Permissions.ModifyPortalContent,'setPropertyListFromContent')
def setPropertyListFromContent(self):
'''docstring'''
atrs=self.Document_getPropertyListFromContent(self,self.getTextContent(),self.getPortalType())
doctype=atrs.get('doctype','None')
if doctype!='None' and doctype!=self.getPortalType():
raise Exception('portal type mismatch - content gave %s, I have %s' % (doctype,self.getPortalType()))
for a in atrs:
self.setProperty(a,atrs[a])
security.declarePrivate('_setMetaData') security.declarePrivate('_setMetaData')
def _setMetaData(self,meta): def _setMetaData(self,meta):
...@@ -289,16 +317,16 @@ class OOoDocument(File, ConversionCacheMixin): ...@@ -289,16 +317,16 @@ class OOoDocument(File, ConversionCacheMixin):
XXX - I think it does (BG) XXX - I think it does (BG)
""" """
for k,v in meta.items(): for k,v in meta.items():
meta[k]=v.encode('utf-8') meta[k] = v.encode('utf-8')
self.setTitle(meta.get('title','')) self.setTitle(meta.get('title', ''))
self.setSubject(meta.get('keywords','').split()) self.setSubject(meta.get('keywords', '').split())
self.setDescription(meta.get('description','')) self.setDescription(meta.get('description', ''))
#self.setLanguage(meta.get('language','')) #self.setLanguage(meta.get('language',''))
if meta.get('MIMEType',False): if meta.get('MIMEType', False):
self.setContentType(meta['MIMEType']) self.setContentType(meta['MIMEType'])
#self.setReference(meta.get('reference','')) #self.setReference(meta.get('reference',''))
security.declareProtected(Permissions.View,'getOOFile') security.declareProtected(Permissions.View, 'getOOFile')
def getOOFile(self): def getOOFile(self):
""" """
Return the converted OOo document. Return the converted OOo document.
...@@ -307,27 +335,27 @@ class OOoDocument(File, ConversionCacheMixin): ...@@ -307,27 +335,27 @@ class OOoDocument(File, ConversionCacheMixin):
called data in property sheet. Look at File implementation called data in property sheet. Look at File implementation
XXX - doesn't seem to be there... XXX - doesn't seem to be there...
""" """
data=self.oo_data data = self.oo_data
return data return data
security.declareProtected(Permissions.View,'hasOOFile') security.declareProtected(Permissions.View, 'hasOOFile')
def hasOOFile(self): def hasOOFile(self):
""" """
Checks whether we have an OOo converted file Checks whether we have an OOo converted file
""" """
_marker=[] _marker = []
if getattr(self,'oo_data',_marker) is not _marker: # XXX - use propertysheet accessors if getattr(self, 'oo_data',_marker) is not _marker: # XXX - use propertysheet accessors
return getattr(self,'oo_data') is not None return getattr(self, 'oo_data') is not None
return False return False
security.declareProtected(Permissions.View,'hasSnapshot') security.declareProtected(Permissions.View, 'hasSnapshot')
def hasSnapshot(self): def hasSnapshot(self):
""" """
Checks whether we have a snapshot. Checks whether we have a snapshot.
""" """
_marker=[] _marker = []
if getattr(self,'snapshot',_marker) is not _marker: # XXX - use propertysheet accessors if getattr(self, 'snapshot', _marker) is not _marker: # XXX - use propertysheet accessors
return getattr(self,'snapshot') is not None return getattr(self, 'snapshot') is not None
return False return False
security.declareProtected(Permissions.ModifyPortalContent,'createSnapshot') security.declareProtected(Permissions.ModifyPortalContent,'createSnapshot')
...@@ -341,26 +369,25 @@ class OOoDocument(File, ConversionCacheMixin): ...@@ -341,26 +369,25 @@ class OOoDocument(File, ConversionCacheMixin):
""" """
if self.hasSnapshot(): if self.hasSnapshot():
if REQUEST is not None: if REQUEST is not None:
return self.returnMessage('already has a snapshot',1) return self.returnMessage('already has a snapshot', 1)
raise ConversionError('already has a snapshot') raise ConversionError('already has a snapshot')
# making snapshot # making snapshot
# we have to figure out which pdf format to use # we have to figure out which pdf format to use
tgts=[x[1] for x in self.getTargetFormatItemList() if x[1].endswith('pdf')] tgts = [x[1] for x in self.getTargetFormatItemList() if x[1].endswith('pdf')]
if len(tgts)>1: if len(tgts) > 1:
return self.returnMessage('multiple pdf formats found - this shouldnt happen',2) return self.returnMessage('multiple pdf formats found - this shouldnt happen', 2)
if len(tgts)==0: if len(tgts)==0:
return self.returnMessage('no pdf format found',1) return self.returnMessage('no pdf format found',1)
fmt=tgts[0] fmt = tgts[0]
self.makeFile(fmt) self.makeFile(fmt)
self.snapshot = Pdata(self._unpackData(self.getConversion(format = fmt)[1])) self.snapshot = Pdata(self._unpackData(self.getConversion(format = fmt)[1]))
return self.returnMessage('snapshot created') return self.returnMessage('snapshot created')
security.declareProtected(Permissions.View,'getSnapshot') security.declareProtected(Permissions.View,'getSnapshot')
def getSnapshot(self,REQUEST=None): def getSnapshot(self, REQUEST=None):
""" """
Returns the snapshot. Returns the snapshot.
""" """
'''getSnapshot'''
if not self.hasSnapshot(): if not self.hasSnapshot():
self.createSnapshot() self.createSnapshot()
return self.snapshot return self.snapshot
...@@ -376,51 +403,62 @@ class OOoDocument(File, ConversionCacheMixin): ...@@ -376,51 +403,62 @@ class OOoDocument(File, ConversionCacheMixin):
pass pass
def getHtmlRepresentation(self): def getHtmlRepresentation(self):
''' """
get simplified html version to display get simplified html version to display
''' """
# we have to figure out which html format to use # we have to figure out which html format to use
tgts=[x[1] for x in self.getTargetFormatItemList() if x[1].startswith('html')] tgts = [x[1] for x in self.getTargetFormatItemList() if x[1].startswith('html')]
if len(tgts)==0: if len(tgts) == 0:
return 'no html representation available' return 'no html representation available'
fmt=tgts[0] fmt = tgts[0]
fmt,data=self.getTargetFile(fmt) fmt, data = self.convert(fmt)
cs=cStringIO.StringIO() cs = cStringIO.StringIO()
cs.write(self._unpackData(data)) cs.write(self._unpackData(data))
z=zipfile.ZipFile(cs) z = zipfile.ZipFile(cs)
h='could not extract anything' h = 'could not extract anything'
for f in z.infolist(): for f in z.infolist():
fn=f.filename fn = f.filename
if fn.endswith('html'): if fn.endswith('html'):
h=z.read(fn) h = z.read(fn)
break break
z.close() z.close()
cs.close() cs.close()
return stripHtml(h) return stripHtml(h)
security.declareProtected(Permissions.View,'getTargetFile') security.declareProtected(Permissions.View, 'convert')
def getTargetFile(self,format,REQUEST=None): def convert(self, format, REQUEST=None, force=0):
""" """
Get (possibly generate) file in a given format Get file in a given format.
Runs makeFile to make sure we have the requested version cached,
then returns from cache.
""" """
# first check if we have base
if not self.hasOOFile():
self.convertToBase()
if not self.isAllowed(format): if not self.isAllowed(format):
return self.returnMessage('can not convert to '+format+' for some reason',1) if REQUEST is not None:
return self.returnMessage('can not convert to ' + format + ' for some reason',1)
else:
raise ConversionError, 'can not convert to ' + format + ' for some reason'
try: try:
self.makeFile(format) # make if necessary, return from cache
self.makeFile(format, force)
return self.getConversion(format = format) return self.getConversion(format = format)
except ConversionError,e: except ConversionError,e:
return self.returnMessage(str(e),2) if REQUEST is not None:
return self.returnMessage(str(e), 2)
raise
security.declareProtected(Permissions.View,'isFileChanged') security.declareProtected(Permissions.View, 'isFileChanged')
def isFileChanged(self,format): def isFileChanged(self, format):
""" """
Checks whether the file was converted (or uploaded) after last generation of Checks whether the file was converted (or uploaded) after last generation of
the target format the target format
""" """
return not self.hasConversion(format = format) return not self.hasConversion(format=format)
security.declareProtected(Permissions.ModifyPortalContent,'makeFile') security.declareProtected(Permissions.ModifyPortalContent, 'makeFile')
def makeFile(self,format,REQUEST=None): def makeFile(self, format, force=0, REQUEST=None, **kw):
""" """
This method implement the file conversion cache: This method implement the file conversion cache:
* check if the format is supported * check if the format is supported
...@@ -428,26 +466,28 @@ class OOoDocument(File, ConversionCacheMixin): ...@@ -428,26 +466,28 @@ class OOoDocument(File, ConversionCacheMixin):
* if necessary, create new file and cache * if necessary, create new file and cache
* update file generation time * update file generation time
Fails silently if we have an up to date version.
TODO: TODO:
* support of images in html conversion (as subobjects for example) * support of images in html conversion (as subobjects for example)
""" """
if not self.isAllowed(format): if not self.isAllowed(format):
errstr='%s format is not supported' % format errstr = '%s format is not supported' % format
if REQUEST is not None: if REQUEST is not None:
return self.returnMessage(errstr,2) return self.returnMessage(errstr, 2)
raise ConversionError(errstr) raise ConversionError(errstr)
if not self.hasOOFile(): if not self.hasOOFile():
if REQUEST is not None: if REQUEST is not None:
return self.returnMessage('needs conversion',1) return self.returnMessage('needs conversion', 1)
raise ConversionError('needs conversion') raise ConversionError('needs conversion')
if self.isFileChanged(format): if self.isFileChanged(format) or force:
try: try:
mime,data=self._makeFile(format) mime, data = self._makeFile(format)
self.setConversion(data, mime, format = format) self.setConversion(data, mime, format = format)
self._p_changed=1 # XXX not sure it is necessary self._p_changed = 1 # XXX not sure it is necessary
except xmlrpclib.Fault,e: except xmlrpclib.Fault, e:
if REQUEST is not None: if REQUEST is not None:
return self.returnMessage('Problem: %s' % str(e),2) return self.returnMessage('Problem: %s' % str(e), 2)
else: else:
raise ConversionError(str(e)) raise ConversionError(str(e))
self.updateConversion(format = format) self.updateConversion(format = format)
...@@ -455,8 +495,7 @@ class OOoDocument(File, ConversionCacheMixin): ...@@ -455,8 +495,7 @@ class OOoDocument(File, ConversionCacheMixin):
return self.returnMessage('%s created' % format) return self.returnMessage('%s created' % format)
else: else:
if REQUEST is not None: if REQUEST is not None:
return self.returnMessage('%s file is up to date' % format,1) return self.returnMessage('%s file is up to date' % format, 1)
return ConversionError('%s file is up to date' % format)
security.declarePrivate('_makeFile') security.declarePrivate('_makeFile')
def _makeFile(self,format): def _makeFile(self,format):
...@@ -464,16 +503,15 @@ class OOoDocument(File, ConversionCacheMixin): ...@@ -464,16 +503,15 @@ class OOoDocument(File, ConversionCacheMixin):
Communicates with server to convert a file Communicates with server to convert a file
""" """
# real version: # real version:
sp=self._mkProxy() sp = self._mkProxy()
kw=sp.run_generate(self.getSourceReference(),enc(self._unpackData(self.oo_data)),None,format) kw = sp.run_generate(self.getSourceReference(), enc(self._unpackData(self.oo_data)), None, format)
return kw['mime'],Pdata(dec(kw['data'])) return kw['mime'], Pdata(dec(kw['data']))
# make sure to call the right edit methods # make sure to call the right edit methods
_edit=File._edit _edit = File._edit
edit=File.edit edit = File.edit
# BG copied from File in case # BG copied from File in case
index_html = File.index_html
security.declareProtected('FTP access', 'manage_FTPget', 'manage_FTPstat', 'manage_FTPlist') security.declareProtected('FTP access', 'manage_FTPget', 'manage_FTPstat', 'manage_FTPlist')
manage_FTPget = File.manage_FTPget manage_FTPget = File.manage_FTPget
manage_FTPlist = File.manage_FTPlist manage_FTPlist = File.manage_FTPlist
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment