-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
46 lines (33 loc) · 1.45 KB
/
utils.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
import sys
from psutil import process_iter
from ctypes import windll, byref, create_unicode_buffer, create_string_buffer
FR_PRIVATE = 0x10
FR_NOT_ENUM = 0x20
def load_unload_font(font_path, private=True, enumerable=False, load=True):
"""
This function was taken from
https://github.com/ifwe/digsby/blob/master/digsby/src/gui/native/win/winfonts.py
and converted from Python 2.x to 3.x by changing
isinstance checks [to bytes from str] and [to str from unicode]
"""
func = "Add" if load else "Remove"
if isinstance(font_path, bytes):
pathbuf = create_string_buffer(font_path)
FontResourceEx = getattr(windll.gdi32, f"{func}FontResourceExA")
elif isinstance(font_path, str):
pathbuf = create_unicode_buffer(font_path)
FontResourceEx = getattr(windll.gdi32, f"{func}FontResourceExW")
else:
raise TypeError('font path must be a str or unicode')
flags = (FR_PRIVATE if private else 0) | (FR_NOT_ENUM if not enumerable else 0)
return bool(FontResourceEx(byref(pathbuf), flags, 0))
def is_already_running():
if not getattr(sys, 'frozen', False):
return False
singleton, sys_exe = False, sys.executable
for p in process_iter(attrs=['exe']):
if p.info['exe'] == sys_exe and p.parent().exe() != sys_exe:
if p.is_running() and singleton:
return True
singleton = True
return False