-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.py
More file actions
executable file
·41 lines (37 loc) · 1.16 KB
/
common.py
File metadata and controls
executable file
·41 lines (37 loc) · 1.16 KB
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
#!/usr/bin/python3 -u
"""
Description: Common module
Author: thnikk
"""
from subprocess import Popen, PIPE
import sys
import os
import re
import json
def get_selection(input_list, prompt="") -> str:
""" Get selection from list """
lines = str(min(len(input_list), 8))
with Popen(
["fuzzel", "--dmenu", "-l", lines, "-p", prompt],
stdin=PIPE, stdout=PIPE, stderr=PIPE
) as fuzzel:
selection = fuzzel.communicate(
input=bytes("\n".join(input_list), 'utf-8'))[0]
if fuzzel.returncode != 0:
sys.exit(1)
return selection.decode().strip()
def get_config(path, default) -> dict:
try:
with open(
os.path.expanduser(path), 'r', encoding='utf-8'
) as file:
output = file.read()
# output = re.sub(re.compile(r"/\*.*?\*/", re.DOTALL), "", output)
# output = re.sub(re.compile(r"//.*?\n"), "", output)
return json.loads(output)
except FileNotFoundError:
with open(
os.path.expanduser(path), 'w', encoding='utf-8'
) as file:
file.write(json.dumps(default, indent=4))
return default