Add preliminary logging to organizer and tmp_dir

This commit is contained in:
I-Al-Istannen 2020-04-20 14:37:44 +02:00
parent 930d821dd7
commit b2fe7cc064
2 changed files with 22 additions and 1 deletions

View File

@ -3,12 +3,15 @@
A organizer is bound to a single directory. A organizer is bound to a single directory.
""" """
import logging
import shutil import shutil
from pathlib import Path from pathlib import Path
from typing import List, Set from typing import List, Set
from .utils import prompt_yes_no from .utils import prompt_yes_no
logger = logging.getLogger(__name__)
class Organizer(): class Organizer():
"""A helper for managing downloaded files.""" """A helper for managing downloaded files."""
@ -28,13 +31,19 @@ class Organizer():
"""Move a file to this organizer and mark it.""" """Move a file to this organizer and mark it."""
source_absolute = self.path.joinpath(source).absolute() source_absolute = self.path.joinpath(source).absolute()
target_absolute = self.path.joinpath(target).absolute() target_absolute = self.path.joinpath(target).absolute()
logger.debug(f"Copying '{source_absolute}' to '{target_absolute}")
shutil.move(str(source_absolute), str(target_absolute)) shutil.move(str(source_absolute), str(target_absolute))
self.mark_file(target) self.mark_file(target)
# TODO: Name this method :/ track_file? # TODO: Name this method :/ track_file?
def mark_file(self, path: Path) -> None: def mark_file(self, path: Path) -> None:
"""Mark a file as used so it will not get cleaned up.""" """Mark a file as used so it will not get cleaned up."""
self._known_files.add(self.path.joinpath(path).absolute()) absolute_path = self.path.joinpath(path).absolute()
self._known_files.add(absolute_path)
logger.debug(f"Tracked {absolute_path}")
def resolve_file(self, file_path: Path) -> Path: def resolve_file(self, file_path: Path) -> Path:
"""Resolve a file relative to the path of this organizer.""" """Resolve a file relative to the path of this organizer."""
@ -42,6 +51,8 @@ class Organizer():
def cleanup(self) -> None: def cleanup(self) -> None:
"""Remove all untracked files in the organizer's dir.""" """Remove all untracked files in the organizer's dir."""
logger.debug("Deleting all untracked files...")
self._cleanup(self.path) self._cleanup(self.path)
def _cleanup(self, start_dir: Path) -> None: def _cleanup(self, start_dir: Path) -> None:

View File

@ -1,10 +1,13 @@
"""Helper functions and classes for temporary folders.""" """Helper functions and classes for temporary folders."""
import logging
import shutil import shutil
from pathlib import Path from pathlib import Path
from types import TracebackType from types import TracebackType
from typing import Optional, Type from typing import Optional, Type
logger = logging.getLogger(__name__)
class TmpDir(): class TmpDir():
"""A temporary folder that can create files or nested temp folders.""" """A temporary folder that can create files or nested temp folders."""
@ -38,6 +41,9 @@ class TmpDir():
def new_file(self, prefix: Optional[str] = None) -> Path: def new_file(self, prefix: Optional[str] = None) -> Path:
"""Return a unique path inside the folder, but don't create a file.""" """Return a unique path inside the folder, but don't create a file."""
name = f"{prefix if prefix else 'tmp'}-{self._inc_and_get_counter():03}" name = f"{prefix if prefix else 'tmp'}-{self._inc_and_get_counter():03}"
logger.debug(f"Creating temp file '{name}'")
return self.path.joinpath(name) return self.path.joinpath(name)
def new_folder(self, prefix: Optional[str] = None) -> 'TmpDir': def new_folder(self, prefix: Optional[str] = None) -> 'TmpDir':
@ -47,10 +53,14 @@ class TmpDir():
sub_path = self.path.joinpath(name) sub_path = self.path.joinpath(name)
sub_path.mkdir(parents=True) sub_path.mkdir(parents=True)
logger.debug(f"Creating temp dir '{name}' at {sub_path}")
return TmpDir(sub_path) return TmpDir(sub_path)
def cleanup(self) -> None: def cleanup(self) -> None:
"""Delete this folder and all contained files.""" """Delete this folder and all contained files."""
logger.debug(f"Deleting temp folder {self.path}")
shutil.rmtree(self.path.absolute()) shutil.rmtree(self.path.absolute())
def _inc_and_get_counter(self) -> int: def _inc_and_get_counter(self) -> int: