Hack of the day: transaction management

Need help, or want to share a macro? Post here!
DeepSOIC
Posts: 4505
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Hack of the day: transaction management

Postby DeepSOIC » Tue Mar 21, 2017 12:23 am

Hi!
I'm still learning python. Today it struck me, that python's with statement and transactions are friends.
Start with this:

Code: Select all

class Transaction(object):
    def __init__(self, title, doc= None):
        if doc is None:
            doc = App.ActiveDocument
        self.title = title
        self.document = doc
       
    def __enter__(self):
        self.document.openTransaction(self.title)
   
    def __exit__(self, exc_type, exc_value, exc_traceback):
        if exc_value is None:
            self.document.commitTransaction()
        else:
            self.document.abortTransaction()
           

Now, transaction management becomes neat and tidy:

Code: Select all

with Transaction("Create Cube"):
    App.ActiveDocument.addObject("Part::Box")

This automatically opens and closes a transaction. And if exception happens inside with, abortTransaction is called, and all the mess that was created before the exception fires is automatically cleaned up.

Isn't it nice? Shouldn't we integrate this into FC?
User avatar
yorik
Site Admin
Posts: 8570
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: Hack of the day: transaction management

Postby yorik » Tue Mar 21, 2017 3:06 pm

Wow, that's neat!!
User avatar
microelly2
Posts: 2363
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Hack of the day: transaction management

Postby microelly2 » Wed Mar 22, 2017 9:06 am

realy nice idea for exception handling. :D
User avatar
microelly2
Posts: 2363
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Hack of the day: transaction management

Postby microelly2 » Wed Mar 22, 2017 9:51 am

DeepSOIC wrote:Isn't it nice? Shouldn't we integrate this into FC?



It would be nice to have a common python toolbox for user interactions

Code: Select all

from PySide import QtGui
import sys,traceback

def showdialog(title="Fehler",text="Schau in den ReportView fuer mehr Details",detail=None):
   msg = QtGui.QMessageBox()
   msg.setIcon(QtGui.QMessageBox.Warning)
   msg.setText(text)
   msg.setWindowTitle(title)
   if detail<>None:   msg.setDetailedText(detail)
   msg.exec_()

def sayexc(title='Fehler',mess=''):
   exc_type, exc_value, exc_traceback = sys.exc_info()
   ttt=repr(traceback.format_exception(exc_type, exc_value,exc_traceback))
   lls=eval(ttt)
   l=len(lls)
   l2=lls[(l-3):]
   FreeCAD.Console.PrintError(mess + "\n" +"-->  ".join(l2))
   showdialog(title,text=mess,detail="--> ".join(l2))



I often use this to pop up critical events because many users do not read the report log window.
DeepSOIC
Posts: 4505
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: Hack of the day: transaction management

Postby DeepSOIC » Wed Mar 22, 2017 4:49 pm

microelly2 wrote:It would be nice to have a common python toolbox for user interactions

Can be integrated into Show module, which is basically aimed at that kind of things.