Move two functions in the tools.py file

This commit is contained in:
Huluti 2019-02-23 20:48:49 +01:00
parent b829b6ac5f
commit c040b63595
2 changed files with 43 additions and 41 deletions

View file

@ -1,9 +1,46 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys
import errno
from subprocess import call, PIPE
def human_readable_size(num, suffix='B'):
for unit in ['','K','M','G','T','P','E','Z']: def check_dependencies():
"""Check if the required command line apps exist."""
exe = ".exe" if (sys.platform == "win32") else ""
status = True
dependencies = {
"jpegoptim": "--version",
"optipng": "-v",
"advpng": "--version",
"pngcrush": "-version"
}
for elt in dependencies:
retcode = safe_call(elt + exe + " " + dependencies[elt])
if retcode != 0:
status = False
print("[error] please install {}".format(elt), file=sys.stderr)
return status
def safe_call(command):
"""Cross-platform command-line check."""
while True:
try:
return call(command, shell=True, stdout=PIPE)
except OSError as e:
if e.errno == errno.EINTR:
continue
else:
raise
def human_readable_size(num, suffix="B"):
"""Bytes to a readable size format"""
for unit in ["", "K", "M", "G", "T", "P", "E", "Z"]:
if abs(num) < 1024.0: if abs(num) < 1024.0:
return "%3.1f%s%s" % (num, unit, suffix) return "%3.1f%s%s" % (num, unit, suffix)
num /= 1024.0 num /= 1024.0
return "%.1f%s%s" % (num, 'Y', suffix) return "%.1f%s%s" % (num, "Y", suffix)

View file

@ -2,10 +2,9 @@
import time import time
import sys import sys
import errno
from os import listdir, path, remove, access, W_OK from os import listdir, path, remove, access, W_OK
from shutil import copy from shutil import copy
from subprocess import call, PIPE
from optparse import OptionParser from optparse import OptionParser
from multiprocessing import cpu_count from multiprocessing import cpu_count
from queue import Queue from queue import Queue
@ -15,8 +14,8 @@ from PyQt5.QtGui import *
from PyQt5.QtWidgets import * from PyQt5.QtWidgets import *
from ThreadPool import ThreadPool from ThreadPool import ThreadPool
from tools import human_readable_size
from ui import Ui_trimage from ui import Ui_trimage
from tools import *
VERSION = "1.0.5" VERSION = "1.0.5"
@ -41,7 +40,7 @@ class StartQt(QMainWindow):
self.restoreGeometry(self.settings.value("geometry")) self.restoreGeometry(self.settings.value("geometry"))
# check if dependencies are installed # check if dependencies are installed
if not self.check_dependencies(): if not check_dependencies():
quit() quit()
# add quit shortcut # add quit shortcut
@ -240,46 +239,12 @@ class StartQt(QMainWindow):
# enable recompress button # enable recompress button
self.enable_recompress() self.enable_recompress()
"""
Helper functions
"""
def enable_recompress(self): def enable_recompress(self):
"""Enable the recompress button.""" """Enable the recompress button."""
self.ui.recompress.setEnabled(True) self.ui.recompress.setEnabled(True)
if QSystemTrayIcon.isSystemTrayAvailable() and not self.cli: if QSystemTrayIcon.isSystemTrayAvailable() and not self.cli:
self.systemtray.recompress.setEnabled(True) self.systemtray.recompress.setEnabled(True)
def check_dependencies(self):
"""Check if the required command line apps exist."""
exe = ".exe" if (sys.platform == "win32") else ""
status = True
dependencies = {
"jpegoptim": "--version",
"optipng": "-v",
"advpng": "--version",
"pngcrush": "-version"
}
for elt in dependencies:
retcode = self.safe_call(elt + exe + " " + dependencies[elt])
if retcode != 0:
status = False
print("[error] please install {}".format(elt), file=sys.stderr)
return status
def safe_call(self, command):
"""Cross-platform command-line check."""
while True:
try:
return call(command, shell=True, stdout=PIPE)
except OSError as e:
if e.errno == errno.EINTR:
continue
else:
raise
def hide_main_window(self): def hide_main_window(self):
if self.isVisible(): if self.isVisible():
self.hide() self.hide()