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