| 1 |
import json |
| 2 |
import os |
| 3 |
import subprocess |
| 4 |
import sys |
| 5 |
|
| 6 |
|
| 7 |
def eprint(*args, **kwargs): |
| 8 |
print(*args, file=sys.stderr, **kwargs) |
| 9 |
|
| 10 |
|
| 11 |
def is_command(cmd): |
| 12 |
cmd = cmd.split()[0] |
| 13 |
cmd = "command -v {}".format(cmd) |
| 14 |
try: |
| 15 |
is_cmd = subprocess.check_output( |
| 16 |
cmd, shell=True).decode("utf-8").strip() |
| 17 |
if is_cmd: |
| 18 |
return True |
| 19 |
|
| 20 |
except subprocess.CalledProcessError: |
| 21 |
return False |
| 22 |
|
| 23 |
|
| 24 |
def temp_dir(): |
| 25 |
if os.getenv("TMPDIR"): |
| 26 |
return os.getenv("TMPDIR") |
| 27 |
elif os.getenv("TEMP"): |
| 28 |
return os.getenv("TEMP") |
| 29 |
elif os.getenv("TMP"): |
| 30 |
return os.getenv("TMP") |
| 31 |
|
| 32 |
return "/tmp" |
| 33 |
|
| 34 |
|
| 35 |
def load_text_file(path): |
| 36 |
try: |
| 37 |
with open(path, 'r') as file: |
| 38 |
data = file.read() |
| 39 |
return data |
| 40 |
except Exception as e: |
| 41 |
return None |
| 42 |
|
| 43 |
|
| 44 |
def save_string(string, file): |
| 45 |
try: |
| 46 |
file = open(file, "wt") |
| 47 |
file.write(string) |
| 48 |
file.close() |
| 49 |
except Exception as e: |
| 50 |
print(f"Error writing '{file}': {e}") |
| 51 |
|
| 52 |
|
| 53 |
def load_json(path): |
| 54 |
try: |
| 55 |
with open(path, 'r') as f: |
| 56 |
return json.load(f) |
| 57 |
except Exception as e: |
| 58 |
print("Error loading json: {}".format(e)) |
| 59 |
return None |
| 60 |
|
| 61 |
|
| 62 |
def get_shell_data_dir(): |
| 63 |
data_dir = "" |
| 64 |
home = os.getenv("HOME") |
| 65 |
xdg_data_home = os.getenv("XDG_DATA_HOME") |
| 66 |
|
| 67 |
if xdg_data_home: |
| 68 |
data_dir = os.path.join(xdg_data_home, "nwg-shell/") |
| 69 |
else: |
| 70 |
if home: |
| 71 |
data_dir = os.path.join(home, ".local/share/nwg-shell/") |
| 72 |
|
| 73 |
return data_dir |
| 74 |
|
| 75 |
|
| 76 |
def load_shell_data(): |
| 77 |
shell_data_file = os.path.join(get_shell_data_dir(), "data") |
| 78 |
shell_data = load_json(shell_data_file) if os.path.isfile(shell_data_file) else {} |
| 79 |
|
| 80 |
defaults = { |
| 81 |
"interface-locale": "" |
| 82 |
} |
| 83 |
|
| 84 |
for key in defaults: |
| 85 |
if key not in shell_data: |
| 86 |
shell_data[key] = defaults[key] |
| 87 |
|
| 88 |
return shell_data |