#!/usr/bin/env python
import sys
import argparse
import json
from jsonlibconfig import (encoder, decoder)

parser = argparse.ArgumentParser(
    description="Pure python library provides JSON <- convert --> Libconfig")

parser.add_argument(
    "--target",
    default="libconfig",
    choices=["json", "libconfig"],
    help="specify output format: json, libconfig (default: libconfig)")

parser.add_argument(
    "--pretty",
    default=False,
    action="store_true",
    help="Pretty print")

parser.add_argument(
    "--hextoint",
    default=False,
    action="store_true",
    help="Convert HEX string to integer (only for json)")

parser.add_argument(
    "--file",
    required=False,
    default=None,
    help="Input file")

args = parser.parse_args()

inputs = ""
if args.file is not None:
    with open(args.file) as f:
        for s in f:
            inputs = inputs + s
else:
    for s in sys.stdin:
        inputs = inputs + s

indent = None
if args.pretty is True:
    indent = 2

if args.target == "libconfig":
    print (encoder.dumps(json.loads(inputs), indent=indent))
elif args.target == "json":
    print json.dumps(
        decoder.loads(
            inputs, _hextoint=args.hextoint), indent=indent)
