diff --git a/ephemeris.py b/ephemeris.py index e5723a7..a9e74d2 100755 --- a/ephemeris.py +++ b/ephemeris.py @@ -1,21 +1,21 @@ #!/usr/bin/python3 -# see https://pypi.org/project/icalendar/ -# get calendars for Germany from https://www.schulferien.org/deutschland/ical/ +""" see https://pypi.org/project/icalendar/ +get calendars for Germany from https://www.schulferien.org/deutschland/ical/ +""" -from datetime import timedelta +from datetime import timedelta, timezone, datetime import glob import argparse import os from icalendar import Calendar +from dateutil.rrule import rruleset, rrulestr -OpenHabConf = "/etc/openhab/" +OPENHAB_CONF = "/etc/openhab/" try: - OpenHabConf = os.environ["OPENHAB_CONF"] + OPENHAB_CONF = os.environ["OPENHAB_CONF"] except KeyError: print("No variable OPENHAB_CONF found") -count = 0 - parser = argparse.ArgumentParser( description="Convert school holidays ICS files, e.g. from https://www.schulferien.org/deutschland/ical/" ) @@ -26,19 +26,19 @@ parser.add_argument( "-i", "--inPath", - default=os.path.join(OpenHabConf, "scripts/"), + default=os.path.join(OPENHAB_CONF, "scripts/"), help="set path where the ics files are", ) parser.add_argument( "-o", "--outFile", - default=os.path.join(OpenHabConf, "services/holidays.xml"), + default=os.path.join(OPENHAB_CONF, "services/holidays.xml"), help="set the out file", ) args = parser.parse_args() -FileNameFilter = "*.ics" +FILE_NAME_FILTER = "*.ics" MONTHS = { 1: "JANUARY", @@ -55,7 +55,7 @@ 12: "DECEMBER", } -xmlFileContent = """\ +XML_FILE_CONTENT = """\ 0: print(f"parsing file {filename}") - file = open(filename, "rb") - cal = Calendar.from_ical(file.read()) + with open(filename, "rb") as ics_file: + cal = Calendar.from_ical(ics_file.read()) if args.verbose > 1: print(cal) - xmlFileContent += f"\t\n" + XML_FILE_CONTENT += f"\t\n" for component in cal.walk(): if component.name == "VEVENT": @@ -110,7 +132,7 @@ def daterange(start_date, end_date): location, ) ) - xmlFileContent += f"\t\t\n" + XML_FILE_CONTENT += f"\t\t\n" for single_date in daterange(startdt, enddt): if args.verbose > 0: print(single_date.strftime("%Y-%m-%d")) @@ -120,23 +142,16 @@ def daterange(start_date, end_date): print(month) addString = f"""\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"), - ) + addString = f"\t\t\n" if args.verbose > 0: print(addString) - xmlFileContent += addString - count = count + 1 + XML_FILE_CONTENT += addString + COUNT += 1 -xmlFileContent += "\t" -xmlFileContent += "" +XML_FILE_CONTENT += "\t" +XML_FILE_CONTENT += "" -filehandle = open(args.outFile, mode="w", encoding="utf-8") -filehandle.write(xmlFileContent) -filehandle.close +with open(args.outFile, mode="w", encoding="utf-8") as out_file: + out_file.write(XML_FILE_CONTENT) -print("Added %d items to file %s" % (count, args.outFile)) +print(f"Added {COUNT} items to file {args.outFile}")