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

Complete rework of time descriptors, A New Hope

This commit is contained in:
Krateng 2019-04-11 10:49:56 +02:00
parent 935691dd58
commit c9c53aa592
3 changed files with 29 additions and 15 deletions

View File

@ -5,6 +5,7 @@ from malojatime import *
from urihandler import compose_querystring, internal_to_uri, uri_to_internal
import urllib
import datetime
import math
#def getpictures(ls,result,tracks=False):
@ -602,6 +603,12 @@ def module_filterselection(keys,time=True,delimit=False):
html += "<span class='stat_selector' style='opacity:0.5;'>Long Trailing</span>"
else:
html += "<a href='?" + compose_querystring(unchangedkeys,unchangedkeys_sub,{"trail":"3"}) + "'><span class='stat_selector'>Long Trailing</span></a>"
html += " | "
if delimitkeys.get("trail") == math.inf:
html += "<span class='stat_selector' style='opacity:0.5;'>Cumulative</span>"
else:
html += "<a href='?" + compose_querystring(unchangedkeys,unchangedkeys_sub,{"cumulative":"yes"}) + "'><span class='stat_selector'>Cumulative</span></a>"
html += "</div>"

View File

@ -1,6 +1,7 @@
import datetime
from calendar import monthrange
from os.path import commonprefix
import math
FIRST_SCROBBLE = int(datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).timestamp())
@ -173,6 +174,7 @@ class MTime(MRangeDescriptor):
# next range of equal length (not exactly same amount of days, but same precision level)
def next(self,step=1):
if abs(step) == math.inf: return None
if self.precision == 1:
return MTime(self.year + step)
elif self.precision == 2:
@ -252,6 +254,7 @@ class MTimeWeek(MRangeDescriptor):
return int(datetime.datetime.combine(day,datetime.time(tzinfo=datetime.timezone.utc)).timestamp() - 1)
def next(self,step=1):
if abs(step) == math.inf: return None
try:
return MTimeWeek(self.year,self.week + step)
except:
@ -265,7 +268,8 @@ class MRange(MRangeDescriptor):
since,to = time_pad(since,to)
self.since = since
self.to = to
if isinstance(self.since,MRange): self.since = self.since.start()
if isinstance(self.to,MRange): self.to = self.to.end()
def __str__(self):
return str(self.since) + " - " + str(self.to)
@ -319,6 +323,7 @@ class MRange(MRangeDescriptor):
else: return self.to.last_stamp()
def next(self,step=1):
if abs(step) == math.inf: return None
# hop from the start element by one until we reach the end element
diff = 1
nxt = self.since
@ -512,7 +517,8 @@ def delimit_desc(step="month",stepn=1,trail=1):
if stepn is not 1: txt += _num(stepn) + "-"
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 += " Trailing" #we don't need all the info in the title
if trail is math.inf: txt += " Cumulative"
elif trail is not 1: txt += " Trailing" #we don't need all the info in the title
return txt
@ -547,19 +553,17 @@ def ranges(since=None,to=None,within=None,timerange=None,step="month",stepn=1,tr
(firstincluded,lastincluded) = time_stamps(since=since,to=to,within=within,range=timerange)
d_start = from_timestamp(firstincluded,step)
d_start = d_start.next(stepn)
d_start = d_start.next(stepn*trail*-1)
first_range = MRange(d_start,d_start.next(stepn*trail-1))
d_start = d_start.next(stepn-1) #last part of first included range
i = 0
current = d_start
while current.first_stamp() <= lastincluded and (max_ is None or i < max_):
current_end = current.next(stepn*trail-1)
if current == current_end:
yield current
current_end = d_start
while current_end.first_stamp() <= lastincluded and (max_ is None or i < max_):
current_start = current_end.next((stepn*trail-1)*-1)
if current_start == current_end:
yield current_start
else:
yield MRange(current,current_end)
current = current.next(stepn)
yield MRange(current_start,current_end)
current_end = current_end.next(stepn)
i += 1

View File

@ -1,7 +1,7 @@
import urllib
from bottle import FormsDict
from malojatime import time_fix, time_str, get_range_object
import math
# necessary because urllib.parse.urlencode doesnt handle multidicts
def compose_querystring(*dicts,exclude=[]):
@ -91,7 +91,6 @@ def uri_to_internal(keys,forceTrack=False,forceArtist=False):
elif "within" in keys: within = keys.get("within")
elif "during" in keys: within = keys.get("during")
resultkeys2["timerange"] = get_range_object(since=since,to=to,within=within)
print(resultkeys2["timerange"].desc())
#3
resultkeys3 = {"step":"month","stepn":1,"trail":1}
@ -99,6 +98,7 @@ def uri_to_internal(keys,forceTrack=False,forceArtist=False):
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 "trail" in keys: resultkeys3["trail"] = int(keys["trail"])
if "cumulative" in keys: resultkeys3["trail"] = math.inf
@ -140,7 +140,10 @@ def internal_to_uri(keys):
if "stepn" in keys:
urikeys.append("stepn",str(keys["stepn"]))
if "trail" in keys:
urikeys.append("trail",str(keys["trail"]))
if keys["trail"] == math.inf:
urikeys.append("cumulative","yes")
else:
urikeys.append("trail",str(keys["trail"]))
# stuff
if "max_" in keys: