Commit 9555514fdfc381f5bda2de064ac0a37c8383b652
Committed by
mykhaly
1 parent
4244abca
Remake set_to_object
Showing
1 changed file
with
25 additions
and
5 deletions
@@ -315,11 +315,6 @@ def set_access_key(tender, access_token): | @@ -315,11 +315,6 @@ def set_access_key(tender, access_token): | ||
315 | return tender | 315 | return tender |
316 | 316 | ||
317 | 317 | ||
318 | -def set_to_object(obj, attribute, value): | ||
319 | - xpathnew(obj, attribute, value, separator='.') | ||
320 | - return obj | ||
321 | - | ||
322 | - | ||
323 | def get_from_object(obj, attribute): | 318 | def get_from_object(obj, attribute): |
324 | """Gets data from a dictionary using a dotted accessor-string""" | 319 | """Gets data from a dictionary using a dotted accessor-string""" |
325 | jsonpath_expr = parse_path(attribute) | 320 | jsonpath_expr = parse_path(attribute) |
@@ -330,6 +325,31 @@ def get_from_object(obj, attribute): | @@ -330,6 +325,31 @@ def get_from_object(obj, attribute): | ||
330 | raise AttributeError('Attribute not found: {0}'.format(attribute)) | 325 | raise AttributeError('Attribute not found: {0}'.format(attribute)) |
331 | 326 | ||
332 | 327 | ||
328 | +def set_to_object(obj, attribute, value): | ||
329 | + # Search the list index in path to value | ||
330 | + list_index = re.search('\d+', attribute) | ||
331 | + if list_index: | ||
332 | + list_index = list_index.group(0) | ||
333 | + parent, child = attribute.split('[' + list_index + '].')[:2] | ||
334 | + # Split attribute to path to lits (parent) and path to value in list element (child) | ||
335 | + try: | ||
336 | + # Get list from parent | ||
337 | + listing = get_from_object(obj, parent) | ||
338 | + # Create object with list_index if he don`t exist | ||
339 | + if len(listing) < int(list_index) + 1: | ||
340 | + listing.append({}) | ||
341 | + except AttributeError: | ||
342 | + # Create list if he don`t exist | ||
343 | + listing = [{}] | ||
344 | + # Update list in parent | ||
345 | + xpathnew(obj, parent, listing, separator='.') | ||
346 | + # Set value in obj | ||
347 | + xpathnew(obj, '.'.join([parent, list_index, child]), value, separator='.') | ||
348 | + else: | ||
349 | + xpathnew(obj, attribute, value, separator='.') | ||
350 | + return munchify(obj) | ||
351 | + | ||
352 | + | ||
333 | def wait_to_date(date_stamp): | 353 | def wait_to_date(date_stamp): |
334 | date = parse(date_stamp) | 354 | date = parse(date_stamp) |
335 | LOGGER.log_message(Message("date: {}".format(date.isoformat()), "INFO")) | 355 | LOGGER.log_message(Message("date: {}".format(date.isoformat()), "INFO")) |
Please
register
or
login
to post a comment