Skip to content

Commit

Permalink
pylint fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Schrotti <[email protected]>
  • Loading branch information
Rosi2143 committed Aug 26, 2024
1 parent 2e1dd99 commit 4914e17
Showing 1 changed file with 46 additions and 31 deletions.
77 changes: 46 additions & 31 deletions ephemeris.py
Original file line number Diff line number Diff line change
@@ -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/"
)
Expand All @@ -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",
Expand All @@ -55,7 +55,7 @@
12: "DECEMBER",
}

xmlFileContent = """\
XML_FILE_CONTENT = """\
<?xml version="1.0" encoding="UTF-8"?>
<tns:Configuration hierarchy="de" description="Germany"
\txmlns:tns="http://www.example.org/Holiday" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand All @@ -76,15 +76,37 @@ def daterange(start_date, end_date):
yield start_date + timedelta(n)


for filename in glob.glob(os.path.join(args.inPath, FileNameFilter)):
def parse_recurrences(recur_rule, start, exclusions):
"""Find all reoccuring events"""
rules = rruleset()
first_rule = rrulestr(recur_rule, dtstart=start)
rules.rrule(first_rule)
if not isinstance(exclusions, list):
exclusions = [exclusions]
for xdate in exclusions:
try:
rules.exdate(xdate.dts[0].dt)
except AttributeError:
pass
now = datetime.now(timezone.utc)
this_year = now + timedelta(days=60)
dates = []
for rule in rules.between(now, this_year):
dates.append(rule.strftime("%D %H:%M UTC "))
return dates


COUNT = 0

for filename in glob.glob(os.path.join(args.inPath, FILE_NAME_FILTER)):
if args.verbose > 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<!-- filename = {filename} -->\n"
XML_FILE_CONTENT += f"\t<!-- filename = {filename} -->\n"

for component in cal.walk():
if component.name == "VEVENT":
Expand All @@ -110,7 +132,7 @@ def daterange(start_date, end_date):
location,
)
)
xmlFileContent += f"\t\t<!-- reason = {summary} -->\n"
XML_FILE_CONTENT += f"\t\t<!-- reason = {summary} -->\n"
for single_date in daterange(startdt, enddt):
if args.verbose > 0:
print(single_date.strftime("%Y-%m-%d"))
Expand All @@ -120,23 +142,16 @@ def daterange(start_date, end_date):
print(month)
addString = f"""\t\t<tns:Fixed month=\"{month}\" """
print(addString)
addString = """\t\t<tns:Fixed month=\"{_month}\" day=\"{_day}\" descriptionPropertiesKey=\"{_summary}\" validFrom=\"{_from}\" validTo=\"{_to}\" />\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<tns:Fixed month=\"{MONTHS[int(single_date.strftime('%m'))]}\" day=\"{single_date.strftime('%d')}\" descriptionPropertiesKey=\"{summary}\" validFrom=\"{single_date.strftime('%Y')}\" validTo=\"{single_date.strftime('%Y')}\" />\n"
if args.verbose > 0:
print(addString)
xmlFileContent += addString
count = count + 1
XML_FILE_CONTENT += addString
COUNT += 1

xmlFileContent += "\t</tns:Holidays>"
xmlFileContent += "</tns:Configuration>"
XML_FILE_CONTENT += "\t</tns:Holidays>"
XML_FILE_CONTENT += "</tns:Configuration>"

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}")

0 comments on commit 4914e17

Please sign in to comment.