#!/usr/bin/env python
import json
import sys

# The stream of data on standard in (stdin) contains two things:
#
#  1. files - their names and a diff of what changed
#  2. settings - the user's modified settings for this plugin
#
# It's JSON formatted which makes if very portable.
#
# We will read from stdin and create a Python object from the data.
data = json.loads(sys.stdin.read())
files = data['files']
config = data['config']

# This will hold our output
out = {}

for f in files:
    # Add a new key to our dictionary based on the filename
    out[f['name']] = []

    # You can read the settings like this
    if config.get('verbose', 'yes') == 'yes':
        # Iterate through the diff of the file
        for line, kind, content in f['diff']:
            # For every line, write a message
            out[f['name']].append(
                (line, 'info', '{0} is {1}'.format(content, kind)))
    else:
        out[f['name']] = 'File has been modified'

# Finally, write to stdout the JSON encoded output
sys.stdout.write(json.dumps(out, indent=4))

# Tell Git that the plugin ran successfully (according to design)
sys.exit(0)
