summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEd Bartosh <ed.bartosh@linux.intel.com>2016-05-10 08:39:04 (GMT)
committerRichard Purdie <richard.purdie@linuxfoundation.org>2016-05-16 22:32:41 (GMT)
commit909f1b7bf83d17efd06bcf470364385efe2704be (patch)
tree25b87ef6c5141c06e7537b5d37f5ce88310443fb
parent51738f9fa5aa07f19f24dd4a55b1fd8d210c6b3f (diff)
downloadpoky-909f1b7bf83d17efd06bcf470364385efe2704be.tar.gz
bitbake: toaster: get rid of using reduce
Replaced compicated calls of reduce with more clear code. As reduce was removed from python 3 this change is mandatory for the code to work on both pythons. Here is an example change for illustration purposes: original code: querydict = dict(zip(or_keys, or_values)) query = reduce(operator.or_, map(lambda x: __get_q_for_val(x, querydict[x]), [k for k in querydict]))) replaced with: query = None for key, val in zip(or_keys, or_values): x = __get_q_for_val(k, val) query = query | x if query else x [YOCTO #9584] (Bitbake rev: 0a25b723431602dc5eeb10a4002872c05b390f23) Signed-off-by: Ed Bartosh <ed.bartosh@linux.intel.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rwxr-xr-xbitbake/lib/toaster/toastergui/views.py64
-rw-r--r--bitbake/lib/toaster/toastergui/widgets.py21
2 files changed, 57 insertions, 28 deletions
diff --git a/bitbake/lib/toaster/toastergui/views.py b/bitbake/lib/toaster/toastergui/views.py
index e78b0da..22d1a0c 100755
--- a/bitbake/lib/toaster/toastergui/views.py
+++ b/bitbake/lib/toaster/toastergui/views.py
@@ -229,10 +229,18 @@ OR_VALUE_SEPARATOR = "|"
229DESCENDING = "-" 229DESCENDING = "-"
230 230
231def __get_q_for_val(name, value): 231def __get_q_for_val(name, value):
232 if "OR" in value: 232 if "OR" in value or "AND" in value:
233 return reduce(operator.or_, map(lambda x: __get_q_for_val(name, x), [ x for x in value.split("OR") ])) 233 result = None
234 for x in value.split("OR"):
235 x = __get_q_for_val(name, x)
236 result = result | x if result else x
237 return result
234 if "AND" in value: 238 if "AND" in value:
235 return reduce(operator.and_, map(lambda x: __get_q_for_val(name, x), [ x for x in value.split("AND") ])) 239 result = None
240 for x in value.split("AND"):
241 x = __get_q_for_val(name, x)
242 result = result & x if result else x
243 return result
236 if value.startswith("NOT"): 244 if value.startswith("NOT"):
237 value = value[3:] 245 value = value[3:]
238 if value == 'None': 246 if value == 'None':
@@ -251,14 +259,18 @@ def _get_filtering_query(filter_string):
251 and_keys = search_terms[0].split(AND_VALUE_SEPARATOR) 259 and_keys = search_terms[0].split(AND_VALUE_SEPARATOR)
252 and_values = search_terms[1].split(AND_VALUE_SEPARATOR) 260 and_values = search_terms[1].split(AND_VALUE_SEPARATOR)
253 261
254 and_query = [] 262 and_query = None
255 for kv in zip(and_keys, and_values): 263 for kv in zip(and_keys, and_values):
256 or_keys = kv[0].split(OR_VALUE_SEPARATOR) 264 or_keys = kv[0].split(OR_VALUE_SEPARATOR)
257 or_values = kv[1].split(OR_VALUE_SEPARATOR) 265 or_values = kv[1].split(OR_VALUE_SEPARATOR)
258 querydict = dict(zip(or_keys, or_values)) 266 query = None
259 and_query.append(reduce(operator.or_, map(lambda x: __get_q_for_val(x, querydict[x]), [k for k in querydict]))) 267 for key, val in zip(or_keys, or_values):
268 x = __get_q_for_val(k, val)
269 query = query | x if query else x
260 270
261 return reduce(operator.and_, [k for k in and_query]) 271 and_query = and_query & query if and_query else query
272
273 return and_query
262 274
263def _get_toggle_order(request, orderkey, toggle_reverse = False): 275def _get_toggle_order(request, orderkey, toggle_reverse = False):
264 if toggle_reverse: 276 if toggle_reverse:
@@ -295,21 +307,24 @@ def _validate_input(field_input, model):
295 # Check we are looking for a valid field 307 # Check we are looking for a valid field
296 valid_fields = model._meta.get_all_field_names() 308 valid_fields = model._meta.get_all_field_names()
297 for field in field_input_list[0].split(AND_VALUE_SEPARATOR): 309 for field in field_input_list[0].split(AND_VALUE_SEPARATOR):
298 if not reduce(lambda x, y: x or y, [ field.startswith(x) for x in valid_fields ]): 310 if True in [field.startswith(x) for x in valid_fields]:
299 return None, (field, [ x for x in valid_fields ]) 311 break
312 else:
313 return None, (field, valid_fields)
300 314
301 return field_input, invalid 315 return field_input, invalid
302 316
303# uses search_allowed_fields in orm/models.py to create a search query 317# uses search_allowed_fields in orm/models.py to create a search query
304# for these fields with the supplied input text 318# for these fields with the supplied input text
305def _get_search_results(search_term, queryset, model): 319def _get_search_results(search_term, queryset, model):
306 search_objects = [] 320 search_object = None
307 for st in search_term.split(" "): 321 for st in search_term.split(" "):
308 q_map = map(lambda x: Q(**{x+'__icontains': st}), 322 queries = None
309 model.search_allowed_fields) 323 for field in model.search_allowed_fields:
324 query = Q(**{x+'__icontains': st})
325 queries = queries | query if queries else query
310 326
311 search_objects.append(reduce(operator.or_, q_map)) 327 search_object = search_object & queries if search_object else queries
312 search_object = reduce(operator.and_, search_objects)
313 queryset = queryset.filter(search_object) 328 queryset = queryset.filter(search_object)
314 329
315 return queryset 330 return queryset
@@ -1938,10 +1953,10 @@ if True:
1938 if ptype == "build": 1953 if ptype == "build":
1939 mandatory_fields.append('projectversion') 1954 mandatory_fields.append('projectversion')
1940 # make sure we have values for all mandatory_fields 1955 # make sure we have values for all mandatory_fields
1941 if reduce( lambda x, y: x or y, map(lambda x: len(request.POST.get(x, '')) == 0, mandatory_fields)): 1956 missing = [field for field in mandatory_fields if len(request.POST.get(field, '')) == 0]
1942 # set alert for missing fields 1957 if missing:
1943 raise BadParameterException("Fields missing: " + 1958 # set alert for missing fields
1944 ", ".join([x for x in mandatory_fields if len(request.POST.get(x, '')) == 0 ])) 1959 raise BadParameterException("Fields missing: %s" % ", ".join(missing))
1945 1960
1946 if not request.user.is_authenticated(): 1961 if not request.user.is_authenticated():
1947 user = authenticate(username = request.POST.get('username', '_anonuser'), password = 'nopass') 1962 user = authenticate(username = request.POST.get('username', '_anonuser'), password = 'nopass')
@@ -2035,13 +2050,22 @@ if True:
2035 from collections import Counter 2050 from collections import Counter
2036 freqtargets = [] 2051 freqtargets = []
2037 try: 2052 try:
2038 freqtargets += map(lambda x: x.target, reduce(lambda x, y: x + y, map(lambda x: list(x.target_set.all()), Build.objects.filter(project = prj, outcome__lt = Build.IN_PROGRESS)))) 2053 btargets = sum(build.target_set.all() for build in Build.objects.filter(project=prj, outcome__lt=Build.IN_PROGRESS))
2039 freqtargets += map(lambda x: x.target, reduce(lambda x, y: x + y, map(lambda x: list(x.brtarget_set.all()), BuildRequest.objects.filter(project = prj, state = BuildRequest.REQ_FAILED)))) 2054 brtargets = sum(br.brtarget_set.all() for br in BuildRequest.objects.filter(project = prj, state = BuildRequest.REQ_FAILED))
2055 freqtargets = [x.target for x in btargets] + [x.target for x in brtargets]
2040 except TypeError: 2056 except TypeError:
2041 pass 2057 pass
2042 freqtargets = Counter(freqtargets) 2058 freqtargets = Counter(freqtargets)
2043 freqtargets = sorted(freqtargets, key = lambda x: freqtargets[x], reverse=True) 2059 freqtargets = sorted(freqtargets, key = lambda x: freqtargets[x], reverse=True)
2044 2060
2061 layers = [{"id": x.layercommit.pk, "orderid": x.pk, "name" : x.layercommit.layer.name,
2062 "vcs_url": x.layercommit.layer.vcs_url, "vcs_reference" : x.layercommit.get_vcs_reference(),
2063 "url": x.layercommit.layer.layer_index_url, "layerdetailurl": x.layercommit.get_detailspage_url(prj.pk),
2064 # This branch name is actually the release
2065 "branch" : {"name" : x.layercommit.get_vcs_reference(),
2066 "layersource" : x.layercommit.up_branch.layer_source.name if x.layercommit.up_branch != None else None}
2067 } for x in prj.projectlayer_set.all().order_by("id")]
2068
2045 context = { 2069 context = {
2046 "project" : prj, 2070 "project" : prj,
2047 "lvs_nos" : Layer_Version.objects.all().count(), 2071 "lvs_nos" : Layer_Version.objects.all().count(),
diff --git a/bitbake/lib/toaster/toastergui/widgets.py b/bitbake/lib/toaster/toastergui/widgets.py
index d2ef5d3..0f10350 100644
--- a/bitbake/lib/toaster/toastergui/widgets.py
+++ b/bitbake/lib/toaster/toastergui/widgets.py
@@ -243,18 +243,23 @@ class ToasterTable(TemplateView):
243 raise Exception("Search fields aren't defined in the model %s" 243 raise Exception("Search fields aren't defined in the model %s"
244 % self.queryset.model) 244 % self.queryset.model)
245 245
246 search_queries = [] 246 search_queries = None
247 for st in search_term.split(" "): 247 for st in search_term.split(" "):
248 q_map = [Q(**{field + '__icontains': st}) 248 queries = None
249 for field in self.queryset.model.search_allowed_fields] 249 for field in self.queryset.model.search_allowed_fields:
250 250 query = Q(**{field + '__icontains': st})
251 search_queries.append(reduce(operator.or_, q_map)) 251 if queries:
252 252 queries |= query
253 search_queries = reduce(operator.and_, search_queries) 253 else:
254 queries = query
255
256 if search_queries:
257 search_queries &= queries
258 else:
259 search_queries = queries
254 260
255 self.queryset = self.queryset.filter(search_queries) 261 self.queryset = self.queryset.filter(search_queries)
256 262
257
258 def get_data(self, request, **kwargs): 263 def get_data(self, request, **kwargs):
259 """ 264 """
260 Returns the data for the page requested with the specified 265 Returns the data for the page requested with the specified