Mercurial > repos > shellac > guppy_basecaller
comparison env/lib/python3.7/site-packages/pyaml/__main__.py @ 0:26e78fe6e8c4 draft
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
| author | shellac |
|---|---|
| date | Sat, 02 May 2020 07:14:21 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:26e78fe6e8c4 |
|---|---|
| 1 # -*- coding: utf-8 -*- | |
| 2 | |
| 3 import os, sys, stat, tempfile, contextlib, yaml, pyaml | |
| 4 | |
| 5 | |
| 6 @contextlib.contextmanager | |
| 7 def safe_replacement(path, *open_args, **open_kws): | |
| 8 path = str(path) | |
| 9 try: mode = stat.S_IMODE(os.stat(path).st_mode) | |
| 10 except (OSError, IOError): mode = None | |
| 11 open_kws.update( delete=False, | |
| 12 dir=os.path.dirname(path), prefix=os.path.basename(path)+'.' ) | |
| 13 with tempfile.NamedTemporaryFile(*open_args, **open_kws) as tmp: | |
| 14 try: | |
| 15 if mode is not None: os.fchmod(tmp.fileno(), mode) | |
| 16 yield tmp | |
| 17 if not tmp.closed: tmp.flush() | |
| 18 os.rename(tmp.name, path) | |
| 19 finally: | |
| 20 try: os.unlink(tmp.name) | |
| 21 except (OSError, IOError): pass | |
| 22 | |
| 23 | |
| 24 def main(argv=None): | |
| 25 import argparse | |
| 26 parser = argparse.ArgumentParser( | |
| 27 description='Process and dump prettified YAML to stdout.') | |
| 28 parser.add_argument('path', nargs='?', metavar='path', | |
| 29 help='Path to YAML to read (default: use stdin).') | |
| 30 parser.add_argument('-r', '--replace', action='store_true', | |
| 31 help='Replace specified path with prettified version in-place.') | |
| 32 parser.add_argument('-w', '--width', type=int, metavar='chars', | |
| 33 help='Max line width hint to pass to pyyaml for the dump.' | |
| 34 ' Only used to format scalars and collections (e.g. lists).') | |
| 35 opts = parser.parse_args(argv or sys.argv[1:]) | |
| 36 | |
| 37 src = open(opts.path) if opts.path else sys.stdin | |
| 38 try: data = yaml.safe_load(src) | |
| 39 finally: src.close() | |
| 40 | |
| 41 pyaml_kwargs = dict() | |
| 42 if opts.width: pyaml_kwargs['width'] = opts.width | |
| 43 if opts.replace and opts.path: | |
| 44 with safe_replacement(opts.path) as tmp: | |
| 45 pyaml.pprint(data, file=tmp, **pyaml_kwargs) | |
| 46 else: pyaml.pprint(data, **pyaml_kwargs) | |
| 47 | |
| 48 if __name__ == '__main__': sys.exit(main()) |
