<commit_before>#!/usr/bin/env python3

import sys
import os
import os.path as path
import subprocess
import shutil
import argparse


XCLIP = shutil.which('xclip')
XDOTOOL = shutil.which('xdotool')
DMENU = shutil.which('dmenu')
PASS = shutil.which('pass')
STORE = path.normpath(path.expanduser('~/.password-store'))


def check_output(args):
    output = subprocess.check_output(args)
    output = output.decode('utf-8').split('\n')
    return output


def dmenu(choices, args=[], path=DMENU):
    """
    Displays a menu with the given choices by executing dmenu
    with the provided list of arguments. Returns the selected choice
    or None if the menu was aborted.
    """
    dmenu = subprocess.Popen([path] + args,
                             stdin=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             stdout=subprocess.PIPE)
    choice_lines = '\n'.join(map(str, choices))
    choice, errors = dmenu.communicate(choice_lines.encode('utf-8'))
    if dmenu.returncode not in [0, 1]:
        print("dmenu returned {} and error:\n{}"
              .format(dmenu.returncode, errors.decode('utf-8')),
              file=sys.stderr)
        sys.exit(1)
    choice = choice.decode('utf-8').rstrip()
    return choice if choice in choices else None


def collect_choices(store):
    choices = []
    for dirpath, dirs, files in os.walk(store, followlinks=True):
        dirsubpath = dirpath[len(store):].lstrip('/')
        for f in files:
            if f.endswith('.gpg'):
                choices += [os.path.join(dirsubpath, f[:-4])]
    return choices


def xdotool(entries, press_return, delay=None, window_id=None):
    getwin = ""
    always_opts = "--clearmodifiers"
    if delay:
        always_opts += " --delay '{}'".format(delay)
    if not window_id:
        getwin = "getactivewindow\n"
    else:
        always_opts += " --window {}".format(window_id)

    commands = [c for e in entries[:-1] for c in (
        "type {} '{}'".format(always_opts, e),
        always_opts + " Tab")]
    if len(entries) > 0:
        commands += ["type {} '{}'".format(always_opts, entries[-1])]
    if press_return:
        commands += ["key {} Return".format(always_opts)]
    for command in commands:
        input_text = "{}{}".format(getwin, command)
        subprocess.check_output([XDOTOOL, "-"],
                                input=input_text,
                                universal_newlines=True)


def get_pass_output(gpg_file, path=PASS, store=STORE):
    environ = os.environ.copy()
    environ["PASSWORD_STORE_DIR"] = store
    passp = subprocess.Popen([path, gpg_file], env=environ,
                             stderr=subprocess.PIPE,<commit_msg>Fix missing key command for sending Tab keystrokes after each entry<commit_after>#!/usr/bin/env python3

import sys
import os
import os.path as path
import subprocess
import shutil
import argparse


XCLIP = shutil.which('xclip')
XDOTOOL = shutil.which('xdotool')
DMENU = shutil.which('dmenu')
PASS = <PASSWORD>('pass')
STORE = path.normpath(path.expanduser('~/.password-store'))


def check_output(args):
    output = subprocess.check_output(args)
    output = output.decode('utf-8').split('\n')
    return output


def dmenu(choices, args=[], path=DMENU):
    """
    Displays a menu with the given choices by executing dmenu
    with the provided list of arguments. Returns the selected choice
    or None if the menu was aborted.
    """
    dmenu = subprocess.Popen([path] + args,
                             stdin=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             stdout=subprocess.PIPE)
    choice_lines = '\n'.join(map(str, choices))
    choice, errors = dmenu.communicate(choice_lines.encode('utf-8'))
    if dmenu.returncode not in [0, 1]:
        print("dmenu returned {} and error:\n{}"
              .format(dmenu.returncode, errors.decode('utf-8')),
              file=sys.stderr)
        sys.exit(1)
    choice = choice.decode('utf-8').rstrip()
    return choice if choice in choices else None


def collect_choices(store):
    choices = []
    for dirpath, dirs, files in os.walk(store, followlinks=True):
        dirsubpath = dirpath[len(store):].lstrip('/')
        for f in files:
            if f.endswith('.gpg'):
                choices += [os.path.join(dirsubpath, f[:-4])]
    return choices


def xdotool(entries, press_return, delay=None, window_id=None):
    getwin = ""
    always_opts = "--clearmodifiers"
    if delay:
        always_opts += " --delay '{}'".format(delay)
    if not window_id:
        getwin = "getactivewindow\n"
    else:
        always_opts += " --window {}".format(window_id)

    commands = [c for e in entries[:-1] for c in (
        "type {} '{}'".format(always_opts, e),
        "key {} Tab".format(always_opts))]
    if len(entries) > 0:
        commands += ["type {} '{}'".format(always_opts, entries[-1])]
    if press_return:
        commands += ["key {} Return".format(always_opts)]
    for command in commands:
        input_text = "{}{}".format(getwin, command)
        subprocess.check_output([XDOTOOL, "-"],
                                input=input_text,
                                universal_newlines=True)


def get_pass_output(gpg_file, path=PASS, store=STORE):
    environ = os.environ.copy()
    environ["PASSWORD_STORE_DIR"] = store
    passp = subprocess.Popen([path, gpg_file], env=environ,
                             stderr=subprocess.PIPE,
{'fraction_non_alphanumeric': 0.13599853961299743, 'fraction_numerical': 0.004381161007667032, 'mean_word_length': 2.512179487179487, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, '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': '2427804', 'n_tokens_mistral': 1729, 'n_tokens_neox': 1705, 'n_words': 399}