-
-
Notifications
You must be signed in to change notification settings - Fork 704
Update iCalendar generation tool: add output filename template support #3679
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
Open
KJhellico
wants to merge
5
commits into
vacanza:dev
Choose a base branch
from
KJhellico:upd-generate-ics
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+211
−16
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
797c7ef
Update iCalendar generation tool: add output filename template support
KJhellico 3854ada
Add template validation
KJhellico e939b4d
Update template validation
KJhellico 8b46fac
Fix regex
KJhellico dd29d03
Refactor validation
KJhellico File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| from argparse import ArgumentParser, ArgumentTypeError, Namespace | ||
| from collections.abc import Callable | ||
| from datetime import datetime, timezone | ||
| from string import Formatter | ||
|
|
||
| import holidays | ||
| from holidays.holiday_base import HolidayBase | ||
|
|
@@ -61,7 +62,17 @@ def __init__(self): | |
| parser.add_argument( | ||
| "-l", "--language", help="Language code for holiday names (e.g., en_US, es)" | ||
| ) | ||
| parser.add_argument("-o", "--output", help="Output file path (e.g., holidays.ics)") | ||
|
|
||
| output_group = parser.add_mutually_exclusive_group() | ||
| output_group.add_argument("-o", "--output", help="Output file path") | ||
| output_group.add_argument( | ||
| "--output-template", | ||
| help=( | ||
| "Output filename template. Available placeholders: {code}, {subdiv}, " | ||
| "{language}, {categories}, {start_year}, {end_year}, {today}. " | ||
| "Use '{{' and '}}' for literal '{' and '}'" | ||
| ), | ||
| ) | ||
|
|
||
| list_group = parser.add_mutually_exclusive_group() | ||
| list_group.add_argument( | ||
|
|
@@ -166,6 +177,28 @@ def validate_language(self) -> None: | |
| "Use --list-languages to see supported values" | ||
| ) | ||
|
|
||
| def validate_output_template(self, placeholders: set[str]) -> None: | ||
| if not self.args.output_template: | ||
| return None | ||
|
|
||
| has_placeholder = False | ||
| try: | ||
| for _, field_name, _, _ in Formatter().parse(self.args.output_template): | ||
|
KJhellico marked this conversation as resolved.
Outdated
|
||
| if field_name is None: | ||
| continue | ||
| has_placeholder = True | ||
| if field_name not in placeholders: | ||
| supported = ", ".join(f"{{{p}}}" for p in sorted(placeholders)) | ||
| raise SystemExit( | ||
| f"Unknown placeholder '{{{field_name}}}' in output template. " | ||
| f"Supported placeholders: {supported}" | ||
| ) | ||
| except ValueError as e: | ||
| raise SystemExit(f"Invalid output template: {e}") | ||
|
|
||
| if not has_placeholder: | ||
| raise SystemExit("Output template must contain at least one placeholder") | ||
|
|
||
| def handle_list_options(self) -> bool: | ||
| if self.args.list_subdivisions: | ||
| print(f"Supported subdivisions for {self.args.code}:") | ||
|
|
@@ -184,6 +217,26 @@ def handle_list_options(self) -> bool: | |
|
|
||
| return False | ||
|
|
||
| def get_default_output_template(self) -> str: | ||
| start_year, end_year = self.args.years | ||
| parts = ["{code}"] | ||
|
|
||
| if self.args.subdiv: | ||
| parts.append("{subdiv}") | ||
|
|
||
| if self.args.language: | ||
| parts.append("{language}") | ||
|
|
||
| if self.args.categories: | ||
| parts.append("{categories}") | ||
|
|
||
| parts.append("{start_year}") | ||
|
|
||
| if start_year != end_year: | ||
| parts.append("{end_year}") | ||
|
|
||
| return "_".join(parts) + ".ics" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not good, str |
||
|
|
||
| def run(self) -> None: | ||
| self.validate_code() | ||
| if self.handle_list_options(): | ||
|
|
@@ -194,9 +247,24 @@ def run(self) -> None: | |
| self.validate_categories() | ||
|
|
||
| start_year, end_year = self.args.years | ||
| years_part = f"{start_year}_{end_year}" if start_year != end_year else f"{start_year}" | ||
| subdiv_part = f"_{self.args.subdiv.upper().replace(' ', '_')}" if self.args.subdiv else "" | ||
| output_path = self.args.output or f"{self.args.code}{subdiv_part}_{years_part}.ics" | ||
|
|
||
| if self.args.output: | ||
| output_path = self.args.output | ||
| else: | ||
| values = { | ||
| "code": self.args.code, | ||
| "subdiv": (self.args.subdiv or "ALL").upper().replace(" ", "_"), | ||
| "language": self.args.language.upper() if self.args.language else "DEFAULT", | ||
| "categories": ( | ||
| "_".join(self.args.categories).upper() if self.args.categories else "PUBLIC" | ||
| ), | ||
| "start_year": start_year, | ||
| "end_year": end_year, | ||
| "today": datetime.now(timezone.utc).strftime("%Y%m%d"), | ||
| } | ||
| self.validate_output_template(set(values)) | ||
| template = self.args.output_template or self.get_default_output_template() | ||
| output_path = template.format(**values) | ||
|
|
||
| try: | ||
| holiday_obj = self.entity_loader( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.