Use optparse.OptionParser to parse script arguments.
[depgraph.git] / depgraph2dot.py
index 23ad7f374b78207e0fdba8e7a08d82960d4f1161..c347947beaf5de4ae654bdf3a187da80412d616e 100755 (executable)
 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-import sys, getopt, colorsys, imp, re, pprint
+"""Convert `py2depgraph` dependency data to Graphviz dot syntax.
+"""
+
+import sys, colorsys, imp, re, pprint
 from hashlib import md5
 from os import popen, getuid  # for finding C extension dependencies with system calls
 from pwd import getpwuid
@@ -31,7 +34,7 @@ USER=getpwuid(getuid())[0] # get effective user name
 
 INVISIBLE_MODS=('__future__','copy','doctest','glob','optparse','os','qt','re',
                'StringIO','string','sys','textwrap','time','types','unittest')
-INVISIBLE_PATHS=(r'.*',)
+INVISIBLE_PATHS=[] #(r'.*',)
 VISIBLE_PATHS=(r'.*%s.*' % USER,r'.*comedi.*')
 
 def _pathmatch(regexp_tuple, path) :
@@ -315,8 +318,8 @@ class pydepgraphdot (object) :
         self.reset()
         self._debug=False
 
-    def render(self, root_module='__main__'):
-        depgraph,types,paths = self.get_data()
+    def render(self, ifile, root_module='__main__'):
+        depgraph,types,paths = self.get_data(ifile)
 
         if root_module != None :
             self.add_module_target(root_module)
@@ -362,8 +365,8 @@ class pydepgraphdot (object) :
         f.write(self._dotformat.footer())
 
     # data processing methods (input, output, checking)
-    def get_data(self):
-        t = eval(sys.stdin.read())
+    def get_data(self, ifile):
+        t = eval(ifile.read())
         return t['depgraph'],t['types'],t['paths']
     def get_output_file(self):
         return sys.stdout
@@ -405,22 +408,33 @@ class pydepgraphdot (object) :
             return None # no more modules! we're done.
 
             
-def main():
-    opts,args = getopt.getopt(sys.argv,'',['mono'])
-    colored = True
-    for o,v in opts:
-        if o=='--mono':
-            colored = False
+if __name__=='__main__':
+    from optparse import OptionParser
+
+    usage = '%prog [options]'
+    p = OptionParser(usage=usage, description=__doc__)
+    p.add_option('-m', '--mono', dest='color', default=True,
+                 action='store_false', help="Don't use color.")
+    p.add_option('-i', '--input', dest='input', default='-',
+                 help="Path to input file ('-' for stdin, %default).")
+    options,args = p.parse_args()
 
     # Fancyness with shared hooks instance so we can do slick thinks like
     # printing all modules just inside an invisible zone, since we'll need 
     # the dotformatter to know which nodes are visible.
     hk = hooks(link_outside_visited_nodes=False)
     #hk._debug = True
-    dt = dotformat_Cext(colored=colored, hooks_instance=hk)
+    dt = dotformat_Cext(colored=options.color, hooks_instance=hk)
     py = pydepgraphdot(hooks_instance=hk, dotformat_instance=dt)
     #py._debug = True
-    py.render()
 
-if __name__=='__main__':
-    main()
+    if options.input == '-':
+        ifile = sys.stdin
+    else:
+        ifile = open(options.input, 'r')
+
+    try:
+        py.render(ifile)
+    finally:
+        if options.input != '-':
+            ifile.close()