-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·144 lines (117 loc) · 3.79 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python3
"""
Making a simple date offset calculator for calendar of your life apps
"""
from __future__ import annotations
import json
import os
import string
import sys
from argparse import ArgumentParser
from datetime import date
from enum import StrEnum
from pathlib import Path
from typing import List, Optional, Union
from dateutil.relativedelta import relativedelta
from pydantic import BaseModel
WEEKS_PER_YEAR = 52
TOTAL_YEARS = 100
BASE64 = string.ascii_uppercase + string.ascii_lowercase + string.digits + '+/'
ANSI_BOLD = "\033[1m"
ANSI_INVERT = "\033[7m"
ANSI_RESET = "\033[0m"
class Color(StrEnum):
BLACK = 'black'
RED = 'red'
GREEN = 'green'
YELLOW = 'yellow'
BLUE = 'blue'
MAGENTA = 'magenta'
CYAN = 'cyan'
GRAY = 'gray'
COLOR_MAP = {
Color.BLACK: "\033[0;30m",
Color.RED: "\033[0;31m",
Color.GREEN: "\033[0;32m",
Color.YELLOW: "\033[0;33m",
Color.BLUE: "\033[0;34m",
Color.MAGENTA: "\033[0;35m",
Color.CYAN: "\033[0;36m",
Color.GRAY: "\033[0;37m",
}
class DateRange(BaseModel):
start: date
end: date
@property
def duration(self) -> int:
return (self.end - self.start).days
class LifeEvent(DateRange):
name: str
color: Color
class LifeCalendar(BaseModel):
name: str
birthday: date
events: List[LifeEvent]
@classmethod
def from_json(cls, calendar_path: Path) -> LifeCalendar:
with open(calendar_path, 'r') as calendar_file:
calendar_data = json.load(calendar_file)
return cls(**calendar_data)
def __getitem__(self, week: DateRange) -> Optional[LifeEvent]:
life_event = None
# smaller events have a higher priority on the calendar!
for event in reversed(sorted(self.events, key=lambda x: x.duration)):
# fully bounded
if event.start >= week.start and event.end <= week.end:
return event
# out of bounds cases
elif week.start > event.end or week.end < event.start:
continue
# partial intersection
elif week.start >= event.start and week.start <= event.end:
life_event = event
elif week.end >= event.start and week.end <= event.end:
life_event = event
return life_event
def render_calendar(calendar: LifeCalendar):
PADDING = " "*6
print(f"\n{PADDING}{ANSI_BOLD}{calendar.name}{ANSI_RESET}")
print(PADDING + "-"*WEEKS_PER_YEAR)
for year_idx in range(TOTAL_YEARS):
year_start = None
year_end = None
print(f"{year_idx+1:3} : ", end='')
for week_idx in range(WEEKS_PER_YEAR):
week_start = (
calendar.birthday + \
relativedelta(years=year_idx, weeks=week_idx)
)
week_end = (
calendar.birthday + \
relativedelta(years=year_idx, weeks=week_idx+1)
)
if year_start is None:
year_start = week_start
year_end = week_end
week = DateRange(start=week_start, end=week_end)
event = calendar[week]
display_char = '.'
if event:
base64_char = BASE64[week_idx]
display_char = (
COLOR_MAP[event.color] +
ANSI_INVERT +
ANSI_BOLD +
base64_char +
ANSI_RESET
)
print(display_char, end='')
print(f" [{year_start}, {year_end}] // Age {year_idx}")
def main() -> None:
parser = ArgumentParser()
parser.add_argument('calendar_path', type=Path)
args = parser.parse_args()
calendar = LifeCalendar.from_json(args.calendar_path)
render_calendar(calendar)
if __name__ == "__main__":
main()