-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
78 lines (56 loc) · 1.95 KB
/
utils.py
File metadata and controls
78 lines (56 loc) · 1.95 KB
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
import os
import sys
from PyQt5.QtCore import QByteArray, QBuffer, QIODevice, QFile
from PyQt5.QtGui import QColor, QPixmap
from PyQt5.QtWidgets import QWidget, QErrorMessage
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
bundle_dir = getattr(sys, '_MEIPASS', os.path.abspath('.'))
return os.path.abspath(os.path.join(bundle_dir, relative_path))
def show_error_dialog(parent: QWidget, message: str):
em = QErrorMessage(parent)
em.adjustSize()
em.showMessage(message)
def format_file_size(size):
# 2**10 = 1024
power = 2 ** 10
n = 0
power_labels = {0: '', 1: 'K', 2: 'M', 3: 'G', 4: 'T'}
while size > power:
size /= power
n += 1
return str(int(size)) + power_labels[n]+'B'
def get_file_size(path):
return os.stat(path).st_size
def copyPixmap(src):
return src.copy(0, 0, src.width(), src.height())
def generateDrawingPixmap(source):
p = QPixmap(source.size())
p.fill(QColor(255, 255, 255, alpha=0))
return p
def generateTestPixmap(source):
p = QPixmap(source.size())
p.fill(QColor(255, 0, 0))
return p
def writePixmap(pixmap, path, format):
f = QFile(path)
f.open(QFile.WriteOnly)
pixmap.save(f, format)
def qtPixmapToJPG(pixmap):
arr = QByteArray()
b = QBuffer(arr)
b.open(QIODevice.WriteOnly)
if pixmap.save(b, "JPG"):
return arr.data()
return None
def get_download_path():
"""Returns the default downloads path for linux or windows"""
if os.name == 'nt':
import winreg
sub_key = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
downloads_guid = '{374DE290-123F-4565-9164-39C4925E467B}'
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, sub_key) as key:
location = winreg.QueryValueEx(key, downloads_guid)[0]
return location
else:
return os.path.join(os.path.expanduser('~'), 'downloads')