Skip to content

Commit

Permalink
Refactoring with ruff, using auto fix and changing List[..] to list[..].
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudius Röhl committed Oct 21, 2024
1 parent a694636 commit 208b8eb
Show file tree
Hide file tree
Showing 26 changed files with 112 additions and 93 deletions.
2 changes: 1 addition & 1 deletion drafts/copyxmpfromduplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class File:
tags: str

cnt = 0
taggedfiles: List[File] = []
taggedfiles: list[File] = []
with ExifToolHelper() as et:
for root, dirs, files in os.walk(taggedfilesfolder):
for f in files:
Expand Down
2 changes: 1 addition & 1 deletion modules/general/filenamehelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def isCorrectTimestamp(candidate: str) -> CheckResult:
return CheckResult(
dummy is not None, "Could not parse timestamp, invalid format"
)
except:
except Exception:
return CheckResult(False, f"Candidate {candidate}'s timestamp is wrong")


Expand Down
24 changes: 12 additions & 12 deletions modules/general/mediaaggregator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Dict, List, Set
from typing import Dict, Set

from pathlib import Path
from os.path import basename, splitext
Expand Down Expand Up @@ -36,23 +36,23 @@ class MediaAggregator(MediaTransitioner):
def __init__(self, input: AggregatorInput):
super().__init__(input)
self.jpgSingleSourceOfTruth = input.jpgSingleSourceOfTruth
self.toTransition: List[TransitionTask] = []
self.toTransition: list[TransitionTask] = []

def getAllTagRelevantFilenamesFor(self, file: MediaFile) -> List[str]:
def getAllTagRelevantFilenamesFor(self, file: MediaFile) -> list[str]:
return file.getAllFileNames()

def getTagsFromTasks(self) -> Dict[int, List[Dict[str, str]]]:
def getTagsFromTasks(self) -> Dict[int, list[Dict[str, str]]]:
"""
Returns index to tags of all files of mediafile
"""
self.print_info("Collect file meta tags..")
out: Dict[int, List[Dict[str, str]]] = {}
out: Dict[int, list[Dict[str, str]]] = {}

with ExifToolHelper() as et:
for task in track(self.toTransition):
files = self.getAllTagRelevantFilenamesFor(self.toTreat[task.index])
try:
tagdictlist: List[Dict[str, str]] = et.get_tags(
tagdictlist: list[Dict[str, str]] = et.get_tags(
files, tags=MowTags.all, params=["-L"]
)
out[task.index] = [
Expand Down Expand Up @@ -81,7 +81,7 @@ def prepareTransition(self):
self.setMetaTagsToWrite(indexToTags)
self.deleteBasedOnRating(indexToTags)

def getTasks(self) -> List[TransitionTask]:
def getTasks(self) -> list[TransitionTask]:
self.prepareTransition()
return self.toTransition

Expand All @@ -100,7 +100,7 @@ def checkFileNamesHaveCorrectTimestamp(self):
else:
self.toTransition.append(TransitionTask(index=index))

def checkGrouping(self, indexToTags: Dict[int, List[Dict[str, str]]]):
def checkGrouping(self, indexToTags: Dict[int, list[Dict[str, str]]]):
for task in self.toTransition:
if task.skip:
continue
Expand All @@ -126,7 +126,7 @@ def checkGrouping(self, indexToTags: Dict[int, List[Dict[str, str]]]):
task.skipReason = result.error

def isCorrectDescriptionTag(
self, groupnameToTest: str, tagDicts: List[Dict[str, str]]
self, groupnameToTest: str, tagDicts: list[Dict[str, str]]
):
for tagDict in tagDicts:
if MowTags.description not in tagDict:
Expand All @@ -140,7 +140,7 @@ def isCorrectDescriptionTag(

return CheckResult(ok=True)

def setMetaTagsToWrite(self, indexToTags: Dict[int, List[Dict[str, str]]]):
def setMetaTagsToWrite(self, indexToTags: Dict[int, list[Dict[str, str]]]):
for task in self.toTransition:
if task.skip:
continue
Expand All @@ -153,7 +153,7 @@ def setMetaTagsToWrite(self, indexToTags: Dict[int, List[Dict[str, str]]]):
task.skipReason = result.error

def setMetaTagsToWriteFor(
self, task: TransitionTask, tagsDictList: List[Dict[str, str]]
self, task: TransitionTask, tagsDictList: list[Dict[str, str]]
) -> CheckResult:
for tag in MowTags.all:
allValuesThisTag: Set[str] = set()
Expand Down Expand Up @@ -186,7 +186,7 @@ def setMetaTagsToWriteFor(

return CheckResult(ok=True)

def deleteBasedOnRating(self, indexToTags: Dict[int, List[Dict[str, str]]]):
def deleteBasedOnRating(self, indexToTags: Dict[int, list[Dict[str, str]]]):
for task in self.toTransition:
if task.skip:
continue
Expand Down
2 changes: 1 addition & 1 deletion modules/general/mediaconverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def converter_wrapper(

try:
convertedFile = converter(toTransition, os.path.dirname(newPath), settings)
except Exception as e:
except Exception:
return toTransition, None, task_index

for file in convertedFile.getAllFileNames():
Expand Down
3 changes: 1 addition & 2 deletions modules/general/mediacopier.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import datetime
from typing import List
import os
from ..general.mediafile import MediaFile
from ..general.mediatransitioner import TransitionTask
Expand Down Expand Up @@ -33,7 +32,7 @@ def getIndexWithLast(self) -> int:
return index
return -1

def getTasks(self) -> List[TransitionTask]:
def getTasks(self) -> list[TransitionTask]:
self.toTreat = sorted(self.toTreat, key=self._getModificationDate)
self.indexWithLAST = self.getIndexWithLast()

Expand Down
4 changes: 2 additions & 2 deletions modules/general/mediafile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
from typing import Callable, List
from typing import Callable
import os
from shutil import copyfile, move
import datetime as dt
Expand Down Expand Up @@ -68,7 +68,7 @@ def copyTo(self, dst: str) -> str:
def readDateTime(self) -> dt.datetime:
raise NotImplementedError()

def getAllFileNames(self) -> List[str]:
def getAllFileNames(self) -> list[str]:
return [self.pathnoext + ext for ext in self.extensions]

def getDescriptiveBasenames(self) -> str:
Expand Down
26 changes: 13 additions & 13 deletions modules/general/mediagrouper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import DefaultDict, List, Tuple
from typing import DefaultDict, Tuple


import os
Expand Down Expand Up @@ -57,13 +57,13 @@ def isCorrectGroupName(candidate: str) -> CheckResult:
if len(candidate) <= 18:
return CheckResult(
ok=False,
error=f"has no or too short description, should at least have length 2",
error="has no or too short description, should at least have length 2",
)

if "@" in candidate[11:]:
return CheckResult(
ok=False,
error=f"contains '@' at index > 10",
error="contains '@' at index > 10",
)

return isCorrectTimestamp(candidate=candidate[0:17])
Expand Down Expand Up @@ -102,7 +102,7 @@ def __init__(self, input: GrouperInput):
input.mediaFileFactory = createAnyValidMediaFile
input.maintainFolderStructure = True
super().__init__(input)
self.toTransition: List[TransitionTask] = []
self.toTransition: list[TransitionTask] = []

def prepareTransition(self):
if self.input.undoAutomatedGrouping:
Expand Down Expand Up @@ -130,7 +130,7 @@ def prepareTransition(self):
self.printStatisticsOf(grouped)
self.setOptionalXMP(grouped)

def getTasks(self) -> List[TransitionTask]:
def getTasks(self) -> list[TransitionTask]:
self.prepareTransition()
return self.toTransition

Expand Down Expand Up @@ -212,7 +212,7 @@ def addMissingTimestamps(self):
f"Renamed {len(renamed)} folders without timestamps to folders that have one."
)

def printStatisticsOf(self, grouped: DefaultDict[str, List[int]]):
def printStatisticsOf(self, grouped: DefaultDict[str, list[int]]):
self.print_info(
f"Found {len(grouped.keys())} groups that were correct (YYYY-MM-DD@HHMMSS#, #=Groupname)."
)
Expand All @@ -224,9 +224,9 @@ def getUngroupedFiles(self) -> list[MediaFile]:

def getCorrectlyGroupedFiles(
self,
) -> Tuple[DefaultDict[str, List[int]], List[TransitionTask]]:
out: DefaultDict[str, List[int]] = defaultdict(lambda: [])
toTransitionOut: List[TransitionTask] = []
) -> Tuple[DefaultDict[str, list[int]], list[TransitionTask]]:
out: DefaultDict[str, list[int]] = defaultdict(lambda: [])
toTransitionOut: list[TransitionTask] = []
wrongSubfolders = set()

for index, file in enumerate(self.toTreat):
Expand Down Expand Up @@ -263,7 +263,7 @@ def groupUngrouped(self):
if len(ungrouped) == 0:
return
ungrouped.sort(key=lambda file: extractDatetimeFromFileName(str(file)))
groupToFiles: DefaultDict[str, List[MediaFile]] = defaultdict(lambda: [])
groupToFiles: DefaultDict[str, list[MediaFile]] = defaultdict(lambda: [])

self.print_info("Start creating new group names..")
currentGroup = self.getGroupBasedOnFirstFile(str(ungrouped[0]))
Expand Down Expand Up @@ -291,8 +291,8 @@ def groupUngrouped(self):
def checkCorrectSequence(self):
groupToFiles, _ = self.getCorrectlyGroupedFiles()

orderedFiles: List[
Tuple[str, List[str]]
orderedFiles: list[
Tuple[str, list[str]]
] = [] # Tuple[0] = groupname, Tuple[1] = filenames

for key, values in groupToFiles.items():
Expand Down Expand Up @@ -328,7 +328,7 @@ def checkCorrectSequence(self):
self.print_info(f"Found {len(overlappingFiles)} overlapping grouped files.")
self.print_info(f"Found {len(wrongGroupTimestamps)} wrong group timestamps.")

def setOptionalXMP(self, grouped: DefaultDict[str, List[int]]):
def setOptionalXMP(self, grouped: DefaultDict[str, list[int]]):
if not self.writeMetaTags:
return

Expand Down
7 changes: 3 additions & 4 deletions modules/general/medialocalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from pathlib import Path
import sys
import traceback
from typing import List
import polars as pl
import gpxpy
from zoneinfo import ZoneInfo
Expand Down Expand Up @@ -103,7 +102,7 @@ def __init__(self, input: LocalizerInput):

self.positions = self.getAllPositionsDataframe()

def getTasks(self) -> List[TransitionTask]:
def getTasks(self) -> list[TransitionTask]:
out = []
for index, mediafile in enumerate(self.toTreat):
try:
Expand All @@ -126,7 +125,7 @@ def getTasks(self) -> List[TransitionTask]:
TransitionTask(
index,
skip=True,
skipReason=f"Could not localize because of missing GPS data.",
skipReason="Could not localize because of missing GPS data.",
)
)
self.print_debug(
Expand Down Expand Up @@ -218,7 +217,7 @@ def createMapWithMediafiles(self, tasks: list[TransitionTask]):
if not self.suppress_map_open:
os.startfile(Path(self.src) / "map.html")

def getAllGpxFiles(self) -> List[str]:
def getAllGpxFiles(self) -> list[str]:
all_files = os.listdir(self.src)
return [
os.path.abspath(os.path.join(self.src, f))
Expand Down
5 changes: 2 additions & 3 deletions modules/general/mediarater.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from typing import List
from rich.progress import track
import os

Expand All @@ -21,10 +20,10 @@ def __init__(self, input: TransitionerInput, overrulingfiletype: str = None):
super().__init__(input)
self.overrulingfiletype = overrulingfiletype

def getTasks(self) -> List[TransitionTask]:
def getTasks(self) -> list[TransitionTask]:
self.print_info("Check every file for rating..")

out: List[TransitionTask] = []
out: list[TransitionTask] = []

with ExifToolHelper() as et:
for index, file in track(enumerate(self.toTreat), total=len(self.toTreat)):
Expand Down
6 changes: 3 additions & 3 deletions modules/general/mediarenamer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import dataclass
import os
from os.path import join
from typing import Callable, List, Tuple
from typing import Callable, Tuple
from pathlib import Path
from datetime import datetime
import re
Expand Down Expand Up @@ -53,7 +53,7 @@ def __init__(self, input: RenamerInput):
raise Exception("Filerenamer was not given to MediaRenamer!")

self.replace: str = input.replace
self.transitionTasks: List[TransitionTask] = []
self.transitionTasks: list[TransitionTask] = []

self.replace = self.initReplace(input.replace)

Expand Down Expand Up @@ -87,7 +87,7 @@ def performReplacement(self):
f"Replaced {Path(str(file)).relative_to(self.src)} ---> {newFileName}{ext}"
)

def getTasks(self) -> List[TransitionTask]:
def getTasks(self) -> list[TransitionTask]:
self.prepareTransition()
return self.transitionTasks

Expand Down
3 changes: 1 addition & 2 deletions modules/general/mediatagger.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from typing import List

from ..general.medafilefactories import createAnyValidMediaFile
from .mediatransitioner import MediaTransitioner, TransitionTask, TransitionerInput
Expand All @@ -9,5 +8,5 @@ def __init__(self, input: TransitionerInput):
input.mediaFileFactory = createAnyValidMediaFile
super().__init__(input)

def getTasks(self) -> List[TransitionTask]:
def getTasks(self) -> list[TransitionTask]:
return [TransitionTask(index) for index, _ in enumerate(self.toTreat)]
Loading

0 comments on commit 208b8eb

Please sign in to comment.