#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim: set fileencoding=utf-8 :
# SETUPTOOLS_DO_NOT_WRAP

# jsonedit json editing script
#
#  - Copyright (C) 2014 SkyTruth
#    Author: Egil Moeller <egil.moller@skytruth.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import json
import jsonpath
import sys

if len(sys.argv) < 4:
    print """Edits json files using jsonpaths

For more information on jsonpaths see http://goessner.net/articles/JsonPath/

There are three possible usages:

jsonedit.py infile.json outfile.json '$..bbox'
    This will compile a list of all bbox attribute values

jsonedit.py infile.json outfile.json '$..options' '{"layer": "47-11"}'
   This will replace all options attribute values with the json object {"layer": "47-11"}

jsonedit.py infile.json outfile.json '$..bbox' delete
   This will delete all bbox attributes
"""
    sys.exit(0)

def main(input, output, query, replacement = None):
    delete = False
    replace = False
    if replacement == 'delete':
        delete = True
    elif replacement:
        replace = True
        replacement = json.loads(replacement)


    with open(input) as f:
        data = json.load(f)

    if replace or delete:
        for path in jsonpath.jsonpath(data, query, "path"):
            path, item = path.rsplit("[", 1)
            item = item[1:-2] # remove ' and ']
            obj = jsonpath.jsonpath(data, path)[0]
            if isinstance(obj, (list, tuple)): item = int(item)
            if delete:
                del obj[item]
            else:
                obj[item] = replacement
    else:
        data = jsonpath.jsonpath(data, query)

    with open(output, "w") as f:
        json.dump(data, f)

main(*sys.argv[1:])
