|
| 1 | +#!/usr/bin/python |
| 2 | +# ls *.yang | xargs -n1 pyang -f depend > packages.txt |
| 3 | +# pkg --title=TITLE --inputfile=packages.txt > packages.uml |
| 4 | +# java -jar plantuml.jar packages.uml |
| 5 | +# open img/packages.png |
| 6 | + |
| 7 | +import sys |
| 8 | +from optparse import OptionParser |
| 9 | + |
| 10 | +useslist = [] |
| 11 | +foundpkgs = [] |
| 12 | +refpkgs = [] |
| 13 | +def main(*args): |
| 14 | + parser = OptionParser() |
| 15 | + parser.add_option("--title", action="store", dest="TITLE", |
| 16 | + help="Title of UML Package Diagram") |
| 17 | + parser.add_option("--inputfile", action="store", dest="INPUT", |
| 18 | + help="Input file generated with pyang -f depend") |
| 19 | + (options, args) = parser.parse_args() |
| 20 | + |
| 21 | + if (options.TITLE is None) or (options.INPUT is None): |
| 22 | + print "Incorrect number of arguments (--title=TITLE --inputfile=INPUTFILE)" |
| 23 | + sys.exit(-1) |
| 24 | + title = options.TITLE.capitalize() |
| 25 | + infile = options.INPUT |
| 26 | + try: |
| 27 | + infile = open(options.INPUT, 'r+') |
| 28 | + except IOError: |
| 29 | + print 'cannot open', infile |
| 30 | + except IndexError: |
| 31 | + print 'supply input file' |
| 32 | + else: |
| 33 | + sys.stdout.write('@startuml img/packages.png \n') |
| 34 | + sys.stdout.write('title <b> %s </b> \n' %title) |
| 35 | + lines = infile.readlines() |
| 36 | + for line in lines: |
| 37 | + # sys.stderr.write('checking line %s' %line) |
| 38 | + pkg = line.split(":") |
| 39 | + sys.stdout.write('package \"%s\" as %s \n' %(stripws(pkg[0]), plantuml(stripws(pkg[0])))) |
| 40 | + sys.stdout.write('end package\n') |
| 41 | + foundpkgs.append(stripws(pkg[0])) |
| 42 | + imports = pkg[1].split(" ") |
| 43 | + for i in imports: |
| 44 | + if (i != "") and (i != "\n"): |
| 45 | + useslist.append(plantuml(stripws(pkg[0])) + ' --+ ' + plantuml(stripws(i)) + ".yang" + '\n' ) |
| 46 | + refpkgs.append(stripws(i) + ".yang") |
| 47 | + externalrefs = False |
| 48 | + for r in refpkgs: |
| 49 | + if r not in foundpkgs: |
| 50 | + if not externalrefs: # first time, write begin |
| 51 | + sys.stdout.write('package \"other modules\" as other\n') |
| 52 | + externalrefs = True |
| 53 | + sys.stdout.write('package \"%s\" as %s \n' %(stripws(r), plantuml(stripws(r)))) |
| 54 | + sys.stdout.write('end package\n') |
| 55 | + if externalrefs: # close the package for external refs |
| 56 | + sys.stdout.write('package other\n') |
| 57 | + for u in useslist: |
| 58 | + sys.stdout.write('%s \n' %u) |
| 59 | + sys.stdout.write('@enduml \n') |
| 60 | + |
| 61 | + |
| 62 | +def plantuml(s): |
| 63 | + s = s.replace('-', '_') |
| 64 | + s = s.replace('/', '_') |
| 65 | + s = s.replace(':', '_') |
| 66 | + return s |
| 67 | +def stripws(s): |
| 68 | + s = s.replace(" ", "") |
| 69 | + s = s.replace("\n", "") |
| 70 | + return s |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == '__main__': |
| 74 | + sys.exit(main()) |
0 commit comments