-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
99 lines (89 loc) · 2.58 KB
/
util.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os
import platform
import subprocess
import json
if platform.system() == "Windows":
configPath = os.path.expandvars(r'%APPDATA%/BeamIT')
else:
configPath = os.path.expandvars(r'/home/$USER/.beamit')
"""
Method to read config out of config.json file
"""
def getConfig():
try:
with open(os.path.join(configPath, "config.json"), 'r') as f:
config = json.load(f)
return config
except:
return None
"""
Method to store a config in a config.json file
"""
def storeConfig(serverurl: str, username: str, devicename: str, devicetoken: str, filepath: str, autoOpen: bool):
jsonConfig = json.loads('{"serverurl": "%s", "username": "%s", "devicename": "%s", "devicetoken": "%s", "filepath": "%s", "autoOpen": "%s"}' % (serverurl, username, devicename, devicetoken, filepath, autoOpen))
try:
if not os.path.exists(configPath):
os.makedirs(configPath)
with open(os.path.join(configPath, "config.json"), 'w') as f:
json.dump(jsonConfig, f)
return True
except Exception as e:
return False
"""
Method to remove the config.json file
"""
def removeConfig():
try:
if os.path.exists(os.path.join(configPath, "config.json")):
os.remove(os.path.join(configPath, "config.json"))
return True
except Exception as e:
return False
"""
Method to create a new folder at a given path
"""
def createFolder(path: str):
try:
if not os.path.exists(path):
os.makedirs(path)
return True
except:
return False
"""
Method to read a file at a given path
"""
def getFile(filepath: str):
filepath = filepath.removeprefix("file:///")
filename = os.path.basename(filepath)
f = open(filepath, 'rb')
return filename, f
"""
Method to store a file at a given path
"""
def storeFile(path: str, filename: str, content):
try:
if not os.path.exists(path):
os.makedirs(path)
except Exception as e:
print(e)
return False
destfile = os.path.join(path, filename)
try:
f = open(destfile, "wb")
f.write(content)
f.close
except Exception as e:
print(e)
return False
"""
Method to launch a file with its default program
"""
def startfile(path: str, filename: str, args: str = ""):
destfile = os.path.join(path, filename)
try:
if platform.system() == "Windows":
os.startfile(destfile, args)
else:
subprocess.call(["open", destfile])
except Exception as e:
print(e)