Search code examples
pythontwistedtwisted.web

How to plot tree of resources in twisted.web python


There is Resource class in module twisted.web.resource. Is it possible to output path to executing code of handlers or even classname by using of this class, when I'm making requests from browser? Make full tree of resources? It needs for making easier way of coding for developers.


Solution

  • Solved this problem. It's able to make changes for sources of twisted in such way: In site-packages/twisted/web/resource.py make metaclass with plotting of graphs with class-subclass relation

    import pygraphviz as pgv
    A=pgv.AGraph()
    A.node_attr.update(color="red", style="filled")
    A.edge_attr.update(color="blue", len="10.0", width="2.0")
    class Watcher(type):
        def __init__(cls, name, bases, clsdict):
            print(bases,"was subclassed by " + name)
            print("Class", cls)
            print("CLS DICT", clsdict)
            print("\n\n"*3)
            A.add_edge(bases, name)
            super(Watcher, cls).__init__(name, bases, clsdict)
            A.write('resources_graph.dot')
            B=pgv.AGraph('resources_graph.dot')
            B.layout()
            B.draw('resources_graph.png')
    class Resource:
        __metaclass__ = Watcher
    

    And also available to output way to handler of the current request by printing of

    resrc = self.site.getResourceFor(self)
    

    in the source code site-packages/twisted/web/server.py