Doc storage: port logging to pythonic one.

A cleanup to get rid of netsvc.Logger()

Conflicts:

	document/document_storage.py

bzr revid: p_christ@hol.gr-20100625102933-pdymhisr2c7xzemr
This commit is contained in:
P. Christeas
2010-06-25 13:29:33 +03:00
parent a82a4a5b21
commit c759d43405
+17 -16
View File
@@ -24,6 +24,8 @@ from osv import osv, fields
import os import os
import tools import tools
import base64 import base64
import logging
from tools.misc import ustr from tools.misc import ustr
from tools.translate import _ from tools.translate import _
@@ -96,6 +98,8 @@ class document_storage(osv.osv):
""" """
_name = 'document.storage' _name = 'document.storage'
_description = 'Storage Media' _description = 'Storage Media'
_doclog = logging.getLogger('document')
_columns = { _columns = {
'name': fields.char('Name', size=64, required=True, select=1), 'name': fields.char('Name', size=64, required=True, select=1),
'write_date': fields.datetime('Date Modified', readonly=True), 'write_date': fields.datetime('Date Modified', readonly=True),
@@ -151,7 +155,7 @@ class document_storage(osv.osv):
# On a migrated db, some files may have the wrong storage type # On a migrated db, some files may have the wrong storage type
# try to fix their directory. # try to fix their directory.
if ira.file_size: if ira.file_size:
netsvc.Logger().notifyChannel('document', netsvc.LOG_WARNING, "ir.attachment #%d does not have a filename, but is at filestore, fix it!" % ira.id) self._doclog.warning( "ir.attachment #%d does not have a filename, but is at filestore, fix it!" % ira.id)
return None return None
fpath = os.path.join(boo.path, ira.store_fname) fpath = os.path.join(boo.path, ira.store_fname)
return file(fpath, 'rb').read() return file(fpath, 'rb').read()
@@ -167,7 +171,7 @@ class document_storage(osv.osv):
# On a migrated db, some files may have the wrong storage type # On a migrated db, some files may have the wrong storage type
# try to fix their directory. # try to fix their directory.
if ira.file_size: if ira.file_size:
netsvc.Logger().notifyChannel('document',netsvc.LOG_WARNING,"ir.attachment #%d does not have a filename, trying the name." %ira.id) self._doclog.warning("ir.attachment #%d does not have a filename, trying the name." %ira.id)
sfname = ira.name sfname = ira.name
fpath = os.path.join(boo.path,ira.store_fname or ira.name) fpath = os.path.join(boo.path,ira.store_fname or ira.name)
if os.path.exists(fpath): if os.path.exists(fpath):
@@ -187,7 +191,6 @@ class document_storage(osv.osv):
if not context: if not context:
context = {} context = {}
boo = self.browse(cr, uid, id, context) boo = self.browse(cr, uid, id, context)
logger = netsvc.Logger()
if fil_obj: if fil_obj:
ira = fil_obj ira = fil_obj
else: else:
@@ -195,7 +198,7 @@ class document_storage(osv.osv):
if not boo.online: if not boo.online:
raise RuntimeError('media offline') raise RuntimeError('media offline')
logger.notifyChannel('document', netsvc.LOG_DEBUG, "Store data for ir.attachment #%d" % ira.id) self._doclog.debug( "Store data for ir.attachment #%d" % ira.id)
store_fname = None store_fname = None
fname = None fname = None
if boo.type == 'filestore': if boo.type == 'filestore':
@@ -214,14 +217,14 @@ class document_storage(osv.osv):
fp = file(fname, 'wb') fp = file(fname, 'wb')
fp.write(data) fp.write(data)
fp.close() fp.close()
logger.notifyChannel('document', netsvc.LOG_DEBUG, "Saved data to %s" % fname) self._doclog.debug( "Saved data to %s" % fname)
filesize = len(data) # os.stat(fname).st_size filesize = len(data) # os.stat(fname).st_size
store_fname = os.path.join(flag, filename) store_fname = os.path.join(flag, filename)
# TODO Here, an old file would be left hanging. # TODO Here, an old file would be left hanging.
except Exception, e : except Exception, e:
netsvc.Logger().notifyChannel('document', netsvc.LOG_WARNING, "Couldn't save data: %s" % str(e)) self._doclog.warning( "Couldn't save data to %s", path, exc_info=True)
raise except_orm(_('Error!'), str(e)) raise except_orm(_('Error!'), str(e))
elif boo.type == 'db': elif boo.type == 'db':
filesize = len(data) filesize = len(data)
@@ -250,14 +253,12 @@ class document_storage(osv.osv):
fp = file(fname,'wb') fp = file(fname,'wb')
fp.write(data) fp.write(data)
fp.close() fp.close()
logger.notifyChannel('document',netsvc.LOG_DEBUG,"Saved data to %s" % fname) self._doclog.debug("Saved data to %s", fname)
filesize = len(data) # os.stat(fname).st_size filesize = len(data) # os.stat(fname).st_size
store_fname = os.path.join(*npath) store_fname = os.path.join(*npath)
# TODO Here, an old file would be left hanging. # TODO Here, an old file would be left hanging.
except Exception,e : except Exception,e :
import traceback self._doclog.warning("Couldn't save data:", exc_info=True)
traceback.print_exc()
netsvc.Logger().notifyChannel('document',netsvc.LOG_WARNING,"Couldn't save data: %s" % e)
raise except_orm(_('Error!'), str(e)) raise except_orm(_('Error!'), str(e))
else: else:
raise TypeError("No %s storage" % boo.type) raise TypeError("No %s storage" % boo.type)
@@ -271,8 +272,8 @@ class document_storage(osv.osv):
try: try:
mime, icont = cntIndex.doIndex(data, ira.datas_fname, mime, icont = cntIndex.doIndex(data, ira.datas_fname,
ira.file_type or None, fname) ira.file_type or None, fname)
except Exception, e: except Exception:
logger.notifyChannel('document', netsvc.LOG_DEBUG, 'Cannot index file: %s' % str(e)) self._doclog.debug('Cannot index file:', exc_info=True)
pass pass
# a hack: /assume/ that the calling write operation will not try # a hack: /assume/ that the calling write operation will not try
@@ -284,7 +285,7 @@ class document_storage(osv.osv):
file_node.content_type = mime file_node.content_type = mime
return True return True
except Exception, e : except Exception, e :
netsvc.Logger().notifyChannel('document', netsvc.LOG_WARNING, "Couldn't save data: %s" % str(e)) self._doclog.warning( "Couldn't save data:", exc_info=True)
# should we really rollback once we have written the actual data? # should we really rollback once we have written the actual data?
# at the db case (only), that rollback would be safe # at the db case (only), that rollback would be safe
raise except_orm(_('Error at doc write!'), str(e)) raise except_orm(_('Error at doc write!'), str(e))
@@ -319,9 +320,9 @@ class document_storage(osv.osv):
try: try:
os.unlink(fname) os.unlink(fname)
except Exception, e: except Exception, e:
netsvc.Logger().notifyChannel('document', netsvc.LOG_WARNING, "Could not remove file %s, please remove manually." % fname) self._doclog.warning("Could not remove file %s, please remove manually.", fname, exc_info=True)
else: else:
netsvc.Logger().notifyChannel('document', netsvc.LOG_WARNING, "Unknown unlink key %s" % ktype) self._doclog.warning("Unknown unlink key %s" % ktype)
return True return True