import re
from colorsys import rgb_to_hls, hls_to_rgb
from cgi import escape as html_escape
-from datetime import datetime
+from datetime import datetime, timedelta
+from decimal import Decimal
+from collections import namedtuple
# Map of special chars to ascii from Darkplace's console.c.
_qfont_table = [
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', '{', '|', '}', '~', '<',
-
+
'<', '=', '>', '#', '#', '.', '#', '#',
'#', '#', ' ', '#', ' ', '>', '.', '.',
'[', ']', '0', '1', '2', '3', '4', '5',
return '<span style="color:rgb(%d,%d,%d)">' % (255 * r, 255 * g, 255 * b)
-def html_colors(qstr=''):
- qstr = html_escape(qfont_decode(qstr).replace('^^', '^'))
+def html_colors(qstr='', limit=None):
+ qstr = html_escape(qfont_decode(qstr))
+ qstr = qstr.replace('^^', '^')
+
+ if limit is not None and limit > 0:
+ qstr = limit_printable_characters(qstr, limit)
+
html = _dec_colors.sub(lambda match: _dec_spans[int(match.group(1))], qstr)
html = _hex_colors.sub(hex_repl, html)
return html + "</span>" * len(_all_colors.findall(qstr))
+def limit_printable_characters(qstr, limit):
+ # initialize assuming all printable characters
+ pc = [1 for i in range(len(qstr))]
+
+ groups = _all_colors.finditer(qstr)
+ for g in groups:
+ pc[g.start():g.end()] = [0 for i in range(g.end() - g.start())]
+
+ # printable characters in the string is less than or equal to what was requested
+ if limit >= len(qstr) or sum(pc) <= limit:
+ return qstr
+ else:
+ sumpc = 0
+ for i,v in enumerate(pc):
+ sumpc += v
+ if sumpc == limit:
+ return qstr[0:i+1]
+
+
def page_url(page):
return current_route_url(request, page=page, _query=request.GET)
def pretty_date(time=False):
- """
- Get a datetime object or a int() Epoch timestamp and return a
- pretty string like 'an hour ago', 'Yesterday', '3 months ago',
- 'just now', etc
- """
+ '''Returns a human-readable relative date.'''
now = datetime.utcnow()
if type(time) is int:
diff = now - datetime.fromtimestamp(time)
elif isinstance(time,datetime):
- diff = now - time
+ diff = now - time
elif not time:
+ print "not a time value"
diff = now - now
- second_diff = diff.seconds
- day_diff = diff.days
-
- if day_diff < 0:
- return ''
-
- if day_diff == 0:
- if second_diff < 10:
- return "just now"
- if second_diff < 60:
- return str(second_diff) + " seconds ago"
- if second_diff < 120:
- return "a minute ago"
- if second_diff < 3600:
- return str( second_diff / 60 ) + " minutes ago"
- if second_diff < 7200:
- return "an hour ago"
- if second_diff < 86400:
- return str( second_diff / 3600 ) + " hours ago"
- if day_diff == 1:
- return "Yesterday"
- if day_diff < 7:
- return str(day_diff) + " days ago"
- if day_diff < 31:
- if day_diff/7 == 1:
- return "a week ago"
- else:
- return str(day_diff/7) + " weeks ago"
- if day_diff < 365:
- if day_diff/30 == 1:
- return "a month ago"
- else:
- return str(day_diff/30) + " months ago"
+
+ dim = round(diff.seconds/60.0 + diff.days*1440.0)
+
+ if dim == 0:
+ return "less than a minute ago"
+ elif dim == 1:
+ return "1 minute ago"
+ elif dim >= 2 and dim <= 44:
+ return "{0} minutes ago".format(int(dim))
+ elif dim >= 45 and dim <= 89:
+ return "about 1 hour ago"
+ elif dim >= 90 and dim <= 1439:
+ return "about {0} hours ago".format(int(round(dim/60.0)))
+ elif dim >= 1440 and dim <= 2519:
+ return "1 day ago"
+ elif dim >= 2520 and dim <= 43199:
+ return "{0} days ago".format(int(round(dim/1440.0)))
+ elif dim >= 43200 and dim <= 86399:
+ return "about 1 month ago"
+ elif dim >= 86400 and dim <= 525599:
+ return "{0} months ago".format(int(round(dim/43200.0)))
+ elif dim >= 525600 and dim <= 655199:
+ return "about 1 year ago"
+ elif dim >= 655200 and dim <= 914399:
+ return "over 1 year ago"
+ elif dim >= 914400 and dim <= 1051199:
+ return "almost 2 years ago"
else:
- if day_diff/365 == 1:
- return "a year ago"
+ return "about {0} years ago".format(int(round(dim/525600.0)))
+
+def datetime_seconds(td):
+ return float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
+
+def to_json(data):
+ if not type(data) == dict:
+ # assume it's a named tuple
+ data = data._asdict()
+ result = {}
+ for key,value in data.items():
+ if value == None:
+ result[key] = None
+ elif type(value) in [bool,int,long,float,complex,str]:
+ result[key] = value
+ elif type(value) == unicode:
+ result[key] = value.encode('utf-8')
+ elif type(value) == Decimal:
+ result[key] = float(value)
+ elif type(value) == datetime:
+ result[key] = value.strftime('%Y-%m-%dT%H:%M:%SZ')
+ elif type(value) == timedelta:
+ result[key] = datetime_seconds(value)
else:
- return str(day_diff/365) + " years ago"
+ result[key] = to_json(value.to_dict())
+ return result
+