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 19:06:57 +00:00
|
|
|
from typing import List, 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],
|
2022-04-24 19:06:57 +00:00
|
|
|
ignore_names: List[str] = [],
|
2022-04-24 18:24:34 +00:00
|
|
|
) -> 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()
|
2022-06-12 00:53:41 +00:00
|
|
|
for ((start, end), (place, _place_id), (name, description, language)) in events:
|
2022-04-24 19:06:57 +00:00
|
|
|
if name.lower() not in ignore_names:
|
|
|
|
ev = json.dumps((int(start.timestamp()), int(end.timestamp()),
|
|
|
|
place, name, description, language))
|
|
|
|
evh = hashlib.md5(ev.encode('ascii')).hexdigest()
|
|
|
|
start = timezone.localize(start)
|
|
|
|
end = timezone.localize(end)
|
|
|
|
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
|
|
|
|
|
2022-06-12 00:53:41 +00:00
|
|
|
def multi_decomposed_args(
|
|
|
|
ml_merging_strategy: bool,
|
|
|
|
ml_merging_sets: List[List[str]],
|
|
|
|
ml_ignore: List[str],
|
|
|
|
ml_year: int,
|
|
|
|
ml_month: int,
|
|
|
|
ml_day: int,
|
|
|
|
ml_tzcode: str,
|
|
|
|
en_origin: Source,
|
|
|
|
en_url: str,
|
|
|
|
en_format: Format,
|
|
|
|
en_axis: AxisMeaning,
|
|
|
|
en_lang: str,
|
|
|
|
en_content: ContentMeaning,
|
|
|
|
es_origin: Source,
|
|
|
|
es_url: str,
|
|
|
|
es_format: Format,
|
|
|
|
es_axis: AxisMeaning,
|
|
|
|
es_lang: str,
|
|
|
|
es_content: ContentMeaning,
|
|
|
|
pt_origin: Source,
|
|
|
|
pt_url: str,
|
|
|
|
pt_format: Format,
|
|
|
|
pt_axis: AxisMeaning,
|
|
|
|
pt_lang: str,
|
|
|
|
pt_content: ContentMeaning,
|
|
|
|
) -> List[dict]:
|
|
|
|
timezone = (into_timezone(ml_tzcode)
|
|
|
|
if isinstance(ml_tzcode, str) else ml_tzcode)
|
|
|
|
en_table = en_format[en_origin[en_url]].parse(en_axis == AxisMeaning.place)
|
|
|
|
en_table.interpret_lang('EN,ES,PT'.split(','), en_lang, en_content.as_index())
|
|
|
|
en_table.interpret_line_labels_as_time_series((ml_year, ml_month, ml_day))
|
|
|
|
es_table = es_format[es_origin[es_url]].parse(es_axis == AxisMeaning.place)
|
|
|
|
es_table.interpret_lang('EN,ES,PT'.split(','), es_lang, es_content.as_index())
|
|
|
|
es_table.interpret_line_labels_as_time_series((ml_year, ml_month, ml_day))
|
|
|
|
pt_table = pt_format[pt_origin[pt_url]].parse(pt_axis == AxisMeaning.place)
|
|
|
|
pt_table.interpret_lang('EN,ES,PT'.split(','), pt_lang, pt_content.as_index())
|
|
|
|
pt_table.interpret_line_labels_as_time_series((ml_year, ml_month, ml_day))
|
|
|
|
en_events = en_table.get_column_sequences()
|
|
|
|
es_events = es_table.get_column_sequences()
|
|
|
|
pt_events = pt_table.get_column_sequences()
|
|
|
|
ml_events: List[dict] = list()
|
|
|
|
for i, (en_event, es_event, pt_event) in enumerate(zip(en_events, es_events, pt_events)):
|
|
|
|
print(en_event)
|
|
|
|
print(es_event)
|
|
|
|
print(pt_event)
|
|
|
|
((en_start, en_end), (en_place, en_place_id), (en_name, en_description, en_language)) = en_event
|
|
|
|
((es_start, es_end), (es_place, es_place_id), (es_name, es_description, es_language)) = es_event
|
|
|
|
((pt_start, pt_end), (pt_place, pt_place_id), (pt_name, pt_description, pt_language)) = pt_event
|
|
|
|
if en_name in ml_ignore or es_name in ml_ignore or pt_name in ml_ignore:
|
|
|
|
continue
|
|
|
|
if en_start != es_start or en_start != pt_start:
|
|
|
|
raise ValueError(f'Tables differ on event #{i+1}\'s start: {en_start=} {es_start=} {pt_start=}')
|
|
|
|
if en_end != es_end or en_end != pt_end:
|
|
|
|
raise ValueError(f'Tables differ on event #{i+1}\'s end: {en_end=} {es_end=} {pt_end=}')
|
|
|
|
if en_language != es_language or en_language != pt_language:
|
|
|
|
raise ValueError(f'Tables differ on event #{i+1}\'s language: {en_language=} {es_language=} {pt_language=}')
|
|
|
|
if en_place_id != es_place_id or en_place_id != pt_place_id:
|
|
|
|
raise ValueError(f'Tables differ on event #{i+1}\'s place id: {en_place_id=} {es_place_id=} {pt_place_id=}')
|
|
|
|
ml_events.append(dict(
|
|
|
|
start=timezone.localize(en_start).isoformat(),
|
|
|
|
end=timezone.localize(en_end).isoformat(),
|
|
|
|
place=dict(en=en_place, es=es_place, pt=pt_place, id=en_place_id),
|
|
|
|
name=dict(en=en_name, es=es_name, pt=pt_name),
|
|
|
|
description=dict(en=en_description, es=es_description, pt=pt_description),
|
|
|
|
language=en_language,
|
2022-06-12 15:10:34 +00:00
|
|
|
uid=f'{hashlib.md5(f"{timezone.localize(en_start).isoformat()}_{timezone.localize(en_end).isoformat()}_{en_place_id}".encode("ascii")).hexdigest()}@table2ics'
|
2022-06-12 00:53:41 +00:00
|
|
|
))
|
|
|
|
return ml_events
|
|
|
|
|
2022-04-24 18:24:34 +00:00
|
|
|
|
|
|
|
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()))
|