summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/toaster/toastergui/templatetags/projecttags.py
diff options
context:
space:
mode:
Diffstat (limited to 'bitbake/lib/toaster/toastergui/templatetags/projecttags.py')
-rw-r--r--bitbake/lib/toaster/toastergui/templatetags/projecttags.py255
1 files changed, 255 insertions, 0 deletions
diff --git a/bitbake/lib/toaster/toastergui/templatetags/projecttags.py b/bitbake/lib/toaster/toastergui/templatetags/projecttags.py
new file mode 100644
index 0000000..be75b21
--- /dev/null
+++ b/bitbake/lib/toaster/toastergui/templatetags/projecttags.py
@@ -0,0 +1,255 @@
1#
2# ex:ts=4:sw=4:sts=4:et
3# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
4#
5# BitBake Toaster Implementation
6#
7# Copyright (C) 2013 Intel Corporation
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License version 2 as
11# published by the Free Software Foundation.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License along
19# with this program; if not, write to the Free Software Foundation, Inc.,
20# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22from datetime import datetime, timedelta
23import re
24from django import template
25from django.utils import timezone
26from django.template.defaultfilters import filesizeformat
27
28register = template.Library()
29
30@register.simple_tag
31def time_difference(start_time, end_time):
32 return end_time - start_time
33
34@register.filter(name = 'sectohms')
35def sectohms(time):
36 try:
37 tdsec = int(time)
38 except ValueError:
39 tdsec = 0
40 hours = int(tdsec / 3600)
41 return "%02d:%02d:%02d" % (hours, int((tdsec - (hours * 3600))/ 60), int(tdsec) % 60)
42
43@register.assignment_tag
44def query(qs, **kwargs):
45 """ template tag which allows queryset filtering. Usage:
46 {% query books author=author as mybooks %}
47 {% for book in mybooks %}
48 ...
49 {% endfor %}
50 """
51 return qs.filter(**kwargs)
52
53@register.filter
54def divide(value, arg):
55 if int(arg) == 0:
56 return -1
57 return int(value) / int(arg)
58
59@register.filter
60def multiply(value, arg):
61 return int(value) * int(arg)
62
63@register.assignment_tag
64def datecompute(delta, start = timezone.now()):
65 return start + timedelta(delta)
66
67
68@register.filter(name = 'sortcols')
69def sortcols(tablecols):
70 return sorted(tablecols, key = lambda t: t['name'])
71
72@register.filter
73def task_color(task_object, show_green=False):
74 """ Return css class depending on Task execution status and execution outcome.
75 By default, green is not returned for executed and successful tasks;
76 show_green argument should be True to get green color.
77 """
78 if not task_object.task_executed:
79 return 'class=muted'
80 elif task_object.outcome == task_object.OUTCOME_FAILED:
81 return 'class=error'
82 elif task_object.outcome == task_object.OUTCOME_SUCCESS and show_green:
83 return 'class=green'
84 else:
85 return ''
86
87@register.filter
88def filtered_icon(options, filter):
89 """Returns btn-primary if the filter matches one of the filter options
90 """
91 for option in options:
92 if filter == option[1]:
93 return "btn-primary"
94 return ""
95
96@register.filter
97def filtered_tooltip(options, filter):
98 """Returns tooltip for the filter icon if the filter matches one of the filter options
99 """
100 for option in options:
101 if filter == option[1]:
102 return "Showing only %s"%option[0]
103 return ""
104
105@register.filter
106def format_none_and_zero(value):
107 """Return empty string if the value is None, zero or Not Applicable
108 """
109 return "" if (not value) or (value == 0) or (value == "0") or (value == 'Not Applicable') else value
110
111@register.filter
112def filtered_filesizeformat(value):
113 """
114 If the value is -1 return an empty string. Otherwise,
115 change output from fileformatsize to suppress trailing '.0'
116 and change 'bytes' to 'B'.
117 """
118 if value == -1:
119 return ''
120
121 return filesizeformat(value).replace("bytes", "B")
122
123@register.filter
124def filtered_packagespec(value):
125 """Strip off empty version and revision"""
126 return re.sub(r'(--$)', '', value)
127
128@register.filter
129def check_filter_status(options, filter):
130 """Check if the active filter is among the available options, and return 'checked'
131 if filter is not active.
132 Used in FilterDialog to select the first radio button if the filter is not active.
133 """
134 for option in options:
135 if filter == option[1]:
136 return ""
137 return "checked"
138
139@register.filter
140def variable_parent_name(value):
141 """ filter extended variable names to the parent name
142 """
143 value=re.sub('_\$.*', '', value)
144 return re.sub('_[a-z].*', '', value)
145
146@register.filter
147def filter_setin_files(file_list,matchstr):
148 """ filter/search the 'set in' file lists. Note
149 that this output is not autoescaped to allow
150 the <p> marks, but this is safe as the data
151 is file paths
152 """
153
154 # no filters, show last file (if any)
155 if matchstr == ":":
156 if file_list:
157 return file_list[len(file_list)-1].file_name
158 else:
159 return ''
160
161 search, filter = matchstr.partition(':')[::2]
162 htmlstr=""
163 # match only filters
164 if search == '':
165 for i in range(len(file_list)):
166 if re.search(filter, file_list[i].file_name):
167 if htmlstr.find(file_list[i].file_name + "<p>") < 0:
168 htmlstr += file_list[i].file_name + "<p>"
169 return htmlstr
170
171 # match only search string, plus always last file
172 if filter == "":
173 for i in range(len(file_list)-1):
174 if re.search(search,file_list[i].file_name):
175 if htmlstr.find(file_list[i].file_name + "<p>") < 0:
176 htmlstr += file_list[i].file_name + "<p>"
177 if htmlstr.find(file_list[len(file_list)-1].file_name) < 0:
178 htmlstr += file_list[len(file_list)-1].file_name
179 return htmlstr
180
181 # match filter or search string
182 for i in range(len(file_list)):
183 if re.search(filter, file_list[i].file_name) or re.search(search,file_list[i].file_name):
184 if htmlstr.find(file_list[i].file_name + "<p>") < 0:
185 htmlstr += file_list[i].file_name + "<p>"
186 return htmlstr
187
188
189@register.filter
190def string_slice(strvar,slicevar):
191 """ slice a string with |string_slice:'[first]:[last]'
192 """
193 first,last= slicevar.partition(':')[::2]
194 if first=='':
195 return strvar[:int(last)]
196 elif last=='':
197 return strvar[int(first):]
198 else:
199 return strvar[int(first):int(last)]
200
201@register.filter
202def string_remove_regex(value,ex):
203 """ remove sub-string of string that matches regex
204 """
205 return re.sub(ex, '', value)
206
207@register.filter
208def filtered_installedsize(size, installed_size):
209 """If package.installed_size not null and not empty return it,
210 else return package.size
211 """
212 return size if (installed_size == 0) or (installed_size == "") or (installed_size == None) else installed_size
213
214@register.filter
215def filtered_packageversion(version, revision):
216 """ Emit "version-revision" if version and revision are not null
217 else "version" if version is not null
218 else ""
219 """
220 return "" if (not version or version == "") else version if (not revision or revision == "") else version + "-" + revision
221
222@register.filter
223def filter_sizeovertotal(package_object, total_size):
224 """ Return the % size of the package over the total size argument
225 formatted nicely.
226 """
227 size = package_object.installed_size
228 if size == None or size == '':
229 size = package_object.size
230
231 return '{:.1%}'.format(float(size)/float(total_size))
232
233from django.utils.safestring import mark_safe
234@register.filter
235def format_vpackage_rowclass(size):
236 if size == -1:
237 return mark_safe('class="muted"')
238 return ''
239
240@register.filter
241def format_vpackage_namehelp(name):
242 r = name + '&nbsp;'
243 r += '<i class="icon-question-sign get-help hover-help"'
244 r += ' title = "' + name + ' has not been built">'
245 r += '</i>'
246 return mark_safe(r)
247
248@register.filter
249def get_dict_value(dictionary, key):
250 """ return the value of a dictionary key
251 """
252 try:
253 return dictionary[key]
254 except (KeyError, IndexError):
255 return ''