-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.py
84 lines (63 loc) · 2.3 KB
/
profile.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
import sys,os
import i18n
IPS_PROFILES_PATH = os.getenv('APPDATA') + '\\pings'
def get_profile_path(profile):
return IPS_PROFILES_PATH + "\\" + profile
def check_profiles():
if not os.path.isdir(IPS_PROFILES_PATH):
os.makedirs(IPS_PROFILES_PATH)
def get_all_profiles():
profile_names = [f for f in os.listdir(IPS_PROFILES_PATH) if os.path.isfile(os.path.join(IPS_PROFILES_PATH, f))]
profile_content = []
for p in profile_names:
with open(get_profile_path(p), 'r') as _profile_content:
profile_content.append(_profile_content.read(15).split(',')[0])
return (profile_names, profile_content)
def get_ips_from_profile(profile):
profile_path = get_profile_path(profile)
if not os.path.isfile(profile_path):
print(i18n.PROFILE_NOT_EXISTS.format(profile))
sys.exit()
return _parse_file(profile_path)
def _parse_file(file_path):
raw_ips = raw_ips_names = []
try:
f = open(file_path, 'r')
raw_ips = f.readline().split(',')
raw_ips_names = f.readline().split(',')
f.close()
except:
print(i18n.FILE_UNABLE_ACCESS.format(file_path))
return (raw_ips, raw_ips_names)
def save_profile(profile, current_profile, ips):
file_path = get_profile_path(profile)
current_file_path = get_profile_path(current_profile)
if file_path:
return (_save_file(file_path, ips, False), profile)
else:
if current_file_path:
return (_save_file(current_file_path, ips, True), current_file_path)
else:
return (i18n.FILE_PATH_NOT_PRESENT, '')
def _save_file(file_path, ips, is_overwrite):
result = ''
w_ips, w_ips_names = _parse_ips(ips)
try:
f = open(file_path, 'w')
f.truncate(0)
f.write(w_ips)
f.write('\n')
f.write(w_ips_names)
f.close()
if is_overwrite: result = i18n.SAVE_PROFILE_SUCCESS_OVERWRITE.format(file_path)
else: result = i18n.SAVE_PROFILE_SUCCESS_NEW.format(file_path)
except:
result = i18n.FILE_UNABLE_ACCESS.format(file_path)
return result
def _parse_ips(ips):
parsed_ips = ''
parsed_names = ''
for ip in ips:
parsed_ips += ip + ','
parsed_names += ips[ip].get_name() + ','
return (parsed_ips[:-1], parsed_names[:-1])