Hack of the day: transaction management

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Hack of the day: transaction management

Post by DeepSOIC »

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
Founder
Posts: 13640
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Hack of the day: transaction management

Post by yorik »

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

Re: Hack of the day: transaction management

Post by microelly2 »

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

Re: Hack of the day: transaction management

Post by microelly2 »

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.
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Hack of the day: transaction management

Post by DeepSOIC »

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.
Post Reply