1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
"""Command line utility for pyjdata.
Provides a routine for converting text-/binary-based JData files to Python data.
Call
python -m jdata.cmd -h
to get help with command line usage.
"""
import argparse
import os
import sys
from jdata import load, save
def main():
#
# get arguments and invoke the conversion routines
#
parser = argparse.ArgumentParser(
description='Convert a text JData file to a binary JData file and vice versa.')
parser.add_argument(
'file', nargs='+',
help='path to a text-JData (.json, .jdat) file or a binary JData (.bjd, .jbat) file')
parser.add_argument(
'-f', '--force', action='store_const', const=True,
default=False, help='overwrite existing files when converting')
args = parser.parse_args()
for path in args.file:
spl = os.path.splitext(path)
ext = spl[1].lower()
if ext == '.json' or ext == '.jdat':
dest = spl[0] + '.jbat'
try:
if os.path.exists(dest) and not args.force:
raise Exception('File {} already exists.'.format(dest))
data = load(path)
save(data, dest)
if args.remove_input:
os.remove(path)
except Exception as e:
print('Error: {}'.format(e))
sys.exit(1)
elif ext == '.bjd' or ext == '.jbat':
dest = spl[0] + '.json'
try:
if os.path.exists(dest) and not args.force:
raise Exception('File {} already exists.'.format(dest))
data = load(path)
save(data,dest)
if args.remove_input:
os.remove(path)
except RuntimeError as e:
print('Error: {}'.format(e))
sys.exit(1)
else:
print('Unsupported file extension on file: {}'.format(path))
sys.exit(1)
if __name__ == '__main__':
main()
|