vnrecode: pathlib for all paths
This commit is contained in:
parent
4e6fd332c5
commit
1c1e8a9292
5 changed files with 108 additions and 104 deletions
|
@ -1,12 +1,16 @@
|
|||
from shutil import copyfile
|
||||
from pathlib import Path
|
||||
import hashlib
|
||||
import sys
|
||||
import os
|
||||
|
||||
from vnrecode.printer import Printer
|
||||
from vnrecode.params import Params
|
||||
|
||||
|
||||
class Utils:
|
||||
|
||||
def __init__(self, params_inst, printer_inst):
|
||||
def __init__(self, params_inst: Params, printer_inst: Printer):
|
||||
self.__errors = 0
|
||||
self.__params = params_inst
|
||||
self.__printer = printer_inst
|
||||
|
@ -18,12 +22,13 @@ class Utils:
|
|||
os.system("pause")
|
||||
|
||||
@staticmethod
|
||||
def get_size(directory: str) -> int:
|
||||
def get_size(directory: Path) -> int:
|
||||
total_size = 0
|
||||
for folder, folders, files in os.walk(directory):
|
||||
for file in files:
|
||||
if not os.path.islink(os.path.join(folder, file)):
|
||||
total_size += os.path.getsize(os.path.join(folder, file))
|
||||
path = Path(folder, file)
|
||||
if not path.is_symlink():
|
||||
total_size += path.stat().st_size
|
||||
return total_size
|
||||
|
||||
@staticmethod
|
||||
|
@ -39,7 +44,7 @@ class Utils:
|
|||
|
||||
for folder, folders, files in os.walk(self.__params.dest):
|
||||
for file in files:
|
||||
if not os.path.splitext(file)[1].count("(vncopy)"):
|
||||
if not file.count("(vncopy)"):
|
||||
output_len += 1
|
||||
|
||||
if self.__errors != 0:
|
||||
|
@ -58,20 +63,20 @@ class Utils:
|
|||
except ZeroDivisionError:
|
||||
self.__printer.warning("Nothing compressed!")
|
||||
|
||||
def catch_unprocessed(self, source, output, error):
|
||||
self.copy_unprocessed(source, error)
|
||||
def catch_unprocessed(self, input_path: Path, output_path: Path, error):
|
||||
self.copy_unprocessed(input_path, output_path)
|
||||
self.__errors += 1
|
||||
if not self.__params.hide_errors:
|
||||
self.__printer.error(f"File {os.path.split(source)[-1]} can't be processed! Error: {error}")
|
||||
self.__printer.error(f"File {input_path.name} can't be processed! Error: {error}")
|
||||
|
||||
def copy_unprocessed(self, source, output):
|
||||
def copy_unprocessed(self, input_path: Path, output_path: Path):
|
||||
if self.__params.copy_unprocessed:
|
||||
copyfile(source, output)
|
||||
self.__printer.info(f"File {os.path.split(source)[-1]} copied to compressed folder.")
|
||||
copyfile(input_path, output_path)
|
||||
self.__printer.info(f"File {input_path.name} copied to compressed folder.")
|
||||
|
||||
def catch_duplicates(self, path: str) -> str:
|
||||
if os.path.exists(path):
|
||||
new_path = os.path.splitext(path)[0] + "(vncopy)" + os.path.splitext(path)[1]
|
||||
def catch_duplicates(self, path: Path) -> Path:
|
||||
if path.exists():
|
||||
new_path = Path(path.stem + "(vncopy)" + path.suffix)
|
||||
self.__duplicates.append(new_path)
|
||||
return new_path
|
||||
return path
|
||||
|
@ -79,13 +84,13 @@ class Utils:
|
|||
def print_duplicates(self):
|
||||
for filename in self.__duplicates:
|
||||
self.__printer.warning(
|
||||
f'Duplicate file has been found! Check manually this files - "{filename}", '
|
||||
f'"{os.path.splitext(filename)[0] + "(vncopy)" + os.path.splitext(filename)[1]}"'
|
||||
f'Duplicate file has been found! Check manually this files - "{filename.name}", '
|
||||
f'"{filename.stem + "(vncopy)" + filename.suffix}"'
|
||||
)
|
||||
|
||||
def out_rename(self, filename: str, target: str):
|
||||
def out_rename(self, out_path: Path, target: str):
|
||||
if not self.__params.mimic_mode:
|
||||
dest_name = self.catch_duplicates(os.path.join(os.path.dirname(filename), target))
|
||||
os.rename(filename, dest_name)
|
||||
dest_name = self.catch_duplicates(Path(out_path.parent, target))
|
||||
os.rename(out_path, dest_name)
|
||||
else:
|
||||
os.rename(filename, os.path.join(os.path.dirname(filename), target))
|
||||
os.rename(out_path, Path(out_path.parent, target))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue