<commit_before># -*- coding: utf-8 -*-
"""Reads and writes Google Chome preferences in OS X.

Based on the `defaults` utility for reading/writing PList files in OS X.

The two sub-commands currently supported are "read" and "write".

Nested attribute names of JSON objects are referred to using dot-notation, e.g.
"my.object.property"

If the user attempts to "write" to a property that does not currently exist, it
will be created. If the property is nested, a series of required JSON objects
will be created containing only the nested attribute names. For example, if the
user writes to "my.nonexistent.property" with the value 42, and the "my"
attribute does not exist at the top level, the following structure will be
created in the JSON:
{
    ...other attributes in the JSON
    "my": {
        "nonexistent": {
            "property": 42
        }
    }
}

Examples:

    $ python chrome_defaults.py read "/Users/myusername/Library/Application Support/Google/Chrome/Default/Preferences"

    $ python chrome_defaults.py read "/Users/myusername/Library/Application Support/Google/Chrome/Default/Preferences" download.directory_upgrade

    $ python chrome_defaults.py write "/Users/myusername/Library/Application Support/Google/Chrome/Default/Preferences" download.directory_upgrade -bool true

Todo:
    * Unit tests
    * Add support for writing list values
    * Add support for writing null value
"""

import sys
import json
from datetime import datetime
import shutil
from copy import deepcopy
import re

UNDERLINE = '\033[4m'
ENDC = '\033[0m'

def _main():
    (action, preferences_filename, chrome_property, value) = get_args()
    preferences_json = _get_json(preferences_filename)
    if action == 'read':
        if chrome_property is None:
            print "%s" % json.dumps(preferences_json, indent=4)
            sys.exit()
        else:
            value = get_json_field(
                preferences_json, chrome_property, preferences_filename)
            print "%s" % normalize(value)
    else: #'write'
        _make_backup(preferences_filename)<commit_msg>scripts/chrome_defaults: display message to user when attribute read is missing<commit_after># -*- coding: utf-8 -*-
"""Reads and writes Google Chome preferences in OS X.

Based on the `defaults` utility for reading/writing PList files in OS X.

The two sub-commands currently supported are "read" and "write".

Nested attribute names of JSON objects are referred to using dot-notation, e.g.
"my.object.property"

If the user attempts to "write" to a property that does not currently exist, it
will be created. If the property is nested, a series of required JSON objects
will be created containing only the nested attribute names. For example, if the
user writes to "my.nonexistent.property" with the value 42, and the "my"
attribute does not exist at the top level, the following structure will be
created in the JSON:
{
    ...other attributes in the JSON
    "my": {
        "nonexistent": {
            "property": 42
        }
    }
}

Examples:

    $ python chrome_defaults.py read "/Users/myusername/Library/Application Support/Google/Chrome/Default/Preferences"

    $ python chrome_defaults.py read "/Users/myusername/Library/Application Support/Google/Chrome/Default/Preferences" download.directory_upgrade

    $ python chrome_defaults.py write "/Users/myusername/Library/Application Support/Google/Chrome/Default/Preferences" download.directory_upgrade -bool true

Todo:
    * Unit tests
    * Add support for writing list values
    * Add support for writing null value
"""

import sys
import json
from datetime import datetime
import shutil
from copy import deepcopy
import re

UNDERLINE = '\033[4m'
ENDC = '\033[0m'

def _main():
    (action, preferences_filename, chrome_property, value) = get_args()
    preferences_json = _get_json(preferences_filename)
    if action == 'read':
        if chrome_property is None:
            print "%s" % json.dumps(preferences_json, indent=4)
            sys.exit()
        else:
            try:
                value = get_json_field(
                    preferences_json, chrome_property, preferences_filename)
                print "%s" % normalize(value)
            except KeyError:
                print("The attribute '%s' does not exist in '%s'." %
                      (chrome_property, preferences_filename))
    else: #'write'
        _make_backup(preferences_filename)
{'fraction_non_alphanumeric': 0.0834841628959276, 'fraction_numerical': 0.006334841628959276, 'mean_word_length': 3.242802303262956, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3485634', 'n_tokens_mistral': 1222, 'n_tokens_neox': 1175, 'n_words': 447}