|
1 | 1 | #!/usr/bin/env python |
2 | 2 | import logging |
| 3 | +import os |
3 | 4 | import sys |
4 | 5 | from types import TracebackType |
5 | 6 | from typing import Any |
|
44 | 45 | else: |
45 | 46 | from typing import Self |
46 | 47 |
|
| 48 | +## TODO: this is also declared in davclient.DAVClient.__init__(...) |
| 49 | +## TODO: it should be consolidated, duplication is a bad thing |
| 50 | +## TODO: and it's almost certain that we'll forget to update this list |
| 51 | +CONNKEYS = set( |
| 52 | + ( |
| 53 | + "url", |
| 54 | + "proxy", |
| 55 | + "username", |
| 56 | + "password", |
| 57 | + "timeout", |
| 58 | + "headers", |
| 59 | + "huge_tree", |
| 60 | + "ssl_verify_cert", |
| 61 | + "ssl_cert", |
| 62 | + "auth", |
| 63 | + ) |
| 64 | +) |
| 65 | + |
47 | 66 |
|
48 | 67 | class DAVResponse: |
49 | 68 | """ |
@@ -75,9 +94,11 @@ def __init__( |
75 | 94 |
|
76 | 95 | content_type = self.headers.get("Content-Type", "") |
77 | 96 | xml = ["text/xml", "application/xml"] |
78 | | - no_xml = ["text/plain", "text/calendar"] |
| 97 | + no_xml = ["text/plain", "text/calendar", "application/octet-stream"] |
79 | 98 | expect_xml = any((content_type.startswith(x) for x in xml)) |
80 | 99 | expect_no_xml = any((content_type.startswith(x) for x in no_xml)) |
| 100 | + if content_type and not expect_xml and not expect_no_xml: |
| 101 | + error.weirdness(f"Unexpected content type: {content_type}") |
81 | 102 | try: |
82 | 103 | content_length = int(self.headers["Content-Length"]) |
83 | 104 | except: |
@@ -818,3 +839,82 @@ def request( |
818 | 839 | commlog.write(b"\n") |
819 | 840 |
|
820 | 841 | return response |
| 842 | + |
| 843 | + |
| 844 | +def auto_calendars( |
| 845 | + configfile: str = f"{os.environ.get('HOME')}/.config/calendar.conf", |
| 846 | + testconfig=False, |
| 847 | + environment: bool = True, |
| 848 | + config_data: dict = None, |
| 849 | + config_name: str = None, |
| 850 | +) -> Iterable["Calendar"]: |
| 851 | + """ |
| 852 | + This will replace plann.lib.findcalendars() |
| 853 | + """ |
| 854 | + raise NotImplementedError("auto_calendars not implemented yet") |
| 855 | + |
| 856 | + |
| 857 | +def auto_calendar(*largs, **kwargs) -> Iterable["Calendar"]: |
| 858 | + """ |
| 859 | + Alternative to auto_calendars - in most use cases, one calendar suffices |
| 860 | + """ |
| 861 | + return next(auto_calendars(*largs, **kwargs), None) |
| 862 | + |
| 863 | + |
| 864 | +def auto_conn( |
| 865 | + configfile: str = f"{os.environ.get('HOME')}/.config/calendar.conf", |
| 866 | + testconfig=False, |
| 867 | + environment: bool = True, |
| 868 | + config_data: dict = None, |
| 869 | + name: str = None, |
| 870 | +) -> "DAVClient": |
| 871 | + """ |
| 872 | + Normally you would like to look into auto_calendars or |
| 873 | + auto_calendar instead. However, in some cases it's needed |
| 874 | + with a DAVClient object rather than a Calendar object. |
| 875 | +
|
| 876 | + This function will yield a DAVClient object. It will not try to |
| 877 | + connect (see auto_calendars for that). It will read configuration |
| 878 | + from various sources, dependent on the parameters given, in this |
| 879 | + order: |
| 880 | +
|
| 881 | + * Data from the given dict |
| 882 | + * Environment variables prepended with "CALDAV_" |
| 883 | + * Data from `./tests/conf.py` or `./conf.py` (this includes the possibility to spin up a test server) |
| 884 | + * Configuration file. Documented in the plann project as for now. (TODO - move it) |
| 885 | +
|
| 886 | + """ |
| 887 | + if config_data: |
| 888 | + return DAVClient(**config_data) |
| 889 | + |
| 890 | + if testconfig: |
| 891 | + sys.path.insert(0, "tests") |
| 892 | + sys.path.insert(1, ".") |
| 893 | + ## TODO: move the code from client into here |
| 894 | + try: |
| 895 | + from conf import client |
| 896 | + |
| 897 | + try: |
| 898 | + idx = int(name) |
| 899 | + except ValueError: |
| 900 | + idx = None |
| 901 | + try: |
| 902 | + conn = client(idx, name, **config_data) |
| 903 | + if conn: |
| 904 | + return conn |
| 905 | + except: |
| 906 | + error.weirdness("traceback from client()") |
| 907 | + except ImportError: |
| 908 | + pass |
| 909 | + finally: |
| 910 | + sys.path = sys.path[2:] |
| 911 | + |
| 912 | + if environment: |
| 913 | + raise NotImplementedError( |
| 914 | + "Not possible to configure the caldav server through environmental variables yet" |
| 915 | + ) |
| 916 | + |
| 917 | + if configfile: |
| 918 | + raise NotImplementedError( |
| 919 | + "Support for configuration file not made yet (TODO: copy the code from the plann tool)" |
| 920 | + ) |
0 commit comments