1
0
Fork 0
mirror of synced 2024-05-19 11:42:40 +12:00
bulk-downloader-for-reddit/bdfr/completion.py

69 lines
3.5 KiB
Python
Raw Normal View History

2022-12-17 08:56:44 +13:00
#!/usr/bin/env python3
2022-12-21 07:32:43 +13:00
# -*- coding: utf-8 -*-
2022-12-17 08:56:44 +13:00
import subprocess
2022-12-21 07:05:50 +13:00
from os import environ
from pathlib import Path
2022-12-17 08:56:44 +13:00
import appdirs
class Completion:
def __init__(self, shell: str):
self.shell = shell
2022-12-21 07:05:50 +13:00
self.env = environ.copy()
2022-12-17 08:56:44 +13:00
self.share_dir = appdirs.user_data_dir()
self.entry_points = ["bdfr", "bdfr-archive", "bdfr-clone", "bdfr-download"]
2022-12-17 08:56:44 +13:00
def install(self):
if self.shell in ("all", "bash"):
comp_dir = self.share_dir + "/bash-completion/completions/"
2022-12-21 07:05:50 +13:00
if not Path(comp_dir).exists():
print("Creating Bash completion directory.")
2022-12-21 07:05:50 +13:00
Path(comp_dir).mkdir(parents=True, exist_ok=True)
2022-12-17 08:56:44 +13:00
for point in self.entry_points:
self.env[f"_{point.upper().replace('-', '_')}_COMPLETE"] = "bash_source"
2023-01-26 16:23:59 +13:00
with Path(comp_dir + point).open(mode="w") as file:
2022-12-17 08:56:44 +13:00
file.write(subprocess.run([point], env=self.env, capture_output=True, text=True).stdout)
print(f"Bash completion for {point} written to {comp_dir}{point}")
if self.shell in ("all", "fish"):
comp_dir = self.share_dir + "/fish/vendor_completions.d/"
2022-12-21 07:05:50 +13:00
if not Path(comp_dir).exists():
print("Creating Fish completion directory.")
2022-12-21 07:05:50 +13:00
Path(comp_dir).mkdir(parents=True, exist_ok=True)
2022-12-17 08:56:44 +13:00
for point in self.entry_points:
self.env[f"_{point.upper().replace('-', '_')}_COMPLETE"] = "fish_source"
2023-01-26 16:23:59 +13:00
with Path(comp_dir + point + ".fish").open(mode="w") as file:
2022-12-17 08:56:44 +13:00
file.write(subprocess.run([point], env=self.env, capture_output=True, text=True).stdout)
print(f"Fish completion for {point} written to {comp_dir}{point}.fish")
2022-12-17 08:56:44 +13:00
if self.shell in ("all", "zsh"):
comp_dir = self.share_dir + "/zsh/site-functions/"
2022-12-21 07:05:50 +13:00
if not Path(comp_dir).exists():
print("Creating Zsh completion directory.")
2022-12-21 07:05:50 +13:00
Path(comp_dir).mkdir(parents=True, exist_ok=True)
2022-12-17 08:56:44 +13:00
for point in self.entry_points:
self.env[f"_{point.upper().replace('-', '_')}_COMPLETE"] = "zsh_source"
2023-01-26 16:23:59 +13:00
with Path(comp_dir + "_" + point).open(mode="w") as file:
2022-12-17 08:56:44 +13:00
file.write(subprocess.run([point], env=self.env, capture_output=True, text=True).stdout)
print(f"Zsh completion for {point} written to {comp_dir}_{point}")
2022-12-17 08:56:44 +13:00
def uninstall(self):
if self.shell in ("all", "bash"):
comp_dir = self.share_dir + "/bash-completion/completions/"
for point in self.entry_points:
2022-12-21 07:05:50 +13:00
if Path(comp_dir + point).exists():
Path(comp_dir + point).unlink()
2022-12-17 08:56:44 +13:00
print(f"Bash completion for {point} removed from {comp_dir}{point}")
if self.shell in ("all", "fish"):
comp_dir = self.share_dir + "/fish/vendor_completions.d/"
for point in self.entry_points:
2022-12-21 07:05:50 +13:00
if Path(comp_dir + point + ".fish").exists():
Path(comp_dir + point + ".fish").unlink()
print(f"Fish completion for {point} removed from {comp_dir}{point}.fish")
2022-12-17 08:56:44 +13:00
if self.shell in ("all", "zsh"):
comp_dir = self.share_dir + "/zsh/site-functions/"
for point in self.entry_points:
2022-12-21 07:05:50 +13:00
if Path(comp_dir + "_" + point).exists():
Path(comp_dir + "_" + point).unlink()
print(f"Zsh completion for {point} removed from {comp_dir}_{point}")