# # BitBake Toaster Implementation # # Copyright (C) 2013 Intel Corporation # # SPDX-License-Identifier: GPL-2.0-only # from datetime import timedelta from os.path import relpath 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 = 'get_tasks') def get_tasks(queryset): return list(target + ':' + task if task else target \ for target, task in queryset.values_list('target', 'task')) @register.filter(name = "json") def json(value, default = None): # 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, indent=2, default = default, ensure_ascii=False).replace('' 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 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 @register.filter def cut_path_prefix(fullpath, prefixes): """Cut path prefix from fullpath.""" for prefix in prefixes: if fullpath.startswith(prefix): return relpath(fullpath, prefix) return fullpath @register.filter def for_target(package_dependencies, target): """ filter the dependencies to be displayed by the supplied target if no dependences are found for the target then return the predicted dependences""" return package_dependencies.for_target_or_none(target)