mirror of
https://github.com/redicculus/arch2appimage.git
synced 2025-05-15 15:00:14 -07:00
Refactory of the code
Rewrote some part of the code in a more pythonic way and made the code follow the PEP8 style guide
This commit is contained in:
parent
709ab25ba8
commit
069a5f04db
70
utils.py
70
utils.py
@ -7,7 +7,7 @@ import tarfile
|
||||
import inquirer
|
||||
import requests
|
||||
import tempfile
|
||||
import zstandard
|
||||
import zstandard
|
||||
import subprocess
|
||||
|
||||
from pathlib import Path
|
||||
@ -24,6 +24,7 @@ from rich.progress import (
|
||||
|
||||
from config import *
|
||||
|
||||
|
||||
class Utils:
|
||||
def __init__(self) -> None:
|
||||
self.aur_pkgs = {}
|
||||
@ -41,7 +42,7 @@ class Utils:
|
||||
TimeRemainingColumn(),
|
||||
" "
|
||||
)
|
||||
|
||||
|
||||
def get_pkg(self, pkg_name):
|
||||
output = None
|
||||
data = requests.get(ARCH_SEARCH_URL.format(pkg=pkg_name)).json()
|
||||
@ -49,70 +50,58 @@ class Utils:
|
||||
|
||||
if len(res) > 0:
|
||||
output = ARCH_URL.format(
|
||||
repo = res[0]["repo"],
|
||||
arch = res[0]["arch"],
|
||||
pkg = res[0]["pkgname"]
|
||||
repo=res[0]["repo"],
|
||||
arch=res[0]["arch"],
|
||||
pkg=res[0]["pkgname"]
|
||||
)
|
||||
return output
|
||||
|
||||
aur_matches = {}
|
||||
for pkg in self.aur_pkgs:
|
||||
if pkg.startswith(pkg_name):
|
||||
aur_matches[pkg] = self.aur_pkgs[pkg]
|
||||
|
||||
aur_matches = {pkg: self.aur_pkgs[pkg]
|
||||
for pkg in self.aur_pkgs if pkg.startswith(pkg_name)}
|
||||
|
||||
if len(aur_matches) == 1:
|
||||
output = os.path.join(AUR_URL, list(aur_matches.values())[0])
|
||||
|
||||
elif len(aur_matches) > 1:
|
||||
choices = list(aur_matches.keys())
|
||||
choices.append("None of the above")
|
||||
s = self.user_select("Multiple Packages were found. Please select which one to use", choices)
|
||||
s = self.user_select(
|
||||
"Multiple Packages were found. Please select which one to use", choices)
|
||||
if s != "None of the above":
|
||||
output = os.path.join(AUR_URL, aur_matches[s])
|
||||
|
||||
return output
|
||||
|
||||
|
||||
def user_select(self, que, choices):
|
||||
print()
|
||||
print(que)
|
||||
print(f"\n{que}")
|
||||
q = [inquirer.List('a', message=">>", choices=choices)]
|
||||
return inquirer.prompt(q)["a"]
|
||||
|
||||
|
||||
def user_text(self, que):
|
||||
print()
|
||||
print(que)
|
||||
print(f"\n{que}")
|
||||
q = [inquirer.Text('a', message=">>")]
|
||||
return inquirer.prompt(q)["a"]
|
||||
|
||||
|
||||
def user_confirm(self, que, y_otp="Yes", n_opt="No"):
|
||||
ans = self.user_select(que, [y_otp, n_opt])
|
||||
if ans == y_otp:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
return ans == y_otp
|
||||
|
||||
def user_path(self, que, required=False):
|
||||
while True:
|
||||
print()
|
||||
print(que)
|
||||
print(f"\n{que}")
|
||||
q = [inquirer.Text('a', message=">>")]
|
||||
p = inquirer.prompt(q)["a"]
|
||||
if not p:
|
||||
if required:
|
||||
print("This field is required.")
|
||||
continue
|
||||
else:
|
||||
p = None
|
||||
break
|
||||
p = None
|
||||
break
|
||||
|
||||
if os.path.exists(p):
|
||||
break
|
||||
|
||||
print("File does not exist. Please try again.")
|
||||
print()
|
||||
print("File does not exist. Please try again.\n")
|
||||
|
||||
return p
|
||||
|
||||
@ -122,24 +111,24 @@ class Utils:
|
||||
x = re.findall('href="(.*tar.zst)"', txt)
|
||||
|
||||
for i in x:
|
||||
name = urllib.parse.unquote(i, encoding='utf-8', errors='replace').replace(".pkg.tar.zst", "")
|
||||
name = urllib.parse.unquote(
|
||||
i, encoding='utf-8', errors='replace').replace(".pkg.tar.zst", "")
|
||||
self.aur_pkgs[name] = i
|
||||
|
||||
|
||||
def download(self, url, dest, name):
|
||||
progress = self.new_progress()
|
||||
with progress:
|
||||
print(f"Downloading {name}...")
|
||||
task_id = progress.add_task("download", filename=" ", start=False)
|
||||
response = urlopen(url)
|
||||
progress.update(task_id, total=int(response.info()["Content-length"]))
|
||||
progress.update(task_id, total=int(
|
||||
response.info()["Content-length"]))
|
||||
with open(dest, "wb") as dest_file:
|
||||
progress.start_task(task_id)
|
||||
for data in iter(partial(response.read, 32768), b""):
|
||||
dest_file.write(data)
|
||||
progress.update(task_id, advance=len(data))
|
||||
|
||||
|
||||
def extract_zst(self, zst_file, out_path):
|
||||
zst_file = Path(zst_file).expanduser()
|
||||
out_path = Path(out_path).expanduser().resolve()
|
||||
@ -153,8 +142,7 @@ class Utils:
|
||||
z.extractall(out_path)
|
||||
|
||||
def validate_desktop_file(self, file_path):
|
||||
out = self.run_cmd(f"desktop-file-validate {file_path}", True)
|
||||
if out:
|
||||
if out := self.run_cmd(f"desktop-file-validate {file_path}", True):
|
||||
return False, out
|
||||
else:
|
||||
return True, None
|
||||
@ -171,9 +159,10 @@ class Utils:
|
||||
return proc.stdout.decode("utf-8").strip()
|
||||
else:
|
||||
subprocess.run(shlex.split(cmd))
|
||||
|
||||
|
||||
def set_icon_desktop_file(self, desktop_file, icon_name):
|
||||
self.run_cmd(f"desktop-file-edit {desktop_file} --set-icon={icon_name}")
|
||||
self.run_cmd(
|
||||
f"desktop-file-edit {desktop_file} --set-icon={icon_name}")
|
||||
|
||||
def make_executable(self, file_path):
|
||||
self.run_cmd(f"chmod +x {file_path}")
|
||||
@ -194,9 +183,4 @@ class Utils:
|
||||
return pkgs
|
||||
|
||||
def max_len(self, lst):
|
||||
max1 = len(lst[0])
|
||||
for i in lst:
|
||||
if(len(i) > max1):
|
||||
max1 = len(i)
|
||||
|
||||
return max1
|
||||
return max(len(i) for i in lst)
|
||||
|
Loading…
x
Reference in New Issue
Block a user