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