2022-04-24 15:09:38 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
|
2022-04-24 18:24:34 +00:00
|
|
|
import datetime
|
2022-04-24 15:09:38 +00:00
|
|
|
import hashlib
|
|
|
|
import json
|
2022-04-24 18:24:34 +00:00
|
|
|
from typing import Union
|
2022-04-24 15:09:38 +00:00
|
|
|
|
|
|
|
from ics import Calendar, Event
|
|
|
|
|
2022-04-24 18:24:34 +00:00
|
|
|
from .cli import (AxisMeaning, ContentMeaning, Format, Source, into_timezone,
|
|
|
|
parseArguments)
|
2022-04-24 15:09:38 +00:00
|
|
|
|
|
|
|
|
2022-04-24 18:24:34 +00:00
|
|
|
def main_decomposed_args(origin: Source,
|
|
|
|
path: str,
|
|
|
|
format_: Format,
|
|
|
|
line: AxisMeaning,
|
|
|
|
column: AxisMeaning,
|
|
|
|
content: ContentMeaning,
|
|
|
|
year: int,
|
|
|
|
month: int,
|
|
|
|
day: int,
|
|
|
|
lang: str,
|
|
|
|
timezone_: Union[datetime.tzinfo, str],
|
|
|
|
) -> Calendar:
|
|
|
|
timezone = (into_timezone(timezone_)
|
|
|
|
if isinstance(timezone_, str) else timezone_)
|
|
|
|
if line == column:
|
|
|
|
raise ValueError('Line and column meanings must be different')
|
|
|
|
should_transpose = column.value == 'time'
|
|
|
|
table = format_[origin[path]].parse(should_transpose)
|
2022-04-24 15:09:38 +00:00
|
|
|
table.interpret_lang(
|
|
|
|
'EN,ES,PT'.split(','),
|
2022-04-24 18:24:34 +00:00
|
|
|
lang,
|
|
|
|
content.as_index()
|
2022-04-24 15:09:38 +00:00
|
|
|
)
|
2022-04-24 18:24:34 +00:00
|
|
|
arg_ts = (year, month, day)
|
2022-04-24 15:09:38 +00:00
|
|
|
table.interpret_line_labels_as_time_series(arg_ts)
|
|
|
|
events = table.get_column_sequences()
|
|
|
|
calendar = Calendar()
|
|
|
|
for ((start, end), place, (name, description, language)) in events:
|
|
|
|
ev = json.dumps((int(start.timestamp()), int(end.timestamp()),
|
2022-04-24 18:24:34 +00:00
|
|
|
place, name, description, language))
|
2022-04-24 15:09:38 +00:00
|
|
|
evh = hashlib.md5(ev.encode('ascii')).hexdigest()
|
2022-04-24 18:24:34 +00:00
|
|
|
start = timezone.localize(start)
|
|
|
|
end = timezone.localize(end)
|
2022-04-24 15:09:38 +00:00
|
|
|
calendar_event = Event(name=name, begin=start,
|
|
|
|
end=end, location=place, uid=f'{evh}@table2ics')
|
|
|
|
if description != '':
|
|
|
|
calendar_event.description = description
|
|
|
|
if language != '':
|
|
|
|
calendar_event.classification = language
|
|
|
|
calendar.events.add(calendar_event)
|
2022-04-24 18:24:34 +00:00
|
|
|
return calendar
|
|
|
|
|
|
|
|
|
|
|
|
def main_namespace_args(args) -> Calendar:
|
|
|
|
return main_decomposed_args(
|
|
|
|
args.origin,
|
|
|
|
args.path,
|
|
|
|
args.format,
|
|
|
|
args.line,
|
|
|
|
args.column,
|
|
|
|
args.content,
|
|
|
|
args.year,
|
|
|
|
args.month,
|
|
|
|
args.day,
|
|
|
|
args.lang,
|
|
|
|
args.timezone,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
print(main_namespace_args(parseArguments()))
|