2 # Copyright 2004 Toby Dickenson
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish,
8 # distribute, sublicense, and/or sell copies of the Software, and to
9 # permit persons to whom the Software is furnished to do so, subject
10 # to the following conditions:
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 import sys, getopt, colorsys, imp, hashlib
29 opts,args = getopt.getopt(argv,'',['mono'])
37 # Convert a module name to a syntactically correct node name
38 return s.replace('.','_')
43 # normalise our input data
49 f = self.get_output_file()
51 f.write('digraph G {\n')
52 #f.write('concentrate = true;\n')
53 #f.write('ordering = out;\n')
54 f.write('ranksep=1.0;\n')
55 f.write('node [style=filled,fontname=Helvetica,fontsize=10];\n')
65 if self.use(v,tv) and not self.toocommon(v,tv):
66 f.write('%s -> %s' % ( self.fix(k),self.fix(v) ) )
67 self.write_attributes(f,self.edge_attributes(k,v))
70 self.write_attributes(f,self.node_attributes(k,tk))
74 def write_attributes(self,f,a):
80 def node_attributes(self,k,type):
82 a.append('label="%s"' % self.label(k))
84 a.append('fillcolor="%s"' % self.color(k,type))
86 a.append('fillcolor=white')
87 if self.toocommon(k,type):
88 a.append('peripheries=2')
91 def edge_attributes(self,k,v):
93 weight = self.weight(k,v)
95 a.append('weight=%d' % weight)
96 length = self.alien(k,v)
98 a.append('minlen=%d' % length)
102 t = eval(sys.stdin.read())
103 return t['depgraph'],t['types']
105 def get_output_file(self):
108 def use(self,s,type):
109 # Return true if this module is interesting and should be drawn. Return false
110 # if it should be completely omitted. This is a default policy - please override.
111 if s in ('os','sys','qt','time','__future__','types','re','string'):
112 # nearly all modules use all of these... more or less. They add nothing to
115 if s.startswith('encodings.'):
119 if self.toocommon(s,type):
120 # A module where we dont want to draw references _to_. Dot doesnt handle these
121 # well, so it is probably best to not draw them at all.
125 def toocommon(self,s,type):
126 # Return true if references to this module are uninteresting. Such references
127 # do not get drawn. This is a default policy - please override.
130 # references *to* __main__ are never interesting. omitting them means
131 # that main floats to the top of the page
133 if type==imp.PKG_DIRECTORY:
134 # dont draw references to packages.
138 def weight(self,a,b):
139 # Return the weight of the dependency from a to b. Higher weights
140 # usually have shorter straighter edges. Return 1 if it has normal weight.
141 # A value of 4 is usually good for ensuring that a related pair of modules
142 # are drawn next to each other. This is a default policy - please override.
144 if b.split('.')[-1].startswith('_'):
145 # A module that starts with an underscore. You need a special reason to
146 # import these (for example random imports _random), so draw them close
152 # Return non-zero if references to this module are strange, and should be drawn
153 # extra-long. the value defines the length, in rank. This is also good for putting some
154 # vertical space between seperate subsystems. This is a default policy - please override.
159 # Convert a module name to a formatted node label. This is a default policy - please override.
161 return '\\.\\n'.join(s.split('.'))
163 def color(self,s,type):
164 # Return the node color for this module name. This is a default policy - please override.
166 # Calculate a color systematically based on the hash of the module name. Modules in the
167 # same package have the same color. Unpackaged modules are grey
168 t = self.normalise_module_name_for_hash_coloring(s,type)
169 return self.color_from_name(t)
171 def normalise_module_name_for_hash_coloring(self,s,type):
172 if type==imp.PKG_DIRECTORY:
181 def color_from_name(self,name):
182 n = hashlib.md5(name).digest()
183 hf = float(ord(n[0])+ord(n[1])*0xff)/0xffff
184 sf = float(ord(n[2]))/0xff
185 vf = float(ord(n[3]))/0xff
186 r,g,b = colorsys.hsv_to_rgb(hf, 0.3+0.6*sf, 0.8+0.2*vf)
187 return '#%02x%02x%02x' % (r*256,g*256,b*256)
191 pydepgraphdot().main(sys.argv[1:])
193 if __name__=='__main__':