| Total Complexity | 11 |
| Total Lines | 36 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | # -*- coding: utf-8 -*- |
||
| 2 | |||
| 3 | from benedict.utils import dict_util |
||
| 4 | |||
| 5 | from six import string_types |
||
| 6 | |||
| 7 | |||
| 8 | def check_keys(d, separator): |
||
| 9 | """ |
||
| 10 | Check if dict keys contain keypath separator. |
||
| 11 | """ |
||
| 12 | if not isinstance(d, dict) or not separator: |
||
| 13 | return |
||
| 14 | |||
| 15 | def check_key(parent, key, value): |
||
| 16 | if key and isinstance(key, string_types) and separator in key: |
||
| 17 | raise ValueError( |
||
| 18 | 'keys should not contain keypath separator ' |
||
| 19 | '\'{}\', found: \'{}\'.'.format(separator, key)) |
||
| 20 | dict_util.traverse(d, check_key) |
||
| 21 | |||
| 22 | |||
| 23 | def list_keys(keypath, separator): |
||
| 24 | """ |
||
| 25 | List keys splitting a keypath using the given separator. |
||
| 26 | """ |
||
| 27 | if isinstance(keypath, string_types): |
||
| 28 | if separator: |
||
| 29 | return keypath.split(separator) |
||
| 30 | elif isinstance(keypath, (list, tuple, )): |
||
| 31 | keys = [] |
||
| 32 | for key in keypath: |
||
| 33 | keys += list_keys(key, separator) |
||
| 34 | return keys |
||
| 35 | return [keypath] |
||
| 36 |