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):
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 = []
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):
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)
@ -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 += "</div>"
now = datetime.datetime.utcnow()
today = [now.year,now.month,now.day]
thismonth = today[:2]
thisyear = thismonth[:1]
from malojatime import today, thisweek, thismonth, thisyear
### temp!!! this will not allow weekly rank changes
weekday = ((now.isoweekday()) % 7)
weekbegin = now - datetime.timedelta(days=weekday)
weekend = weekbegin + datetime.timedelta(days=6)
weekbegin = [weekbegin.year,weekbegin.month,weekbegin.day]
weekend = [weekend.year,weekend.month,weekend.day]
weekbeginstr = "/".join((str(num) for num in weekbegin))
weekendstr = "/".join((str(num) for num in weekend))
# weekday = ((now.isoweekday()) % 7)
# weekbegin = now - datetime.timedelta(days=weekday)
# weekend = weekbegin + datetime.timedelta(days=6)
# weekbegin = [weekbegin.year,weekbegin.month,weekbegin.day]
# weekend = [weekend.year,weekend.month,weekend.day]
# weekbeginstr = "/".join((str(num) for num in weekbegin))
# weekendstr = "/".join((str(num) for num in weekend))
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>"
else:
html += "<a href='?" + compose_querystring(unchangedkeys,{"in":"today"}) + "'><span class='stat_selector'>Today</span></a>"
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>"
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 += " | "
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>"
else:
html += "<a href='?" + compose_querystring(unchangedkeys,{"in":"month"}) + "'><span class='stat_selector'>This Month</span></a>"
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>"
else:
html += "<a href='?" + compose_querystring(unchangedkeys,{"in":"year"}) + "'><span class='stat_selector'>This Year</span></a>"
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>"
else:
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 += " | "
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>"
else:
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"})
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>"
else:
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:
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):
return hash((self.first_stamp(),self.last_stamp()))
@ -286,6 +287,8 @@ class MRange(MRangeDescriptor):
return "since " + self.since.desc()
if self.since is None and self.to is not None:
return "until " + self.to.desc()
if self.since is None and self.to is None:
return ""
def informal_desc(self):
# 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):
return str(t)
obj = time_fix(t)
return obj.desc()
# converts strings and stuff to objects
def time_fix(t):
@ -347,11 +354,13 @@ def time_fix(t):
weekdays = ["sunday","monday","tuesday","wednesday","thursday","friday","saturday"]
if t.lower() in ["today","day"]:
t = [tod.year,tod.month,tod.day]
elif t.lower() == "month":
t = [tod.year,tod.month]
elif t.lower() == "year":
t = [tod.year]
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:
@ -421,8 +430,7 @@ def time_pad(f,t,full=False):
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):
txt = ""
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 += " 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)
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 = 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
while current.first_stamp() <= lastincluded and (max_ is None or i < max_):
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)
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):
# date = datetime.datetime.utcfromtimestamp(timestamp)
# if unit == "year":

View File

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

View File

@ -29,7 +29,7 @@ def instructions(keys):
if moreartists != []:
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)