working logic+working watching files+added calibration feature+instructions
This commit is contained in:
parent
09d181eba8
commit
97ed8217bf
25 changed files with 1743 additions and 192 deletions
|
|
@ -35,6 +35,22 @@ class FileService:
|
|||
return new_path
|
||||
counter += 1
|
||||
|
||||
@classmethod
|
||||
def generate_new_filename(cls, object_name: str, timestamp: datetime, original_suffix: str) -> str:
|
||||
"""
|
||||
Генерирует новое имя файла в формате:
|
||||
ИмяОбъекта_ГГГГ-ММ-ДД_ЧЧ-ММ-СС.расширение
|
||||
"""
|
||||
# Очищаем имя объекта от недопустимых символов
|
||||
safe_object_name = "".join(c for c in object_name if c.isalnum() or c in (' ', '-', '_')).strip()
|
||||
safe_object_name = safe_object_name.replace(' ', '_')
|
||||
|
||||
# Форматируем дату и время
|
||||
date_str = timestamp.strftime("%Y-%m-%d")
|
||||
time_str = timestamp.strftime("%H-%M-%S")
|
||||
|
||||
return f"{safe_object_name}_{date_str}_{time_str}{original_suffix}"
|
||||
|
||||
@classmethod
|
||||
def write_object_log(cls, folder: Path, filename: str, camera: str, optics: str,
|
||||
timestamp: Optional[datetime] = None) -> None:
|
||||
|
|
@ -52,23 +68,45 @@ class FileService:
|
|||
print(f"Ошибка записи лога: {e}")
|
||||
|
||||
@classmethod
|
||||
def move_file(cls, source: Path, target_folder: Path, camera: str, optics: str) -> bool:
|
||||
"""Перемещает файл в целевую папку"""
|
||||
def move_file(cls, source: Path, target_folder: Path, camera: str, optics: str,
|
||||
object_name: str = None) -> bool:
|
||||
"""
|
||||
Перемещает файл в целевую папку с переименованием
|
||||
Если object_name указан, файл переименовывается в формат: ИмяОбъекта_дата_время.расширение
|
||||
"""
|
||||
if not source.exists():
|
||||
print(f"Файл не существует: {source}")
|
||||
return False
|
||||
|
||||
if not cls.is_photo(source):
|
||||
print(f"Неподдерживаемый формат: {source}")
|
||||
return False
|
||||
|
||||
try:
|
||||
target_folder.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Получаем время создания файла
|
||||
creation_time = datetime.fromtimestamp(source.stat().st_ctime)
|
||||
cls.write_object_log(target_folder, source.name, camera, optics, creation_time)
|
||||
target_path = cls.resolve_conflict(target_folder / source.name)
|
||||
|
||||
# Генерируем новое имя файла, если указано имя объекта
|
||||
if object_name:
|
||||
new_filename = cls.generate_new_filename(object_name, creation_time, source.suffix)
|
||||
else:
|
||||
new_filename = source.name
|
||||
|
||||
# Записываем в лог (сохраняем оригинальное имя в логе для отслеживания)
|
||||
cls.write_object_log(target_folder, f"{source.name} -> {new_filename}", camera, optics, creation_time)
|
||||
|
||||
# Формируем целевой путь
|
||||
target_path = cls.resolve_conflict(target_folder / new_filename)
|
||||
|
||||
# Перемещаем файл
|
||||
shutil.move(str(source), str(target_path))
|
||||
print(f"Файл перемещён и переименован: {source.name} -> {target_path.name}")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"Ошибка перемещения {source.name}: {e}")
|
||||
print(f"Ошибка перемещения файла {source.name}: {e}")
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue