From 241e3f8e07902235470b438c4da4a2760de782a1 Mon Sep 17 00:00:00 2001 From: Millian Poquet <millian.poquet@irit.fr> Date: Wed, 14 Sep 2022 10:47:06 +0200 Subject: [PATCH] fetch-ics -> fetch-celcat (more output options) --- README.md | 11 +++++- flake.nix | 2 +- lflex_celcat_survival/cmd/fetch_celcat.py | 46 +++++++++++++++++++++++ lflex_celcat_survival/cmd/fetch_ics.py | 27 ------------- pyproject.toml | 2 +- 5 files changed, 58 insertions(+), 30 deletions(-) create mode 100755 lflex_celcat_survival/cmd/fetch_celcat.py delete mode 100755 lflex_celcat_survival/cmd/fetch_ics.py diff --git a/README.md b/README.md index a1dc814..2126281 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,16 @@ This project tries to make CELCAT usable by: - **TODO**: Providing a way to sync ics with a CalDAV server, to enable all calendar clients to synchronize from it ### Usage -`fetch-ics <COURSE-REQUESTS-FILE.csv> [-o <OUTPUT-FILE.ics>]` +``` +Usage: fetch-celcat [OPTIONS] COURSE_REQUEST_FILE + +Options: + --json, --json-raw TEXT If set, raw CELCAT events are written as JSON to this file. + --csv-raw TEXT If set, raw (unfiltered) events are written as CSV to this file. + --csv TEXT If set, filteret events are written as CSV to this file. + --ics TEXT If set, filtered events are written as ICS to this file. + --help Show this message and exit. +``` The course request input file defines which courses you are interested in. For example, if you give the following courses: diff --git a/flake.nix b/flake.nix index 5a44b9d..b7f62ee 100644 --- a/flake.nix +++ b/flake.nix @@ -9,7 +9,7 @@ let pkgs = nixpkgs.legacyPackages.${system}; in rec { packages = import ./default.nix { inherit pkgs; }; - apps.fetch-ics = flake-utils.lib.mkApp { drv = packages.lflex_celcat_survival; exePath = "/bin/fetch-ics"; }; + apps.fetch-celcat = flake-utils.lib.mkApp { drv = packages.lflex_celcat_survival; exePath = "/bin/fetch-celcat"; }; defaultPackage = packages.lflex_celcat_survival; } ); diff --git a/lflex_celcat_survival/cmd/fetch_celcat.py b/lflex_celcat_survival/cmd/fetch_celcat.py new file mode 100755 index 0000000..49898ef --- /dev/null +++ b/lflex_celcat_survival/cmd/fetch_celcat.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +import click +import logging +import lflex_celcat_survival as lcs + +@click.command() +@click.argument('course_request_file') +@click.option('--json', '--json-raw', default=None, help='If set, raw CELCAT events are written as JSON to this file.') +@click.option('--csv-raw', default=None, help='If set, raw (unfiltered) events are written as CSV to this file.') +@click.option('--csv', default=None, help='If set, filteret events are written as CSV to this file.') +@click.option('--ics', default=None, help='If set, filtered events are written as ICS to this file.') +def main(course_request_file, json, csv_raw, csv, ics): + logging.basicConfig(level=logging.INFO) + + req = lcs.course_request.CourseRequest(course_request_file) + if all(o is None for o in [json, csv_raw, csv, ics]): + logging.warning('No option set, doing nothing.') + return + + celcat_raw_response = req.do_request() + if json is not None: + with open(json, 'w') as f: + f.write(celcat_raw_response) + if all(o is None for o in [csv_raw, csv, ics]): + return + + celcat_events = lcs.events.CelcatEvents(celcat_raw_response) + if csv_raw is not None: + celcat_events.df.to_csv(csv_raw, index=False) + if all(o is None for o in [csv, ics]): + return + + filtered_celcat_events = lcs.events.FilteredCelcatEvents(req, celcat_events) + filtered_celcat_events.check_expected_nb_timeslots() + if csv is not None: + filtered_celcat_events.df.to_csv(csv, index=False) + if all(o is None for o in [ics]): + return + + calendar = lcs.ics.course_df_to_ics(filtered_celcat_events.df) + if ics is not None: + with open(ics, 'w') as f: + f.write(str(calendar)) + +if __name__ == "__main__": + main() diff --git a/lflex_celcat_survival/cmd/fetch_ics.py b/lflex_celcat_survival/cmd/fetch_ics.py deleted file mode 100755 index 2fc03e0..0000000 --- a/lflex_celcat_survival/cmd/fetch_ics.py +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env python3 -import click -import logging -import lflex_celcat_survival as lcs - -@click.command() -@click.argument('course_request_file') -@click.option('--output-file', '-o', default=None, help='Where to write the generated ICS (stdout if unset).') -def main(course_request_file, output_file): - logging.basicConfig(level=logging.INFO) - req = lcs.course_request.CourseRequest(course_request_file) - celcat_raw_response = req.do_request() - - celcat_events = lcs.events.CelcatEvents(celcat_raw_response) - filtered_celcat_events = lcs.events.FilteredCelcatEvents(req, celcat_events) - filtered_celcat_events.check_expected_nb_timeslots() - - calendar = lcs.ics.course_df_to_ics(filtered_celcat_events.df) - - if output_file is None: - print(calendar) - else: - with open(output_file, 'w') as f: - f.write(str(calendar)) - -if __name__ == "__main__": - main() diff --git a/pyproject.toml b/pyproject.toml index 77e0127..b88149d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,4 +29,4 @@ dependencies = [ ] [project.scripts] -fetch-ics = "lflex_celcat_survival.cmd.fetch_ics:main" +fetch-celcat = "lflex_celcat_survival.cmd.fetch_celcat:main" -- GitLab