Commit e8c49e4941a100bf05a78927b0980ae2bc9affcf
1 parent
2f743673
Rewrite get_id_from_object()
This change makes the function more reliable, also, the error messages are now saner.
Showing
1 changed file
with
20 additions
and
4 deletions
| @@ -457,10 +457,26 @@ def munch_dict(arg=None, data=False): | @@ -457,10 +457,26 @@ def munch_dict(arg=None, data=False): | ||
| 457 | 457 | ||
| 458 | 458 | ||
| 459 | def get_id_from_object(obj): | 459 | def get_id_from_object(obj): |
| 460 | - obj_id = re.match(r'(^[filq]-[0-9a-fA-F]{8}): ', obj.get('title', '')) | ||
| 461 | - if not obj_id: | ||
| 462 | - obj_id = re.match(r'(^[filq]-[0-9a-fA-F]{8}): ', obj.get('description', '')) | ||
| 463 | - return obj_id.group(1) | 460 | + regex = r'(^[filq]-[0-9a-fA-F]{8}): ' |
| 461 | + | ||
| 462 | + title = obj.get('title', '') | ||
| 463 | + if title: | ||
| 464 | + if not isinstance(title, STR_TYPES): | ||
| 465 | + raise TypeError('title must be one of %s' % str(STR_TYPES)) | ||
| 466 | + obj_id = re.match(regex, title) | ||
| 467 | + if obj_id and len(obj_id.groups()) >= 1: | ||
| 468 | + return obj_id.group(1) | ||
| 469 | + | ||
| 470 | + description = obj.get('description', '') | ||
| 471 | + if description: | ||
| 472 | + if not isinstance(description, STR_TYPES): | ||
| 473 | + raise TypeError('description must be one of %s' % str(STR_TYPES)) | ||
| 474 | + obj_id = re.match(regex, description) | ||
| 475 | + if obj_id and len(obj_id.groups()) >= 1: | ||
| 476 | + return obj_id.group(1) | ||
| 477 | + | ||
| 478 | + raise VaueError('could not find object ID in "title": "%s", ' | ||
| 479 | + '"description": "%s"' % (title, description)) | ||
| 464 | 480 | ||
| 465 | 481 | ||
| 466 | def get_id_from_string(string): | 482 | def get_id_from_string(string): |
Please
register
or
login
to post a comment