uowdumper.py :  » Web-Frameworks » Spyce » spyce-2.1 » sqlalchemy » orm » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Spyce 
Spyce » spyce 2.1 » sqlalchemy » orm » uowdumper.py
from sqlalchemy.orm import unitofwork

"""dumps out a string representation of a UOWTask structure"""

class UOWDumper(unitofwork.UOWExecutor):
    def __init__(self, task, buf, verbose=False):
        self.verbose = verbose
        self.indent = 0
        self.task = task
        self.buf = buf
        self.starttask = task
        self.headers = {}
        self.execute(None, task)

    def execute(self, trans, task, isdelete=None):
        oldstarttask = self.starttask
        oldheaders = self.headers
        self.starttask = task
        self.headers = {}
        try:
            i = self._indent()
            if len(i):
                i += "-"
                #i = i[0:-1] + "-"
            if task.circular is not None:
                self.buf.write(self._indent() + "\n")
                self.buf.write(i + " " + self._repr_task(task))
                self.buf.write(" (contains cyclical sub-tasks)")
            else:
                self.buf.write(self._indent() + "\n")
                self.buf.write(i + " " + self._repr_task(task))
            self.buf.write(" (" + (isdelete and "delete " or "save/update ") + "phase) \n")
            self.indent += 1
            super(UOWDumper, self).execute(trans, task, isdelete)
        finally:
            self.indent -= 1
            if self.starttask.is_empty():   
                self.buf.write(self._indent() + "   |- (empty task)\n")
            else:
                self.buf.write(self._indent() + "   |----\n")

            self.buf.write(self._indent() + "\n")           
            self.starttask = oldstarttask
            self.headers = oldheaders
            
    def save_objects(self, trans, task):
        # sort elements to be inserted by insert order
        def comparator(a, b):
            if a.obj is None:
                x = None
            elif not hasattr(a.obj, '_sa_insert_order'):
                x = None
            else:
                x = a.obj._sa_insert_order
            if b.obj is None:
                y = None
            elif not hasattr(b.obj, '_sa_insert_order'):
                y = None
            else:
                y = b.obj._sa_insert_order
            return cmp(x, y)
        
        l = list(task.polymorphic_tosave_elements)
        l.sort(comparator)
        for rec in l:
            if rec.listonly:
                continue
            self.header("Save elements"+ self._inheritance_tag(task))
            self.buf.write(self._indent() + "- " + self._repr_task_element(rec)  + "\n")
            self.closeheader()
            
    def delete_objects(self, trans, task):
        for rec in task.polymorphic_todelete_elements:
            if rec.listonly:
                continue
            self.header("Delete elements"+ self._inheritance_tag(task))
            self.buf.write(self._indent() + "- " + self._repr_task_element(rec)  + "\n")
            self.closeheader()

    def _inheritance_tag(self, task):
        if not self.verbose:
            return ""
        elif task is not self.starttask:
            return (" (inheriting task %s)" % self._repr_task(task))
        else:
            return ""

    def header(self, text):
        """write a given header just once"""
        if not self.verbose:
            return
        try:
            self.headers[text]
        except KeyError:
            self.buf.write(self._indent() +  "- " + text + "\n")
            self.headers[text] = True

    def closeheader(self):
        if not self.verbose:
            return
        self.buf.write(self._indent() + "- ------\n")

    def execute_dependency(self, transaction, dep, isdelete):
        self._dump_processor(dep, isdelete)

    def execute_save_steps(self, trans, task):
        super(UOWDumper, self).execute_save_steps(trans, task)

    def execute_delete_steps(self, trans, task):    
        super(UOWDumper, self).execute_delete_steps(trans, task)

    def execute_dependencies(self, trans, task, isdelete=None):
        super(UOWDumper, self).execute_dependencies(trans, task, isdelete)

    def execute_childtasks(self, trans, task, isdelete=None):
        self.header("Child tasks" + self._inheritance_tag(task))
        super(UOWDumper, self).execute_childtasks(trans, task, isdelete)
        self.closeheader()
        
    def execute_cyclical_dependencies(self, trans, task, isdelete):
        self.header("Cyclical %s dependencies" % (isdelete and "delete" or "save"))
        super(UOWDumper, self).execute_cyclical_dependencies(trans, task, isdelete)
        self.closeheader()
            
    def execute_per_element_childtasks(self, trans, task, isdelete):
        super(UOWDumper, self).execute_per_element_childtasks(trans, task, isdelete)

    def execute_element_childtasks(self, trans, element, isdelete):
        self.header("%s subelements of UOWTaskElement(%s)" % ((isdelete and "Delete" or "Save"), hex(id(element))))
        super(UOWDumper, self).execute_element_childtasks(trans, element, isdelete)
        self.closeheader()
        
    def _dump_processor(self, proc, deletes):
        if deletes:
            val = proc.targettask.polymorphic_todelete_elements
        else:
            val = proc.targettask.polymorphic_tosave_elements

        if self.verbose:
            self.buf.write(self._indent() + "   |- %s attribute on %s (UOWDependencyProcessor(%d) processing %s)\n" % (
                repr(proc.processor.key), 
                    ("%s's to be %s" % (self._repr_task_class(proc.targettask), deletes and "deleted" or "saved")),
                hex(id(proc)), 
                self._repr_task(proc.targettask))
            )
        elif False:
            self.buf.write(self._indent() + "   |- %s attribute on %s\n" % (
                repr(proc.processor.key), 
                    ("%s's to be %s" % (self._repr_task_class(proc.targettask), deletes and "deleted" or "saved")),
                )
            )
            
        if len(val) == 0:
            if self.verbose:
                self.buf.write(self._indent() + "   |- " + "(no objects)\n")
        for v in val:
            self.buf.write(self._indent() + "   |- " + self._repr_task_element(v, proc.processor.key, process=True) + "\n")

    def _repr_task_element(self, te, attribute=None, process=False):
        if te.obj is None:
            objid = "(placeholder)"
        else:
            if attribute is not None:
                objid = "%s(%s).%s" % (te.obj.__class__.__name__, hex(id(te.obj)), attribute)
            else:
                objid = "%s(%s)" % (te.obj.__class__.__name__, hex(id(te.obj)))
        if self.verbose:
            return "%s (UOWTaskElement(%s, %s))" % (objid, hex(id(te)), (te.listonly and 'listonly' or (te.isdelete and 'delete' or 'save')))
        elif process:
            return "Process %s" % (objid)
        else:
            return "%s %s" % ((te.isdelete and "Delete" or "Save"), objid)

    def _repr_task(self, task):
        if task.mapper is not None:
            if task.mapper.__class__.__name__ == 'Mapper':
                name = task.mapper.class_.__name__ + "/" + task.mapper.local_table.name + "/" + str(task.mapper.entity_name)
            else:
                name = repr(task.mapper)
        else:
            name = '(none)'
        if task.circular_parent:
            return ("UOWTask(%s->%s, %s)" % (hex(id(task.circular_parent)), hex(id(task)), name))
        else:
            return ("UOWTask(%s, %s)" % (hex(id(task)), name))
        
    def _repr_task_class(self, task):
        if task.mapper is not None and task.mapper.__class__.__name__ == 'Mapper':
            return task.mapper.class_.__name__
        else:
            return '(none)'

    def _repr(self, obj):
        return "%s(%s)" % (obj.__class__.__name__, hex(id(obj)))

    def _indent(self):
        return "   |" * self.indent

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.