Skip to content

Commit ca0defc

Browse files
committed
Added get_uuid and get_uuid_list methods.
1 parent 691f67e commit ca0defc

File tree

6 files changed

+73
-0
lines changed

6 files changed

+73
-0
lines changed

benedict/dicts/parse/parse_dict.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,19 @@ def get_str_list(self, key, default=None, separator=','):
221221
"""
222222
return self._get_values_list(
223223
key, default, separator, parse_util.parse_str)
224+
225+
def get_uuid(self, key, default='', choices=None):
226+
"""
227+
Get value by key or keypath trying to return it as valid uuid.
228+
If choices and value is in choices return value otherwise default.
229+
"""
230+
return self._get_value(
231+
key, default, choices, parse_util.parse_uuid)
232+
233+
def get_uuid_list(self, key, default=None, separator=','):
234+
"""
235+
Get value by key or keypath trying to return it as list of valid uuid values.
236+
If separator is specified and value is a string it will be splitted.
237+
"""
238+
return self._get_values_list(
239+
key, default, separator, parse_util.parse_uuid)

benedict/dicts/parse/parse_util.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,8 @@ def parse_str(val):
205205
val = val.strip()
206206
val = ' '.join(val.split())
207207
return val
208+
209+
210+
def parse_uuid(val):
211+
s = parse_str(val)
212+
return s if type_util.is_uuid(s) else None

benedict/utils/type_util.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import re
88

99
regex = re.compile('').__class__
10+
uuid_re = re.compile(
11+
'^([0-9a-f]{32}){1}$|^([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}){1}$',
12+
flags=re.IGNORECASE)
1013

1114

1215
def is_bool(val):
@@ -81,3 +84,7 @@ def is_string(val):
8184

8285
def is_tuple(val):
8386
return isinstance(val, tuple)
87+
88+
89+
def is_uuid(val):
90+
return is_string(val) and uuid_re.match(val)

tests/dicts/parse/test_parse_dict.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,3 +490,36 @@ def test_get_str_with_choices(self):
490490
self.assertEqual(b.get_str('a', choices=['Sunday', 'Saturday']), 'Sunday')
491491
self.assertEqual(b.get_str('b', choices=['Sunday', 'Saturday'], default='Saturday'), 'Saturday')
492492
self.assertEqual(b.get_str('c', choices=['Sunday', 'Saturday'], default='Saturday'), 'Saturday')
493+
494+
def test_get_uuid(self):
495+
d = {
496+
'a': 'CA761232-ED42-11CE-BACD-00AA0057B223',
497+
'b': ' CA761232-ED42-11CE-BACD-00AA0057B223 ',
498+
'c': ' CA761232ED4211CEBACD00AA0057B223 ',
499+
'd': ' ca761232ed4211cebacd00aa0057b223 ',
500+
'e': 'CA761232ED4211CEBACD00AA0057B223',
501+
'f': 'CA761232ED4211CEBACD00AA0057B2233', # invalid too long
502+
'g': 'CA761232ED4211CEBACD00AA0057B22x', # invalid chars
503+
'h': 'CA761232ED4211CEBACD00AA0057B22', # invalid too short
504+
'i': 'CA761232-ED42-11CE-BACD-00AA0057B2234', # invalid too long
505+
}
506+
b = ParseDict(d)
507+
self.assertEqual(b.get_uuid('a'), 'CA761232-ED42-11CE-BACD-00AA0057B223')
508+
self.assertEqual(b.get_uuid('b'), 'CA761232-ED42-11CE-BACD-00AA0057B223')
509+
self.assertEqual(b.get_uuid('c'), 'CA761232ED4211CEBACD00AA0057B223')
510+
self.assertEqual(b.get_uuid('d'), 'ca761232ed4211cebacd00aa0057b223')
511+
self.assertEqual(b.get_uuid('e'), 'CA761232ED4211CEBACD00AA0057B223')
512+
self.assertEqual(b.get_uuid('f'), '')
513+
self.assertEqual(b.get_uuid('f', 'none'), 'none')
514+
self.assertEqual(b.get_uuid('g'), '')
515+
self.assertEqual(b.get_uuid('h'), '')
516+
self.assertEqual(b.get_uuid('i'), '')
517+
518+
def test_get_uuid_list(self):
519+
d = {
520+
'a': ['CA761232-ED42-11CE-BACD-00AA0057B223', ' FB761232-ED42-314E-BFCA-00AA0057B118 ', 99.9],
521+
'b': 'Hello World, See you later, 99.9',
522+
}
523+
b = ParseDict(d)
524+
self.assertEqual(b.get_uuid_list('a'), ['CA761232-ED42-11CE-BACD-00AA0057B223', 'FB761232-ED42-314E-BFCA-00AA0057B118', None])
525+
self.assertEqual(b.get_uuid_list('b'), [None, None, None])

tests/dicts/parse/test_parse_util.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ def test_parse_slug(self):
6464
def test_parse_str(self):
6565
# TODO
6666
pass
67+
68+
def test_parse_uuid(self):
69+
# TODO
70+
pass

tests/utils/test_type_util.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,11 @@ def test_is_tuple(self):
281281
self.assertFalse(f({'a':0, 'b':1, 'c':2}))
282282
self.assertFalse(f('hello world'))
283283
self.assertFalse(f(lambda a: a))
284+
285+
def test_is_uuid(self):
286+
f = type_util.is_uuid
287+
self.assertTrue(f('ca761232ed4211cebacd00aa0057b223'))
288+
self.assertTrue(f('CA761232-ED42-11CE-BACD-00AA0057B223'))
289+
self.assertTrue(f('CA761232-ED42-11CE-BACD-00AA0057B223'))
290+
self.assertFalse(f('CA761232-ED42-11CE-BACD-00AA0057B22X'))
291+
self.assertFalse(f('CA761232-ED42-11CE-BACD-00AA0057B22'))

0 commit comments

Comments
 (0)