|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +github3.helpers |
| 5 | +~~~~~~~~~~~~~~~ |
| 6 | +
|
| 7 | +This module provides various helper functions to the rest of the package. |
| 8 | +""" |
| 9 | + |
| 10 | + |
| 11 | +from datetime import datetime |
| 12 | + |
| 13 | +from dateutil.parser import parse as parse_datetime |
| 14 | + |
| 15 | + |
| 16 | +def is_collection(obj): |
| 17 | + """Tests if an object is a collection.""" |
| 18 | + |
| 19 | + col = getattr(obj, '__getitem__', False) |
| 20 | + val = False if (not col) else True |
| 21 | + |
| 22 | + if isinstance(obj, basestring): |
| 23 | + val = False |
| 24 | + |
| 25 | + return val |
| 26 | + |
| 27 | + |
| 28 | +def to_python(obj, in_dict, string_keys=None, date_keys=None, object_map=None, **kwargs): |
| 29 | + """Extends a given object for API Consumption. |
| 30 | +
|
| 31 | + :param obj: Object to extend. |
| 32 | + :param in_dict: Dict to extract data from. |
| 33 | + :param string_keys: List of in_dict keys that will be extracted as strings. |
| 34 | + :param date_keys: List of in_dict keys that will be extrad as datetimes. |
| 35 | + :param object_map: Dict of {key, obj} map, for nested object results. |
| 36 | + """ |
| 37 | + |
| 38 | + if string_keys: |
| 39 | + for in_key in string_keys: |
| 40 | + # print in_key |
| 41 | + obj.__dict__[in_key] = in_dict.get(in_key) |
| 42 | + |
| 43 | + if date_keys: |
| 44 | + for in_key in date_keys: |
| 45 | + in_date = in_dict.get(in_key) |
| 46 | + try: |
| 47 | + out_date = datetime.strptime(in_date, '%Y-%m-%d %H:%M:%S') |
| 48 | + except TypeError: |
| 49 | + out_date = None |
| 50 | + |
| 51 | + obj.__dict__[in_key] = out_date |
| 52 | + |
| 53 | + if object_map: |
| 54 | + |
| 55 | + for (k, v) in object_map.items(): |
| 56 | + obj.__dict__[k] = v.new_from_dict(in_dict.get(k)) |
| 57 | + |
| 58 | + obj.__dict__.update(kwargs) |
| 59 | + |
| 60 | + return obj |
| 61 | + |
| 62 | + |
| 63 | +def to_api(in_dict, int_keys=None, date_keys=None): |
| 64 | + """Extends a given object for API Production.""" |
| 65 | + |
| 66 | + # Cast all int_keys to int() |
| 67 | + if int_keys: |
| 68 | + for in_key in int_keys: |
| 69 | + if (in_key in in_dict) and (in_dict.get(in_key, None) is not None): |
| 70 | + in_dict[in_key] = int(in_dict[in_key]) |
| 71 | + |
| 72 | + # Cast all date_keys to datetime.isoformat |
| 73 | + if date_keys: |
| 74 | + for in_key in date_keys: |
| 75 | + if (in_key in in_dict) and (in_dict.get(in_key, None) is not None): |
| 76 | + |
| 77 | + _from = in_dict[in_key] |
| 78 | + |
| 79 | + if isinstance(_from, basestring): |
| 80 | + dtime = parse_datetime(_from) |
| 81 | + |
| 82 | + elif isinstance(_from, datetime): |
| 83 | + dtime = _from |
| 84 | + |
| 85 | + in_dict[in_key] = dtime.isoformat() |
| 86 | + |
| 87 | + elif (in_key in in_dict) and in_dict.get(in_key, None) is None: |
| 88 | + del in_dict[in_key] |
| 89 | + |
| 90 | + # Remove all Nones |
| 91 | + for k, v in in_dict.items(): |
| 92 | + if v is None: |
| 93 | + del in_dict[k] |
| 94 | + |
| 95 | + return in_dict |
0 commit comments