1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00

Complete rework of time descriptors, Electric Boogaloo

This commit is contained in:
Krateng 2019-04-10 17:56:40 +02:00
parent e3a6201b4b
commit a36f0c4ab9
5 changed files with 70 additions and 40 deletions

View File

@ -382,7 +382,7 @@ def get_pulse_external():
def get_pulse(**keys): def get_pulse(**keys):
rngs = ranges(**{k:keys[k] for k in keys if k in ["since","to","within","step","stepn","trail"]}) rngs = ranges(**{k:keys[k] for k in keys if k in ["timerange","step","stepn","trail"]})
results = [] results = []
for rng in rngs: for rng in rngs:

View File

@ -64,7 +64,7 @@ def module_scrobblelist(max_=None,pictures=False,shortTimeDesc=False,earlystop=F
def module_pulse(max_=None,**kwargs): def module_pulse(max_=None,**kwargs):
kwargs_filter = pickKeys(kwargs,"artist","track","associated") kwargs_filter = pickKeys(kwargs,"artist","track","associated")
kwargs_time = pickKeys(kwargs,"since","to","within","step","stepn","trail") kwargs_time = pickKeys(kwargs,"timerange","step","stepn","trail")
ranges = database.get_pulse(**kwargs_time,**kwargs_filter) ranges = database.get_pulse(**kwargs_time,**kwargs_filter)
@ -499,47 +499,44 @@ def module_filterselection(keys,time=True,delimit=False):
# html += "to <input id='dateselect_to' onchange='datechange()' type='date' value='" + "-".join(todate) + "'/>" # html += "to <input id='dateselect_to' onchange='datechange()' type='date' value='" + "-".join(todate) + "'/>"
# html += "</div>" # html += "</div>"
from malojatime import today, thisweek, thismonth, thisyear
now = datetime.datetime.utcnow()
today = [now.year,now.month,now.day]
thismonth = today[:2]
thisyear = thismonth[:1]
### temp!!! this will not allow weekly rank changes ### temp!!! this will not allow weekly rank changes
weekday = ((now.isoweekday()) % 7) # weekday = ((now.isoweekday()) % 7)
weekbegin = now - datetime.timedelta(days=weekday) # weekbegin = now - datetime.timedelta(days=weekday)
weekend = weekbegin + datetime.timedelta(days=6) # weekend = weekbegin + datetime.timedelta(days=6)
weekbegin = [weekbegin.year,weekbegin.month,weekbegin.day] # weekbegin = [weekbegin.year,weekbegin.month,weekbegin.day]
weekend = [weekend.year,weekend.month,weekend.day] # weekend = [weekend.year,weekend.month,weekend.day]
weekbeginstr = "/".join((str(num) for num in weekbegin)) # weekbeginstr = "/".join((str(num) for num in weekbegin))
weekendstr = "/".join((str(num) for num in weekend)) # weekendstr = "/".join((str(num) for num in weekend))
html += "<div>" html += "<div>"
if timekeys.get("since") == today or timekeys.get("within") == today: if timekeys.get("timerange") == today():
html += "<span class='stat_selector' style='opacity:0.5;'>Today</span>" html += "<span class='stat_selector' style='opacity:0.5;'>Today</span>"
else: else:
html += "<a href='?" + compose_querystring(unchangedkeys,{"in":"today"}) + "'><span class='stat_selector'>Today</span></a>" html += "<a href='?" + compose_querystring(unchangedkeys,{"in":"today"}) + "'><span class='stat_selector'>Today</span></a>"
html += " | " html += " | "
if timekeys.get("since") == weekbegin and timekeys.get("to") == weekend: if timekeys.get("timerange") == thisweek():
html += "<span class='stat_selector' style='opacity:0.5;'>This Week</span>" html += "<span class='stat_selector' style='opacity:0.5;'>This Week</span>"
else: else:
html += "<a href='?" + compose_querystring(unchangedkeys,{"since":weekbeginstr,"to":weekendstr}) + "'><span class='stat_selector'>This Week</span></a>" html += "<a href='?" + compose_querystring(unchangedkeys,{"in":"week"}) + "'><span class='stat_selector'>This Week</span></a>"
html += " | " html += " | "
if timekeys.get("since") == thismonth or timekeys.get("within") == thismonth: if timekeys.get("timerange") == thismonth():
html += "<span class='stat_selector' style='opacity:0.5;'>This Month</span>" html += "<span class='stat_selector' style='opacity:0.5;'>This Month</span>"
else: else:
html += "<a href='?" + compose_querystring(unchangedkeys,{"in":"month"}) + "'><span class='stat_selector'>This Month</span></a>" html += "<a href='?" + compose_querystring(unchangedkeys,{"in":"month"}) + "'><span class='stat_selector'>This Month</span></a>"
html += " | " html += " | "
if timekeys.get("since") == thisyear or timekeys.get("within") == thisyear: if timekeys.get("timerange") == thisyear():
html += "<span class='stat_selector' style='opacity:0.5;'>This Year</span>" html += "<span class='stat_selector' style='opacity:0.5;'>This Year</span>"
else: else:
html += "<a href='?" + compose_querystring(unchangedkeys,{"in":"year"}) + "'><span class='stat_selector'>This Year</span></a>" html += "<a href='?" + compose_querystring(unchangedkeys,{"in":"year"}) + "'><span class='stat_selector'>This Year</span></a>"
html += " | " html += " | "
if timekeys.get("since") is None and timekeys.get("within") is None: if timekeys == {}:
html += "<span class='stat_selector' style='opacity:0.5;'>All Time</span>" html += "<span class='stat_selector' style='opacity:0.5;'>All Time</span>"
else: else:
html += "<a href='?" + compose_querystring(unchangedkeys) + "'><span class='stat_selector'>All Time</span></a>" html += "<a href='?" + compose_querystring(unchangedkeys) + "'><span class='stat_selector'>All Time</span></a>"
@ -561,7 +558,13 @@ def module_filterselection(keys,time=True,delimit=False):
html += "<a href='?" + compose_querystring(unchangedkeys,unchangedkeys_sub,{"step":"day"}) + "'><span class='stat_selector'>Daily</span></a>" html += "<a href='?" + compose_querystring(unchangedkeys,unchangedkeys_sub,{"step":"day"}) + "'><span class='stat_selector'>Daily</span></a>"
html += " | " html += " | "
if (delimitkeys.get("step") == "month" or delimitkeys.get("step") is None) and delimitkeys.get("stepn") == 1: if delimitkeys.get("step") == "week" and delimitkeys.get("stepn") == 1:
html += "<span class='stat_selector' style='opacity:0.5;'>Weekly</span>"
else:
html += "<a href='?" + compose_querystring(unchangedkeys,unchangedkeys_sub,{"step":"week"}) + "'><span class='stat_selector'>Weekly</span></a>"
html += " | "
if delimitkeys.get("step") == "month" and delimitkeys.get("stepn") == 1:
html += "<span class='stat_selector' style='opacity:0.5;'>Monthly</span>" html += "<span class='stat_selector' style='opacity:0.5;'>Monthly</span>"
else: else:
html += "<a href='?" + compose_querystring(unchangedkeys,unchangedkeys_sub,{"step":"month"}) + "'><span class='stat_selector'>Monthly</span></a>" html += "<a href='?" + compose_querystring(unchangedkeys,unchangedkeys_sub,{"step":"month"}) + "'><span class='stat_selector'>Monthly</span></a>"
@ -579,7 +582,7 @@ def module_filterselection(keys,time=True,delimit=False):
unchangedkeys_sub = internal_to_uri({k:delimitkeys[k] for k in delimitkeys if k != "trail"}) unchangedkeys_sub = internal_to_uri({k:delimitkeys[k] for k in delimitkeys if k != "trail"})
html += "<div>" html += "<div>"
if delimitkeys.get("trail") == 1 or delimitkeys.get("trail") is None: if delimitkeys.get("trail") == 1:
html += "<span class='stat_selector' style='opacity:0.5;'>Standard</span>" html += "<span class='stat_selector' style='opacity:0.5;'>Standard</span>"
else: else:
html += "<a href='?" + compose_querystring(unchangedkeys,unchangedkeys_sub,{"trail":"1"}) + "'><span class='stat_selector'>Standard</span></a>" html += "<a href='?" + compose_querystring(unchangedkeys,unchangedkeys_sub,{"trail":"1"}) + "'><span class='stat_selector'>Standard</span></a>"

View File

@ -58,7 +58,8 @@ date = expandeddate
class MRangeDescriptor: class MRangeDescriptor:
def __eq__(self,other): def __eq__(self,other):
return self.first_stamp() == other.first_stamp() and self.last_stamp() == other.last_stamp()# if not isinstance(other,MRangeDescriptor): return False
return (self.first_stamp() == other.first_stamp() and self.last_stamp() == other.last_stamp())
def __hash__(self): def __hash__(self):
return hash((self.first_stamp(),self.last_stamp())) return hash((self.first_stamp(),self.last_stamp()))
@ -286,6 +287,8 @@ class MRange(MRangeDescriptor):
return "since " + self.since.desc() return "since " + self.since.desc()
if self.since is None and self.to is not None: if self.since is None and self.to is not None:
return "until " + self.to.desc() return "until " + self.to.desc()
if self.since is None and self.to is None:
return ""
def informal_desc(self): def informal_desc(self):
# dis gonna be hard # dis gonna be hard
@ -332,9 +335,13 @@ y = MTime(2020)
def range_desc(r,**kwargs):
if r is None: return ""
return r.desc(**kwargs)
def time_str(t): def time_str(t):
return str(t) obj = time_fix(t)
return obj.desc()
# converts strings and stuff to objects # converts strings and stuff to objects
def time_fix(t): def time_fix(t):
@ -347,11 +354,13 @@ def time_fix(t):
weekdays = ["sunday","monday","tuesday","wednesday","thursday","friday","saturday"] weekdays = ["sunday","monday","tuesday","wednesday","thursday","friday","saturday"]
if t.lower() in ["today","day"]: if t.lower() in ["today","day"]:
t = [tod.year,tod.month,tod.day] return today()
elif t.lower() == "month": elif t.lower() in ["month","thismonth"]:
t = [tod.year,tod.month] return thismonth()
elif t.lower() == "year": elif t.lower() in ["year","thisyear"]:
t = [tod.year] return thisyear()
elif t.lower() in ["week","thisweek"]:
return thisweek()
elif t.lower() in months: elif t.lower() in months:
@ -421,8 +430,7 @@ def time_pad(f,t,full=False):
return f,t return f,t
def range_desc(f,t,short=False):
return MRange(time_fix(f),time_fix(t)).desc()
@ -496,7 +504,7 @@ def time_stamps(since=None,to=None,within=None,range=None):
def delimit_desc(step="month",stepn=1,trail=1): def delimit_desc(step="month",stepn=1,trail=1):
txt = "" txt = ""
if stepn is not 1: txt += _num(stepn) + "-" if stepn is not 1: txt += _num(stepn) + "-"
txt += {"year":"Yearly","month":"Monthly","day":"Daily"}[step.lower()] txt += {"year":"Yearly","month":"Monthly","week":"Weekly","day":"Daily"}[step.lower()]
#if trail is not 1: txt += " " + _num(trail) + "-Trailing" #if trail is not 1: txt += " " + _num(trail) + "-Trailing"
if trail is not 1: txt += " Trailing" #we don't need all the info in the title if trail is not 1: txt += " Trailing" #we don't need all the info in the title
@ -527,9 +535,9 @@ def from_timestamp(stamp,unit):
if unit == "year": return year_from_timestamp(stamp) if unit == "year": return year_from_timestamp(stamp)
def ranges(since=None,to=None,within=None,superrange=None,step="month",stepn=1,trail=1,max_=None): def ranges(since=None,to=None,within=None,timerange=None,step="month",stepn=1,trail=1,max_=None):
(firstincluded,lastincluded) = time_stamps(since=since,to=to,within=within,range=superrange) (firstincluded,lastincluded) = time_stamps(since=since,to=to,within=within,range=timerange)
d_start = from_timestamp(firstincluded,step) d_start = from_timestamp(firstincluded,step)
d_start = d_start.next(stepn) d_start = d_start.next(stepn)
@ -540,12 +548,30 @@ def ranges(since=None,to=None,within=None,superrange=None,step="month",stepn=1,t
current = d_start current = d_start
while current.first_stamp() <= lastincluded and (max_ is None or i < max_): while current.first_stamp() <= lastincluded and (max_ is None or i < max_):
current_end = current.next(stepn*trail-1) current_end = current.next(stepn*trail-1)
yield MRange(current,current_end) if current == current_end:
yield current
else:
yield MRange(current,current_end)
current = current.next(stepn) current = current.next(stepn)
i += 1 i += 1
def today():
tod = date.today()
return MTime(tod.year,tod.month,tod.day)
def thisweek():
tod = date.today()
y,w,_ = tod.chrcalendar()
return MTimeWeek(y,w)
def thismonth():
tod = date.today()
return MTime(tod.year,tod.month)
def thisyear():
tod = date.today()
return MTime(tod.year)
#def _get_start_of(timestamp,unit): #def _get_start_of(timestamp,unit):
# date = datetime.datetime.utcfromtimestamp(timestamp) # date = datetime.datetime.utcfromtimestamp(timestamp)
# if unit == "year": # if unit == "year":

View File

@ -39,7 +39,7 @@ def remove_identical(*dicts):
return new return new
# this also sets defaults!
def uri_to_internal(keys,forceTrack=False,forceArtist=False): def uri_to_internal(keys,forceTrack=False,forceArtist=False):
# output: # output:
@ -94,13 +94,14 @@ def uri_to_internal(keys,forceTrack=False,forceArtist=False):
print(resultkeys2["timerange"].desc()) print(resultkeys2["timerange"].desc())
#3 #3
resultkeys3 = {} resultkeys3 = {"step":"month","stepn":1,"trail":1}
if "step" in keys: [resultkeys3["step"],resultkeys3["stepn"]] = (keys["step"].split("-") + [1])[:2] if "step" in keys: [resultkeys3["step"],resultkeys3["stepn"]] = (keys["step"].split("-") + [1])[:2]
if "stepn" in keys: resultkeys3["stepn"] = keys["stepn"] #overwrite if explicitly given if "stepn" in keys: resultkeys3["stepn"] = keys["stepn"] #overwrite if explicitly given
if "stepn" in resultkeys3: resultkeys3["stepn"] = int(resultkeys3["stepn"]) #in both cases, convert it here if "stepn" in resultkeys3: resultkeys3["stepn"] = int(resultkeys3["stepn"]) #in both cases, convert it here
if "trail" in keys: resultkeys3["trail"] = int(keys["trail"]) if "trail" in keys: resultkeys3["trail"] = int(keys["trail"])
#4 #4
resultkeys4 = {} resultkeys4 = {}
if "max" in keys: resultkeys4["max_"] = int(keys["max"]) if "max" in keys: resultkeys4["max_"] = int(keys["max"])
@ -121,8 +122,8 @@ def internal_to_uri(keys):
urikeys.append("title",keys["track"]["title"]) urikeys.append("title",keys["track"]["title"])
#time #time
if "range" in keys: if "timerange" in keys:
keydict = keys["range"].urikeys() keydict = keys["timerange"].urikeys()
for k in keydict: for k in keydict:
urikeys.append(k,keydict[k]) urikeys.append(k,keydict[k])
elif "within" in keys: elif "within" in keys:

View File

@ -29,7 +29,7 @@ def instructions(keys):
if moreartists != []: if moreartists != []:
limitstring += " <span class='extra'>including " + artistLinks(moreartists) + "</span>" limitstring += " <span class='extra'>including " + artistLinks(moreartists) + "</span>"
limitstring += " " + timekeys["timerange"].desc(prefix=True) limitstring += " " + range_desc(timekeys["timerange"],prefix=True)
delimitstring = delimit_desc(**delimitkeys) delimitstring = delimit_desc(**delimitkeys)