This repository has been archived by the owner on Dec 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utility.py
214 lines (188 loc) · 4.47 KB
/
Utility.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!C:\Python27\pythonw.exe
# Arves100's Utility Library (v.1.2 ~ Revolution)
## Changelog
### 1.0
#### Initial Build
### 1.1
#### Added URLLib and Mega function
### 1.2
#### Restylized Module
#### Added RunFile
#### Added more Module Info
#### Fixed Wait-Time
## Examples:
### Check if you are using Windows:
#### Utility.OsUtil().IsWin32()
##### return True when you are using Windows
##### return False when you are not using Windows
class OsUtil():
def IsWin32(self):
import sys
if sys.platform == "win32":
return True
else:
return False
class PythonUtil():
def Version(self,major,minor,micro):
import sys
mjver = 0
mnver = 0
mcver = 0
try:
mjver = int(major)
except:
raise ValueError
if not minor == False:
try:
mnver = int(minor)
except:
raise ValueError
if not micro == False:
try:
mcver = int(micro)
except:
raise ValueError
if sys.version_info[:3] == (mjver,mnver,mcver):
return True
else:
return False
if sys.version_info[:2] == (mjver,mnver):
return True
else:
return False
if sys.version_info[0] == (mjver):
return True
else:
return False
class ConsoleUtil():
def ClearConsole(self):
import os
if OsUtil().IsWin32():
os.system("cls")
else:
os.system("clear")
def Quit(self,status):
import sys
print "Premi INVIO per uscire..."
try:
raw_input()
except EOFError:
pass
sys.exit(status)
def SetTitle(self,title):
if OsUtil().IsWin32():
import os
os.system("title " + title)
else:
import sys
sys.stdout.write("\x1b]2;" + title + "\x07")
class FileUtil:
def ReadLineByURL(self,url):
import urllib2
data = urllib2.urlopen(url)
return data
def ReadLineByFile(self,filename,num):
rtn1 = ""
var1 = 1
tmp1 = open(filename,"r")
for line in tmp1:
if var1 == num:
rtn1 = line
break
var1 = var1 + 1
tmp1.close()
return rtn1
def WriteFile(self,filename,text):
f = open(filename,"w")
f.write(str(text))
f.close()
def AppendFile(self,filename,text):
f = open(filename,"a")
f.write(str(text) + "\n")
f.close()
def RemoveFile(self,filename):
import os
if OsUtil().IsWin32():
os.system("del " + filename + ">nul 2>&1")
else:
os.system("rm -rf " + filename)
def CheckFile(self,filename):
import os
if os.path.isfile(filename):
return True
else:
return False
def CheckDirectory(self,dirname):
import os
if os.path.isdir(dirname):
return True
else:
return False
def DownloadFileFromMega(self,url):
from mega import Mega
m = Mega.from_ephemeral()
try:
m.download_from_url(url)
except:
return False
return True
def DownloadFile(self,url,savefile):
import urllib
urllib.urlretrieve(url,savefile)
## TODO
'''def RunAppWithExitcode(app,elevate):
if os.system(app) == 0:
return True
else:
return False
def RunAppWithArgs(app,arg,elevate):
if elevate == True:
if not os.system("Powershell") == 0:
print("Powershell e\' richiesto per l'avvio di app come administratore!\nDisabilita l'UAC o installa Powershell\nWindows XP:https://www.microsoft.com/it-it/download/details.aspx?id=9591\nWindows Vista:https://www.microsoft.com/it-it/download/details.aspx?id=9864")
Quit(1)
subprocess.call("powershell Start-Process \"" + app + )
subprocess.call([app,arg1])
def RunAppWithNoArgs(app):
subprocess.call([app,""])'''
class INIUtil():
def ReadStringByIni(self,section,value,ini):
import ConfigParser
config = ConfigParser.RawConfigParser()
try:
config.read(ini)
except:
print("Impossibile leggere l'INI " + ini)
ConsoleUtil().Quit(1)
return config.get(section,value)
def ReadBooleanByIni(self,section,value,ini):
import ConfigParser
config = ConfigParser.RawConfigParser()
try:
config.read(ini)
except:
print("Impossibile leggere l'INI " + ini)
ConsoleUtil().Quit(1)
return config.getboolean(section,value)
def ReadIntByIni(self,section,value,ini):
import ConfigParser
config = ConfigParser.RawConfigParser()
try:
config.read(ini)
except:
print("Impossibile leggere l'INI " + ini)
ConsoleUtil().Quit(1)
return config.getint(section,value)
def ReadFloatByIni(self,section,value,ini):
config = ConfigParser.RawConfigParser()
try:
config.read(ini)
except:
print("Impossibile leggere l'INI " + ini)
ConsoleUtil().Quit(1)
return config.getfloat(section,value)
def Version():
return 1.2
def NameVersion():
return "Revolution"
def Author():
return "Arves100"