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