X-Git-Url: https://de.git.xonotic.org/?a=blobdiff_plain;f=xonstat%2Futil.py;h=8af24efc1a16ec7c75d8da08c1476c9185dfba9c;hb=7d2fe941049ee699c1516378c8ac92c673d41784;hp=4f0b0da55541d71af5bc96b944c49cdadcfc4311;hpb=2a5be1fcd70d63e52adc4611caac43c81dfa625d;p=xonotic%2Fxonstat.git diff --git a/xonstat/util.py b/xonstat/util.py index 4f0b0da..8af24ef 100644 --- a/xonstat/util.py +++ b/xonstat/util.py @@ -1,26 +1,239 @@ import re +from colorsys import rgb_to_hls, hls_to_rgb +from cgi import escape as html_escape +from datetime import datetime, timedelta +from decimal import Decimal +from collections import namedtuple -def strip_colors(str=None): - str = re.sub(r'\^x\w\w\w', '', str) - str = re.sub(r'\^\d', '', str) - return str - -def html_colors(str=None): - orig = str - str = re.sub(r'\^x(\w)(\w)(\w)', - "", str) - str = re.sub(r'\^1', "", str) - str = re.sub(r'\^2', "", str) - str = re.sub(r'\^3', "", str) - str = re.sub(r'\^4', "", str) - str = re.sub(r'\^5', "", str) - str = re.sub(r'\^6', "", str) - str = re.sub(r'\^7', "", str) - str = re.sub(r'\^8', "", str) - str = re.sub(r'\^9', "", str) - str = re.sub(r'\^0', "", str) - - for span in range(len(re.findall(r'\^x\w\w\w|\^\d', orig))): - str += "" - - return str +# Map of special chars to ascii from Darkplace's console.c. +_qfont_table = [ + '\0', '#', '#', '#', '#', '.', '#', '#', + '#', '\t', '\n', '#', ' ', '\r', '.', '.', + '[', ']', '0', '1', '2', '3', '4', '5', + '6', '7', '8', '9', '.', '<', '=', '>', + ' ', '!', '"', '#', '$', '%', '&', '\'', + '(', ')', '*', '+', ',', '-', '.', '/', + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', ':', ';', '<', '=', '>', '?', + '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', + 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', + 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', + 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', + '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', + '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', + '6', '7', '8', '9', '.', '<', '=', '>', + ' ', '!', '"', '#', '$', '%', '&', '\'', + '(', ')', '*', '+', ',', '-', '.', '/', + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', ':', ';', '<', '=', '>', '?', + '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', + 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', + 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', + 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', + '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', + 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', + 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', + 'x', 'y', 'z', '{', '|', '}', '~', '<' +] + +# Hex-colored spans for decimal color codes ^0 - ^9 +_dec_spans = [ + "", + "", + "", + "", + "", + "", + "", + "", + "", + "" +] + +# Color code patterns +_all_colors = re.compile(r'\^(\d|x[\dA-Fa-f]{3})') +_dec_colors = re.compile(r'\^(\d)') +_hex_colors = re.compile(r'\^x([\dA-Fa-f])([\dA-Fa-f])([\dA-Fa-f])') + +# On a light scale of 0 (black) to 1.0 (white) +_contrast_threshold = 0.5 + + +def qfont_decode(qstr=''): + """ Convert the qfont characters in a string to ascii. + """ + if qstr == None: + qstr = '' + chars = [] + for c in qstr: + if u'\ue000' <= c <= u'\ue0ff': + c = _qfont_table[ord(c) - 0xe000] + chars.append(c) + return ''.join(chars) + + +def strip_colors(qstr=''): + if qstr == None: + qstr = '' + return _all_colors.sub('', qstr) + + +def hex_repl(match): + """Convert Darkplaces hex color codes to CSS rgb. + Brighten colors with HSL light value less than 50%""" + + # Extend hex char to 8 bits and to 0.0-1.0 scale + r = int(match.group(1) * 2, 16) / 255.0 + g = int(match.group(2) * 2, 16) / 255.0 + b = int(match.group(3) * 2, 16) / 255.0 + + # Check if color is too dark + hue, light, satur = rgb_to_hls(r, g, b) + if light < _contrast_threshold: + light = _contrast_threshold + r, g, b = hls_to_rgb(hue, light, satur) + + # Convert back to 0-255 scale for css + return '' % (255 * r, 255 * g, 255 * b) + + +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 + "" * 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): + '''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 + elif not time: + print "not a time value" + diff = now - now + + 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: + 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: + result[key] = to_json(value.to_dict()) + return result + + +def is_leap_year(today_dt=None): + if today_dt is None: + today_dt = datetime.utcnow() + + if today_dt.year % 400 == 0: + leap_year = True + elif today_dt.year % 100 == 0: + leap_year = False + elif today_dt.year % 4 == 0: + leap_year = True + else: + leap_year = False + + return leap_year + + +def is_cake_day(create_dt, today_dt=None): + cake_day = False + + if today_dt is None: + today_dt = datetime.utcnow() + + # cakes are given on the first anniversary, not the actual create date! + if datetime.date(today_dt) != datetime.date(create_dt): + if today_dt.day == create_dt.day and today_dt.month == create_dt.month: + cake_day = True + + # leap year people get their cakes on March 1 + if not is_leap_year(today_dt) and create_dt.month == 2 and create_dt.day == 29: + if today_dt.month == 3 and today_dt.day == 1: + cake_day = True + + return cake_day