Fix inport errors and maintain compatibility with python 3.5x)

This commit is contained in:
Ellwood Zwovic
2017-07-22 20:14:09 -07:00
parent 6752108930
commit 06a05c81b2
4 changed files with 38 additions and 27 deletions

View File

@@ -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()