-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_functions.py
72 lines (64 loc) · 2.32 KB
/
custom_functions.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
65
66
67
68
69
70
71
72
import time, traceback, sys
from subprocess import run
from shutil import get_terminal_size as t_size
# If I need a module that isn't installed
def install(module, module_name=""):
print(f"{module} is not installed!")
if module_name == "":
# Using pip instead of subprocess as calling
# with terminal results in an error
run([sys.executable, "-m", "pip", "install", module, "--quiet"], check=True)
#pip.main(["install", module, "--quiet"])
else:
run([sys.executable, "-m", "pip", "install", module_name, "--quiet"], check=True)
#pip.main(["install", module_name, "--quiet"])
print(f"Installed {module}!")
time.sleep(1)
# It becomes really slow for some reason, I'm not really sure why
try:
from ujson import *
except ModuleNotFoundError:
install("ujson")
from ujson import *
try:
from colorama import Fore, init
except ModuleNotFoundError:
install("colorama")
from colorama import Fore, init
init(strip=False, convert=False, autoreset=True)
# Simple function to load json from file
def load_json(path):
try:
with open(path, "r") as file:
return loads(file.read())
except JSONDecodeError:
print(f"\n{Fore.RED}{path} got a JSON Decode Error!")
print(f"Redownload from https://github.com/NSPC911/scripts/blob/main/config.json if you can't fix it!")
print(f"{Fore.YELLOW}{traceback.format_exc()}")
exit(1)
except FileNotFoundError:
print(f"\n{Fore.RED}{path} not found!")
print(f"Make sure that the config file still exists and not deleted!")
print(f"{Fore.YELLOW}{traceback.format_exc()}")
exit(1)
# Simple function to save json into file
def dump_json(path, dictionary):
the_json = dumps(dictionary, indent=4)
the_json = the_json.replace(r"\/","/")
with open(path, "w") as file:
file.write(the_json)
# Function to clear a line
def clear_line(withchar=' ', end=""):
print(f"\r{t_size().columns * withchar}", end=end)
# Function to check whether a file is binary
def is_binary(file_path):
try:
with open(file_path, 'rb') as f:
for byte in f.read(1024):
if byte > 127:
return True
return False
except:
return True
def replace_unicode(match):
return chr(int(match.group(0)[2:], 16))