<commit_before>"""Module containing NFC Viewer preferences."""


from __future__ import print_function
import json
        
            
_COMMAND_SETS = {
                 
    'Coarse': '''
        c Call
        C Call Page
        Alt-c Call All
        n Noise
        N Noise Page
        Alt-n Noise All
        t Tone
        T Tone Page
        Alt-t Tone All
        u Unclassified
        U Unclassified Page
        Alt-u Unclassified All
    ''',
    
    'Calls': '''
        a AMRE
        A ATSP
        b BTBW
        B BAWW
        k Call
        c CSWA
        C CMWA
        Ctrl-c COYE
        Ctrl-C CCSP
        Alt-c CAWA
        Alt-C CHSP
        D DbUp
        f FOSP
        g GHSP
        h HOWA
        H HESP
        i INBU
        l LCSP
        m MOWA
        n Noise
        N NWTH
        o OVEN
        O Other
        p PAWA
        P PROW
        q Unknown
        s SVSP
        S SwLi
        Ctrl-s SNBULALO
        t Tone
        T ATSP
        u Unclassified
        v VESP
        w WIWA
        W WTSP
        Alt-w WCSP
        x Weak
        y YRWA
        z Zeep
    '''
    
}


def _load_preferences():
    
    try:
        preferences = json.loads(r'''
{
    "archiveDirPaths": {
        "2012": "/Users/Harold/Desktop/NFC/Data/Old Bird/2012 Summer and Fall",
        "2014": "/Users/Harold/Desktop/NFC/Data/Old Bird/2014"
    },
    "defaultArchive": "2012",
    "stationName": "Alfred",
    "detectorName": "Tseep",
    "clipClassName": "Call",
    "monthName": "September 2012",
    "mainWindow.width": 1000,
    "mainWindow.height": 800,
    "mainWindow.countDisplayType": "archive calendar",
    "clipsWindow.width": 1200,
    "clipsWindow.height": 800,
    "clipGrid.numRows": 4,
    "clipGrid.numColumns": 5,
    "clipGrid.showMouseTimeFreq": true,
    "clipGrid.showClipClassNames": true,
    "clipGrid.showClipTimes": true,
    "clipGrid.clipTextFontSize": 11,
    "clipGrid.clipTextColor": "white",
    "clipGrid.clipClassTextColors": { "Call": "cyan" },
    "classification.defaultCommandSet": "Coarse"
    
}
''')
        
    except ValueError:
        print('Could not parse preferences file.')
        return {}
    
    try:
        preferences['classification.commandSets'] = \
            _parse_command_sets(_COMMAND_SETS)
    except ValueError as e:
        print(str(e))
        
    return preferences


def _parse_command_sets(commandSets):
    return dict(_parse_command_set(*i) for i in commandSets.iteritems())


def _parse_command_set(name, text):
    lines = [line.strip() for line in text.splitlines()]
    pairs = [_parseCommand(line, i + 1, name)
             for i, line in enumerate(lines) if line != '']
    return (name, dict(pairs))


_SCOPES = frozenset(['Selected', 'Page', 'All'])


def _parseCommand(line, lineNum, commandSetName):
    
    items = line.split()
    n = len(items)<commit_msg>Move preferences JSON to its own file.<commit_after>"""Module containing NFC Viewer preferences."""


from __future__ import print_function

import json
import os.path
import sys
        
            
_NFC_DIR_NAME = 'NFC'
_PREFERENCES_FILE_NAME = 'Preferences.json'


def _load_preferences():
    
    home_dir_path = os.path.expanduser('~')
    file_path = \
        os.path.join(home_dir_path, _NFC_DIR_NAME, _PREFERENCES_FILE_NAME)
    
    try:
        file_ = open(file_path, 'rU')
    except:
        f = 'Could not open preferences file "{:s}".'
        _handle_error(f.format(file_path))
    
    try:
        preferences = json.load(file_)
    except ValueError:
        file_.close()
        f = 'Could not parse preferences file "{:s}".'
        _handle_error(f.format(file_path))

    file_.close()
        
    try:
        preferences['classification.commandSets'] = \
            _parse_command_sets(preferences['classification.commandSets'])
    except ValueError as e:
        f = 'Could not parse command sets in preferences file "{:s}". {:s}'
        _handle_error(f.format(file_path, str(e)))

    return preferences


def _handle_error(message):
    print(message, file=sys.stderr)
    sys.exit(1)
    
    
def _parse_command_sets(commandSets):
    return dict(_parse_command_set(*i) for i in commandSets.iteritems())


def _parse_command_set(name, lines):
    lines = [line.strip() for line in lines]
    pairs = [_parseCommand(line, i + 1, name)
             for i, line in enumerate(lines) if line != '']
    return (name, dict(pairs))


_SCOPES = frozenset(['Selected', 'Page', 'All'])


def _parseCommand(line, lineNum, commandSetName):
    
    items = line.split()
    n = len(items)
{'fraction_non_alphanumeric': 0.10704960835509138, 'fraction_numerical': 0.0097911227154047, 'mean_word_length': 2.0935397039030956, 'pattern_counts': {'":': 23, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '940670', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1462, 'n_words': 373}