def main(): parser = optparse.OptionParser(usage=usgmsg) parser.add_option('-f', '--format', action='store', dest='format', default='Graphml', help='selects the output format (Graphml|GML|GDF) [default : %default]') parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False, help='enables messages (infos, warnings)') parser.add_option('-s', '--sweep', action='store_true', dest='sweep', default=False, help='sweep nodes (remove nodes that are not connected)') parser.add_option('--nn', '--no-nodes', action='store_false', dest='NodeLabels', default=True, help='do not output any node labels [Graphml]') parser.add_option('--ne', '--no-edges', action='store_false', dest='EdgeLabels', default=True, help='do not output any edge labels [Graphml]') parser.add_option('--nu', '--no-uml', action='store_false', dest='NodeUml', default=True, help='do not output any node methods/attributes in UML [Graphml]') parser.add_option('--na', '--no-arrows', action='store_false', dest='Arrows', default=True, help='do not output any arrows [Graphml]') parser.add_option('--nc', '--no-colors', action='store_false', dest='Colors', default=True, help='do not output any colors [Graphml]') parser.add_option('--la', '--lump-attributes', action='store_true', dest='LumpAttributes', default=False, help='lump class attributes/methods together with the node label [Graphml]') parser.add_option('--sc', '--separator-char', action='store', dest='SepChar', default='_', metavar='SEPCHAR', help='default separator char when lumping attributes/methods [default : "_"]') parser.add_option('--ae', '--auto-edges', action='store_true', dest='EdgeLabelsAutoComplete', default=False, help='auto-complete edge labels') parser.add_option('--ah', '--arrowhead', action='store', dest='DefaultArrowHead', default='none', metavar='TYPE', help='sets the default appearance of arrow heads for edges (normal|diamond|dot|...) [default : %default]') parser.add_option('--at', '--arrowtail', action='store', dest='DefaultArrowTail', default='none', metavar='TYPE', help='sets the default appearance of arrow tails for edges (normal|diamond|dot|...) [default : %default]') parser.add_option('--cn', '--color-nodes', action='store', dest='DefaultNodeColor', default='#CCCCFF', metavar='COLOR', help='default node color [default : "#CCCCFF"]') parser.add_option('--ce', '--color-edges', action='store', dest='DefaultEdgeColor', default='#000000', metavar='COLOR', help='default edge color [default : "#000000"]') parser.add_option('--cnt', '--color-nodes-text', action='store', dest='DefaultNodeTextColor', default='#000000', metavar='COLOR', help='default node text color for labels [default : "#000000"]') parser.add_option('--cet', '--color-edges-text', action='store', dest='DefaultEdgeTextColor', default='#000000', metavar='COLOR', help='default edge text color for labels [default : "#000000"]') parser.add_option('--ienc', '--input-encoding', action='store', dest='InputEncoding', default='', metavar='ENCODING', help='override encoding for input file [default : locale setting]') parser.add_option('--oenc', '--output-encoding', action='store', dest='OutputEncoding', default='', metavar='ENCODING', help='override encoding for text output files [default : locale setting]') options, args = parser.parse_args() if len(args) < 2: usage() sys.exit(1) infile = args[0] outfile = args[1] options.DefaultNodeColor = dot.colorNameToRgb(options.DefaultNodeColor, '#CCCCFF') options.DefaultEdgeColor = dot.colorNameToRgb(options.DefaultEdgeColor, '#000000') options.DefaultNodeTextColor = dot.colorNameToRgb(options.DefaultNodeTextColor, '#000000') options.DefaultEdgeTextColor = dot.colorNameToRgb(options.DefaultEdgeTextColor, '#000000') preferredEncoding = locale.getpreferredencoding() if options.InputEncoding == "": options.InputEncoding = preferredEncoding if options.OutputEncoding == "": options.OutputEncoding = preferredEncoding if options.verbose: print "Input file: %s " % infile print "Output file: %s " % outfile print "Output format: %s" % options.format.lower() print "Input encoding: %s" % options.InputEncoding if options.format.lower() == "graphml": print "Output encoding: utf-8 (fix for Graphml)" else: print "Output encoding: %s" % options.OutputEncoding # Collect nodes and edges nodes = {} edges = [] default_edge = None default_node = None nid = 1 eid = 1 f = open(infile, 'r') content = f.read().splitlines() f.close() idx = 0 while idx < len(content): l = unicode(content[idx], options.InputEncoding) if '->' in l: # Check for multiline edge if '[' in l and ']' not in l: ml = "" while ']' not in ml: idx += 1 ml = unicode(content[idx], options.InputEncoding) l = ' '.join([l.rstrip(), ml.lstrip()]) # Process edge e = dot.Edge() e.initFromString(l) e.id = eid eid += 1 if default_edge: e.complementAttributes(default_edge) edges.append(e) elif '[' in l: # Check for multiline node if ']' not in l: ml = "" while ']' not in ml: idx += 1 ml = unicode(content[idx], options.InputEncoding) l = ' '.join([l.rstrip(), ml.lstrip()]) # Process node n = dot.Node() n.initFromString(l) lowlabel = n.label.lower() if (lowlabel != 'graph' and lowlabel != 'edge' and lowlabel != 'node'): n.id = nid nid += 1 if default_node: n.complementAttributes(default_node) nodes[n.label] = n else: if lowlabel == 'edge': default_edge = n elif lowlabel == 'node': default_node = n elif 'charset=' in l: # Pick up input encoding from DOT file li = l.strip().split('=') if len(li) == 2: ienc = li[1].strip('"') if ienc != "": options.InputEncoding = ienc if options.verbose: print "Info: Picked up input encoding '%s' from the DOT file." % ienc idx += 1 # Add single nodes, if required for e in edges: if not nodes.has_key(e.src): n = dot.Node() n.label = e.src n.id = nid nid += 1 nodes[e.src] = n if not nodes.has_key(e.dest): n = dot.Node() n.label = e.dest n.id = nid nid += 1 nodes[e.dest] = n nodes[e.src].referenced = True nodes[e.dest].referenced = True if options.verbose: print "\nNodes: %d " % len(nodes) print "Edges: %d " % len(edges) if options.sweep: rnodes = {} for key, n in nodes.iteritems(): if n.referenced: rnodes[key] = n nodes = rnodes if options.verbose: print "\nNodes after sweep: %d " % len(nodes) # Output o = open(outfile, 'w') format = options.format.lower() if format == 'dot': exportDot(o, nodes, edges, options) elif format == 'graphml': exportGraphml(o, nodes, edges, options) elif format == 'gdf': exportGDF(o, nodes, edges, options) else: # GML exportGML(o, nodes, edges, options) o.close() if options.verbose: print "\nDone."
def main(): parser = optparse.OptionParser(usage=usgmsg) parser.add_option( '-f', '--format', action='store', dest='format', default='Graphml', help='selects the output format (Graphml|GML|GDF) [default : %default]' ) parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False, help='enables messages (infos, warnings)') parser.add_option('-s', '--sweep', action='store_true', dest='sweep', default=False, help='sweep nodes (remove nodes that are not connected)') parser.add_option('--nn', '--no-nodes', action='store_false', dest='NodeLabels', default=True, help='do not output any node labels [Graphml]') parser.add_option('--ne', '--no-edges', action='store_false', dest='EdgeLabels', default=True, help='do not output any edge labels [Graphml]') parser.add_option( '--nu', '--no-uml', action='store_false', dest='NodeUml', default=True, help='do not output any node methods/attributes in UML [Graphml]') parser.add_option('--na', '--no-arrows', action='store_false', dest='Arrows', default=True, help='do not output any arrows [Graphml]') parser.add_option('--nc', '--no-colors', action='store_false', dest='Colors', default=True, help='do not output any colors [Graphml]') parser.add_option( '--la', '--lump-attributes', action='store_true', dest='LumpAttributes', default=False, help= 'lump class attributes/methods together with the node label [Graphml]') parser.add_option( '--sc', '--separator-char', action='store', dest='SepChar', default='_', metavar='SEPCHAR', help= 'default separator char when lumping attributes/methods [default : "_"]' ) parser.add_option('--ae', '--auto-edges', action='store_true', dest='EdgeLabelsAutoComplete', default=False, help='auto-complete edge labels') parser.add_option( '--ah', '--arrowhead', action='store', dest='DefaultArrowHead', default='normal', metavar='TYPE', help= 'sets the default appearance of arrow heads for edges (normal|diamond|dot|...) [default : %default]' ) parser.add_option( '--at', '--arrowtail', action='store', dest='DefaultArrowTail', default='none', metavar='TYPE', help= 'sets the default appearance of arrow tails for edges (normal|diamond|dot|...) [default : %default]' ) parser.add_option('--cn', '--color-nodes', action='store', dest='DefaultNodeColor', default='#CCCCFF', metavar='COLOR', help='default node color [default : "#CCCCFF"]') parser.add_option('--ce', '--color-edges', action='store', dest='DefaultEdgeColor', default='#000000', metavar='COLOR', help='default edge color [default : "#000000"]') parser.add_option( '--cnt', '--color-nodes-text', action='store', dest='DefaultNodeTextColor', default='#000000', metavar='COLOR', help='default node text color for labels [default : "#000000"]') parser.add_option( '--cet', '--color-edges-text', action='store', dest='DefaultEdgeTextColor', default='#000000', metavar='COLOR', help='default edge text color for labels [default : "#000000"]') parser.add_option( '--ienc', '--input-encoding', action='store', dest='InputEncoding', default='utf-8', metavar='ENCODING', help='override encoding for input file [default : locale setting]') parser.add_option( '--oenc', '--output-encoding', action='store', dest='OutputEncoding', default='utf-8', metavar='ENCODING', help= 'override encoding for text output files [default : locale setting]') options, args = parser.parse_args() if len(args) < 2: usage() sys.exit(1) infile = args[0] outfile = args[1] options.DefaultNodeColor = dot.colorNameToRgb(options.DefaultNodeColor, '#CCCCFF') options.DefaultEdgeColor = dot.colorNameToRgb(options.DefaultEdgeColor, '#000000') options.DefaultNodeTextColor = dot.colorNameToRgb( options.DefaultNodeTextColor, '#000000') options.DefaultEdgeTextColor = dot.colorNameToRgb( options.DefaultEdgeTextColor, '#000000') # preferredEncoding = locale.getpreferredencoding() preferredEncoding = 'utf-8' if options.InputEncoding == "": options.InputEncoding = preferredEncoding if options.OutputEncoding == "": options.OutputEncoding = preferredEncoding if options.verbose: print("Input file: %s " % infile) print("Output file: %s " % outfile) print("Output format: %s" % options.format.lower()) print("Input encoding: %s" % options.InputEncoding) if options.format.lower() == "graphml": print("Output encoding: utf-8 (fix for Graphml)") else: print("Output encoding: %s" % options.OutputEncoding) # Collect nodes and edges nodes = {} edges = [] default_edge = None default_node = None nid = 1 eid = 1 f = open(infile, 'r') content = f.read().splitlines() f.close() idx = 0 while idx < len(content): l = str(content[idx]) if '->' in l: # Check for multiline edge if '[' in l and ']' not in l: ml = "" while ']' not in ml: idx += 1 ml = str(content[idx]) l = ' '.join([l.rstrip(), ml.lstrip()]) # Process edge e = dot.Edge() e.initFromString(l) e.id = eid eid += 1 if default_edge: e.complementAttributes(default_edge) edges.append(e) elif '[' in l: # Check for multiline node if ']' not in l: ml = "" while ']' not in ml: idx += 1 ml = str(content[idx]) l = ' '.join([l.rstrip(), ml.lstrip()]) # Process node n = dot.Node() n.initFromString(l) lowlabel = n.label.lower() if (lowlabel != 'graph' and lowlabel != 'edge' and lowlabel != 'node'): n.id = nid nid += 1 if default_node: n.complementAttributes(default_node) nodes[n.label] = n else: if lowlabel == 'edge': default_edge = n elif lowlabel == 'node': default_node = n elif 'charset=' in l: # Pick up input encoding from DOT file li = l.strip().split('=') if len(li) == 2: ienc = li[1].strip('"') if ienc != "": options.InputEncoding = ienc if options.verbose: print( "Info: Picked up input encoding '%s' from the DOT file." % ienc) idx += 1 # Add single nodes, if required for e in edges: if e.src not in nodes: n = dot.Node() n.label = e.src n.id = nid nid += 1 nodes[e.src] = n if e.dest not in nodes: n = dot.Node() n.label = e.dest n.id = nid nid += 1 nodes[e.dest] = n nodes[e.src].referenced = True nodes[e.dest].referenced = True if options.verbose: print("\nNodes: %d " % len(nodes)) print("Edges: %d " % len(edges)) if options.sweep: rnodes = {} for key, n in nodes.items(): if n.referenced: rnodes[key] = n nodes = rnodes if options.verbose: print("\nNodes after sweep: %d " % len(nodes)) # Output o = open(outfile, 'w') format = options.format.lower() if format == 'dot': exportDot(o, nodes, edges, options) elif format == 'graphml': exportGraphml(o, nodes, edges, options) elif format == 'gdf': exportGDF(o, nodes, edges, options) else: # GML exportGML(o, nodes, edges, options) o.close() if options.verbose: print("\nDone.")