-
Notifications
You must be signed in to change notification settings - Fork 2
/
misc.py
477 lines (399 loc) · 14.9 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# -*- coding: utf-8 -*-
# Falcon miscellaneous helper objects
# Copyright (C) 2019-2020 Yukio Nozawa <[email protected]>
# Copyright (C) 2019-2020 yamahubuki <[email protected]>
# Note: All comments except these top lines will be written in Japanese.
import ctypes
import json
import socket
import pywintypes
import os
import re
import time
import hashlib
import winreg
import win32api
import win32con
import win32file
import win32gui
import constants
import globalVars
import lists
import workerThreadTasks
from logging import getLogger
from simpleDialog import dialog
from win32com.shell import shell, shellcon
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(json.dumps(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):
ext = os.path.splitext(path)[1].lower()[1:]
if ext in constants.TEXT_READABLE_DOCUMENT_FORMATS:
try:
with open(path, 'r', encoding="UTF-8") as f:
data = f.read()
return data
except UnicodeDecodeError:
try:
with open(path, 'r', encoding="cp932") as f:
data = f.read()
return data
except Exception as e:
return ""
except Exception as e:
return ""
else:
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行をリストに入れるので、検索の終了判定などに使える。"""
try:
for elem in win32file.FindFilesIterator(os.path.join(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])
# end iterate
except pywintypes.error as e:
log.error(
"Access denied while searching paths at %s (%s)." %
(path, e))
# end except
# EOL挿入
if append_eol:
yield "eol"
def GetDirectorySize(path, fileCount=0, dirCount=0):
"""ディレクトリのサイズをバイト数で返す。"""
total = 0
try:
with os.scandir(path) as it:
for entry in it:
try:
ret = isLink(entry.path)
except BaseException:
continue
if ret:
print("Synlink skipped:" + entry.path)
log.debug("Synlink skipped:" + entry.path)
continue
elif entry.is_file(follow_symlinks=False):
total += entry.stat().st_size
fileCount += 1
elif entry.is_dir(follow_symlinks=False):
dirCount += 1
r = GetDirectorySize(entry.path, fileCount, dirCount)
if r == (-1, 0 - 1, -1):
continue
total += r[0]
fileCount = r[1]
dirCount = r[2]
except OSError as er:
log.error("GetDirectorySize failed (%s" % er)
return constants.DIR_SIZE_CHECK_FAILED, -1, -1
# end except
return total, fileCount, dirCount
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
try:
with open(path, 'rb') as f:
BinaryData = f.read(len)
while BinaryData:
h.update(BinaryData)
BinaryData = f.read(len)
return h.hexdigest()
except BaseException:
return _("エラー")
# 環境変数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)
# Send WM_SETTINGCHANGE(#430)
win32gui.SendMessageTimeout(0xffff, 0x001A, 0, "Environment", 0x0002, 100)
return True
except BaseException:
return False
def RunFile(path, admin=False, prm="", workdir=""):
"""ファイルを起動する。admin=True の場合、管理者として実行する。"""
path = os.path.expandvars(path)
msg = "running '%s' prm='%s' workdir='%s' asAdmin=%s" % (
path, prm, workdir, str(admin))
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 \"%s\" %s" % (path, prm)
ret = shell.ShellExecuteEx(
shellcon.SEE_MASK_NOCLOSEPROCESS, 0, "runas", p, a, workdir)
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.lower() in constants.SUPPORTED_DOCUMENT_FORMATS | globalVars.app.documentFormats | constants.TEXT_READABLE_DOCUMENT_FORMATS
def ResolveLocalIpAddress(name):
addr = ""
try:
addr = socket.getaddrinfo(name, None)[0][4][0]
except Exception:
pass
# end error
return addr
def GetNetworkResource(target):
# targetに指定した名前のネットワークリソースを取得(例:\\network。存在しない場合や何らかのエラー発生時はNoneを返す。)
try:
resources = workerThreadTasks.GetNetworkResources()
for resource in resources:
if resource.fullpath == target:
return resource
log.info("network resource \"" + target + "\" not found.")
return None
except Exception as e:
return None
def GetRootObject(rootPath):
"""
与えられたパスが示すルートドライブのオブジェクトを返す。
パスの妥当性・パスが示すアイテムの存在は確認しない。
DriveまたはNetworkResourceが返る
"""
elem = None
rootPath = rootPath[0]
if rootPath != "\\": # \でないならそれはドライブ
try:
elem = lists.drive.GetDriveObject(int(ord(rootPath) - 65))
except BaseException:
pass
else: # ネットワークリソース
rootPath = self.listObject.rootDirectory
rootPath = rootPath[0:rootPath[2:].find("\\") + 2]
elem = misc.GetNetworkResource(rootPath)
return elem
def PathParamSplit(input):
"""
pathとparamがつながった1つのStringをPathとParamに分けて返します。
windowsの規則に従い、Pathは" "で囲われているか、半角スペースを含まずに記述されている必要があります。
inputが不正な場合、path,param共にNoneが返ります。
"""
if input[0] == "\"": # " "で囲われた範囲を抽出
end = input.find("\"", 1)
if end == 1 or end == -1:
return None, None
path = input[1:end]
prm = input[end + 1:]
return path, prm
else:
end = input.find(" ", 0)
if end <= 0:
return None, None
path = input[0:end]
prm = input[end + 1:]
return path, prm
def isLink(path):
"""
pathがシンボリックリンクまたはジャンクションならTrue
呼び出す際は権限がない場合等のエラーをキャッチすべき
"""
attrs = win32api.GetFileAttributes(path)
return attrs & win32con.FILE_ATTRIBUTE_REPARSE_POINT != 0
def analyzeUserInputPath(path, parent="C:\\"):
"""
ユーザが入力したパスを最適化
・入力は絶対パスを想定
・環境変数の展開
・ドライブ直下以外では末尾の\\を除去
・相対パスの場合はparentからの絶対パスへ変換
・C:でもCでもC:\\にする
"""
path = os.path.expandvars(path)
if not isinstance(path, str):
return None
if len(path) == 2 and path[0].upper(
) >= 'A' and path[0].upper() <= 'Z' and path[1] == ':':
# アルファベット:はドライブルートと仮定
return path.upper() + "\\"
if path == "":
# 空文字はドライブ一覧を示す
return ""
else:
# それ以外
if os.path.isabs(path):
return os.path.normpath(path)
else:
return os.path.normpath(os.path.join(parent, path))