"""Script to convert HJSON file to JSON file format."""
import hjson
import const #const.py

const.DEFAULT_CONFIG_FILE = "osx-config.hjson"
#http://stackoverflow.com/questions/244777/can-i-use-comments-inside-a-json-file#244858
const.JSON_WARNING = {'_comment': ('DO NOT EDIT THIS FILE. THIS WAS '
                                   'AUTOMATICALLY GENERATED BY THE '
                                   'hjson_to_json.py SCRIPT. INSTEAD, EDIT THE '
                                   'osx-config.hjson FILE.')}

def convert(hjson_filename):
    """Convert HJson file on disk to JSON format and write to disk."""
    assert hjson_filename.endswith('.hjson')
    json_filename = hjson_filename.replace('.hjson', '.json')

    with open(json_filename, 'w') as json_out:
        with open(hjson_filename, 'r') as hjson_in:
            config = hjson.loads(hjson_in.read())
            config = [const.JSON_WARNING] + config
            json_format = hjson.dumpsJSON(config)
            json_out.write(json_format)

def _main():
    convert(const.DEFAULT_CONFIG_FILE)

if __name__ == '__main__':
    _main()
