<commit_before>import click

import contextlib
import ctypes
import ctypes.wintypes
import pathlib
import winreg


@contextlib.contextmanager
def open_environment_key(access=winreg.KEY_READ):
    key = winreg.OpenKey(
        winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER),
        'Environment',
        access=access,
    )
    yield key
    winreg.CloseKey(key)


def get_path_values():
    with open_environment_key() as key:
        try:
            value, vtype = winreg.QueryValueEx(key, 'Path')
        except FileNotFoundError:
            return [], 1
    if not value:
        return [], 1
    return value.split(';'), vtype


def set_path_values(values, vtype):
    joined_value = ';'.join(values)
    with open_environment_key(winreg.KEY_SET_VALUE) as key:
        winreg.SetValueEx(key, 'Path', 0, vtype, joined_value)
    print('Set Path={}'.format(joined_value))


def add_lnk_ext():
    with open_environment_key(winreg.KEY_READ | winreg.KEY_SET_VALUE) as key:
        try:
            value, vtype = winreg.QueryValueEx(key, 'PathExt')
        except FileNotFoundError:
            value, vtype = '', 1
        parts = [v for v in value.split(';') if v]
        if 'LNK' in parts:
            return False
        parts.append('LNK')
        value = ';'.join(parts)
        winreg.SetValueEx(key, 'PathExt', 0, vtype, value)
    print('Set PathExt={}'.format(value))
    return True


def get_snafu_path_values(instdir):
    return [
        str(instdir.joinpath('cmd')),
        str(instdir.joinpath('scripts')),
    ]


def install(instdir):
    changed = add_lnk_ext()
    values, vtype = get_path_values()
    current_values = list(values)
    for val in get_snafu_path_values(instdir):
        if val not in values:
            values.append(val)
    if current_values != values:
        set_path_values(values, vtype)
        return True
    return changed


def uninstall(instdir):
    current_values, vtype = get_path_values()
    values = [
        v for v in current_values
        if v not in get_snafu_path_values(instdir)
    ]
    if current_values != values:
        set_path_values(values, vtype)
        return True
    return False


SendMessage = ctypes.windll.user32.SendMessageW
SendMessage.argtypes = (
    ctypes.wintypes.HWND, ctypes.wintypes.UINT,
    ctypes.wintypes.WPARAM, ctypes.wintypes.LPVOID,
)
SendMessage.restype = ctypes.wintypes.LPARAM    # Synonymous to LRESULT.
HWND_BROADCAST = 0xFFFF
WM_SETTINGCHANGE = 0x1A


def publish():
    SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 'Environment')


@click.command()
@click.argument('base', type=click.Path(exists=True, file_okay=False))
@click.option('--uninstall', 'uninstalling', is_flag=True)
def cli(base, uninstalling):
    path = pathlib.Path(base)
    if uninstalling:
        changed = uninstall(path)
    else:
        changed = install(path)
    if changed:
        publish()


if __name__ == '__main__':
    cli()
<commit_msg>Check global when applying environ registry<commit_after>import click

import contextlib
import ctypes
import ctypes.wintypes
import os
import pathlib
import winreg


def get_parsed_environ(key):
    value = os.environ[key]
    return value.split(';')


@contextlib.contextmanager
def open_environment_key(access=winreg.KEY_READ):
    key = winreg.OpenKey(
        winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER),
        'Environment',
        access=access,
    )
    yield key
    winreg.CloseKey(key)


def get_path_values():
    with open_environment_key() as key:
        try:
            value, vtype = winreg.QueryValueEx(key, 'Path')
        except FileNotFoundError:
            return [], 1
    if not value:
        return [], 1
    return [v for v in value.split(';') if v], vtype


def set_path_values(values, vtype):
    joined_value = ';'.join(values)
    with open_environment_key(winreg.KEY_SET_VALUE) as key:
        winreg.SetValueEx(key, 'Path', 0, vtype, joined_value)
    print('Set Path={}'.format(joined_value))


def add_snafu_paths(values):
    paths = set(get_parsed_environ('Path'))
    values, vtype = get_path_values()
    current_length = len(values)
    for value in paths:
        if value not in paths:
            values.append(value)
    if current_length != len(values):
        set_path_values(values, vtype)
        return True
    return False


def add_lnk_ext():
    if 'LNK' in get_parsed_environ('PathExt'):
        return False
    with open_environment_key(winreg.KEY_READ | winreg.KEY_SET_VALUE) as key:
        try:
            value, vtype = winreg.QueryValueEx(key, 'PathExt')
            parts = [v for v in value.split(';') if v]
        except FileNotFoundError:
            vtype = 1
            parts = ['%PathExt%']
        parts.append('LNK')
        value = ';'.join(parts)
        winreg.SetValueEx(key, 'PathExt', 0, vtype, value)
    print('Set PathExt={}'.format(value))
    return True


def get_snafu_path_values(instdir):
    return [
        str(instdir.joinpath('cmd')),
        str(instdir.joinpath('scripts')),
    ]


def install(instdir):
    changed = []
    changed.append(add_lnk_ext())
    changed.append(add_snafu_paths())
    return any(changed)


def uninstall(instdir):
    current_values, vtype = get_path_values()
    values = [
        v for v in current_values
        if v not in get_snafu_path_values(instdir)
    ]
    if current_values != values:
        set_path_values(values, vtype)
        return True
    return False


SendMessage = ctypes.windll.user32.SendMessageW
SendMessage.argtypes = (
    ctypes.wintypes.HWND, ctypes.wintypes.UINT,
    ctypes.wintypes.WPARAM, ctypes.wintypes.LPVOID,
)
SendMessage.restype = ctypes.wintypes.LPARAM    # Synonymous to LRESULT.
HWND_BROADCAST = 0xFFFF
WM_SETTINGCHANGE = 0x1A


def publish():
    SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 'Environment')


@click.command()
@click.argument('base', type=click.Path(exists=True, file_okay=False))
@click.option('--uninstall', 'uninstalling', is_flag=True)
def cli(base, uninstalling):
    path = pathlib.Path(base)
    if uninstalling:
        changed = uninstall(path)
    else:
        changed = install(path)
    if changed:
        publish()


if __name__ == '__main__':
    cli()

{'fraction_non_alphanumeric': 0.10982658959537572, 'fraction_numerical': 0.0035324341682723185, 'mean_word_length': 3.0369410239792614, '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': '1649868', 'n_tokens_mistral': 2099, 'n_tokens_neox': 2060, 'n_words': 451}