]> de.git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/util.py
Dave does have a point.
[xonotic/xonstat.git] / xonstat / util.py
1 import re
2 from datetime import datetime
3
4 # Map of special chars to ascii from Quake'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
42 def qfont_decode(qstr=''):
43     """
44     Convert the qfont characters in a string to ascii.
45     """
46     if qstr is None:
47         qstr = ''
48
49     chars = []
50     for c in qstr:
51         if c >= u'\ue000' and c <= u'\ue0ff':
52             c = qfont_table[ord(c) - 0xe000]
53         chars.append(c)
54     return ''.join(chars)
55
56
57 def strip_colors(str=''):
58     if str is None:
59         str = ''
60
61     str = re.sub(r'\^x\w\w\w', '', str)
62     str = re.sub(r'\^\d', '', str)
63     return str
64
65
66 def html_colors(str=''):
67     if str is None:
68         str = ''
69
70     orig = str
71
72     # "downsample" the given UTF-8 characters to ASCII
73     str = qfont_decode(str)
74
75     str = re.sub(r'\^x(\w)(\w)(\w)', 
76             "<span style='color:#\g<1>\g<1>\g<2>\g<2>\g<3>\g<3>'>", str)
77     str = re.sub(r'\^1', "<span style='color:#FF9900'>", str)
78     str = re.sub(r'\^2', "<span style='color:#33FF00'>", str)
79     str = re.sub(r'\^3', "<span style='color:#FFFF00'>", str)
80     str = re.sub(r'\^4', "<span style='color:#3366FF'>", str)
81     str = re.sub(r'\^5', "<span style='color:#33FFFF'>", str)
82     str = re.sub(r'\^6', "<span style='color:#FF3366'>", str)
83     str = re.sub(r'\^7', "<span style='color:#FFFFFF'>", str)
84     str = re.sub(r'\^8', "<span style='color:#999999'>", str)
85     str = re.sub(r'\^9', "<span style='color:#666666'>", str)
86     str = re.sub(r'\^0', "<span style='color:#333333'>", str)
87
88     for span in range(len(re.findall(r'\^x\w\w\w|\^\d', orig))):
89         str += "</span>"
90
91     return str
92
93
94 def page_url(page):
95     return current_route_url(request, page=page, _query=request.GET)
96
97
98 def pretty_date(time=False):
99     """
100     Get a datetime object or a int() Epoch timestamp and return a
101     pretty string like 'an hour ago', 'Yesterday', '3 months ago',
102     'just now', etc
103     """
104     now = datetime.now()
105     if type(time) is int:
106         diff = now - datetime.fromtimestamp(time)
107     elif isinstance(time,datetime):
108         diff = now - time 
109     elif not time:
110         diff = now - now
111     second_diff = diff.seconds
112     day_diff = diff.days
113
114     if day_diff < 0:
115         return ''
116
117     if day_diff == 0:
118         if second_diff < 10:
119             return "just now"
120         if second_diff < 60:
121             return str(second_diff) + " seconds ago"
122         if second_diff < 120:
123             return  "a minute ago"
124         if second_diff < 3600:
125             return str( second_diff / 60 ) + " minutes ago"
126         if second_diff < 7200:
127             return "an hour ago"
128         if second_diff < 86400:
129             return str( second_diff / 3600 ) + " hours ago"
130     if day_diff == 1:
131         return "Yesterday"
132     if day_diff < 7:
133         return str(day_diff) + " days ago"
134     if day_diff < 31:
135         if day_diff/7 == 1:
136             return "a week ago"
137         else:
138             return str(day_diff/7) + " weeks ago"
139     if day_diff < 365:
140         if day_diff/30 == 1:
141             return "a month ago"
142         else:
143             return str(day_diff/30) + " months ago"
144     else:
145         if day_diff/365 == 1:
146             return "a year ago"
147         else:
148             return str(day_diff/365) + " years ago"