This commit is contained in:
Yessiest 2022-01-29 01:02:14 +04:00
parent 9381e246ce
commit 5f7b0f6a13
1 changed files with 59 additions and 6 deletions

65
decal
View File

@ -29,7 +29,7 @@ parser.add_argument("day",
default=today.day, default=today.day,
nargs="?", nargs="?",
type=int) type=int)
parser.add_argument("-v", "--version", parser.add_argument("-V", "--version",
action="version", action="version",
version=Version, version=Version,
help="Display the version of the program") help="Display the version of the program")
@ -57,7 +57,6 @@ parser.add_argument("-n",
help="show n months") help="show n months")
args = vars(parser.parse_args()) args = vars(parser.parse_args())
#check some stuff, do some warnings, initiate the config, etc. #check some stuff, do some warnings, initiate the config, etc.
if not os.path.exists(configpath): if not os.path.exists(configpath):
config['DEFAULT'] = {'uri': 'your caldap server here', config['DEFAULT'] = {'uri': 'your caldap server here',
@ -113,6 +112,7 @@ def gencal(year,month,start_on_sunday=True,cell_modifier=lambda d: d,append_year
lines[-1] += " "*(21-len(lines[-1])) lines[-1] += " "*(21-len(lines[-1]))
return lines return lines
# introduce some generic color names (idk how this will work, ig through parsing summaries? maybe? we'll figure that out ig)
color_names = { color_names = {
"red":"0;31", "red":"0;31",
"green":"0;32", "green":"0;32",
@ -135,6 +135,8 @@ color_names = {
"light purple":"1;36", "light purple":"1;36",
"light cyan":"0;37" "light cyan":"0;37"
} }
# add a function to colorize terminal text
def colorize(text,color): def colorize(text,color):
if color in color_names: if color in color_names:
return "\033["+color_names[color]+"m"+text+"\033[0m" return "\033["+color_names[color]+"m"+text+"\033[0m"
@ -143,9 +145,11 @@ def colorize(text,color):
return color+text+"\033[0m" return color+text+"\033[0m"
#calculate offset in months
def span(year,month,offset): def span(year,month,offset):
return year+((month+offset-1)//12),((month+offset-1)%12)+1 return year+((month+offset-1)//12),((month+offset-1)%12)+1
#get 2 date values - start value to scan from and end value to scan up to.
def getbounds(y,m,offset): def getbounds(y,m,offset):
start = datetime.date(y,m,1) start = datetime.date(y,m,1)
postnextmonth = span(y,m,offset) postnextmonth = span(y,m,offset)
@ -155,37 +159,86 @@ def getbounds(y,m,offset):
(datetime.date(postnextmonth[0],postnextmonth[1],1)-datetime.timedelta(days=1)).day) (datetime.date(postnextmonth[0],postnextmonth[1],1)-datetime.timedelta(days=1)).day)
return start,end return start,end
# we calculate start/end dates and an offset for later use according to arguments
start = None start = None
end = None end = None
offset = None
if args["1"]: if args["1"]:
start,end = getbounds(args["year"],args["month"],1) start,end = getbounds(args["year"],args["month"],1)
offset = 1
elif args["3"]: elif args["3"]:
y,m = span(args["year"],args["month"],-1) y,m = span(args["year"],args["month"],-1)
start,end = getbounds(y,m,3) start,end = getbounds(y,m,3)
offset = 3
elif args["n"]: elif args["n"]:
start,end = getbounds(args["year"],args["month"],args["n"]) start,end = getbounds(args["year"],args["month"],args["n"])
offset = args["n"]
else: else:
start,end = getbounds(args["year"],args["month"],1) start,end = getbounds(args["year"],args["month"],1)
offset = 1
# connect to the DAV and receive calendars
client = caldav.DAVClient(url = config['DEFAULT']['uri'], client = caldav.DAVClient(url = config['DEFAULT']['uri'],
username = config['DEFAULT']['user'], username = config['DEFAULT']['user'],
password = config['DEFAULT']['password']) password = config['DEFAULT']['password'])
principal = client.principal() principal = client.principal()
calendars = principal.calendars() calendars = principal.calendars()
if "calendar" in config['DEFAULT']:
# aggregate selected calendars
if "calendars" in config['DEFAULT']:
calendars2 = [] calendars2 = []
cals = config['DEFAULT']["calendar"].split(",") cals = config['DEFAULT']["calendars"].split(",")
for cal in calendars: for cal in calendars:
if cal.name in cal: if cal.name in cal:
calendars2.append(cal) calendars2.append(cal)
calendars = calendars2 calendars = calendars2
# hooo boy, fun things start here.
# we generate a dict of (year,month) pairs for easier indexing
events = {}
with (args["year"],args["month"]) as startdate:
if args["3"]:
startdate = span(args["year"],args["month"],-1)
for offset in range(offset):
events[span(startdate[0],startdate[1],offset)] = {}
events = [] # next, we iterate over events in each calendar, sorting them the folliwng way:
# events[(int year,int month)][int day] = [event event1, event event2, ...]
# looks awful, is awful, overall i love this.
for calendar in calendars: for calendar in calendars:
events_fetched = calendar.date_search(start,end) events_fetched = calendar.date_search(start,end)
for event in events_fetched: for event in events_fetched:
if not event in events: if not event in events:
events.append(event) event = event.vobject_instance.vevent.contents
curdate = event["dtstart"][0].value + datetime.timedelta(days=-1)
while curdate < event["dtend"][0].value:
curdate += datetime.timedelta(days=1)
curdindex = (curdate.year,curdate.month)
if curdindex in events:
if not curdate.day in events[curdindex]:
events[curdindex][curdate.day] = []
events[curdindex][curdate.day].append(event)
else:
break
# if you're reading the source code for this (oof), feel free to suggest improvements for this, or, well, anything above or below this comment (as long as it's not just "rewrite this entire thing in C++ for me because i think python bad", "idk how but optimize stuff kthx". just don't be a dick, ok? thanks).
# and now we're just generating calendar lines
calendar_prints = []
with (args["year"],args["month"]) as startdate:
if args["3"]:
startdate = span(args["year"],args["month"],-1)
for offset in range(offset):
# a function to colorize cells in a more or less generic way
curdate = gencal(startdate[0],startdate[1],offset)
def lambdafunc(cell):
day = int(cell)
if day in events[(curdate[0],curdate[1])]:
event = events[(curdate[0],curdate[1])][day]
print(event["uid"][0].value)
exit(0)
calendar_prints.append(gencal(curdate[0],
curdate[1],
cell_modifier=lambdafunc))