Replace autopass with python script that does a similar thing

autopass.cr doesn't compile on current versions of Crystal, so I wrote
my own little script that does what I want better anyways
This commit is contained in:
Adam Goldsmith 2021-08-18 21:03:53 -04:00
parent 2a549f1301
commit 7c18fe2109
2 changed files with 59 additions and 1 deletions

View File

@ -71,7 +71,7 @@ bindsym $mod+Shift+t exec --no-startup-id "rofi -show mworkspace"
bindsym $mod+n exec $config_dir/rename_workspace.py
bindsym $mod+c exec --no-startup-id rofi-pass --last-used
bindsym $mod+Shift+c exec --no-startup-id autopass
bindsym $mod+Shift+c exec --no-startup-id "$config_dir/pass_typer.py"
# Brighness Control
bindsym XF86MonBrightnessUp exec light -A 10

58
.config/i3/pass_typer.py Executable file
View File

@ -0,0 +1,58 @@
#!/usr/bin/env python3
import os
import re
import subprocess
from functools import partial
from pathlib import PosixPath
PASSWORD_STORE = PosixPath(
os.environ.get("PASSWORD_STORE_DIR", PosixPath("~/.password-store").expanduser())
)
ALIASES = {
"salt": "cms-net-svcs",
}
cmd = partial(subprocess.run, capture_output=True, encoding="ascii")
def notify(summary: str, body: str) -> None:
subprocess.run(["notify-send", summary, body])
def rofi_select(options: list[str]) -> str:
options_str = "\n".join(options)
rofi = cmd(["rofi", "-dmenu"], input=options_str)
return rofi.stdout.strip()
def get_password(password_name: str) -> None:
pass_result = cmd(["pass", password_name])
password, _, _ = pass_result.stdout.partition("\n")
return password
def select_and_type(glob: str) -> None:
files = PASSWORD_STORE.glob(glob)
file_list = [str(f.relative_to(PASSWORD_STORE).with_suffix("")) for f in files]
selected = rofi_select(file_list)
password = get_password(selected)
subprocess.run(["xdotool", "type", password + '\n'])
window_name = cmd(["xdotool", "getactivewindow", "getwindowname"]).stdout.strip()
ssh_match = re.search(":(mosh|ssh) (?P<server>.*)", window_name)
if ssh_match:
raw_server_name = ssh_match.group("server")
server_name = ALIASES.get(raw_server_name, raw_server_name)
notify(f"Matched server '{server_name}'", f"Window name: {window_name}")
select_and_type(f"servers/**/{server_name}.gpg")
else:
notify("Window name did not match any rules", f"Window name: {window_name}")