Skip to content

Commit

Permalink
Ignore uppercase and lowercase sorting an array (#814)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lifka authored and jesuslinares committed Jul 13, 2018
1 parent ff77045 commit 87bafb0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,13 @@
# Change Log
All notable changes to this project will be documented in this file.

## [TBD]

### Fixed

- Ignore uppercase and lowercase sorting an array in framework. ([#814](https://github.com/wazuh/wazuh/pull/814))


## [v3.5.0]

### Added
Expand Down
20 changes: 15 additions & 5 deletions framework/wazuh/utils.py
Expand Up @@ -17,6 +17,10 @@
from itertools import groupby, chain
from xml.etree.ElementTree import fromstring
from operator import itemgetter
import sys
# Python 2/3 compability
if sys.version_info[0] == 3:
unicode = str

try:
from subprocess import check_output
Expand Down Expand Up @@ -98,7 +102,7 @@ def cut_array(array, offset, limit):
elif limit == 0:
raise WazuhException(1406)

if not array or limit == None:
if not array or limit is None:
return array

offset = int(offset)
Expand All @@ -125,8 +129,8 @@ def sort_array(array, sort_by=None, order='asc', allowed_sort_fields=None):
def check_sort_fields(allowed_sort_fields, sort_by):
# Check if every element in sort['fields'] is in allowed_sort_fields
if not sort_by.issubset(allowed_sort_fields):
uncorrect_fields = map(lambda x: str(x), sort_by - allowed_sort_fields)
raise WazuhException(1403, 'Allowed sort fields: {0}. Fields: {1}'.format(list(allowed_sort_fields), uncorrect_fields))
incorrect_fields = ', '.join(sort_by - allowed_sort_fields)
raise WazuhException(1403, 'Allowed sort fields: {0}. Fields: {1}'.format(', '.join(allowed_sort_fields), incorrect_fields))

if not array:
return array
Expand All @@ -145,9 +149,13 @@ def check_sort_fields(allowed_sort_fields, sort_by):
if type(array[0]) is dict:
check_sort_fields(set(array[0].keys()), set(sort_by))

return sorted(array, key=lambda o: tuple(o.get(a) for a in sort_by), reverse=order_desc)
return sorted(array,
key=lambda o: tuple(o.get(a).lower() if type(o.get(a)) in (str,unicode) else o.get(a) for a in sort_by),
reverse=order_desc)
else:
return sorted(array, key=lambda o: tuple(getattr(o, a) for a in sort_by), reverse=order_desc)
return sorted(array,
key=lambda o: tuple(getattr(o, a).lower() if type(getattr(o, a)) in (str,unicode) else getattr(o, a) for a in sort_by),
reverse=order_desc)
else:
if type(array) is set or (type(array[0]) is not dict and 'class \'wazuh' not in str(type(array[0]))):
return sorted(array, reverse=order_desc)
Expand Down Expand Up @@ -217,6 +225,7 @@ def search_array(array, text, negation=False, fields=None):

return found


_filemode_table = (
((stat.S_IFLNK, "l"),
(stat.S_IFREG, "-"),
Expand Down Expand Up @@ -367,6 +376,7 @@ def md5(fname):
hash_md5.update(chunk)
return hash_md5.hexdigest()


def get_fields_to_nest(fields, force_fields=[], split_character="_"):
nest = {k:set(filter(lambda x: x != k, chain.from_iterable(g)))
for k,g in groupby(map(lambda x: x.split(split_character), sorted(fields)),
Expand Down

0 comments on commit 87bafb0

Please sign in to comment.