Skip to content

Commit

Permalink
Merge pull request #26 from python-spain/improve-logging
Browse files Browse the repository at this point in the history
Improve logging
  • Loading branch information
astrojuanlu authored Jun 25, 2023
2 parents 4413234 + 6924997 commit ca15488
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ git_committer_name := "Python España"
git_committer_email := "[email protected]"

clean-future:
pyevents clean-after -l $(date -I)
pyevents clean-after -v -l $(date -I)

fetch-upcoming:
pyevents fetch-upcoming -c communities.toml
pyevents fetch-upcoming -v -c communities.toml

commit-events:
git fetch
Expand Down
20 changes: 15 additions & 5 deletions src/pyevents/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,25 @@ def cli():
def clean_after(cutoff_datetime, destination_dirname, verbose):
structlog.configure(
wrapper_class=structlog.make_filtering_bound_logger(
logging.INFO if verbose else logging.WARNING
logging.DEBUG if verbose else logging.INFO
),
)

cutoff_date = cutoff_datetime.date()
logging.debug("Cutoff date set", cutoff_date=cutoff_date)

for event_path in Path(destination_dirname).glob("**/*.json"):
logger.debug("Event file found", event_path=event_path)
event_date = dt.datetime.strptime(
event_path.name.split("_")[0], "%Y-%m-%d"
).date()
logger.debug("Event file found", event_path=event_path, event_date=event_date)
if event_date > cutoff_date:
logger.info(
"Deleting event file", event_path=event_path, event_date=event_date
)
event_path.unlink()
else:
logger.info("No event files deleted")


@cli.command()
Expand All @@ -65,27 +68,34 @@ def clean_after(cutoff_datetime, destination_dirname, verbose):
def fetch_upcoming(communities_path, destination_dirname, verbose):
structlog.configure(
wrapper_class=structlog.make_filtering_bound_logger(
logging.INFO if verbose else logging.WARNING
logging.DEBUG if verbose else logging.INFO
),
)

with open(communities_path, "rb") as f:
communities_data = tomllib.load(f)
communities = communities_data["communities"]

upcoming_events = collect_upcoming_events(communities)

if not (destination_dir := Path(destination_dirname)).is_dir():
destination_dir.mkdir()

upcoming_events = collect_upcoming_events(communities)

for community_slug, events in upcoming_events.items():
if not (community_dir := (destination_dir / community_slug)).is_dir():
community_dir.mkdir()

for event_data in events:
event = event_data["data"]["event"]
logger.debug("Saving new event", event_title=event["title"])
with open(community_dir / f"{event_name_from_data(event)}.json", "w") as fh:
json.dump(event, fh, indent=2)
else:
logger.debug(
"No new events found for this community", community_slug=community_slug
)
else:
logger.info("No new events found")


if __name__ == "__main__":
Expand Down

0 comments on commit ca15488

Please sign in to comment.