forked from actlaboratory/falcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.py
260 lines (225 loc) · 7.85 KB
/
misc.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# -*- coding: utf-8 -*-
#Falcon miscellaneous helper objects
#Copyright (C) 2019-2020 Yukio Nozawa <personal@nyanchangames.com>
#Copyright (C) 2019-2020 yamahubuki <itiro.ishino@gmail.com>
#Note: All comments except these top lines will be written in Japanese.
import ctypes
import pywintypes
import os
import re
import time
import hashlib
import winreg
import win32api
import win32file
import constants
import globalVars
from logging import getLogger
from simpleDialog import dialog
log=getLogger("falcon.misc")
falconHelper=ctypes.cdll.LoadLibrary("falconHelper.dll")
class Timer:
"""シンプルなタイマー。経過時間や処理時間を計測するのに使う。単位は秒で、float。"""
def __init__(self):
self.started=time.time()
@property
def elapsed(self):
return time.time()-self.started
#ヘルパー関数群
UNIT_AUTO=-1
UNIT_B=0
UNIT_KB=1
UNIT_MB=2
UNIT_GB=3
UNIT_TB=4
UNIT_STR=("Bytes", "KB", "MB", "GB", "TB")
def ConvertBytesTo(b, unit, appendUnit=False):
"""バイト数を受け取って、指定された単位を使ったサイズを返す。appendUnit=True にすると、単位の文字列を最後に付けてくれる。"""
if b<0:return ""
num=0
if unit==UNIT_AUTO: unit=DetermineSizeUnit(b)
if unit==UNIT_B:
num=b
elif unit==UNIT_KB:
num=b/1024
elif unit==UNIT_MB:
num=b/1024/1024
elif unit==UNIT_GB:
num=b/1024/1024/1024
if unit==UNIT_TB:
num=b/1024/1024/1024/1024
#end 単位
if not unit==UNIT_B:
ret="%.2f" % num
else:
ret=""+str(num)
if appendUnit: ret+=UNIT_STR[unit]
return ret
def DetermineSizeUnit(b):
"""バイト数を受け取って、それをサイズ単位に変換する際に最適な単位を、UNIT_* 定数で返す。"""
global UNIT_B, UNIT_KB, UNIT_MB, UNIT_GB
m=1024;
if b<m: return UNIT_B
m*=1024
if b<m: return UNIT_KB
m*=1024
if b<m: return UNIT_MB
m*=1024;
if b<m: return UNIT_GB;
return UNIT_TB;
def PTime2string(ptime):
"""ptime 形式のオブジェクトを受け取って、人間が読めるタイムスタンプ文字列に変換して返す。"""
return "%04d/%02d/%02d %02d:%02d:%02d" % (ptime.year, ptime.month, ptime.day, ptime.hour, ptime.minute, ptime.second)
#ptimeをwx.DateTimeに変換する
def PTimeToDateTime(pTime):
return wx.DateTime(pTime.day,pTime.month,pTime.year,pTime.hour,pTime.minute,pTime.second,pTime.msec)
def attrib2dward(readonly=False, hidden=False, system=False, archive=False):
ret=0
if readonly is True: ret=ret|win32file.FILE_ATTRIBUTE_READONLY
if hidden is True: ret=ret|win32file.FILE_ATTRIBUTE_HIDDEN
if system is True: ret=ret|win32file.FILE_ATTRIBUTE_SYSTEM
if archive is True: ret=ret|win32file.FILE_ATTRIBUTE_ARCHIVE
if ret==0: ret=win32file.FILE_ATTRIBUTE_NORMAL
return ret
def getDiscDriveTypes():
ptr=falconHelper.getDiscDriveTypes()
s=ctypes.c_char_p(ptr).value
falconHelper.releasePtr(ptr)
s2=s.decode('utf-8').split("\n")
ret={}
for elem in s2:
if elem=="": continue
tmp=elem.split(",")
ret[tmp[0].rstrip(":\\")]=(tmp[1],tmp[2])
#end for
return ret
#end getDiscDriveTypes
def makeStringForFalconHelper(string):
tmp=bytearray(string.encode('UTF-16LE'))
tmp.extend(b'\x00\x00')
return bytes(tmp)
def InitContextMenu(wnd):
falconHelper.initContextMenu(wnd)
def GetContextMenu():
falconHelper.getContextMenu()
def AddCustomContextMenuItem(name,identifier):
falconHelper.addCustomContextMenuItem(makeStringForFalconHelper(name),identifier)
def AddContextMenuItemsFromWindows(path):
path_bytes=makeStringForFalconHelper(path)
ret=falconHelper.addContextMenuItemsFromWindows(path_bytes)
return ret==1
def ShowContextMenu(x,y):
return falconHelper.showContextMenu(x,y)
def ExecContextMenuItem(id):
falconHelper.execContextMenuItem(id)
def DestroyContextMenu():
falconHelper.destroyContextMenu()
def ExtractText(path):
path_bytes=makeStringForFalconHelper(path)
ptr=falconHelper.extractText(path_bytes)
s=ctypes.c_char_p(ptr).value
falconHelper.releasePtr(ptr)
s2=s.decode('UTF-8')
return s2
def disableWindowStyleFlag(hwnd,flag):
"""指定されたウィンドウハンドルの DWL_STYLE の値を撮って、指定されたフラグを折る。"""
value=win32api.GetWindowLong(hwnd,-16)#-16 が DWL_STYLE らしい
tmp=0xffffffff-flag
value=value&tmp
win32api.SetWindowLong(hwnd,-16,value)
def IteratePaths(path,append_eol=False):
"""FindFirstFile 系を使って、パス名をイテレートする。append_eol=True にすると、最後に "eol" という1行をリストに入れるので、検索の終了判定などに使える。"""
for elem in win32file.FindFilesIterator(path+"\\*"):
if elem[8]=="." or elem[8]=="..": continue
if elem[0]&win32file.FILE_ATTRIBUTE_DIRECTORY:
yield from IteratePaths(os.path.join(path,elem[8]))
#end ディレクトリ
yield os.path.join(path,elem[8])
#EOL挿入
if append_eol: yield "eol"
def GetDirectorySize(path):
"""ディレクトリのサイズをバイト数で返す。"""
total = 0
try:
with os.scandir(path) as it:
for entry in it:
if entry.is_file():
total += entry.stat().st_size
elif entry.is_dir():
s=GetDirectorySize(entry.path)
if s==-1: return -1
total+=s
except OSError as er:
log.error("GetDirectorySize failed (%s" % er)
return -1
#end except
return total
def GetExecutableState(path):
"""指定されたファイルパスが、実行可能ファイルであろうかどうかを調べて boolean で返す。"""
return os.path.splitext(path)[1].upper() in os.environ["pathext"].split(";")
def calcHash(path,algo):
h = hashlib.new(algo)
len = hashlib.new(algo).block_size * 0x800
with open(path,'rb') as f:
BinaryData = f.read(len)
while BinaryData:
h.update(BinaryData)
BinaryData = f.read(len)
return h.hexdigest()
#環境変数PATHに値を追加
def addPath(paths):
try:
h=winreg.CreateKeyEx(winreg.HKEY_CURRENT_USER,"Environment",access=winreg.KEY_WRITE | winreg.KEY_READ)
for path in paths:
winreg.SetValueEx(h, 'Path', 0, winreg.REG_SZ,winreg.QueryValueEx(h, 'Path')[0]+";"+path)
winreg.CloseKey(h)
return True
except:
return False
def RunFile(path, admin=False,prm="", workdir=""):
"""ファイルを起動する。admin=True の場合、管理者として実行する。"""
path=os.path.expandvars(path)
msg="running %s as admin" % (path) if admin else "running %s" % (path)
log.debug(msg)
msg=_("管理者で起動") if admin else _("起動")
globalVars.app.say(msg)
error=""
if admin:
try:
executable=GetExecutableState(path)
p=path if executable else "cmd"
a=prm if executable else "/c "+path+" "+prm
ret=shell.ShellExecuteEx(shellcon.SEE_MASK_NOCLOSEPROCESS,0,"runas",p,a)
except pywintypes.error as e:
error=str(e)
#end shellExecuteEx failure
else:
try:
win32api.ShellExecute(0,"open",path,prm,workdir,1)
except win32api.error as er:
error=str(er)
#end shellExecute failure
#end admin or not
if error!="":
dialog(_("エラー"),_("ファイルを開くことができませんでした(%(error)s)") % {"error": error})
#end ファイル開けなかった
#end RunFile
def ValidateRegularExpression(exp):
try:
test=re.compile(exp)
except Exception as e:
return str(e)
#end error
return "OK"
def CommandLineToArgv(cmd):
nargs=ctypes.c_int()
ctypes.windll.shell32.CommandLineToArgvW.restype=ctypes.POINTER(ctypes.c_wchar_p)
lpargs=ctypes.windll.shell32.CommandLineToArgvW(cmd,ctypes.byref(nargs))
args = [lpargs[i] for i in range(nargs.value)]
if ctypes.windll.kernel32.LocalFree(lpargs):
raise AssertionError
#end error
return args
#渡された拡張子がドキュメント形式であればTrue
def isDocumentExt(ext):
return ext in constants.SUPPORTED_DOCUMENT_FORMATS | globalVars.app.documentFormats