From 87a645d0fc341df4bfeca9a6c465d1fda58b5da3 Mon Sep 17 00:00:00 2001 From: Stuce Date: Sun, 16 Nov 2025 10:55:01 +0100 Subject: [PATCH 1/2] added credentials file --- flake.nix | 70 +++++++++++++++++++++++++++++++++++++------------------ main.py | 46 ++++++++++++++++++------------------ setup.py | 12 ++++++++++ 3 files changed, 82 insertions(+), 46 deletions(-) create mode 100644 setup.py diff --git a/flake.nix b/flake.nix index 5999240..d6b7a10 100644 --- a/flake.nix +++ b/flake.nix @@ -33,33 +33,57 @@ nixosModules.caldavToCsv = { config, lib, ... }: let cfg = config.services.caldavToCsv; in { - options.NixosModule = { - enable = lib.mkEnableOption "Enable calDAVtoCSV service"; - port = lib.mkOption { - type = lib.types.int; - default = 8000; - description = "Port on which calDAVtoCSV will listen"; + options.calDavtoCSV = { + enable = lib.mkOption { + type = lib.types.bool; + default = false; + description = "Enable myService"; }; - url = lib.mkOption { - type = lib.types.str; - description = "url of the calendar"; - }; - calendarUsername = lib.mkOption { - type = lib.types.str; - description = "username of the calendar account"; - }; - calendarName = lib.mkOption { - type = lib.types.str; - description = - "name of the calendar we will fetch the todo items from"; - }; - calendarPasswordFile = lib.mkOption { - type = lib.types.str; - description = - "file where we need to look for password to connect, needs to be readeable by the service user"; + + config = lib.mkOption { + type = lib.types.attrs; + default = { + calDAV = { + address = "localhost:5232"; + username = "username"; + password = "password"; + passwordFile = "/path/to/password"; + }; + server = { port = 8000; }; + }; + description = "User-defined configuration for the service"; }; }; + # options.NixosModule = { + # enable = lib.mkEnableOption "Enable calDAVtoCSV service"; + # port = lib.mkOption { + # type = lib.types.int; + # default = 8000; + # description = "Port on which calDAVtoCSV will listen"; + # }; + # url = lib.mkOption { + # type = lib.types.str; + # description = "url of the calendar"; + # }; + # calendarUsername = lib.mkOption { + # type = lib.types.str; + # description = "username of the calendar account"; + # }; + # calendarName = lib.mkOption { + # type = lib.types.str; + # description = + # "name of the calendar we will fetch the todo items from"; + # }; + # calendarPasswordFile = lib.mkOption { + # type = lib.types.str; + # description = + # "file where we need to look for password to connect, needs to be readeable by the service user"; + # }; + # }; config = lib.mkIf config.myModule.enable { + environment.etc."config.ini".text = '' + ${lib.toIni config.myService.config} + ''; systemd.services.calDAVtoCSV = { description = "calDAV to CSV Service"; after = [ "network.target" ]; diff --git a/main.py b/main.py index b4a38f9..538d861 100755 --- a/main.py +++ b/main.py @@ -2,21 +2,40 @@ from flask import Flask from caldav import DAVClient, Calendar, Principal, Todo +import configparser # TODO: -# [ ] settings file to not have hardcoded credentials +# [x] settings file to not have hardcoded credentials # [ ] change production credentials # [ ] nix flake with complete module # [ ] post behavior (simple) # [ ] post behavior (concurrent protection) # [ ] check if better http server availible or fastcgi better suited #!/usr/bin/env python + + +def parseConfig(): + config = configparser.ConfigParser() + config.read("/home/stuce/calDAVtoCSV/config.ini") # TODO: change this + global calDavAddress + global calDavUsername + global calDavPassword + global calDavCalendarName + global serverPort + calDavAddress = config.get("calDAV", "address") + calDavUsername = config.get("calDAV", "username") + calDavPassword = config.get("calDAV", "password") + calDavPasswordFile = config.get("calDAV", "passwordFile") + calDavCalendarName = config.get("calDAV", "calendarName") + serverPort = config.getint("server", "port") + + def fetch_10_next_todos_as_csv() -> str: def auth() -> Principal: # TODO: on the final version, fetch it locally client = DAVClient( - url="https://cal.stuce.ch", username="eInk", password="4Ftxy9rp8e$F*f" + url=calDavAddress, username=calDavUsername, password=calDavPassword ) principal = client.principal() return principal @@ -45,26 +64,6 @@ def put(todos, uid): item = filter(lambda todo: todo.icalendar_component["uid"] == uid, todos) -# class SimpleHandler(BaseHTTPRequestHandler): -# def do_GET(self): -# self.send_response(200) -# self.send_header("Content-type", "text/plain") -# self.end_headers() -# response_body_str = fetch_10_next_todos_as_csv() -# response_body_utf8 = response_body_str.encode("utf8") -# self.wfile.write(response_body_utf8) -# -# -# def main(server_class=HTTPServer, port=8000): -# server_address = ("", port) -# httpd = server_class(server_address, SimpleHandler) -# httpd.serve_forever() -# -# -# if __name__ == "__main__": -# main() - - app = Flask(__name__) @@ -74,4 +73,5 @@ def send_events(): if __name__ == "__main__": - app.run(host="0.0.0.0", port=8080) + parseConfig() + app.run(host="0.0.0.0", port=serverPort) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..bb208fa --- /dev/null +++ b/setup.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python + +from setuptools import setup, find_packages + +setup( + name="calDAVtoCSV", + version="1.0", + # Modules to import from other scripts: + packages=find_packages(), + # Executables + scripts=["main.py"], +) From 9d7afca6a9ca01460b4a3663ab369a8cea04a588 Mon Sep 17 00:00:00 2001 From: Stuce Date: Sun, 16 Nov 2025 10:55:55 +0100 Subject: [PATCH 2/2] . --- .gitignore | 3 +++ default_config.ini | 8 ++++++++ 2 files changed, 11 insertions(+) create mode 100644 .gitignore create mode 100644 default_config.ini diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f6e3c00 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__ +result +config.ini diff --git a/default_config.ini b/default_config.ini new file mode 100644 index 0000000..80515ae --- /dev/null +++ b/default_config.ini @@ -0,0 +1,8 @@ +[calDAV] +address = localhost:5232 +username = username +password = password +passwordFile = /path/to/password +[server] +port = 8000 +