<commit_before>import logging
import os
import subprocess

logger = logging.getLogger(__name__)

SETTINGS = os.path.join(
    os.path.expanduser('~'),
    'Library',
    'Application Support',
    'Alfred 2',
    'Workflow Data',
    'net.kungfudiscomonkey.alfred.clear',
)
WHITELIST = os.path.join(SETTINGS, 'whitelist.txt')


class Clear(object):
    def _run(self, script):
        p = subprocess.Popen(
            ['osascript'],
            stdin=subprocess.PIPE,
            stderr=subprocess.PIPE,
            stdout=subprocess.PIPE
        )
        out, err = p.communicate(script)

        if err:
            logger.warn(err)

        return out.strip()

    def _write(self, whitelist):
        with open(WHITELIST, 'w') as fp:
            fp.write('\n'.join(whitelist))

    def current_processes(self):
        processes = set(self._run("""
            tell application "System Events" to set the visible of every process to true
            tell application "Finder" to get the name of every process whose visible is true
            """).split(', '))
        processes.remove('Finder')
        return processes

    def kill(self, app):
        print 'Killing', app
        return self._run("""
            tell application "{0}" to quit
            """.format(app))

    def get_whitelist(self):
        if not os.path.exists(WHITELIST):
            return set(['Finder'])
        with open(WHITELIST) as fp:
            whitelist = set(fp.read().strip().split('\n'))
            whitelist.add('Finder')
        return whitelist

    def add_whitelist(self, value):
        print 'Adding', value, 'to whitelist'
        if not os.path.exists(SETTINGS):
            logger.info('Creating settings directory: %s', SETTINGS)<commit_msg>Use gntp directly when available<commit_after>import logging
import os
import subprocess

logger = logging.getLogger(__name__)

BASE = os.path.dirname(__file__)
SETTINGS = os.path.join(
    os.path.expanduser('~'),
    'Library',
    'Application Support',
    'Alfred 2',
    'Workflow Data',
    'net.kungfudiscomonkey.alfred.clear',
)
WHITELIST = os.path.join(SETTINGS, 'whitelist.txt')
try:
    with open(os.path.join(BASE, 'icon.png')) as fp:
        ICON = fp.read()
except:
    ICON = None

try:
    from gntp.config import mini
except:
    def mini(message, **kwargs):
        print message


class Clear(object):
    def message(self, message):
        mini(message, applicationName='Clear', applicationIcon=ICON)

    def _run(self, script):
        p = subprocess.Popen(
            ['osascript'],
            stdin=subprocess.PIPE,
            stderr=subprocess.PIPE,
            stdout=subprocess.PIPE
        )
        out, err = p.communicate(script)

        if err:
            logger.warn(err)

        return out.strip()

    def _write(self, whitelist):
        with open(WHITELIST, 'w') as fp:
            fp.write('\n'.join(whitelist))

    def current_processes(self):
        processes = set(self._run("""
            tell application "System Events" to set the visible of every process to true
            tell application "Finder" to get the name of every process whose visible is true
            """).split(', '))
        processes.remove('Finder')
        return processes

    def kill(self, app):
        mini('Killing {0}'.format(app))
        return self._run("""
            tell application "{0}" to quit
            """.format(app))

    def get_whitelist(self):
        if not os.path.exists(WHITELIST):
            return set(['Finder'])
        with open(WHITELIST) as fp:
            whitelist = set(fp.read().strip().split('\n'))
            whitelist.add('Finder')
        return whitelist

    def add_whitelist(self, value):
        print 'Adding', value, 'to whitelist'
        if not os.path.exists(SETTINGS):
            logger.info('Creating settings directory: %s', SETTINGS)
{'fraction_non_alphanumeric': 0.12390294269488901, 'fraction_numerical': 0.0012906556530717604, 'mean_word_length': 2.292268479184367, 'pattern_counts': {'":': 0, '<': 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': '1248438', 'n_tokens_mistral': 1180, 'n_tokens_neox': 1169, 'n_words': 294}