Commits

jayven committed 717f8d6

Start to add import/export for traceback

Comments (0)

Files changed (2)

 # -*- coding: utf-8 -*-
 # vim: set ai et si ts=4 sts=4 sw=4:
 
-import sys
+import sys, json
 import pooler
 import linecache
+from datetime import datetime
 from itertools import izip, islice
 from pprint import pformat
 from osv import osv, fields
             'profile_server': profile_server,
             'profile_result': profile_result,
         }, context=context)
+
+    def export_tb(self, cr, uid, ids, context=None):
+        rec = self.browse(cr, uid, ids[0], context=context)
+        res = {
+            'last_type': rec.last_type,
+            'last_value': rec.last_value,
+            'last_traceback': [],
+        }
+        for tb in rec.last_traceback:
+            t = {
+                'depth': tb.depth,
+                'filename': tb.filename,
+                'where': tb.where,
+                'lineno': tb.lineno,
+                'line': tb.line,
+                'lines': tb.lines,
+                'locals': [],
+            }
+            for loc in tb.locals:
+                l = {
+                    'name': loc.name,
+                    'value': loc.value,
+                }
+                t['locals'].append((0, 0, l))
+            res['last_traceback'].append((0, 0, t))
+        res = json.dumps(res, separators=(',', ':'))    
+        res_id = self.pool.get('hotdebug.tb.export').create(cr, uid, {
+            'file': res,
+            'filename': datetime.now().strftime(
+                'TracebackExport-%Y%m%d%H%M%S'),
+        }, context=context)
+        return {
+            'name': 'Export Traceback',
+            'view_mode': 'form',
+            'view_id': False,
+            'view_type': 'form',
+            'res_id': res_id,
+            'res_model': 'hotdebug.tb.export',
+            'type': 'ir.actions.act_window',
+            'nodestroy': True,
+            'target': 'new',
+            'context': context,
+        }
+
+    def import_tb(self, cr, uid, ids, context=None):
+        res_id = self.pool.get('hotdebug.tb.import').create(cr, uid, {
+            'console': ids[0],
+        }, context=context)
+        return {
+            'name': 'Import Traceback',
+            'view_mode': 'form',
+            'view_id': False,
+            'view_type': 'form',
+            'res_id': res_id,
+            'res_model': 'hotdebug.tb.import',
+            'type': 'ir.actions.act_window',
+            'nodestroy': True,
+            'target': 'new',
+            'context': context,
+        }
         
     _name = 'hotdebug.console'
     _columns = {
     }
 
 TracebackLocals()
+
+
+class TracebackExport(osv.osv_memory):
+
+    _name = 'hotdebug.tb.export'
+    _columns = {
+        'filename': fields.char('Export File Name', size=128),
+        'file': fields.binary('Export File'),
+    }
+
+
+TracebackExport()
+
+
+class TracebackImport(osv.osv_memory):
+
+    def import_tb(self, cr, uid, ids, context=None):
+        rec = self.browse(cr, uid, ids[0], context=context)
+        if not rec.file:
+            raise osv.except_osv('Error', 'Please select import file')
+        try:
+            value = json.loads(rec.file)
+        except ValueError:
+            raise osv.except_osv('Error', 'Bad content')
+        # TODO: more check for value
+        console_obj = self.pool.get('hotdebug.console')
+        console_id = rec.console.id
+        console_obj._clear_exc(cr, uid, console_id, context)
+        console_obj.write(cr, uid, [console_id,], value, context=context)
+        return {'type': 'ir.actions.act_window_close'}
+
+    _name = 'hotdebug.tb.import'
+    _columns = {
+        'console': fields.many2one('hotdebug.console', 'Console',
+            readonly=True),
+        'filename': fields.char('Import File Name', size=128),
+        'file': fields.binary('Import File'),
+    }
+
+
+TracebackImport()
 <?xml version="1.0" encoding="utf-8"?>
 <openerp>
     <data>
+        <record id="view_hotdebug_tb_export" model="ir.ui.view">
+            <field name="name">hotdebug.tb.export</field>
+            <field name="model">hotdebug.tb.export</field>
+            <field name="type">form</field>
+            <field name="arch" type="xml">
+                <form string="Export Tracebacks">
+                    <field name="filename" select="1" colspan="4"/>
+                    <field name="file" filename="filename" colspan="4"/>
+                </form>
+            </field>
+        </record>
+
+        <record id="view_hotdebug_tb_import" model="ir.ui.view">
+            <field name="name">hotdebug.tb.import</field>
+            <field name="model">hotdebug.tb.import</field>
+            <field name="type">form</field>
+            <field name="arch" type="xml">
+                <form string="Import Tracebacks">
+                    <field name="filename" select="1" colspan="4"/>
+                    <field name="file" filename="filename" colspan="4"/>
+                    <button colspan="4" name="import_tb" string="Import it"
+                        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>
                         <page string="Last exception">
                             <field name="catch_console_exception" invisible="1" />
                             <field name="catch_server_exception" invisible="1" />
-                            <button colspan="2" name="set_catch_console_exception" string="Catch console exception"
+                            <button colspan="2" name="set_catch_console_exception" string="Start to catch console exception"
                                 attrs="{'readonly':[('catch_console_exception','=',True)]}"
                                 context="{'catch_console_exception':True}"
                                 type="object" icon="gtk-go-forward" />
-                            <button colspan="2" name="set_catch_console_exception" string="Don't catch console exception"
+                            <button colspan="2" name="set_catch_console_exception" string="Stop to catch console exception"
                                 attrs="{'readonly':[('catch_console_exception','=',False)]}"
                                 context="{'catch_console_exception':False}"
                                 type="object" icon="gtk-go-forward" />
-                            <button colspan="2" name="set_catch_server_exception" string="Catch server exception (remember to press fresh)"
+                            <button colspan="2" name="set_catch_server_exception" string="Start to catch server exception (remember to press fresh)"
                                 attrs="{'readonly':[('catch_server_exception','=',True)]}"
                                 context="{'catch_server_exception':True}"
                                 type="object" icon="gtk-go-forward" />
-                            <button colspan="2" name="set_catch_server_exception" string="Don't catch server exception"
+                            <button colspan="2" name="set_catch_server_exception" string="Stop to catch server exception"
                                 attrs="{'readonly':[('catch_server_exception','=',False)]}"
                                 context="{'catch_server_exception':False}"
                                 type="object" icon="gtk-go-forward" />
                             <separator colspan="4" string="Last exception value" />
                             <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="export_tb" string="Export Tracebacks"
+                                type="object" icon="gtk-go-forward" />
+                            <button colspan="2" name="import_tb" string="Import Tracebacks"
+                                type="object" icon="gtk-go-forward" />
                         </page>
                         <page string="Profile">
                             <field name="profile_server" invisible="1" />