Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: filter for most recent files #163

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions src/migmose/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,29 @@ def get_latest_file(file_list: list[Path]) -> Path:
try:
# Define the keywords to filter relevant files
keywords = ["konsolidiertelesefassungmitfehlerkorrekturen", "außerordentlicheveröffentlichung"]

files_containing_keywords = [
path for path in file_list if any(keyword in path.name.lower() for keyword in keywords)
]
# Find the most recent file based on keywords and date suffixes
latest_file = max(
(path for path in file_list if any(keyword in path.name.lower() for keyword in keywords)),
key=lambda path: (
int(path.stem.split("_")[-1]), # "gültig von" date
int(path.stem.split("_")[-2]), # "gültig bis" date
),
)
if files_containing_keywords != []:
DeltaDaniel marked this conversation as resolved.
Show resolved Hide resolved
# Find the most recent file based on keywords and date suffixes
latest_file = max(
(path for path in files_containing_keywords),
key=lambda path: (
int(path.stem.split("_")[-1]), # "gültig von" date
int(path.stem.split("_")[-2]), # "gültig bis" date
_extract_document_version(path.stem.split("_")[-3]), # version number
),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

du könntet diesesn gemeinsamen part auch auslagern in eine kleine private function?

def _get_sorting_key(path:??)->tuple[int,int,str]:#??
   return  int(path.stem.split("_")[-1]),  # "gültig von" date
                    int(path.stem.split("_")[-2]),  # "gültig bis" date
                    _extract_document_version(path.stem.split("_")[-3]),

dann müsste man es nicht duplizieren und könnte es außerdem in einem docstring erklären ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

)
else: # different versions but no kosildierte Lesefassung or außerordentliche Veröffentlichung at all
latest_file = max(
(path for path in file_list),
key=lambda path: (
int(path.stem.split("_")[-1]), # "gültig von" date
int(path.stem.split("_")[-2]), # "gültig bis" date
_extract_document_version(path.stem.split("_")[-3]), # version number
),
)

except ValueError as e:
logger.error("Error processing file list: {}", e)
Expand Down Expand Up @@ -160,7 +174,7 @@ def parse_raw_nachrichtenstrukturzeile(input_path: Path) -> list[str]:
)


def _extract_document_version(path: Path) -> str:
def _extract_document_version(path: Path | str) -> str:
document_str = str(path)
DeltaDaniel marked this conversation as resolved.
Show resolved Hide resolved
matches = _pattern.search(document_str)
if matches:
Expand Down