From 06a05c81b2553af4a72aa19fe98921918ec00084 Mon Sep 17 00:00:00 2001 From: Ellwood Zwovic Date: Sat, 22 Jul 2017 20:14:09 -0700 Subject: [PATCH] Fix inport errors and maintain compatibility with python 3.5x) --- package_manager/__init__.py | 8 ++++---- package_manager/bpkg/__init__.py | 23 +++++++++++++++-------- package_manager/bpkg/utils.py | 26 +++++++++++++++----------- package_manager/subproc.py | 8 ++++---- 4 files changed, 38 insertions(+), 27 deletions(-) diff --git a/package_manager/__init__.py b/package_manager/__init__.py index ff5d1fd..5563a6c 100644 --- a/package_manager/__init__.py +++ b/package_manager/__init__.py @@ -250,7 +250,7 @@ class PACKAGE_OT_install(SubprocMixin, bpy.types.Operator): def _subproc_success(self, success: subproc.Success): self.report({'INFO'}, 'Package installed successfully') - getattr(bpy.ops, __package__).refresh_packages() + bpy.ops.package.refresh_packages() self.quit() def _subproc_aborted(self, aborted: subproc.Aborted): @@ -311,7 +311,7 @@ class PACKAGE_OT_uninstall(SubprocMixin, bpy.types.Operator): def _subproc_success(self, success: subproc.Success): self.report({'INFO'}, 'Package uninstalled successfully') - getattr(bpy.ops, __package__).refresh_packages() + bpy.ops.package.refresh_packages() self.quit() def report_process_died(self): @@ -446,7 +446,7 @@ class PACKAGE_OT_refresh(bpy.types.Operator): bl_description = "Check for new and updated packages" def execute(self, context): - getattr(bpy.ops, __package__).refresh_repositories() + bpy.ops.package.refresh_repositories() # getattr(bpy.ops, __package__).refresh_packages() return {'FINISHED'} @@ -791,7 +791,7 @@ class WM_OT_package_toggle_expand(bpy.types.Operator): class PackageManagerPreferences(bpy.types.AddonPreferences): - bl_idname = __package__ + bl_idname = 'package' package_url = bpy.props.StringProperty( name='Package URL', diff --git a/package_manager/bpkg/__init__.py b/package_manager/bpkg/__init__.py index c784b63..fdd9b0e 100644 --- a/package_manager/bpkg/__init__.py +++ b/package_manager/bpkg/__init__.py @@ -1,11 +1,18 @@ -from .exceptions import ( - BpkgException, - InstallException, - DownloadException, - BadRepository, - ) -from .types import ( +import logging + +log = logging.getLogger(__name__) + +if 'bpy' in locals(): + import importlib + + log.debug("Reloading %s", __name__) + exceptions = importlib.reload(exceptions) + Package = importlib.reload(types.Package) + Repository = importlib.reload(types.Repository) + +else: + from . import exceptions + from .types import ( Package, Repository, ) - diff --git a/package_manager/bpkg/utils.py b/package_manager/bpkg/utils.py index 3a34cbd..e5481d8 100644 --- a/package_manager/bpkg/utils.py +++ b/package_manager/bpkg/utils.py @@ -1,10 +1,20 @@ +from pathlib import Path +import shutil +import logging + +def rm(path: Path): + """Delete whatever is specified by `path`""" + if path.is_dir(): + shutil.rmtree(str(path)) + else: + path.unlink() class InplaceBackup: """Utility class for moving a file out of the way by appending a '~'""" log = logging.getLogger('%s.inplace-backup' % __name__) - def __init__(self, path: pathlib.Path): + def __init__(self, path: Path): self.path = path self.backup() @@ -13,10 +23,10 @@ class InplaceBackup: if not self.path.exists(): raise FileNotFoundError("Can't backup path which doesn't exist") - self.backup_path = pathlib.Path(str(self.path) + '~') + self.backup_path = Path(str(self.path) + '~') if self.backup_path.exists(): self.log.warning("Overwriting existing backup '{}'".format(self.backup_path)) - self._rm(self.backup_path) + rm(self.backup_path) shutil.move(str(self.path), str(self.backup_path)) @@ -32,17 +42,11 @@ class InplaceBackup: if self.path.exists(): self.log.warning("Overwriting '{0}' with backup file".format(self.path)) - self._rm(self.path) + rm(self.path) shutil.move(str(self.backup_path), str(self.path)) def remove(self): """Remove 'path~'""" - self._rm(self.backup_path) + rm(self.backup_path) - def _rm(self, path: pathlib.Path): - """Just delete whatever is specified by `path`""" - if path.is_dir(): - shutil.rmtree(str(path)) - else: - path.unlink() diff --git a/package_manager/subproc.py b/package_manager/subproc.py index 01b9071..c817503 100644 --- a/package_manager/subproc.py +++ b/package_manager/subproc.py @@ -6,8 +6,8 @@ import logging import pathlib import shutil import json +from .bpkg import utils from .bpkg import Package, Repository - from .messages import * from .bpkg.exceptions import * @@ -114,7 +114,7 @@ def _install(pipe_to_blender, pkgpath: pathlib.Path, dest: pathlib.Path, searchp # The following is adapted from `addon_install` in bl_operators/wm.py # check to see if the file is in compressed format (.zip) - if zipfile.is_zipfile(pkgpath): + if zipfile.is_zipfile(str(pkgpath)): log.debug("Package is zipfile") try: file_to_extract = zipfile.ZipFile(str(pkgpath), 'r') @@ -135,7 +135,7 @@ def _install(pipe_to_blender, pkgpath: pathlib.Path, dest: pathlib.Path, searchp backups = [] for conflict in conflicts: log.debug("Creating backup of conflict %s", conflict) - backups.append(InplaceBackup(conflict)) + backups.append(utils.InplaceBackup(conflict)) try: file_to_extract.extractall(str(dest)) @@ -152,7 +152,7 @@ def _install(pipe_to_blender, pkgpath: pathlib.Path, dest: pathlib.Path, searchp dest_file = (dest / pkgpath.name) if dest_file.exists(): - backup = InplaceBackup(dest_file) + backup = utils.InplaceBackup(dest_file) try: shutil.copyfile(str(pkgpath), str(dest_file))