move_files.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import csv
  2. import os
  3. import shutil
  4. from dataclasses import dataclass
  5. from pathlib import Path
  6. @dataclass(frozen=True)
  7. class MoveFilesResult:
  8. moved_count: int
  9. skipped_count: int
  10. missing_client_count: int
  11. @dataclass(frozen=True)
  12. class RefreshAndMoveResult:
  13. xml_count: int
  14. file_count: int
  15. moved_count: int
  16. skipped_count: int
  17. missing_client_count: int
  18. def default_desktop() -> Path:
  19. user_profile = Path(os.environ.get("USERPROFILE", str(Path.home())))
  20. return user_profile / "Desktop"
  21. def read_csv(csv_path: Path) -> list[dict[str, str]]:
  22. if not csv_path.is_file():
  23. raise ValueError(f"Die CSV-Datei existiert nicht: {csv_path}")
  24. with csv_path.open("r", newline="", encoding="latin-1") as csv_file:
  25. return list(csv.DictReader(csv_file, delimiter=";"))
  26. def move_files(
  27. files_csv: Path,
  28. clients_csv: Path,
  29. desktop_directory: Path | None = None,
  30. ) -> MoveFilesResult:
  31. desktop_directory = desktop_directory or default_desktop()
  32. client_rows = read_csv(clients_csv)
  33. client_names = {
  34. row.get("ParticipantName", "").strip(): row.get("Kunde", "").strip()
  35. for row in client_rows
  36. if row.get("ParticipantName", "").strip() and row.get("Kunde", "").strip()
  37. }
  38. moved_count = 0
  39. skipped_count = 0
  40. missing_client_count = 0
  41. for row in read_csv(files_csv):
  42. participant_name = row.get("ParticipantName", "").strip()
  43. source = Path(row.get("FilePath", "").strip())
  44. customer = client_names.get(participant_name)
  45. if not customer:
  46. missing_client_count += 1
  47. continue
  48. if not source.is_file():
  49. skipped_count += 1
  50. continue
  51. target_directory = desktop_directory / "PC-Visit" / customer
  52. target_directory.mkdir(parents=True, exist_ok=True)
  53. target = target_directory / source.name
  54. if source.resolve() == target.resolve() or target.exists():
  55. skipped_count += 1
  56. continue
  57. shutil.move(str(source), str(target))
  58. print(f"Datei verschoben: {source} -> {target}")
  59. moved_count += 1
  60. return MoveFilesResult(
  61. moved_count=moved_count,
  62. skipped_count=skipped_count,
  63. missing_client_count=missing_client_count,
  64. )
  65. def refresh_and_move(
  66. directory: Path,
  67. files_csv: Path,
  68. clients_csv: Path,
  69. desktop_directory: Path | None = None,
  70. ignore_timestamp: bool = False,
  71. ) -> RefreshAndMoveResult:
  72. from files import run as run_files
  73. files_result = run_files(directory, files_csv, ignore_timestamp=ignore_timestamp)
  74. move_result = move_files(files_csv, clients_csv, desktop_directory)
  75. return RefreshAndMoveResult(
  76. xml_count=files_result.xml_count,
  77. file_count=files_result.row_count,
  78. moved_count=move_result.moved_count,
  79. skipped_count=move_result.skipped_count,
  80. missing_client_count=move_result.missing_client_count,
  81. )