# # ex:ts=4:sw=4:sts=4:et # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- # # BitBake Toaster Implementation # # Copyright (C) 2013 Intel Corporation # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. from datetime import datetime, timedelta import re from django import template from django.utils import timezone from django.template.defaultfilters import filesizeformat import json as JsonLib from django.utils.safestring import mark_safe register = template.Library() @register.simple_tag def time_difference(start_time, end_time): return end_time - start_time @register.filter(name = 'sectohms') def sectohms(time): try: tdsec = int(time) except ValueError: tdsec = 0 hours = int(tdsec / 3600) return "%02d:%02d:%02d" % (hours, int((tdsec - (hours * 3600))/ 60), int(tdsec) % 60) @register.filter(name = 'mapselect') def mapselect(value, argument): return map(lambda x: vars(x)[argument], value) @register.filter(name = "json") def json(value): # JSON spec says that "\/" is functionally identical to "/" to allow for HTML-tag embedding in JSON strings # unfortunately, I can't find any option in the json module to turn on forward-slash escaping, so we do # it manually here return mark_safe(JsonLib.dumps(value, ensure_ascii=False).replace(' marks, but this is safe as the data is file paths """ # no filters, show last file (if any) if matchstr == ":": if file_list: return file_list[len(file_list)-1].file_name else: return '' search, filter = matchstr.partition(':')[::2] htmlstr="" # match only filters if search == '': for i in range(len(file_list)): if re.search(filter, file_list[i].file_name): if htmlstr.find(file_list[i].file_name + "

") < 0: htmlstr += file_list[i].file_name + "

" return htmlstr # match only search string, plus always last file if filter == "": for i in range(len(file_list)-1): if re.search(search,file_list[i].file_name): if htmlstr.find(file_list[i].file_name + "

") < 0: htmlstr += file_list[i].file_name + "

" if htmlstr.find(file_list[len(file_list)-1].file_name) < 0: htmlstr += file_list[len(file_list)-1].file_name return htmlstr # match filter or search string for i in range(len(file_list)): if re.search(filter, file_list[i].file_name) or re.search(search,file_list[i].file_name): if htmlstr.find(file_list[i].file_name + "

") < 0: htmlstr += file_list[i].file_name + "

" return htmlstr @register.filter def string_slice(strvar,slicevar): """ slice a string with |string_slice:'[first]:[last]' """ first,last= slicevar.partition(':')[::2] if first=='': return strvar[:int(last)] elif last=='': return strvar[int(first):] else: return strvar[int(first):int(last)] @register.filter def string_remove_regex(value,ex): """ remove sub-string of string that matches regex """ return re.sub(ex, '', value) @register.filter def filtered_installedsize(size, installed_size): """If package.installed_size not null and not empty return it, else return package.size """ return size if (installed_size == 0) or (installed_size == "") or (installed_size == None) else installed_size @register.filter def filtered_packageversion(version, revision): """ Emit "version-revision" if version and revision are not null else "version" if version is not null else "" """ return "" if (not version or version == "") else version if (not revision or revision == "") else version + "-" + revision @register.filter def filter_sizeovertotal(package_object, total_size): """ Return the % size of the package over the total size argument formatted nicely. """ size = package_object.installed_size if size == None or size == '': size = package_object.size return '{:.1%}'.format(float(size)/float(total_size)) from django.utils.safestring import mark_safe @register.filter def format_vpackage_rowclass(size): if size == -1: return mark_safe('class="muted"') return '' @register.filter def format_vpackage_namehelp(name): r = name + ' ' r += '' r += '' return mark_safe(r) @register.filter def get_dict_value(dictionary, key): """ return the value of a dictionary key """ try: return dictionary[key] except (KeyError, IndexError): return '' @register.filter def format_build_date(completed_on): now = timezone.now() delta = now - completed_on if delta.days >= 1: return True @register.filter def is_shaid(text): """ return True if text length is 40 characters and all hex-digits """ try: int(text, 16) if len(text) == 40: return True return False except ValueError: return False