Mercurial > repos > shellac > sam_consensus_v3
comparison env/bin/prov-convert @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
| author | shellac |
|---|---|
| date | Mon, 22 Mar 2021 18:12:50 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:4f3585e2f14b |
|---|---|
| 1 #!/Users/cmdms/OneDrive-UOB/Development/Projects/2021/sam-consensus-v3/env/bin/python3 | |
| 2 # encoding: utf-8 | |
| 3 """ | |
| 4 convert -- Convert PROV-JSON to RDF, PROV-N, PROV-XML, or graphical formats (SVG, PDF, PNG) | |
| 5 | |
| 6 @author: Trung Dong Huynh | |
| 7 | |
| 8 @copyright: 2016 University of Southampton, United Kingdom. All rights reserved. | |
| 9 | |
| 10 @license: MIT License | |
| 11 | |
| 12 @contact: trungdong@donggiang.com | |
| 13 @deffield updated: 2016-10-19 | |
| 14 """ | |
| 15 | |
| 16 from argparse import ArgumentParser, RawDescriptionHelpFormatter, FileType | |
| 17 import os | |
| 18 import sys | |
| 19 import logging | |
| 20 import traceback | |
| 21 import six | |
| 22 | |
| 23 from prov.model import ProvDocument | |
| 24 from prov import serializers | |
| 25 | |
| 26 | |
| 27 logger = logging.getLogger(__name__) | |
| 28 | |
| 29 __all__ = [] | |
| 30 __version__ = 0.1 | |
| 31 __date__ = '2014-03-14' | |
| 32 __updated__ = '2016-10-19' | |
| 33 | |
| 34 DEBUG = 0 | |
| 35 TESTRUN = 0 | |
| 36 PROFILE = 0 | |
| 37 | |
| 38 GRAPHVIZ_SUPPORTED_FORMATS = { | |
| 39 'bmp', 'canon', 'cmap', 'cmapx', 'cmapx_np', 'dot', 'eps', 'fig', 'gtk', 'gv', 'ico', 'imap', 'imap_np', 'ismap', | |
| 40 'jpe', 'jpeg', 'jpg', 'pdf', 'plain', 'plain-ext', 'png', 'ps', 'ps2', 'svg', 'svgz', 'tif', 'tiff', 'tk', | |
| 41 'vml', 'vmlz', 'x11', 'xdot', 'xlib' | |
| 42 } | |
| 43 | |
| 44 | |
| 45 @six.python_2_unicode_compatible | |
| 46 class CLIError(Exception): | |
| 47 """Generic exception to raise and log different fatal errors.""" | |
| 48 def __init__(self, msg): | |
| 49 super(CLIError).__init__(type(self)) | |
| 50 self.msg = "E: %s" % msg | |
| 51 | |
| 52 def __str__(self): | |
| 53 return self.msg | |
| 54 | |
| 55 | |
| 56 def convert_file(infile, outfile, output_format): | |
| 57 prov_doc = ProvDocument.deserialize(infile) | |
| 58 | |
| 59 # Formats not supported by prov.serializers | |
| 60 if output_format == 'provn': | |
| 61 outfile.write(prov_doc.get_provn().encode()) | |
| 62 elif output_format in GRAPHVIZ_SUPPORTED_FORMATS: | |
| 63 from prov.dot import prov_to_dot | |
| 64 dot = prov_to_dot(prov_doc) | |
| 65 content = dot.create(format=output_format) | |
| 66 outfile.write(content) | |
| 67 else: | |
| 68 # Try supported serializers: | |
| 69 try: | |
| 70 prov_doc.serialize(outfile, format=output_format) | |
| 71 except serializers.DoNotExist: | |
| 72 raise CLIError('Output format "%s" is not supported.' % output_format) | |
| 73 | |
| 74 | |
| 75 def main(argv=None): # IGNORE:C0111 | |
| 76 """Command line options.""" | |
| 77 | |
| 78 if argv is None: | |
| 79 argv = sys.argv | |
| 80 else: | |
| 81 sys.argv.extend(argv) | |
| 82 | |
| 83 program_name = os.path.basename(sys.argv[0]) | |
| 84 program_version = "v%s" % __version__ | |
| 85 program_build_date = str(__updated__) | |
| 86 program_version_message = '%%(prog)s %s (%s)' % (program_version, program_build_date) | |
| 87 program_shortdesc = __import__('__main__').__doc__.split("\n")[1] | |
| 88 program_license = '''%s | |
| 89 | |
| 90 Created by Trung Dong Huynh on %s. | |
| 91 Copyright 2016 University of Southampton. All rights reserved. | |
| 92 | |
| 93 Licensed under the MIT License | |
| 94 https://github.com/trungdong/prov/blob/master/LICENSE | |
| 95 | |
| 96 Distributed on an "AS IS" basis without warranties | |
| 97 or conditions of any kind, either express or implied. | |
| 98 | |
| 99 USAGE | |
| 100 ''' % (program_shortdesc, str(__date__)) | |
| 101 | |
| 102 try: | |
| 103 # Setup argument parser | |
| 104 parser = ArgumentParser(description=program_license, formatter_class=RawDescriptionHelpFormatter) | |
| 105 parser.add_argument('-f', '--format', dest='format', action='store', default='json', | |
| 106 help='output format: json, xml, provn, or one supported by GraphViz (e.g. svg, pdf)') | |
| 107 parser.add_argument('infile', nargs='?', type=FileType('r'), default=sys.stdin) | |
| 108 parser.add_argument('outfile', nargs='?', type=FileType('wb'), default=sys.stdout) | |
| 109 parser.add_argument('-V', '--version', action='version', version=program_version_message) | |
| 110 | |
| 111 args = None | |
| 112 try: | |
| 113 # Process arguments | |
| 114 args = parser.parse_args() | |
| 115 convert_file(args.infile, args.outfile, args.format.lower()) | |
| 116 finally: | |
| 117 if args: | |
| 118 if args.infile: | |
| 119 args.infile.close() | |
| 120 if args.outfile: | |
| 121 args.outfile.close() | |
| 122 | |
| 123 return 0 | |
| 124 except KeyboardInterrupt: | |
| 125 # handle keyboard interrupt | |
| 126 return 0 | |
| 127 except Exception as e: | |
| 128 if DEBUG or TESTRUN: | |
| 129 traceback.print_exc() | |
| 130 raise e | |
| 131 indent = len(program_name) * " " | |
| 132 sys.stderr.write(program_name + ": " + str(e) + "\n") | |
| 133 sys.stderr.write(indent + " for help use --help") | |
| 134 return 2 | |
| 135 | |
| 136 if __name__ == "__main__": | |
| 137 logging.basicConfig(level=(logging.DEBUG if DEBUG else logging.INFO)) | |
| 138 if TESTRUN: | |
| 139 import doctest | |
| 140 doctest.testmod() | |
| 141 if PROFILE: | |
| 142 import cProfile | |
| 143 import pstats | |
| 144 profile_filename = 'converter_profile.txt' | |
| 145 cProfile.run('main()', profile_filename) | |
| 146 statsfile = open("profile_stats.txt", "wb") | |
| 147 p = pstats.Stats(profile_filename, stream=statsfile) | |
| 148 stats = p.strip_dirs().sort_stats('cumulative') | |
| 149 stats.print_stats() | |
| 150 statsfile.close() | |
| 151 sys.exit(0) | |
| 152 sys.exit(main()) |
