Skip to content

Commit

Permalink
fixes #306 & #358
Browse files Browse the repository at this point in the history
  • Loading branch information
bilelmoussaoui committed May 23, 2017
1 parent 57e2da9 commit bf50dfd
Show file tree
Hide file tree
Showing 15 changed files with 384 additions and 278 deletions.
15 changes: 10 additions & 5 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from json import load
from os import path
from gi.repository import Gio

from time import time
from .const import DESKTOP_ENV, CONFIG_FILE, DB_FOLDER
from .enum import Action, ConversionTools
from .utils import progress, get_scaling_factor, replace_to_6hex
Expand Down Expand Up @@ -104,26 +104,30 @@ def execute(action):
"""
apps = App.get_supported_apps()
done = []
total_time = 0
if len(apps) != 0:
cnt = 0
counter_total = sum(app.parser.total_icons for app in apps)
for i, app in enumerate(apps):
app_name = app.name
start_time = time()
if action == Action.APPLY:
app.install()
elif action == Action.REVERT:
app.reinstall()
elif action == Action.CLEAR_CACHE:
app.clear_cache()
delta = time() - start_time
total_time += delta
if app.is_done:
cnt += app.parser.total_icons
if app_name not in done:
progress(cnt, counter_total, app_name)
progress(cnt, counter_total, delta, app_name)
done.append(app_name)
else:
counter_total -= app.data.supported_icons_cnt
if i == len(apps) - 1:
progress(cnt, counter_total)
counter_total -= app.parser.total_icons
print("Failed to fix {0}".format(app_name))
print("Took {0}s to finish the tasks".format(round(total_time, 2)))
else:
if action == Action.APPLY:
exit("No apps to fix! Please report on GitHub if this is not the case")
Expand All @@ -148,6 +152,7 @@ def config():
@staticmethod
def svg():
if App._svgtopng is None:
conversion_tool = None
if App.args().conversion_tool:
conversion_tool = App.args().conversion_tool
elif App.config().get("conversion-tool"):
Expand Down
22 changes: 11 additions & 11 deletions src/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
You should have received a copy of the GNU General Public License
along with Hardcode-Tray. If not, see <http://www.gnu.org/licenses/>.
"""
from .utils import symlink_file, show_select_backup, create_backup_dir
from .utils import symlink_file


def symlinks_installer(func):
Expand All @@ -42,22 +42,22 @@ def wrapper(application, icon, icon_path):

def install_wrapper(func):
def wrapper(app):
app.backup_dir = create_backup_dir(app.name)
app.backup.create_backup_dir()
app.install_symlinks()
func(app)
return wrapper


def revert_wrapper(func):
def wrapper(application):
if application.BACKUP_IGNORE:
application.remove_symlinks()
func(application)
def wrapper(app):
if app.BACKUP_IGNORE:
app.remove_symlinks()
func(app)
else:
application.selected_backup = show_select_backup(application.name)
if application.selected_backup:
application.remove_symlinks()
func(application)
app.backup.select()
if app.backup.selected_backup:
app.remove_symlinks()
func(app)
else:
application.is_done = False
app.is_done = False
return wrapper
78 changes: 31 additions & 47 deletions src/modules/applications/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
You should have received a copy of the GNU General Public License
along with Hardcode-Tray. If not, see <http://www.gnu.org/licenses/>.
"""
from concurrent.futures import ThreadPoolExecutor as Executor
from os import path
from shutil import rmtree
from src.const import BACKUP_FOLDER
from src.utils import backup, revert, symlink_file, mchown
from src.enum import Action
from src.utils import symlink_file, mchown
from src.decorators import symlinks_installer, revert_wrapper, install_wrapper

from src.modules.backup import Backup

class Application:
"""Application class."""
Expand All @@ -43,6 +45,7 @@ def __init__(self, parser):
self.parser = parser
self._selected_backup = None
self._back_dir = None
self.backup = Backup(self)

@property
def name(self):
Expand All @@ -59,32 +62,16 @@ def app_path(self):
"""Return the application installation paths."""
return self.parser.app_path

@property
def backup_dir(self):
"""Return the backup directory of the current application."""
return self._back_dir

@backup_dir.setter
def backup_dir(self, backup_dir):
self._back_dir = backup_dir

@property
def selected_backup(self):
"""Return the selected backup directory during the revert process."""
return self._selected_backup

@selected_backup.setter
def selected_backup(self, selected_backup):
if path.exists(selected_backup):
self._selected_backup = selected_backup
else:
raise FileNotFoundError

@property
def icons_path(self):
"""Return the application installation paths."""
return self.parser.icons_path

@property
def backup_ignore(self):
"""Return either the backup files should be created or not."""
return self.parser.backup_ignore

@property
def symlinks(self):
"""Return application symlinks."""
Expand All @@ -102,7 +89,7 @@ def install_symlinks(self):
for directory in self.app_path:
root = symlinks[syml]["root"]
dest = directory.append(symlinks[syml]["dest"])
backup(self.backup_dir, dest)
self.backup.create(dest)
symlink_file(root, dest)

def remove_symlinks(self):
Expand All @@ -111,8 +98,7 @@ def remove_symlinks(self):
symlinks = self.symlinks
for syml in symlinks:
for directory in self.app_path:
revert(self.name, self.selected_backup,
directory.append(symlinks[syml]["dest"]))
self.backup.remove(directory.append(symlinks[syml]["dest"]))

def clear_cache(self):
"""Clear Backup cache."""
Expand All @@ -122,35 +108,25 @@ def clear_cache(self):
return True
return False

def get_output_icons(self):
"""Return a list of output icons."""
icons = []
for icon in self.icons:
for icon_path in self.icons_path:
output_icon = icon_path.append(icon.original)
icons.append({
"output_icon": output_icon,
"data": icon,
"path": icon_path
})
return icons
def execute(self, action):
"""Execute actions: Apply/Revert."""
for icon_path in self.icons_path:
with Executor(max_workers=4) as exe:
for icon in self.icons:
if action == Action.APPLY:
exe.submit(self.install_icon, icon, icon_path)
elif action == Action.REVERT:
exe.submit(self.revert_icon, icon, icon_path)

@install_wrapper
def install(self):
"""Install the application icons."""
for icon in self.get_output_icons():
if not self.parser.backup_ignore:
backup(self.backup_dir, icon["output_icon"])
self.install_icon(icon["data"], icon["path"])
self.execute(Action.APPLY)

@revert_wrapper
def reinstall(self):
"""Reinstall the application icons and remove symlinks."""
for icon in self.get_output_icons():
if not self.parser.backup_ignore:
revert(self.name,
self.selected_backup,
icon["output_icon"])
self.execute(Action.REVERT)

@symlinks_installer
def install_icon(self, icon, icon_path):
Expand All @@ -160,9 +136,17 @@ def install_icon(self, icon, icon_path):
ext_theme = icon.theme_ext
icon_size = icon.icon_size
output_icon = icon_path.append(icon.original)
if not self.backup_ignore:
self.backup.create(output_icon)
if ext_theme == ext_orig:
symlink_file(theme_icon, output_icon)
elif ext_theme == "svg" and ext_orig == "png":
from src.app import App
App.svg().to_png(theme_icon, output_icon, icon_size)
mchown(output_icon)

def revert_icon(self, icon, icon_path):
"""Revert to the original icon."""
output_icon = icon_path.append(icon.original)
if not self.backup_ignore:
self.backup.remove(output_icon)
40 changes: 18 additions & 22 deletions src/modules/applications/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,38 @@
You should have received a copy of the GNU General Public License
along with Hardcode-Tray. If not, see <http://www.gnu.org/licenses/>.
"""
from src.decorators import revert_wrapper, install_wrapper
from src.utils import backup, revert
from src.enum import Action
from .application import Application


class BinaryApplication(Application):
"""Pak Application class, based on data_pak file."""

def __init__(self, parser):
"""Init method."""
Application.__init__(self, parser)

def backup_binary(self, icon_path):
"""Backup binary file before modification."""
backup(self.backup_dir, icon_path.append(self.binary))

def revert_binary(self, icon_path):
"""Restore the backed up binary file."""
revert(self.name, self.selected_backup,
icon_path + self.binary)
self.is_corrupted = False

@property
def binary(self):
"""Return the binary file if exists."""
return self.parser.binary

@revert_wrapper
def reinstall(self):
"""Reinstall the old icons."""
for icon_path in self.icons_path:
self.revert_binary(icon_path)
def get_backup_file(self, icon_name):
"""Return the binary content of a backup file."""
backup_file = self.backup.get_backup_file(icon_name)
if backup_file:
with open(backup_file, 'rb') as binary_obj:
pngbytes = binary_obj.read()
return pngbytes
return None

@install_wrapper
def install(self):
"""Install the application icons."""
def execute(self, action):
for icon_path in self.icons_path:
self.backup_binary(icon_path)
for icon in self.icons:
self.install_icon(icon, icon_path)
if self.is_corrupted:
break
if action == Action.APPLY:
self.install_icon(icon, icon_path)
elif action == Action.REVERT:
self.revert_icon(icon, icon_path)
self.is_done = not self.is_corrupted
Loading

0 comments on commit bf50dfd

Please sign in to comment.