Skip to content

Commit

Permalink
Again: Refactor & simplify to address quality complains
Browse files Browse the repository at this point in the history
  • Loading branch information
juhoinkinen committed Apr 12, 2024
1 parent d14ff30 commit cc0c989
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions annif/cli_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,18 @@ def _unzip_member(
) -> None:
dest_path = os.path.join(datadir, member.filename)
if os.path.exists(dest_path) and not force:
if _are_identical_member_and_file(member, dest_path):
logger.debug(f"Skipping unzip to {dest_path}; already in place")
else:
click.echo(f"Not overwriting {dest_path} (use --force to override)")
_handle_existing_file(member, dest_path)
return
logger.debug(f"Unzipping to {dest_path}")
zfile.extract(member, path=datadir)
_restore_timestamps(member, dest_path)


def _handle_existing_file(member: zipfile.ZipInfo, dest_path: str) -> None:
if _are_identical_member_and_file(member, dest_path):
logger.debug(f"Skipping unzip to {dest_path}; already in place")
else:
logger.debug(f"Unzipping to {dest_path}")
zfile.extract(member, path=datadir)
_restore_timestamps(member, dest_path)
click.echo(f"Not overwriting {dest_path} (use --force to override)")


def _are_identical_member_and_file(member: zipfile.ZipInfo, dest_path: str) -> bool:
Expand All @@ -415,8 +419,7 @@ def _restore_timestamps(member: zipfile.ZipInfo, dest_path: str) -> None:
def copy_project_config(src_path: str, force: bool) -> None:
"""Copy a given project configuration file to projects.d/ directory."""
project_configs_dest_dir = "projects.d"
if not os.path.isdir(project_configs_dest_dir):
os.mkdir(project_configs_dest_dir)
os.makedirs(project_configs_dest_dir, exist_ok=True)

dest_path = os.path.join(project_configs_dest_dir, os.path.basename(src_path))
if os.path.exists(dest_path) and not force:
Expand Down

0 comments on commit cc0c989

Please sign in to comment.