From 499a44f00452eb1b8f72a90190178823e52618e2 Mon Sep 17 00:00:00 2001 From: Simon Kelly Date: Wed, 3 Jul 2019 16:56:34 +0200 Subject: [PATCH] add sha1 filter --- commcare_export/env.py | 15 +++++++++++++++ tests/test_minilinq.py | 3 +++ 2 files changed, 18 insertions(+) diff --git a/commcare_export/env.py b/commcare_export/env.py index 093ca1ff..4dbe6ffa 100644 --- a/commcare_export/env.py +++ b/commcare_export/env.py @@ -1,4 +1,6 @@ from __future__ import unicode_literals, print_function, absolute_import, division, generators, nested_scopes + +import hashlib from datetime import datetime import operator import pytz @@ -292,6 +294,18 @@ def bool2int(val): return int(str2bool(val)) +@unwrap('val') +def sha1(val): + if _not_val(val): + return None + + if not isinstance(val, six.text_type): + val = six.text_type(val) + val = val.encode('utf8') + + return hashlib.sha1(val).hexdigest() + + @unwrap('val') def selected_at(val, index): if not val: @@ -410,6 +424,7 @@ def __init__(self, d=None): 'attachment_url': attachment_url, 'filter_empty': _not_val, 'or': _or, + 'sha1': sha1, }) return super(BuiltInEnv, self).__init__(d) diff --git a/tests/test_minilinq.py b/tests/test_minilinq.py index dd3d3118..695e7a66 100644 --- a/tests/test_minilinq.py +++ b/tests/test_minilinq.py @@ -88,6 +88,9 @@ def test_eval_collapsed_list(self): assert Apply(Reference("default"), Literal(None), Literal('a')).eval(env) == 'a' assert Apply(Reference("default"), Literal('b'), Literal('a')).eval(env) == 'b' assert Apply(Reference("count-selected"), Literal(u'a bb 日本')).eval(env) == 3 + assert Apply(Reference("sha1"), Literal(u'a bb 日本')).eval(env) == 'e25a54025417b06d88d40baa8c71f6eee9c07fb1' + assert Apply(Reference("sha1"), Literal(b'2015')).eval(env) == 'd8e9fec0038ade95f6fd6cfbd3c3c344897594ba' + assert Apply(Reference("sha1"), Literal(2015)).eval(env) == '9cdda67ded3f25811728276cefa76b80913b4c54' def test_or(self): env = BuiltInEnv()