opentechcalendartools 0.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
@@ -0,0 +1,4 @@
1
+ import opentechcalendartools.cli
2
+
3
+ if __name__ == "__main__":
4
+ opentechcalendartools.cli.main()
@@ -0,0 +1,40 @@
1
+ import argparse
2
+ import os
3
+
4
+ from .worker import Worker
5
+
6
+
7
+ def main():
8
+
9
+ # First we process the data files into a SQLite database for us to use
10
+ sqlite_database_filename = os.getenv(
11
+ "OPEN_TECH_CALENDAR_TOOLS_SQLITE_DATABASE_FILENAME"
12
+ )
13
+ if not sqlite_database_filename:
14
+ raise Exception(
15
+ "Must specify OPEN_TECH_CALENDAR_TOOLS_SQLITE_DATABASE_FILENAME env var"
16
+ )
17
+ # TODO better would be to open a tempfile ourselves instead, and build it
18
+
19
+ # Now check options ...
20
+ parser = argparse.ArgumentParser()
21
+
22
+ subparsers = parser.add_subparsers(dest="subparser_name")
23
+
24
+ subparsers.add_parser("listgroupstoimport")
25
+
26
+ import_group_parser = subparsers.add_parser("importgroup")
27
+ import_group_parser.add_argument("group_id")
28
+
29
+ args = parser.parse_args()
30
+
31
+ if args.subparser_name == "listgroupstoimport":
32
+ # List groups to import
33
+ worker = Worker(sqlite_database_filename)
34
+ for group_id in worker.get_group_ids_to_import():
35
+ print(group_id)
36
+
37
+ elif args.subparser_name == "importgroup":
38
+ # Import Group
39
+ worker = Worker(sqlite_database_filename)
40
+ worker.import_group(args.group_id)
@@ -0,0 +1,79 @@
1
+ import datetime
2
+ import os
3
+ import sqlite3
4
+ import tempfile
5
+
6
+ import icalendar
7
+ import requests
8
+ import yaml
9
+
10
+
11
+ class Worker:
12
+
13
+ def __init__(self, sqlite_database_filename):
14
+ self._sqlite_database_filename = sqlite_database_filename
15
+ self._data_dir = os.getcwd()
16
+
17
+ def get_group_ids_to_import(self) -> list:
18
+ with sqlite3.connect(self._sqlite_database_filename) as connection:
19
+ res = connection.cursor().execute(
20
+ "SELECT id FROM record_group WHERE field_import_type != '' AND field_import_type IS NOT NULL ORDER BY id ASC"
21
+ )
22
+ return [i[0] for i in res.fetchall()]
23
+
24
+ def import_group(self, group_id):
25
+ with sqlite3.connect(self._sqlite_database_filename) as connection:
26
+ connection.row_factory = sqlite3.Row
27
+ cursor = connection.cursor()
28
+ res = cursor.execute("SELECT * FROM record_group WHERE id=?", [group_id])
29
+ group = res.fetchone()
30
+ if group["field_import_type"] == "ical":
31
+ self._import_group_type_ical(
32
+ group_id,
33
+ self._download_file_to_temp(group["field_import_url"]),
34
+ group_country=group["field_country"],
35
+ group_place=group["field_place"],
36
+ )
37
+
38
+ def _download_file_to_temp(self, url) -> str:
39
+ r = requests.get(url)
40
+ r.raise_for_status()
41
+ new_filename_dets = tempfile.mkstemp(
42
+ suffix="opentechcalendartools_",
43
+ )
44
+ os.write(new_filename_dets[0], r.content)
45
+ os.close(new_filename_dets[0])
46
+ return new_filename_dets[1]
47
+
48
+ def _import_group_type_ical(
49
+ self, group_id, ical_filename, group_country=None, group_place=None
50
+ ):
51
+ os.makedirs(os.path.join(self._data_dir, "event", group_id), exist_ok=True)
52
+ with open(ical_filename) as fp:
53
+ calendar = icalendar.Calendar.from_ical(fp.read())
54
+ for event in calendar.events:
55
+ start = event.get("DTSTART")
56
+ end = event.get("DTEND")
57
+ if end.dt.timestamp() > datetime.datetime.now().timestamp():
58
+ event_data = {
59
+ "title": str(event.get("SUMMARY")),
60
+ "group": group_id,
61
+ "start_at": str(start.dt),
62
+ "end_at": str(end.dt),
63
+ "url": str(event.get("URL")),
64
+ "cancelled": (event.get("STATUS") == "CANCELLED"),
65
+ "imported": True,
66
+ }
67
+ if group_country:
68
+ event_data["country"] = group_country
69
+ if group_place:
70
+ event_data["place"] = group_place
71
+ id = event.get("UID").split("@").pop(0)
72
+ filename = os.path.join(
73
+ self._data_dir, "event", group_id, id + ".md"
74
+ )
75
+ with open(filename, "w") as fp:
76
+ fp.write("---\n")
77
+ fp.write(yaml.dump(event_data))
78
+ fp.write("---\n\n\n")
79
+ fp.write(event.get("DESCRIPTION"))
@@ -0,0 +1,28 @@
1
+ Metadata-Version: 2.4
2
+ Name: opentechcalendartools
3
+ Version: 0.0.0
4
+ Home-page: https://github.com/TeacakeTech/opentechcalendar-tools
5
+ Author: Teacake Tech
6
+ Author-email: hello@teacaketech.scot
7
+ Project-URL: Home Page, https://opentechcalendar.co.uk/
8
+ Project-URL: Issues, https://github.com/TeacakeTech/opentechcalendar-tools/issues
9
+ Project-URL: Source, https://github.com/TeacakeTech/opentechcalendar-tools
10
+ Requires-Python: >=3.12
11
+ License-File: LICENSE
12
+ Requires-Dist: datatig
13
+ Requires-Dist: requests
14
+ Requires-Dist: icalendar
15
+ Requires-Dist: pyyaml
16
+ Provides-Extra: dev
17
+ Requires-Dist: black; extra == "dev"
18
+ Requires-Dist: isort; extra == "dev"
19
+ Requires-Dist: flake8; extra == "dev"
20
+ Requires-Dist: mypy; extra == "dev"
21
+ Dynamic: author
22
+ Dynamic: author-email
23
+ Dynamic: home-page
24
+ Dynamic: license-file
25
+ Dynamic: project-url
26
+ Dynamic: provides-extra
27
+ Dynamic: requires-dist
28
+ Dynamic: requires-python
@@ -0,0 +1,9 @@
1
+ opentechcalendartools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ opentechcalendartools/__main__.py,sha256=k62u01s2PYzYpErY7unEc49JsxnOohcSOG_Gu9pFuYA,98
3
+ opentechcalendartools/cli.py,sha256=fR5XhuniYb15hOGzYmCHc2ORRxbhiB9KOEUZNtkiIx8,1201
4
+ opentechcalendartools/worker.py,sha256=0MD6QVW5BaXFX4aL6M7gf6VG2LuqBrzwa3QZuP076Lc,3215
5
+ opentechcalendartools-0.0.0.dist-info/licenses/LICENSE,sha256=t3-svmFJHEV_slEWg4lRY-LK523IOA56WXz62uOzGFI,1499
6
+ opentechcalendartools-0.0.0.dist-info/METADATA,sha256=iJ-pfMuATfpKCK1Vd0kydMPWN_Rk8OaE5U0-pBsG5Zs,881
7
+ opentechcalendartools-0.0.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
8
+ opentechcalendartools-0.0.0.dist-info/top_level.txt,sha256=cXAUphHY7mCrtEnq3V6i_QyeWcbBEM75SJYjrUtDZ8E,22
9
+ opentechcalendartools-0.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2025, Teacake Tech
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1 @@
1
+ opentechcalendartools