Commit 15769262dac58be90be5b39c7f052d5d0874491c
Merge pull request #56 from Leits/upd_operations
Fix create_data_dict for work with list
Showing
1 changed file
with
15 additions
and
5 deletions
1 | 1 | # -*- coding: utf-8 - |
2 | 2 | from datetime import timedelta |
3 | 3 | from dateutil.parser import parse |
4 | -from dpath.util import set as xpathset | |
4 | +from dpath.util import new as xpathnew | |
5 | 5 | from iso8601 import parse_date |
6 | 6 | from json import load |
7 | 7 | from jsonpath_rw import parse as parse_path |
... | ... | @@ -26,6 +26,7 @@ from .initial_data import ( |
26 | 26 | from .local_time import get_now, TZ |
27 | 27 | import os |
28 | 28 | from barbecue import chef |
29 | +import re | |
29 | 30 | |
30 | 31 | |
31 | 32 | def get_current_tzdate(): |
... | ... | @@ -150,7 +151,7 @@ def set_access_key(tender, access_token): |
150 | 151 | |
151 | 152 | |
152 | 153 | def set_to_object(obj, attribute, value): |
153 | - xpathset(obj, attribute.replace('.', '/'), value) | |
154 | + xpathnew(obj, attribute, value, separator='.') | |
154 | 155 | return obj |
155 | 156 | |
156 | 157 | |
... | ... | @@ -182,10 +183,19 @@ def merge_dicts(left, right): |
182 | 183 | return new |
183 | 184 | |
184 | 185 | |
185 | -def create_data_dict(path_to_key=None, value=None): | |
186 | +def create_data_dict(path_to_value=None, value=None): | |
186 | 187 | data_dict = munchify({'data': {}}) |
187 | - if isinstance(path_to_key, basestring) and isinstance(value, basestring): | |
188 | - data_dict = set_to_object(data_dict, path_to_key, value) | |
188 | + if isinstance(path_to_value, basestring) and value: | |
189 | + list_items = re.search('\d+', path_to_value) | |
190 | + if list_items: | |
191 | + list_items = list_items.group(0) | |
192 | + path_to_value = path_to_value.split('[' + list_items + ']') | |
193 | + path_to_value.insert(1, '.' + list_items) | |
194 | + set_to_object(data_dict, path_to_value[0], []) | |
195 | + set_to_object(data_dict, ''.join(path_to_value[:2]), {}) | |
196 | + set_to_object(data_dict, ''.join(path_to_value), value) | |
197 | + else: | |
198 | + data_dict = set_to_object(data_dict, path_to_value, value) | |
189 | 199 | return data_dict |
190 | 200 | |
191 | 201 | ... | ... |
Please
register
or
login
to post a comment