-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathultra_fetcher_9000.py
executable file
·64 lines (51 loc) · 1.5 KB
/
ultra_fetcher_9000.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python3
import json
import subprocess
import os
import sys
from dataclasses import dataclass
from shutil import which
def is_real_command(exec_name):
return which(exec_name) is not None
def cd_into_script_dir():
os.chdir(os.path.dirname(os.path.abspath(__file__)))
@dataclass
class Fetch:
execu: str
args: list[str]
def run(self):
if not is_real_command(self.execu):
return False
expanded_args = []
for arg in self.args:
expanded_args.append(os.path.expanduser(arg))
full_args = [self.execu] + expanded_args
status = subprocess.call(full_args)
return status == 0
def run_fetches(fetches):
for fetch in fetches:
if fetch.run():
sys.exit(0)
def parse_fetches(config):
cfg_fetches = config["fetches"]
fetches = []
for fetch in cfg_fetches:
fetches.append(Fetch(fetch["exec"], fetch["args"]))
return fetches
def load_config():
with open("ultrafetcher9000cfg.json", "r", encoding="utf8") as ultrafetcher9000cfg:
return json.load(ultrafetcher9000cfg)
def main():
cd_into_script_dir()
config = load_config()
fetches = parse_fetches(config)
run_fetches(fetches)
print("Unable to fetch")
fetch_names = []
for fetch in fetches:
fetch_names.append(fetch.execu)
print(f"Tried: {', '.join(fetch_names)}")
print("Please make sure one of these programs is in your path.")
sys.exit(1)
if __name__ == "__main__":
main()