From dfa28017c9a19a017dc688cb3b8fd72e620d4f2f Mon Sep 17 00:00:00 2001 From: "S.H" Date: Tue, 26 Mar 2024 11:27:05 +0100 Subject: [PATCH 01/10] Add the year as validFrom and validTo to the output, too The dedicated year was missing, causing errors since the output has been valid for every year so far. --- ephemeris.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ephemeris.py b/ephemeris.py index 765d7c2..649bf34 100644 --- a/ephemeris.py +++ b/ephemeris.py @@ -90,7 +90,12 @@ def daterange(start_date, end_date): print (month) addString = """\t\t\n""".format(_month=MONTHS[int(single_date.strftime("%m"))], _day=single_date.strftime("%d"),_summary=summary) + addString = """\t\t\n""".format( + _month=MONTHS[int(single_date.strftime("%m"))], + _day=single_date.strftime("%d"), + _summary=summary, + _from=single_date.strftime("%Y"), + _to=single_date.strftime("%Y")) if args.verbose > 0: print (addString) xmlFileContent += addString @@ -103,4 +108,4 @@ def daterange(start_date, end_date): filehandle.write(xmlFileContent) filehandle.close -print("Added %d items to file %s" %(count, args.outFile)) +print("Added %d items to file %s" %(count, args.outFile)) \ No newline at end of file From 8fe826eaea38b3fd65a247f7a47e2e631969bf36 Mon Sep 17 00:00:00 2001 From: "S.H" Date: Tue, 26 Mar 2024 11:36:20 +0100 Subject: [PATCH 02/10] Updated README.md --- README.md | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4244022..c5e4a79 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,23 @@ # EphemerisCalendarImport -import the school holiday calendar of [schulferien.org](https://www.schulferien.org/deutschland/ferien/) +Make the the school holiday calendar of [schulferien.org](https://www.schulferien.org/deutschland/ferien/) +available as a [JollyDay](https://github.com/svendiedrichsen/jollyday) XML file, e.g. for use in [OpenHab Ephemeris](https://www.openhab.org/docs/configuration/actions.html#ephemeris). The script imports the data from the school calendar files for your state into a xml-file readable by the Ephemeris action. ## get the calendars Calendars for German school holidays are centrally provided by [schulferien.org](https://www.schulferien.org/deutschland/ferien/). -Just download the calendar for your state **[from here](https://www.schulferien.org/deutschland/ical/)** and store it. You can download as many calendars as you want. +Just download the calendar for your state **[from here](https://www.schulferien.org/deutschland/ical/)** and store it. +You can download as many calendars as you want. + +## advantages of this fork +This fork has the following changes: +* ported to Pythos 3 +* added the actual *year* to the generated calendar entry (this was missing causing errors and eoverlapping entries when used with multiple years) ## Preconditions -### python 2.7.x -install [python 2.7](https://www.python.org/downloads/) +### python 3 +install [python 3](https://www.python.org/downloads/) ### install icalendar The script depends on the [icalendar package](https://pypi.org/project/icalendar/) From e886c484128f6c996f9cc2faae3b26a1c3157050 Mon Sep 17 00:00:00 2001 From: "S.H" Date: Tue, 26 Mar 2024 11:38:36 +0100 Subject: [PATCH 03/10] Updated README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c5e4a79..75ce4df 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,8 @@ You can download as many calendars as you want. ## advantages of this fork This fork has the following changes: -* ported to Pythos 3 -* added the actual *year* to the generated calendar entry (this was missing causing errors and eoverlapping entries when used with multiple years) +* ported to Python 3 +* added the actual *year* to the generated calendar entry (this was missing, causing errors and overlapping entries when used with multiple years) ## Preconditions From 9a554ef5503a8017a3bb23966405130db5737121 Mon Sep 17 00:00:00 2001 From: sheilbronn <36453088+sheilbronn@users.noreply.github.com> Date: Tue, 26 Mar 2024 11:41:16 +0100 Subject: [PATCH 04/10] Create pylint.yml --- .github/workflows/pylint.yml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 .github/workflows/pylint.yml diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml new file mode 100644 index 0000000..383e65c --- /dev/null +++ b/.github/workflows/pylint.yml @@ -0,0 +1,23 @@ +name: Pylint + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.8", "3.9", "3.10"] + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pylint + - name: Analysing the code with pylint + run: | + pylint $(git ls-files '*.py') From abf46571569d3639a5e8301997796cc0dec89863 Mon Sep 17 00:00:00 2001 From: "S.H" Date: Tue, 26 Mar 2024 18:22:45 +0100 Subject: [PATCH 05/10] Added a sample input file for testing Test file: holiday_sample.ics added --- holiday_sample.ics | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 holiday_sample.ics diff --git a/holiday_sample.ics b/holiday_sample.ics new file mode 100644 index 0000000..52304a2 --- /dev/null +++ b/holiday_sample.ics @@ -0,0 +1,19 @@ +BEGIN:VCALENDAR +VERSION:2.0 +METHOD:PUBLISH +PRODID:-//example.org//Test file//DE +BEGIN:VEVENT +DTSTAMP:20770313T032001Z +SUMMARY:Test Herbstferien 2088 Bayern +DTSTART;VALUE=DATE:20881119 +DTEND;VALUE=DATE:20881120 +TRANSP:TRANSPARENT +END:VEVENT +BEGIN:VEVENT +DTSTAMP:20770313T032001Z +SUMMARY:Test Weihnachtsferien 2088 Bayern +DTSTART;VALUE=DATE:20881222 +DTEND;VALUE=DATE:20890106 +TRANSP:TRANSPARENT +END:VEVENT +END:VCALENDAR From 5d2414215103228a8ed6777727ada98db96ae9bd Mon Sep 17 00:00:00 2001 From: "S.H" Date: Tue, 26 Mar 2024 18:33:06 +0100 Subject: [PATCH 06/10] Fixes for some pylint suggestions --- ephemeris.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/ephemeris.py b/ephemeris.py index 649bf34..3ea42c7 100644 --- a/ephemeris.py +++ b/ephemeris.py @@ -2,14 +2,13 @@ # see https://pypi.org/project/icalendar/ # get calendars for Germany from https://www.schulferien.org/deutschland/ical/ -from icalendar import Calendar, Event from datetime import date, timedelta, datetime - import calendar -import datetime +# import datetime import glob import argparse import os +from icalendar import Calendar, Event OpenHabConf = "/etc/openhab/" try: @@ -40,7 +39,7 @@ 9 :'SEPTEMBER', 10:'OCTOBER' , 11:'NOVEMBER' , - 12:'DECEMBER'}; + 12:'DECEMBER'} xmlFileContent = """\ @@ -91,7 +90,7 @@ def daterange(start_date, end_date): addString = """\t\t\n""".format( - _month=MONTHS[int(single_date.strftime("%m"))], + _month=MONTHS[int(single_date.strftime("%m"))], _day=single_date.strftime("%d"), _summary=summary, _from=single_date.strftime("%Y"), @@ -102,10 +101,10 @@ def daterange(start_date, end_date): count = count + 1 xmlFileContent += "\t" -xmlFileContent += ""; +xmlFileContent += "" filehandle = open(args.outFile, mode='w') filehandle.write(xmlFileContent) filehandle.close -print("Added %d items to file %s" %(count, args.outFile)) \ No newline at end of file +print("Added %d items to file %s" %(count, args.outFile)) From 29cfba4554d2d69c38812531bd411c8a3a8136b5 Mon Sep 17 00:00:00 2001 From: sheilbronn <36453088+sheilbronn@users.noreply.github.com> Date: Tue, 26 Mar 2024 18:42:11 +0100 Subject: [PATCH 07/10] Update pylint.yml --- .github/workflows/pylint.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index 383e65c..a9dfb25 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ["3.8", "3.9", "3.10"] + python-version: ["3.8", "3.9", "3.10", "3.11"] steps: - uses: actions/checkout@v3 - name: Set up Python ${{ matrix.python-version }} @@ -18,6 +18,7 @@ jobs: run: | python -m pip install --upgrade pip pip install pylint + pip install icalendar - name: Analysing the code with pylint run: | pylint $(git ls-files '*.py') From 81cb6d2cc3dd4d88586e0b1be0ab2062d0a13f74 Mon Sep 17 00:00:00 2001 From: sheilbronn <36453088+sheilbronn@users.noreply.github.com> Date: Tue, 26 Mar 2024 18:51:47 +0100 Subject: [PATCH 08/10] Update pylint.yml with --disable=line-too-long --- .github/workflows/pylint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index a9dfb25..c94856f 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -21,4 +21,4 @@ jobs: pip install icalendar - name: Analysing the code with pylint run: | - pylint $(git ls-files '*.py') + pylint --disable=line-too-long $(git ls-files '*.py') From 5a6faff2530dd54edacd53f297e407699a7f141a Mon Sep 17 00:00:00 2001 From: "S.H" Date: Tue, 26 Mar 2024 19:04:15 +0100 Subject: [PATCH 09/10] Fix more pylint warnings e.g. by partly switching to f-strings and explicitly setting an encoding --- ephemeris.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ephemeris.py b/ephemeris.py index 3ea42c7..28f38eb 100644 --- a/ephemeris.py +++ b/ephemeris.py @@ -55,13 +55,13 @@ def daterange(start_date, end_date): for filename in glob.glob(os.path.join(args.inPath, FileNameFilter)): if args.verbose > 0: - print ("parsing file {0}".format(filename)) + print (f"parsing file {filename}") file = open(filename, 'rb') cal = Calendar.from_ical(file.read()) if args.verbose > 1: print (cal) - xmlFileContent += "\t\n".format(file=filename) + xmlFileContent += f"\t\n" for component in cal.walk(): if component.name == "VEVENT": @@ -75,11 +75,11 @@ def daterange(start_date, end_date): reoccur = component.get('rrule').to_ical().decode('utf-8') for item in parse_recurrences(reoccur, startdt, exdate): if args.verbose > 0: - print("\t{0} {1}: {2} - {3}".format(item, summary, description, location)) + print(f"\t{item} {summary}: {description} - {location}") else: if args.verbose > 0: - print("\t{0}-{1} {2}: {3} - {4}".format(startdt.strftime("%D %H:%M UTC"), enddt.strftime("%D %H:%M UTC"), summary, description, location)) - xmlFileContent += "\t\t\n".format(_summary=summary) + print( f"\t{0}-{1} {2}: {3} - {4}".format(startdt.strftime("%D %H:%M UTC"), enddt.strftime("%D %H:%M UTC"), summary, description, location)) + xmlFileContent += f"\t\t\n" for single_date in daterange(startdt, enddt): if args.verbose > 0: print (single_date.strftime("%Y-%m-%d")) @@ -87,7 +87,7 @@ def daterange(start_date, end_date): print (int(single_date.strftime("%m"))) month = MONTHS[int(single_date.strftime("%m"))] print (month) - addString = """\t\t\n""".format( _month=MONTHS[int(single_date.strftime("%m"))], @@ -103,7 +103,7 @@ def daterange(start_date, end_date): xmlFileContent += "\t" xmlFileContent += "" -filehandle = open(args.outFile, mode='w') +filehandle = open(args.outFile, mode='w', encoding='utf-8') filehandle.write(xmlFileContent) filehandle.close From 851999232ead4e7a244b5b0794c7b75c64fb47d5 Mon Sep 17 00:00:00 2001 From: "S.H" Date: Wed, 27 Mar 2024 00:10:27 +0100 Subject: [PATCH 10/10] No functional changes, only reformatting and documentation --- ephemeris.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/ephemeris.py b/ephemeris.py index 28f38eb..ca7c13d 100644 --- a/ephemeris.py +++ b/ephemeris.py @@ -18,11 +18,11 @@ count = 0 -parser = argparse.ArgumentParser(description='convert school holidays ICS files from https://www.schulferien.org/deutschland/ical/') +parser = argparse.ArgumentParser(description='Convert school holidays ICS files, e.g. from https://www.schulferien.org/deutschland/ical/') -parser.add_argument('-v', '--verbose', action='store_const', const=1, default=0 , help='activate logging') -parser.add_argument('-i', '--inPath', default=os.path.join(OpenHabConf, "scripts/") , help='set path where the ics files are') -parser.add_argument('-o', '--outFile', default=os.path.join(OpenHabConf, "services/holidays.xml"), help='set the out file') +parser.add_argument('-v', '--verbose', default=0, action='store_const', const=1 , help='activate logging') +parser.add_argument('-i', '--inPath' , default=os.path.join(OpenHabConf,"scripts/") , help='set path where the ics files are') +parser.add_argument('-o', '--outFile', default=os.path.join(OpenHabConf,"services/holidays.xml"), help='set the out file') args = parser.parse_args() @@ -50,7 +50,14 @@ """ def daterange(start_date, end_date): - for n in range(int ((end_date - start_date).days)): + """ + Generate a range of dates between the start_date and end_date. Args: + start_date (datetime.date): The start date of the range. + end_date (datetime.date): The end date of the range. + Yields: + datetime.date: The dates in the range. + """ + for n in range(int((end_date - start_date).days)): yield start_date + timedelta(n) for filename in glob.glob(os.path.join(args.inPath, FileNameFilter)):