Exemple #1
0
class TreeCanvas(tk.Canvas):

    def __init__(self, master, tagger=None):
        tk.Canvas.__init__(self, master)
        self.tagger = tagger or Tagger()
        self.config(bg='white')
        # add scrollbar
        self.scrollbar = tk.Scrollbar(master)
        self.config(yscrollcommand=self.scrollbar.set)
        self.scrollbar.config(command=self.yview)
        # pack widgets
        self.pack(side=tk.LEFT, expand=tk.YES, fill=tk.BOTH)
        self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

    def wheel(self, event):
        if event.num == 5 or event.delta <= -120:
            self.yview_scroll(2, 'units')
        if event.num == 4 or event.delta >= 120:
            self.yview_scroll(-2, 'units')

    def show(self, name, data, expanded=False):
        self.node = TreeNode(self, None, self.tagger.item(data, name))
        self.node.expand()
        if expanded:
            for item in self.node.children:
                self._expand(item)
        self.node.update()

    def _expand(self, node):
        node.expand()
        for item in node.children:
            self._expand(item)
def StackBrowser(root, flist=None, tb=None, top=None):
    if top is None:
        top = tk.Toplevel(root)
    sc = ScrolledCanvas(top, bg="white", highlightthickness=0)
    sc.frame.pack(expand=1, fill="both")
    item = StackTreeItem(flist, tb)
    node = TreeNode(sc.canvas, None, item)
    node.expand()
Exemple #3
0
def StackBrowser(root, flist=None, tb=None, top=None):
    if top is None:
        top = tk.Toplevel(root)
    sc = ScrolledCanvas(top, bg="white", highlightthickness=0)
    sc.frame.pack(expand=1, fill="both")
    item = StackTreeItem(flist, tb)
    node = TreeNode(sc.canvas, None, item)
    node.expand()
Exemple #4
0
def StackBrowser(root, flist = None, tb = None, top = None):
    if top is None:
        from Tkinter import Toplevel
        top = Toplevel(root)
    sc = ScrolledCanvas(top, bg='white', highlightthickness=0)
    sc.frame.pack(expand=1, fill='both')
    item = StackTreeItem(flist, tb)
    node = TreeNode(sc.canvas, None, item)
    node.expand()
    return
Exemple #5
0
def StackBrowser(root, flist=None, tb=None, top=None):
    if top is None:
        from Tkinter import Toplevel
        top = Toplevel(root)
    sc = ScrolledCanvas(top, bg='white', highlightthickness=0)
    sc.frame.pack(expand=1, fill='both')
    item = StackTreeItem(flist, tb)
    node = TreeNode(sc.canvas, None, item)
    node.expand()
    return
    def reConstructBinaryTree(self, pre, tin):

        if len(pre) < 1:
            return None

        root = TreeNode(pre[0])
        index = tin.index(root.val)

        root.left = self.reConstructBinaryTree(pre[1:1 + index], pre[:index])
        root.right = self.reConstructBinaryTree(tin[1 + index:],
                                                tin[index + 1:])
Exemple #7
0
def _test():
    import sys
    from Tkinter import Tk
    root = Tk()
    root.configure(bd=0, bg="yellow")
    root.focus_set()
    sc = ScrolledCanvas(root, bg="white", highlightthickness=0, takefocus=1)
    sc.frame.pack(expand=1, fill="both")
    item = make_objecttreeitem("sys", sys)
    node = TreeNode(sc.canvas, None, item)
    node.update()
    root.mainloop()
Exemple #8
0
def _test():
    import sys
    from Tkinter import Tk
    root = Tk()
    root.configure(bd=0, bg="yellow")
    root.focus_set()
    sc = ScrolledCanvas(root, bg="white", highlightthickness=0, takefocus=1)
    sc.frame.pack(expand=1, fill="both")
    item = make_objecttreeitem("sys", sys)
    node = TreeNode(sc.canvas, None, item)
    node.update()
    root.mainloop()
Exemple #9
0
def _object_browser(parent):
    import sys
    from tkinter import Tk
    root = Tk()
    width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
    root.geometry("+%d+%d"%(x, y + 100))
    root.configure(bd=0, bg="yellow")
    root.focus_set()
    sc = ScrolledCanvas(root, bg="white", highlightthickness=0, takefocus=1)
    sc.frame.pack(expand=1, fill="both")
    item = make_objecttreeitem("sys", sys)
    node = TreeNode(sc.canvas, None, item)
    node.update()
    root.mainloop()
 def remote_stack_viewer(self):
     from idlelib import RemoteObjectBrowser
     oid = self.rpcclt.remotequeue("exec", "stackviewer", ("flist",), {})
     if oid is None:
         self.tkconsole.root.bell()
         return
     item = RemoteObjectBrowser.StubObjectTreeItem(self.rpcclt, oid)
     from idlelib.TreeWidget import ScrolledCanvas, TreeNode
     top = Toplevel(self.tkconsole.root)
     theme = idleConf.GetOption('main','Theme','name')
     background = idleConf.GetHighlight(theme, 'normal')['background']
     sc = ScrolledCanvas(top, bg=background, highlightthickness=0)
     sc.frame.pack(expand=1, fill="both")
     node = TreeNode(sc.canvas, None, item)
     node.expand()
 def remote_stack_viewer(self):
     from idlelib import RemoteObjectBrowser
     oid = self.rpcclt.remotequeue("exec", "stackviewer", ("flist",), {})
     if oid is None:
         self.tkconsole.root.bell()
         return
     item = RemoteObjectBrowser.StubObjectTreeItem(self.rpcclt, oid)
     from idlelib.TreeWidget import ScrolledCanvas, TreeNode
     top = Toplevel(self.tkconsole.root)
     theme = idleConf.GetOption('main','Theme','name')
     background = idleConf.GetHighlight(theme, 'normal')['background']
     sc = ScrolledCanvas(top, bg=background, highlightthickness=0)
     sc.frame.pack(expand=1, fill="both")
     node = TreeNode(sc.canvas, None, item)
     node.expand()
Exemple #12
0
def _object_browser(parent):
    import sys
    from Tkinter import Tk
    root = Tk()
    root.title("Test ObjectBrowser")
    width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
    root.geometry("+%d+%d" % (x, y + 150))
    root.configure(bd=0, bg="yellow")
    root.focus_set()
    sc = ScrolledCanvas(root, bg="white", highlightthickness=0, takefocus=1)
    sc.frame.pack(expand=1, fill="both")
    item = make_objecttreeitem("sys", sys)
    node = TreeNode(sc.canvas, None, item)
    node.update()
    root.mainloop()
Exemple #13
0
 def init(self, flist):
     self.flist = flist
     # reset pyclbr
     pyclbr._modules.clear()
     # create top
     self.top = top = ListedToplevel(flist.root)
     top.protocol("WM_DELETE_WINDOW", self.close)
     top.bind("<Escape>", self.close)
     if self._htest:  # place dialog below parent if running htest
         top.geometry(
             "+%d+%d" %
             (flist.root.winfo_rootx(), flist.root.winfo_rooty() + 200))
     self.settitle()
     top.focus_set()
     # create scrolled canvas
     theme = idleConf.GetOption('main', 'Theme', 'name')
     background = idleConf.GetHighlight(theme, 'normal')['background']
     sc = ScrolledCanvas(top,
                         bg=background,
                         highlightthickness=0,
                         takefocus=1)
     sc.frame.pack(expand=1, fill="both")
     item = self.rootnode()
     self.node = node = TreeNode(sc.canvas, None, item)
     node.update()
     node.expand()
Exemple #14
0
 def show(self, name, data, expanded=False):
     self.node = TreeNode(self, None, self.tagger.item(data, name))
     self.node.expand()
     if expanded:
         for item in self.node.children:
             self._expand(item)
     self.node.update()
Exemple #15
0
 def init(self, flist):
     self.flist = flist
     # reset pyclbr
     pyclbr._modules.clear()
     # create top
     # NOTE: later we will be passed in container, rather than creating it
     self.top = top = flist.new_container(own_window=True)
     self.top.add_component(self)
     top.top.bind("<Escape>", self.close)
     if self._htest:  # place dialog below parent if running htest
         top.top.geometry(
             "+%d+%d" %
             (flist.root.winfo_rootx(), flist.root.winfo_rooty() + 200))
     top.top.focus_set()
     # create scrolled canvas
     theme = idleConf.CurrentTheme()
     background = idleConf.GetHighlight(theme, 'normal')['background']
     sc = ScrolledCanvas(top.w,
                         bg=background,
                         highlightthickness=0,
                         takefocus=1)
     sc.frame.pack(expand=1, fill="both")
     item = self.rootnode()
     self.node = node = TreeNode(sc.canvas, None, item)
     node.update()
     node.expand()
Exemple #16
0
 def createtreemap(self,
                   tmrootnode=None,
                   sizepropname=SIZE_PROP,
                   clrpropname=CLR_PROP):
     del self.filetree
     if (tmrootnode != None):
         self.tmrootnode = tmrootnode
     ftreeroot = FileTreeItem(self.tmrootnode, self.tmcanvas)
     self.filetree = TreeNode(self.treecanvas, None, ftreeroot)
     self.filetree.update()
     self.filetree.expand()
     clrmap = self.getPropClrMap(self.tmrootnode, clrpropname)
     self.tmcanvas.set(tmcolormap=clrmap,
                       sizeprop=sizepropname,
                       clrprop=clrpropname,
                       upper=[1200, 700],
                       tooltip=self.tooltip)
     self.tmcanvas.drawTreemap(self.tmrootnode)
Exemple #17
0
 def createtreemap(self, tmrootnode=None,sizepropname=SIZE_PROP, clrpropname=CLR_PROP):
     del self.childtree
     if( tmrootnode != None):
         self.tmrootnode = tmrootnode
     ftreeroot = ChildTreeItem(self.tmrootnode, self.tmcanvas)
     self.childtree = TreeNode(self.treecanvas, None, ftreeroot)
     self.childtree.update()
     self.childtree.expand()
     clrmap = self.getPropClrMap(self.tmrootnode, clrpropname)
     self.tmcanvas.set(tmcolormap=clrmap,sizeprop=sizepropname,clrprop=clrpropname,upper=[1200,700],tooltip=self.tooltip)
     self.tmcanvas.drawTreemap(self.tmrootnode)
Exemple #18
0
    def __init__(self, N, edges: List[str]) -> 'Bitree':
        self.ans = []
        # constrains: 1 <= t, x, y, a, b <= N
        self.idx = [None] * (N + 1)  # [0] not used

        # The node 1 is the root of the tree.
        self.root = TreeNode(1)
        self.idx[1] = self.root

        while len(edges) > 0:
            resolving = []
            for e in edges:
                ev = e.strip().split()
                ev[0], ev[1] = int(ev[0]), int(ev[1])
                n0 = self.findNode(self.root, ev[0])
                n1 = self.findNode(self.root, ev[1])
                if n0 is None and n1 is None:
                    resolving.append(e)
                    continue

                if n0 is None and n1 is not None:
                    n0 = TreeNode(ev[0], n1)
                    n1.addChild(n0)  # no exception raised?
                elif n1 is None and n0 is not None:
                    n1 = TreeNode(ev[1])
                    n0.addChild(n1)
                self.idx[ev[0]], self.idx[ev[1]] = n0, n1
            edges = resolving
Exemple #19
0
 def init(self, flist):
     self.flist = flist
     pyclbr._modules.clear()
     self.top = top = ListedToplevel(flist.root)
     top.protocol('WM_DELETE_WINDOW', self.close)
     top.bind('<Escape>', self.close)
     self.settitle()
     top.focus_set()
     theme = idleConf.GetOption('main', 'Theme', 'name')
     background = idleConf.GetHighlight(theme, 'normal')['background']
     sc = ScrolledCanvas(top, bg=background, highlightthickness=0, takefocus=1)
     sc.frame.pack(expand=1, fill='both')
     item = self.rootnode()
     self.node = node = TreeNode(sc.canvas, None, item)
     node.update()
     node.expand()
     return
Exemple #20
0
 def createLeftSide(self):
     leftSide = Frame(self)
     leftSide.pack(side=LEFT, anchor=NW, expand=1, fill=BOTH)
     canvas = Canvas(leftSide)
     scrollbar = Scrollbar(leftSide, orient=VERTICAL)
     canvas.config(bg='white',
                   scrollregion=canvas.bbox(ALL),
                   yscrollcommand=scrollbar.set)
     canvas.pack(side=LEFT, expand=1, fill=BOTH)
     scrollbar.config(command=canvas.yview)
     scrollbar.pack(side=LEFT, fill=Y)
     l1 = self.createTOC()
     item = HelpMenu('Contents', 1, l1)
     node = TreeNode(canvas, None, item)
     node.update()
     node.expand()
Exemple #21
0
 def showDupListTree(self):
     mtreeroot = DupTreeItem("Duplicate List")
     #Add the tree items for various categories like (1-10 lines, 10-100 lines, 101-500 lines, More than 500 lines"
     dup500_ = mtreeroot.addChildName("More than 500 lines")
     dup101_500 = mtreeroot.addChildName("101-500 lines")
     dup11_100 = mtreeroot.addChildName("11-100 lines")
     dup1_10 = mtreeroot.addChildName("upto 10 lines")
     
     matchid = 0
     matches = self.getMatches()
     for matchset in matches:
         matchid = matchid+1
         matchnode = DupTreeItem("")
         lc = 0
         for match in matchset:
             fname = match.srcfile()
             lc = max(lc, match.getLineCount())
             start = match.getStartLine()
             matchnode.addChildName("%s (line %d - %d)" % (fname, start, start+lc))
         
         matchnode.name = "Match %d (Lines : %d)" % (matchid, lc)
         
         if( lc > 0 and lc <= 10):
             dup1_10.addChild(matchnode)
         elif( lc >10 and lc <= 100):
             dup11_100.addChild(matchnode)
         elif( lc >100 and lc < 500):
             dup101_500.addChild(matchnode)
         else:
             dup500_.addChild(matchnode)        
     
     for child in mtreeroot.children:
         child.name = "%s (count : %d)" % (child.name, len(child.children))
     
     #now create a 'tree display"
     self.filetree = TreeNode(self.duplisttree, None, mtreeroot)
     self.filetree.update()
     self.filetree.expand()
Exemple #22
0
        self.left = None
        self.right = None

class Solution(object):
    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        :左右分支的最大深度加1
        """
        if root == None:
            return 0
        else:
            return max(self.maxDepth(root.left),self.maxDepth(root.right))+1

t = TreeNode(1)
node1 = TreeNode(2)
node2 = TreeNode(3)
node3 = TreeNode(4)
node4 = TreeNode(5)
node5 = TreeNode(6)
node6 = TreeNode(7)
t.left = node1
t.right = node2
node1.left = node3
node1.right = node4
node2.left = node5
#node3.left = node6

s = Solution()
print s.maxDepth(t)
Exemple #23
0
class App(object):
    def __init__(self):
        self.root = Tkinter.Tk()
        self.root.title("Source Monitor Treemap")
        self.initMenu()
        self.initDropDown()
        self.pane = Tkinter.PanedWindow(self.root, orient=Tkinter.HORIZONTAL)
        self.pane.pack(fill=Tkinter.BOTH, expand=1)
        self.initTreeCanvas()
        self.initTreemapCanvas()
        self.filetree = None

    def initTreemapCanvas(self):
        self.tmcanvas = TreemapSquarified(self.pane, width='13i', height='8i')
        self.tmcanvas.config(bg='white')
        self.pane.config(bg='blue')
        self.pane.add(self.tmcanvas.frame)
        self.pane.paneconfigure(self.tmcanvas.frame,
                                sticky=Tkinter.N + Tkinter.S + Tkinter.E +
                                Tkinter.W)
        self.tooltip = TkCanvasToolTip(self.tmcanvas, follow=True)

    def initTreeCanvas(self):
        frame = Tkinter.Frame(self.pane)
        self.treecanvas = createScrollableCanvas(frame, width='2i')
        self.pane.add(frame)
        self.pane.paneconfigure(frame,
                                sticky=Tkinter.N + Tkinter.S + Tkinter.E +
                                Tkinter.W)

    def initMenu(self):
        menubar = Tkinter.Menu(self.root)
        filemenu = Tkinter.Menu(menubar, tearoff=0)
        filemenu.add_command(label="Open", command=self.openSMFile)
        filemenu.add_command(label="Exit", command=self.root.quit)
        menubar.add_cascade(label="File", menu=filemenu)
        self.root.config(menu=menubar)

    def initDropDown(self):
        self.dropdownframe = Tkinter.Frame(self.root)
        self.sizeOption = Tkinter.StringVar()
        self.sizeOption.set(SIZE_PROP)
        self.colorOption = Tkinter.StringVar()
        self.colorOption.set(CLR_PROP)
        # get the list of options
        options = set(SMPROP_MAPPING.itervalues())
        # now convert the set to sorted list
        options = sorted(options)

        self.optionsSize = Tkinter.OptionMenu(self.dropdownframe,
                                              self.sizeOption,
                                              command=self.optionchange,
                                              *options)
        self.optionsSize.grid(row=0, column=0)
        self.optionsClr = Tkinter.OptionMenu(self.dropdownframe,
                                             self.colorOption,
                                             command=self.optionchange,
                                             *options)
        self.optionsClr.grid(row=0, column=1)
        self.dropdownframe.pack()

    def optionchange(self, param):
        sizepropname = self.sizeOption.get()
        clrpropname = self.colorOption.get()
        self.createtreemap(sizepropname=sizepropname, clrpropname=clrpropname)

    def createtreemap(self,
                      tmrootnode=None,
                      sizepropname=SIZE_PROP,
                      clrpropname=CLR_PROP):
        del self.filetree
        if (tmrootnode != None):
            self.tmrootnode = tmrootnode
        ftreeroot = FileTreeItem(self.tmrootnode, self.tmcanvas)
        self.filetree = TreeNode(self.treecanvas, None, ftreeroot)
        self.filetree.update()
        self.filetree.expand()
        clrmap = self.getPropClrMap(self.tmrootnode, clrpropname)
        self.tmcanvas.set(tmcolormap=clrmap,
                          sizeprop=sizepropname,
                          clrprop=clrpropname,
                          upper=[1200, 700],
                          tooltip=self.tooltip)
        self.tmcanvas.drawTreemap(self.tmrootnode)

    def getNeutralVal(self, clrpropname, minval, maxval):
        neutralval = COLOR_PROP_CONFIG.get(clrpropname)

        if (neutralval == None):
            neutralval = 0.5 * (minval + maxval)
        return (neutralval)

    def getPropClrMap(self, tmrootnode, clrpropname):
        clrmap = TMColorMap(minclr=(0, 255, 0), maxclr=(255, 0, 0))
        minval = tmrootnode.minclr(clrpropname)
        maxval = tmrootnode.maxclr(clrpropname)
        neutralval = self.getNeutralVal(clrpropname, minval, maxval)
        clrmap.setlimits(minval, maxval, neutralval=neutralval)
        return (clrmap)

    def openSMFile(self):
        filename = tkFileDialog.askopenfilename(
            title="Choose Source Monitor output file",
            filetypes=SMFILEFORMATS,
            defaultextension=".xml")
        smtree = SMTree(filename)
        self.createtreemap(smtree)

    def run(self):
        self.root.mainloop()
Exemple #24
0
class App(object):
    def __init__(self):
        self.root = Tkinter.Tk()
        self.root.title("Source Monitor Treemap")
        self.initMenu()
        self.initDropDown()
        self.pane = Tkinter.PanedWindow(self.root, orient=Tkinter.HORIZONTAL)
        self.pane.pack(fill=Tkinter.BOTH, expand=1)
        self.initTreeCanvas()
        self.initTreemapCanvas()
        self.filetree = None

    def initTreemapCanvas(self):
        self.tmcanvas = TreemapSquarified(self.pane, width="13i", height="8i")
        self.tmcanvas.config(bg="white")
        self.pane.config(bg="blue")
        self.pane.add(self.tmcanvas.frame)
        self.pane.paneconfigure(self.tmcanvas.frame, sticky=Tkinter.N + Tkinter.S + Tkinter.E + Tkinter.W)
        self.tooltip = TkCanvasToolTip(self.tmcanvas, follow=True)

    def initTreeCanvas(self):
        frame = Tkinter.Frame(self.pane)
        self.treecanvas = createScrollableCanvas(frame, width="2i")
        self.pane.add(frame)
        self.pane.paneconfigure(frame, sticky=Tkinter.N + Tkinter.S + Tkinter.E + Tkinter.W)

    def initMenu(self):
        menubar = Tkinter.Menu(self.root)
        filemenu = Tkinter.Menu(menubar, tearoff=0)
        filemenu.add_command(label="Open", command=self.openSMFile)
        filemenu.add_command(label="Exit", command=self.root.quit)
        menubar.add_cascade(label="File", menu=filemenu)
        self.root.config(menu=menubar)

    def initDropDown(self):
        self.dropdownframe = Tkinter.Frame(self.root)
        self.sizeOption = Tkinter.StringVar()
        self.sizeOption.set(SIZE_PROP)
        self.colorOption = Tkinter.StringVar()
        self.colorOption.set(CLR_PROP)
        # get the list of options
        options = set(SMPROP_MAPPING.itervalues())
        # now convert the set to sorted list
        options = sorted(options)

        self.optionsSize = Tkinter.OptionMenu(self.dropdownframe, self.sizeOption, command=self.optionchange, *options)
        self.optionsSize.grid(row=0, column=0)
        self.optionsClr = Tkinter.OptionMenu(self.dropdownframe, self.colorOption, command=self.optionchange, *options)
        self.optionsClr.grid(row=0, column=1)
        self.dropdownframe.pack()

    def optionchange(self, param):
        sizepropname = self.sizeOption.get()
        clrpropname = self.colorOption.get()
        self.createtreemap(sizepropname=sizepropname, clrpropname=clrpropname)

    def createtreemap(self, tmrootnode=None, sizepropname=SIZE_PROP, clrpropname=CLR_PROP):
        del self.filetree
        if tmrootnode != None:
            self.tmrootnode = tmrootnode
        ftreeroot = FileTreeItem(self.tmrootnode, self.tmcanvas)
        self.filetree = TreeNode(self.treecanvas, None, ftreeroot)
        self.filetree.update()
        self.filetree.expand()
        clrmap = self.getPropClrMap(self.tmrootnode, clrpropname)
        self.tmcanvas.set(
            tmcolormap=clrmap, sizeprop=sizepropname, clrprop=clrpropname, upper=[1200, 700], tooltip=self.tooltip
        )
        self.tmcanvas.drawTreemap(self.tmrootnode)

    def getNeutralVal(self, clrpropname, minval, maxval):
        neutralval = COLOR_PROP_CONFIG.get(clrpropname)

        if neutralval == None:
            neutralval = 0.5 * (minval + maxval)
        return neutralval

    def getPropClrMap(self, tmrootnode, clrpropname):
        clrmap = TMColorMap(minclr=(0, 255, 0), maxclr=(255, 0, 0))
        minval = tmrootnode.minclr(clrpropname)
        maxval = tmrootnode.maxclr(clrpropname)
        neutralval = self.getNeutralVal(clrpropname, minval, maxval)
        clrmap.setlimits(minval, maxval, neutralval=neutralval)
        return clrmap

    def openSMFile(self):
        filename = tkFileDialog.askopenfilename(
            title="Choose Source Monitor output file", filetypes=SMFILEFORMATS, defaultextension=".xml"
        )
        smtree = SMTree(filename)
        self.createtreemap(smtree)

    def run(self):
        self.root.mainloop()
from Tkinter import Tk

from idlelib.TreeWidget import ScrolledCanvas, FileTreeSystem, TreeNode
import os

root = Tk()
root.title("File Browser")

sc = ScrolledCanvas(root, bg="white", highlightthickness=0, takefocus=1)
sc.frame.pack(expand=1, fill="both", side="left")

item = FileTreeItem(os.getcwd())
node = TreeNode(sc.canvas, None, item)
node.expand()

root.mainloop()

class Solution(object):
    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        :左右分支的最大深度加1
        """
        if root == None:
            return 0
        else:
            return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1


t = TreeNode(1)
node1 = TreeNode(2)
node2 = TreeNode(3)
node3 = TreeNode(4)
node4 = TreeNode(5)
node5 = TreeNode(6)
node6 = TreeNode(7)
t.left = node1
t.right = node2
node1.left = node3
node1.right = node4
node2.left = node5
#node3.left = node6

s = Solution()
print s.maxDepth(t)
Exemple #27
0
try:
    from Tkinter import Tk
except ImportError:
    from tkinter import Tk

from idlelib.TreeWidget import ScrolledCanvas, FileTreeItem, TreeNode
import os

root = Tk()
root.title("Test TreeWidget")

sc = ScrolledCanvas(root, bg="white", highlightthickness=0, takefocus=1)
sc.frame.pack(expand=1, fill="both", side="left")

item = FileTreeItem(os.getcwd())
node = TreeNode(sc.canvas, None, item)
node.expand()

root.mainloop()
Exemple #28
0
class CDDApp:
    def __init__(self, dirname, options):
        self.dirname=dirname
        self.options = options
        self.filelist = None
        self.matches = None
        self.dupsInFile = None

    def getFileList(self):
        if( self.filelist == None):
            if( self.options.pattern ==''):
                self.filelist = PreparePygmentsFileList(self.dirname)
            else:
                rawfilelist = GetDirFileList(self.dirname)
                self.filelist = fnmatch.filter(rawfilelist,self.options.pattern)
                
        return(self.filelist)

    def run(self):
        filelist = self.getFileList()        
        self.cdd = CodeDupDetect(filelist,self.options.minimum)
        self.PrintDuplicates()
        if self.options.report:
            self.cdd.html_output(self.options.report)
        if self.options.comments:
            self.cdd.insert_comments(self.dirname)
        
        if( self.options.treemap == True and self.foundMatches()):
            self.ShowDuplicatesTreemap()
            self.root.mainloop()        
    
    def PrintDuplicates(self):
        tm1 = datetime.datetime.now()
        with FileOrStdout(self.options.filename) as output:
            exactmatch = self.cdd.printmatches(output)
            tm2 = datetime.datetime.now()
            output.write("time to find matches - %s\n" %(tm2-tm1))

    def foundMatches(self):
        '''
        return true if there is atleast one match found.
        '''
        matches = self.getMatches()
        return( len(matches) > 0)
            
    def ShowDuplicatesTreemap(self):
        assert(self.foundMatches()==True)
        self.initTk()
        self.createTreemap()
        self.showDupListTree()
        
    def initTk(self):
        self.root = Tkinter.Tk()
        self.root.title("Code Duplication Treemap")
        self.pane = Tkinter.PanedWindow(self.root, orient=Tkinter.HORIZONTAL)
        self.pane.pack(fill=Tkinter.BOTH, expand=1)
        self.initDupListTree()
        self.initTreemap()
        
    def initDupListTree(self):
        frame = Tkinter.Frame(self.pane)
        self.duplisttree = createScrollableCanvas(frame, width='2i')
        self.pane.add(frame)
        self.pane.paneconfigure(frame, sticky=Tkinter.N+Tkinter.S+Tkinter.E+Tkinter.W)

    def initTreemap(self):
        self.tmcanvas = TreemapSquarified(self.pane,width='13i', height='8i',leafnodecb = self.tmLeafnodeCallback)
        self.tmcanvas.config(bg='white')
        self.pane.add(self.tmcanvas.frame)
        self.pane.paneconfigure(self.tmcanvas.frame, sticky=Tkinter.N+Tkinter.S+Tkinter.E+Tkinter.W)
        self.tooltip = TkCanvasToolTip(self.tmcanvas, follow=True)

    def getColormap(self,tmrootnode):
        clrmap = TMColorMap(minclr=(0,255,0),maxclr=(255,0,0))        
        minval = tmrootnode.minclr(CLR_PROP)
        maxval = tmrootnode.maxclr(CLR_PROP)
        clrmap.setlimits(minval,maxval, neutralval=3)
        return(clrmap)
        
    def createTreemap(self):
        tmrootnode = self.makeTree()
        clrmap = self.getColormap(tmrootnode)
        self.tmcanvas.set(tmcolormap=clrmap,sizeprop=SIZE_PROP,clrprop=CLR_PROP,upper=[1200,700])
        self.tmcanvas.drawTreemap(tmrootnode)
        self.tooltip.updatebindings()

    def getMatches(self):
        if( self.matches == None):
            exactmatches = self.cdd.findcopies()
            self.matches = sorted(exactmatches,reverse=True,key=lambda x:x.matchedlines)
        return(self.matches)
    
    def getMatchLcInfo(self):
        #get the line count information of the duplicates
        matches = self.getMatches()
        #convert the matches to dictionary of filename against the number of copied lines
        matchinfodict = dict()
        for matchset in matches:
            for match in matchset:
                fname = match.srcfile()
                lc = matchinfodict.get(fname, 0)
                matchinfodict[fname] = lc + match.getLineCount()
        return(matchinfodict)

    def getLcInfo(self):
        #get the line count information about the files
        filelist = self.getFileList()
        lcinfodict = dict()
        for fname in filelist:
            with open(fname, "r") as f:
                lines = f.readlines()
                lcinfodict[fname] = len(lines)
        return(lcinfodict)
    
    def makeTree(self):
        matchlcinfo = self.getMatchLcInfo()
        lcinfo = self.getLcInfo()        
        tmrootnode = TreemapNode("Duplication Map - ")
        for fname, lc in lcinfo.iteritems():
            namelist = fname.split(os.sep)
            node = tmrootnode.addChild(namelist)
            node.setProp(SIZE_PROP, lc)
            node.setProp(CLR_PROP, matchlcinfo.get(fname, 0))
            node.setProp('filename', fname)
        tmrootnode.MergeSingleChildNodes('/')
        return(tmrootnode)

    def getDupsInFile(self,fname):
        if( self.dupsInFile == None):
            matches = self.getMatches()
            self.dupsInFile = dict()
            for matchset in matches:
                for match in matchset:
                    fname = match.srcfile()
                    duplist = self.dupsInFile.get(fname, [])
                    duplist.append((match.getStartLine(), match.getLineCount()))
                    self.dupsInFile[fname] = duplist
        return(self.dupsInFile.get(fname, None))
                
        
    def showDupListTree(self):
        mtreeroot = DupTreeItem("Duplicate List")
        #Add the tree items for various categories like (1-10 lines, 10-100 lines, 101-500 lines, More than 500 lines"
        dup500_ = mtreeroot.addChildName("More than 500 lines")
        dup101_500 = mtreeroot.addChildName("101-500 lines")
        dup11_100 = mtreeroot.addChildName("11-100 lines")
        dup1_10 = mtreeroot.addChildName("upto 10 lines")
        
        matchid = 0
        matches = self.getMatches()
        for matchset in matches:
            matchid = matchid+1
            matchnode = DupTreeItem("")
            lc = 0
            for match in matchset:
                fname = match.srcfile()
                lc = max(lc, match.getLineCount())
                start = match.getStartLine()
                matchnode.addChildName("%s (line %d - %d)" % (fname, start, start+lc))
            
            matchnode.name = "Match %d (Lines : %d)" % (matchid, lc)
            
            if( lc > 0 and lc <= 10):
                dup1_10.addChild(matchnode)
            elif( lc >10 and lc <= 100):
                dup11_100.addChild(matchnode)
            elif( lc >100 and lc < 500):
                dup101_500.addChild(matchnode)
            else:
                dup500_.addChild(matchnode)        
        
        for child in mtreeroot.children:
            child.name = "%s (count : %d)" % (child.name, len(child.children))
        
        #now create a 'tree display"
        self.filetree = TreeNode(self.duplisttree, None, mtreeroot)
        self.filetree.update()
        self.filetree.expand()
        
    def tmLeafnodeCallback(self, node, tmcanvas, canvasid,lower,upper):
        '''
        draw the approx positions for the duplicates in 'dark brown' color on the
        canvas rectangles of that file.
        '''        
        width = upper[0]-lower[0]
        height = float(upper[1]-lower[1])
            
        if( width > 4 and height > 2):            
            fname = node.getProp('filename')
            assert(fname != None)
            dupsinfile = self.getDupsInFile(fname)
            #if dupsinfile is None, then there are no duplicates in file
            if( dupsinfile != None):
                fsize = float(node.getSize(SIZE_PROP))
                x = lower[0]
                y = lower[1]
                #show small magenta coloured strip of 4 pixels (rectwd) to represent the location
                #of duplication.
                rectwd = 4
                    
                for startline, linecount in dupsinfile:
                    rectht = int(height*(float(linecount)/fsize)+0.5)
                    if( rectht > 1):
                        rectstart = int(height*(float(startline)/fsize)+0.5)
                        #draw rectangle with 'magenta' color fill and no border (border width=0)
                        tmcanvas.create_rectangle(x+2, y+rectstart, x+rectwd+2, y+rectstart+rectht,
                                                  fill='magenta',width=0)
    def IsExpandable(self):
        node = self.node
        return node.hasChildNodes()

    def GetSubList(self):
        parent = self.node
        children = parent.childNodes
        prelist = [DomTreeItem(node) for node in children]
        itemlist = [item for item in prelist if item.GetText().strip()]
        return itemlist


data = '''
<Computer>
  <Local_Devices>
  </Local_Devices>
</Computer>
'''

root = Tk()
canvas = Canvas(root)
canvas.config(bg='white')
canvas.pack()
dom = parseString(data)
item = DomTreeItem(dom.documentElement)
node = TreeNode(canvas, None, item)
node.update()
node.expand()
root.mainloop()
Exemple #30
0
class App:
    def __init__(self):
        self.root = Tkinter.Tk()
        self.root.title("Source Monitor Treemap")
        self.initMenu()
        self.initDropDown()
        self.pane = Tkinter.PanedWindow(self.root, orient=Tkinter.HORIZONTAL)
        self.pane.pack(fill=Tkinter.BOTH, expand=1)
        self.initTreeCanvas()
        self.initTreemapCanvas()
        self.childtree = None

    def initTreemapCanvas(self):
        self.tmcanvas = TreemapSquarified(self.pane,width='13i', height='8i')
        self.tmcanvas.config(bg='white')
        #self.tmcanvas.grid(column=1,row=1, sticky="nsew")
        self.tmcanvas.pack()
        self.pane.add(self.tmcanvas.frame)
        self.tooltip = TkCanvasToolTip(self.tmcanvas, follow=True)
        
    def initTreeCanvas(self):
        frame = Tkinter.Frame(self.pane)
        self.treecanvas = createScrollableCanvas(frame, width='2i')
        self.treecanvas.pack()
        self.pane.add(frame)

    def initMenu(self):
        menubar = Tkinter.Menu(self.root)
        filemenu = Tkinter.Menu(menubar, tearoff=0)
        filemenu.add_command(label="Open", command=self.openTreemapFile)
        filemenu.add_command(label="Exit", command=self.root.quit)
        menubar.add_cascade(label="File", menu=filemenu)
        self.root.config(menu=menubar)

    def initDropDown(self):
        self.dropdownframe = Tkinter.Frame(self.root)
        self.sizeOption = Tkinter.StringVar()
        self.sizeOption.set(SIZE_PROP)
        self.colorOption = Tkinter.StringVar()
        self.colorOption.set(CLR_PROP)
        #get the list of options        
        options = ['Field 0', 'Field 1']
        
        self.optionsSize = Tkinter.OptionMenu(self.dropdownframe, self.sizeOption,command=self.optionchange, *options)
        self.optionsSize.grid(row=0, column=0)
        self.optionsClr = Tkinter.OptionMenu(self.dropdownframe,self.colorOption,command=self.optionchange, *options)
        self.optionsClr.grid(row=0, column=1)
        self.dropdownframe.pack()

    def optionchange(self, param):
        sizepropname = self.sizeOption.get()
        clrpropname = self.colorOption.get()
        self.createtreemap(sizepropname=sizepropname, clrpropname=clrpropname)
        
    def createtreemap(self, tmrootnode=None,sizepropname=SIZE_PROP, clrpropname=CLR_PROP):
        del self.childtree
        if( tmrootnode != None):
            self.tmrootnode = tmrootnode
        ftreeroot = ChildTreeItem(self.tmrootnode, self.tmcanvas)
        self.childtree = TreeNode(self.treecanvas, None, ftreeroot)
        self.childtree.update()
        self.childtree.expand()
        clrmap = self.getPropClrMap(self.tmrootnode, clrpropname)
        self.tmcanvas.set(tmcolormap=clrmap,sizeprop=sizepropname,clrprop=clrpropname,upper=[1200,700],tooltip=self.tooltip)
        self.tmcanvas.drawTreemap(self.tmrootnode)

    def getNeutralVal(self, clrpropname, minval, maxval):
        #neutralval = 0.5*(minval+maxval)
        neutralval = 15
        return(neutralval)
            
    def getPropClrMap(self,tmrootnode, clrpropname):
        clrmap = TMColorMap(minclr=(0,255,0),maxclr=(255,0,0))        
        minval = tmrootnode.minclr(clrpropname)
        maxval = tmrootnode.maxclr(clrpropname)
        neutralval = self.getNeutralVal(clrpropname, minval, maxval)
        clrmap.setlimits(minval,maxval, neutralval=neutralval)
        return(clrmap)

    def openTreemapFile(self):
        filename = tkFileDialog.askopenfilename(title="Choose Treemap CSV file", defaultextension=".csv")
        tmtree = TMTree(filename)
        self.createtreemap(tmtree)
            
    def run(self):
        self.root.mainloop()
Exemple #31
0
                        </Persistence>
                    </ServiceProfile>
                    <ServiceProfile>
                        <IsEnabled>false</IsEnabled>
                        <Protocol>TCP</Protocol>
                        <Port/>
                        <Persistence>
                            <Method/>
                        </Persistence>
                    </ServiceProfile>
                    <Logging>true</Logging>
                    <Pool>DCTM-UKHTTPD</Pool>
                </VirtualServer>
            </LoadBalancerService>
        </EdgeGatewayServiceConfiguration>
        <HaEnabled>true</HaEnabled>
        <UseDefaultRouteForDnsRelay>false</UseDefaultRouteForDnsRelay>
    </Configuration>
</EdgeGateway>
    '''
    root = Tk()
    canvas = Canvas(root)
    canvas.config(bg='white')
    canvas.pack()
    dom = parseString(example_data)
    item = DomTreeItem(dom.documentElement)
    node = TreeNode(canvas, None, item)
    node.update()
    node.expand()
    root.mainloop()
Exemple #32
0
 def __SetDom(self, dom):
   item = DomTreeItem(dom)
   node = TreeNode(self.__canvas, None, item)
   node.update()
   node.expand()