From 35616212ff64feec7fa2fb204e85c7be4b20e2ff Mon Sep 17 00:00:00 2001 From: Krateng Date: Fri, 4 Sep 2020 13:59:04 +0200 Subject: [PATCH] Various fixes --- maloja/database.py | 2 +- maloja/malojatime.py | 112 +++++++++++++++++++++++-------------- maloja/utilities/images.py | 1 + 3 files changed, 72 insertions(+), 43 deletions(-) diff --git a/maloja/database.py b/maloja/database.py index f3daa39..574dd71 100644 --- a/maloja/database.py +++ b/maloja/database.py @@ -1006,7 +1006,7 @@ def add_picture(b64,artist:Multi=[],title=None): for a in artist: keys.append("artist",a) if title is not None: keys.append("title",title) - k_filter, _, _, _ = uri_to_internal(keys) + k_filter, _, _, _, _ = uri_to_internal(keys) if "track" in k_filter: k_filter = k_filter["track"] utilities.set_image(b64,**k_filter) diff --git a/maloja/malojatime.py b/maloja/malojatime.py index 99bae93..6307e21 100644 --- a/maloja/malojatime.py +++ b/maloja/malojatime.py @@ -1,4 +1,5 @@ import datetime +from datetime import datetime as dtm from calendar import monthrange from os.path import commonprefix import math @@ -377,6 +378,27 @@ y = MTime(2020) +def today(): + tod = datetime.datetime.utcnow() + return MTime(tod.year,tod.month,tod.day) +def thisweek(): + tod = datetime.datetime.utcnow() + tod = datetime.date(tod.year,tod.month,tod.day) + y,w,_ = tod.chrcalendar() + return MTimeWeek(y,w) +def thismonth(): + tod = datetime.datetime.utcnow() + return MTime(tod.year,tod.month) +def thisyear(): + tod = datetime.datetime.utcnow() + return MTime(tod.year) +def alltime(): + return MRange(None,None) + + + + + def range_desc(r,**kwargs): if r is None: return "" return r.desc(**kwargs) @@ -385,37 +407,58 @@ def time_str(t): obj = time_fix(t) return obj.desc() + +currenttime_string_representations = ( + (today,["today","day"]), + (thisweek,["week","thisweek"]), + (thismonth,["month","thismonth"]), + (thisyear,["year","thisyear"]), + (lambda:None,["alltime"]) +) +month_string_representations = ( + ["january","jan"], + ["february","feb"], + ["march","mar"], + ["april","apr"], + ["may"], + ["june","jun"], + ["july","jul"], + ["august","aug"], + ["september","sep"], + ["october","oct"], + ["november","nov"], + ["december","dec"], +) +weekday_string_representations = ( + ["sunday","sun"], + ["monday","mon"], + ["tuesday","tue"], + ["wednesday","wed"], + ["thursday","thu"], + ["friday","fri"], + ["saturday","sat"] +) + +def get_last_instance(category,current,target,amount): + offset = (target-current) % -(amount) + return category().next(offset) + +str_to_time_range = { + **{s:callable for callable,strlist in currenttime_string_representations for s in strlist}, + **{s:(lambda i=index:get_last_instance(thismonth,dtm.utcnow().month,i,12)) for index,strlist in enumerate(month_string_representations,1) for s in strlist}, + **{s:(lambda i=index:get_last_instance(today,dtm.utcnow().isoweekday()+1%7,i,7)) for index,strlist in enumerate(weekday_string_representations,1) for s in strlist} +} + + # converts strings and stuff to objects def time_fix(t): - if t is None: return None - if isinstance(t,MRangeDescriptor): return t + if t is None or isinstance(t,MRangeDescriptor): return t if isinstance(t, str): - if t in ["alltime"]: return None - tod = datetime.datetime.utcnow() - months = ["january","february","march","april","may","june","july","august","september","october","november","december"] - weekdays = ["sunday","monday","tuesday","wednesday","thursday","friday","saturday"] + t = t.lower() - if t.lower() in ["today","day"]: - return today() - elif t.lower() in ["month","thismonth"]: - return thismonth() - elif t.lower() in ["year","thisyear"]: - return thisyear() - elif t.lower() in ["week","thisweek"]: - return thisweek() - - - elif t.lower() in months: - #diff = (tod.month - months.index(t.lower()) - 1) - month = months.index(t.lower()) + 1 - t = [tod.year,month] - if month > tod.month: t[0] -= 1 - elif t.lower() in weekdays: - weekday = weekdays.index(t.lower()) - diff = (tod.isoweekday() - weekday) % 7 - dt = tod - datetime.timedelta(diff) - t = [dt.year,dt.month,dt.day] + if t in str_to_time_range: + return str_to_time_range[t]() if isinstance(t,str): t = t.split("/") #if isinstance(t,tuple): t = list(t) @@ -608,22 +651,7 @@ def ranges(since=None,to=None,within=None,timerange=None,step="month",stepn=1,tr -def today(): - tod = datetime.datetime.utcnow() - return MTime(tod.year,tod.month,tod.day) -def thisweek(): - tod = datetime.datetime.utcnow() - tod = datetime.date(tod.year,tod.month,tod.day) - y,w,_ = tod.chrcalendar() - return MTimeWeek(y,w) -def thismonth(): - tod = datetime.datetime.utcnow() - return MTime(tod.year,tod.month) -def thisyear(): - tod = datetime.datetime.utcnow() - return MTime(tod.year) -def alltime(): - return MRange(None,None) + #def _get_start_of(timestamp,unit): # date = datetime.datetime.utcfromtimestamp(timestamp) diff --git a/maloja/utilities/images.py b/maloja/utilities/images.py index 8e5f9b0..9543710 100644 --- a/maloja/utilities/images.py +++ b/maloja/utilities/images.py @@ -12,6 +12,7 @@ import random import base64 from threading import Thread, Timer import re +import datetime if globalconf.USE_THUMBOR: