~yacinechaouche/+junk/BZR

« back to all changes in this revision

Viewing changes to PROG/TAHAR-NG/code-browser-r2.py

  • Committer: yacinechaouche at yahoo
  • Date: 2015-01-14 22:23:03 UTC
  • Revision ID: yacinechaouche@yahoo.com-20150114222303-6gbtqqxii717vyka
Ajout de CODE et PROD. Il faudra ensuite ajouter ce qu'il y avait dan TMP

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import tahar
 
2
import sys
 
3
import cmd
 
4
 
 
5
def remove_whitespaces(line):
 
6
    return [e for e in line.split(" ") if e]
 
7
 
 
8
class CodeBrowser(cmd.Cmd):
 
9
    def __init__(self,stat):
 
10
        cmd.Cmd.__init__(self)
 
11
        self.prompt = "> "
 
12
        self.stat = stat.stats
 
13
        self.current_node = self.stat
 
14
 
 
15
    def path(self,node):
 
16
        parent = node.get("#parent")
 
17
        if not parent:
 
18
            return node.get("#name","root")
 
19
        parents_path = self.path(parent)
 
20
        parents_type = parent.get("#type")
 
21
        nodes_type   = node.get("#type","?")
 
22
        nodes_name   = node.get("#name","?")
 
23
        chunk        = ""
 
24
        if nodes_type == "class" :
 
25
            chunk = "#classes"
 
26
        elif nodes_type == "function":
 
27
            chunk = "#functions"
 
28
        elif nodes_type == "module":
 
29
            chunk = "#modules"
 
30
        elif nodes_type in ("#modules","#classes","#functions","#class_code","#module_code"):
 
31
            chunk = nodes_type
 
32
        return ">".join([parents_path,chunk,nodes_name])
 
33
 
 
34
    def change_node(self,dst,error_msg=""):
 
35
        next_node = self.current_node.get(dst)
 
36
        if next_node :
 
37
            self.current_node = next_node
 
38
        else:
 
39
            if error_msg :
 
40
                print error_msg
 
41
            else : 
 
42
                print "this node has no attribute named",dst
 
43
        self.do_show()
 
44
        #self.do_keys()
 
45
        
 
46
    def do_name(self,line=""):
 
47
        print self.current_node.get("#name")
 
48
 
 
49
    do_n = do_name
 
50
 
 
51
    def do_path(self,line=""):
 
52
        print self.path(self.current_node)
 
53
 
 
54
    def do_show(self,line=""):
 
55
        print "node name",
 
56
        self.do_name()
 
57
        print "node path",
 
58
        self.do_path()
 
59
        print "node ",
 
60
        self.do_keys()
 
61
            
 
62
    do_w = do_s = do_show
 
63
 
 
64
    def do_keys(self,line=""):
 
65
        print self.current_node.keys()
 
66
 
 
67
    do_k = do_down = do_keys
 
68
    
 
69
    def do_parent(self,line=""):
 
70
        self.change_node("#parent","this node has no parent")
 
71
 
 
72
    do_up = do_u = do_parent
 
73
        
 
74
    def do_functions(self,line=""):
 
75
        self.change_node("#functions","this node has no functions")
 
76
 
 
77
    def do_methods(self,line=""):
 
78
        self.change_node("#methods","this node has no methods")
 
79
 
 
80
    def do_methods(self,line=""):
 
81
        self.change_node("#methods","this node has no methods")
 
82
        
 
83
    def do_modules(self,line=""):
 
84
        self.change_node("#modules","this node has no modules")
 
85
 
 
86
    def do_loc(self,line=""):
 
87
        loc = self.current_node.get("#loc")
 
88
        if loc != None : 
 
89
            print loc
 
90
        else:
 
91
            print "This node has no #loc key. This is a bug. Please report it"
 
92
 
 
93
    def do_type(self,line=""):
 
94
        print self.current_node.get("#type")
 
95
    
 
96
    def do_select(self,line=""):
 
97
        node = line.split(":")[0]
 
98
        self.change_node(node)
 
99
 
 
100
    def default(self,line=""):
 
101
        if line in self.current_node.keys():
 
102
            x = self.current_node.get(line)
 
103
            if type(x) != dict:
 
104
                print x
 
105
            else:
 
106
                self.current_node = x
 
107
                self.do_show()
 
108
 
 
109
    def completenames(self,*args):
 
110
        #print "completenames",args
 
111
        user_input = args[1]
 
112
        keys       = self.current_node.keys()
 
113
        if user_input.startswith("#"):
 
114
            user_input = user_input[1:]
 
115
        return [key for key in self.current_node.keys() if key.startswith(user_input)]        
 
116
 
 
117
    def completedefault(self,*args):
 
118
        #print "completedefault",args
 
119
        user_input = args[1]
 
120
        keys       = self.current_node.keys()
 
121
        ## if user_input.startswith("#"):
 
122
        ##     user_input = user_input[1:]
 
123
        return [key[1:] for key in self.current_node.keys() if key.startswith(user_input)]
 
124
        
 
125
def main():
 
126
    if len(sys.argv) < 2 :
 
127
        usage()
 
128
        exit(1)
 
129
    file_path = sys.argv[1]
 
130
    st = tahar.SourceStats(file_path)
 
131
    st.process()
 
132
    CodeBrowser(st).cmdloop()
 
133
    
 
134
 
 
135
def usage():
 
136
    print sys.argv[0],"file_path"
 
137
 
 
138
 
 
139
 
 
140
if __name__ == "__main__":
 
141
    main()