]> git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/util.py
Idea about classifying text as light or dark so css styling could increase contrast...
[xonotic/xonstat.git] / xonstat / util.py
1 import re
2 from colorsys import rgb_to_hls
3 from cgi import escape as html_escape
4 from datetime import datetime
5
6 # Map of special chars to ascii from Darkplace's console.c.
7 _qfont_table = [
8  '\0', '#',  '#',  '#',  '#',  '.',  '#',  '#',
9  '#',  '\t', '\n', '#',  ' ',  '\r', '.',  '.',
10  '[',  ']',  '0',  '1',  '2',  '3',  '4',  '5',
11  '6',  '7',  '8',  '9',  '.',  '<',  '=',  '>',
12  ' ',  '!',  '"',  '#',  '$',  '%',  '&',  '\'',
13  '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/',
14  '0',  '1',  '2',  '3',  '4',  '5',  '6',  '7',
15  '8',  '9',  ':',  ';',  '<',  '=',  '>',  '?',
16  '@',  'A',  'B',  'C',  'D',  'E',  'F',  'G',
17  'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',
18  'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',
19  'X',  'Y',  'Z',  '[',  '\\', ']',  '^',  '_',
20  '`',  'a',  'b',  'c',  'd',  'e',  'f',  'g',
21  'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o',
22  'p',  'q',  'r',  's',  't',  'u',  'v',  'w',
23  'x',  'y',  'z',  '{',  '|',  '}',  '~',  '<',
24  
25  '<',  '=',  '>',  '#',  '#',  '.',  '#',  '#',
26  '#',  '#',  ' ',  '#',  ' ',  '>',  '.',  '.',
27  '[',  ']',  '0',  '1',  '2',  '3',  '4',  '5',
28  '6',  '7',  '8',  '9',  '.',  '<',  '=',  '>',
29  ' ',  '!',  '"',  '#',  '$',  '%',  '&',  '\'',
30  '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/',
31  '0',  '1',  '2',  '3',  '4',  '5',  '6',  '7',
32  '8',  '9',  ':',  ';',  '<',  '=',  '>',  '?',
33  '@',  'A',  'B',  'C',  'D',  'E',  'F',  'G',
34  'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',
35  'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',
36  'X',  'Y',  'Z',  '[',  '\\', ']',  '^',  '_',
37  '`',  'a',  'b',  'c',  'd',  'e',  'f',  'g',
38  'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o',
39  'p',  'q',  'r',  's',  't',  'u',  'v',  'w',
40  'x',  'y',  'z',  '{',  '|',  '}',  '~',  '<'
41 ]
42
43 # Hex-colored spans for decimal color codes ^0 - ^9
44 _dec_spans = [
45  "<span style='color:#333333'>",
46  "<span style='color:#FF0000'>",
47  "<span style='color:#33FF00'>",
48  "<span style='color:#FFFF00'>",
49  "<span style='color:#3366FF'>",
50  "<span style='color:#33FFFF'>",
51  "<span style='color:#FF3366'>",
52  "<span style='color:#FFFFFF'>",
53  "<span style='color:#999999'>",
54  "<span style='color:#666666'>"
55 ]
56
57 # Color code patterns
58 _all_colors = re.compile(r'\^(\d|x[\dA-Fa-f]{3})')
59 _dec_colors = re.compile(r'\^(\d)')
60 _hex_colors = re.compile(r'\^x([\dA-Fa-f])([\dA-Fa-f])([\dA-Fa-f])')
61
62
63 def qfont_decode(qstr=''):
64     """ Convert the qfont characters in a string to ascii.
65     """
66     if qstr == None:
67         qstr = ''
68     chars = []
69     for c in qstr:
70         if u'\ue000' <= c <= u'\ue0ff':
71             c = _qfont_table[ord(c) - 0xe000]
72         chars.append(c)
73     return ''.join(chars)
74
75
76 def strip_colors(qstr=''):
77     if qstr == None:
78         qstr = ''
79     return _all_colors.sub('', qstr)
80     
81     
82 def hex_repl(match):
83     r = match.group(1) * 2
84     g = match.group(2) * 2
85     b = match.group(3) * 2
86     hue, light, satur = rgb_to_hls(int(r, 16), int(g, 16), int(b, 16))
87     klass = 'text_dark' if light < 0x80 else 'text_light'
88     return '<span style="color:#{0}{1}{2}" class="{3}">'.format(r, g, b, klass)
89
90
91 def html_colors(qstr=''):
92     qstr = html_escape(qfont_decode(qstr).replace('^^', '^'))
93     html = _dec_colors.sub(lambda match: _dec_spans[int(match.group(1))], qstr)
94     html = _hex_colors.sub(hex_repl, html)
95     return html + "</span>" * len(_all_colors.findall(qstr))
96
97
98 def page_url(page):
99     return current_route_url(request, page=page, _query=request.GET)
100
101
102 def pretty_date(time=False):
103     """
104     Get a datetime object or a int() Epoch timestamp and return a
105     pretty string like 'an hour ago', 'Yesterday', '3 months ago',
106     'just now', etc
107     """
108     now = datetime.now()
109     if type(time) is int:
110         diff = now - datetime.fromtimestamp(time)
111     elif isinstance(time,datetime):
112         diff = now - time 
113     elif not time:
114         diff = now - now
115     second_diff = diff.seconds
116     day_diff = diff.days
117
118     if day_diff < 0:
119         return ''
120
121     if day_diff == 0:
122         if second_diff < 10:
123             return "just now"
124         if second_diff < 60:
125             return str(second_diff) + " seconds ago"
126         if second_diff < 120:
127             return  "a minute ago"
128         if second_diff < 3600:
129             return str( second_diff / 60 ) + " minutes ago"
130         if second_diff < 7200:
131             return "an hour ago"
132         if second_diff < 86400:
133             return str( second_diff / 3600 ) + " hours ago"
134     if day_diff == 1:
135         return "Yesterday"
136     if day_diff < 7:
137         return str(day_diff) + " days ago"
138     if day_diff < 31:
139         if day_diff/7 == 1:
140             return "a week ago"
141         else:
142             return str(day_diff/7) + " weeks ago"
143     if day_diff < 365:
144         if day_diff/30 == 1:
145             return "a month ago"
146         else:
147             return str(day_diff/30) + " months ago"
148     else:
149         if day_diff/365 == 1:
150             return "a year ago"
151         else:
152             return str(day_diff/365) + " years ago"