Commits

jayven committed 69aa899

Add auto save path for traceback

  • Participants
  • Parent commits 5713f22

Comments (0)

Files changed (2)

 import pooler
 import linecache
 import base64
+import os.path
 from datetime import datetime
 from itertools import izip, islice
 from pprint import pformat
     
     def _set_exc(self, cr, uid, id, last_type, last_value, last_traceback,
             context):
+        rec = self.browse(cr, uid, id, context=context)
+
         # first clear the previous exc
-        self._clear_exc(cr, uid, id, context)
+        self._clear_exc(cr, uid, rec.id, context)
 
         # write last_type and last_value
-        self.write(cr, uid, [id,], {'last_type': str(last_type),
+        self.write(cr, uid, [rec.id,], {'last_type': str(last_type),
             'last_value': str(last_value)}, context=context)
 
         # now add tracebacks
             line, lines = get_file_part(filename, lineno,
                     lines_around,
                     f.f_globals)
-            tb_id = self.tb_obj.create(cr, uid, {'console': id,
+            tb_id = self.tb_obj.create(cr, uid, {'console': rec.id,
                 'depth': depth,
                 'filename': filename,
                 'where': where,
             tb = tb.tb_next
             depth += 1
 
+        # no need to auto save traceback
+        if not rec.tb_save_path:
+            return
+
+        tb_save_path = os.path.abspath(rec.tb_save_path)
+        if not os.path.exists(tb_save_path) or not os.path.isdir(
+                tb_save_path) or not os.access(tb_save_path, os.W_OK):
+            # do nothing silence
+            return
+
+        filename = os.path.join(tb_save_path, datetime.now().strftime(
+            'TracebackExport-auto-%Y%m%d%H%M%S'))
+        try:
+            f = open(filename, 'w')
+            f.write(self._dump_tb(cr, uid, rec.id, context=context))
+            f.close()
+        except IOError, e:
+            return
+            #raise osv.except_osv('Error', '%s %s' % (filename, e))
+
     def create(self, cr, uid, vals, context=None):
         # create the associated bgconsole and create the record
         bgconsole_id = self.bgconsole_obj.create(cr, uid, {}, context=context)
             'profile_result': profile_result,
         }, context=context)
 
-    def export_tb(self, cr, uid, ids, context=None):
-        rec = self.browse(cr, uid, ids[0], context=context)
+    def _dump_tb(self, cr, uid, id, context=None):
+        rec = self.browse(cr, uid, id, context=context)
         res = {
             'last_type': rec.last_type,
             'last_value': rec.last_value,
                 }
                 t['locals'].append((0, 0, l))
             res['last_traceback'].append((0, 0, t))
-        res = base64.b64encode(json.dumps(res, separators=(',', ':')))
+
+        return json.dumps(res, separators=(',', ':'))
+
+    def export_tb(self, cr, uid, ids, context=None):
+        res = base64.b64encode(self._dump_tb(cr, uid, ids[0], context=context))
         res_id = self.pool.get('hotdebug.tb.export').create(cr, uid, {
             'file': res,
             'filename': datetime.now().strftime(
     def clear_tb(self, cr, uid, ids, context=None):
         self._clear_exc(cr, uid, ids[0], context)
         return True
-        
+
+    def enable_tb_auto_save(self, cr, uid, ids, context=None):
+        res_id = self.pool.get('hotdebug.tb.auto.save.path').create(cr, uid, {
+            'console': ids[0]
+        }, context=context)
+        return {
+            'name': 'Enter path for auto saving traceback',
+            'view_mode': 'form',
+            'view_id': False,
+            'view_type': 'form',
+            'res_id': res_id,
+            'res_model': 'hotdebug.tb.auto.save.path',
+            'type': 'ir.actions.act_window',
+            'nodestroy': True,
+            'target': 'new',
+            'context': context,
+        }
+
+    def disable_tb_auto_save(self, cr, uid, ids, context=None):
+        self.write(cr, uid, ids, {
+            'tb_save_path': False,
+        }, context=context)
+        return True
+
     _name = 'hotdebug.console'
     _columns = {
         'bgconsole': fields.many2one('hotdebug.console.bgconsole',
             readonly=True),
         'catch_server_exception': fields.boolean('Catch server exception?',
             readonly=True),
+        'tb_save_path': fields.char('Path for auto saved traceback', size=128,
+            readonly=True),
         'profile_server': fields.boolean('Profile server activity', readonly=True),
         'profile_sort': fields.selection([
             ('time', 'internal time'),
         'last_value': '',
         'catch_console_exception': True,
         'catch_server_exception': False,
+        'tb_save_path': '',
         'profile_server': False,
         'profile_sort': 'time',
         'profile_result': (pstats is None) and """
 
 
 TracebackImport()
+
+
+class TracebackAutoSavePath(osv.osv_memory):
+
+    def set_path(self, cr, uid, ids, context=None):
+        rec = self.browse(cr, uid, ids[0], context=context)
+        if not rec.path:
+            raise osv.except_osv('Error', 'Please enter a path')
+
+        path = os.path.abspath(rec.path)
+        if (not os.path.exists(path)) or (not os.path.isdir(path)):
+            raise osv.except_osv('Error', '%r is not a directory' % path)
+
+        if not os.access(path, os.W_OK):
+            raise osv.except_osv('Error', '%r is not writable' % path)
+
+        self.pool.get('hotdebug.console').write(cr, uid, [rec.console.id], {
+            'tb_save_path': path,
+        }, context=context)
+
+        return {'type': 'ir.actions.act_window_close'}
+
+    _name = 'hotdebug.tb.auto.save.path'
+    _columns = {
+        'console': fields.many2one('hotdebug.console', 'Console',
+            readonly=True),
+        'path': fields.char('Path for auto saving tracebacks', size=128),
+    }
+
+
+TracebackAutoSavePath()

File debugger.xml

 <openerp>
     <data>
         <record id="view_hotdebug_tb_export" model="ir.ui.view">
-            <field name="name">hotdebug.tb.export</field>
+            <field name="name">view.hotdebug.tb.export</field>
             <field name="model">hotdebug.tb.export</field>
             <field name="type">form</field>
             <field name="arch" type="xml">
         </record>
 
         <record id="view_hotdebug_tb_import" model="ir.ui.view">
-            <field name="name">hotdebug.tb.import</field>
+            <field name="name">view.hotdebug.tb.import</field>
             <field name="model">hotdebug.tb.import</field>
             <field name="type">form</field>
             <field name="arch" type="xml">
             </field>
         </record>
 
+        <record id="view_hotdebug_tb_auto_save_path" model="ir.ui.view">
+            <field name="name">view.hotdebug.tb.auto.save.path</field>
+            <field name="model">hotdebug.tb.auto.save.path</field>
+            <field name="type">form</field>
+            <field name="arch" type="xml">
+                <form string="Path for auto save tracebacks">
+                    <field name="path" colspan="4" width="500" />
+                    <button colspan="4" name="set_path" string="Set Path"
+                        type="object" icon="gtk-go-forward" />
+                </form>
+            </field>
+        </record>
+
         <record id="view_hotdebug_tb_locals_tree" model="ir.ui.view">
             <field name="name">hotdebug.tb.locals.tree</field>
             <field name="model">hotdebug.tb.locals</field>
                             <field name="last_value" nolabel="1" colspan="4" />
                             <field name="last_traceback"  string="tracebacks" widget="one2many_list" nolabel="1" readonly="1" colspan="4" />
                             <newline />
+
                             <button colspan="2" name="clear_tb" string="Clear Tracebacks"
                                 type="object" icon="gtk-go-forward" />
+                            <button colspan="1" name="import_tb" string="Import Tracebacks"
+                                type="object" icon="gtk-go-forward" />
+                            <button colspan="1" name="export_tb" string="Export Tracebacks"
+                                type="object" icon="gtk-go-forward" />
                             <newline />
-                            <button colspan="2" name="export_tb" string="Export Tracebacks"
+
+                            <field colspan="2" name="tb_save_path" attrs="{'invisible':[('tb_auto_save','=',False)]}" />
+                            <button colspan="1" name="enable_tb_auto_save" string="Enable Traceback Auto-saving"
                                 type="object" icon="gtk-go-forward" />
-                            <button colspan="2" name="import_tb" string="Import Tracebacks"
+                            <button colspan="1" name="disable_tb_auto_save" string="Disable Traceback Auto-saving"
                                 type="object" icon="gtk-go-forward" />
+
                         </page>
                         <page string="Profile">
                             <field name="profile_server" invisible="1" />