aboutsummaryrefslogtreecommitdiffstats
path: root/github3/helpers.py
diff options
context:
space:
mode:
authorKenneth Reitz <me@kennethreitz.com>2011-06-23 02:59:49 -0400
committerKenneth Reitz <me@kennethreitz.com>2011-06-23 02:59:49 -0400
commitecad4a9e256a10208b5e97d66f88416fecdf9cb3 (patch)
tree9c8922645118812cb2c3dd7bce57b46cb78f6a66 /github3/helpers.py
parentclean slate (diff)
downloadpython-github3-ecad4a9e256a10208b5e97d66f88416fecdf9cb3.tar.xz
python-github3-ecad4a9e256a10208b5e97d66f88416fecdf9cb3.zip
updates
Diffstat (limited to 'github3/helpers.py')
-rw-r--r--github3/helpers.py95
1 files changed, 95 insertions, 0 deletions
diff --git a/github3/helpers.py b/github3/helpers.py
new file mode 100644
index 0000000..3b148d6
--- /dev/null
+++ b/github3/helpers.py
@@ -0,0 +1,95 @@
+# -*- coding: utf-8 -*-
+
+"""
+github3.helpers
+~~~~~~~~~~~~~~~
+
+This module provides various helper functions to the rest of the package.
+"""
+
+
+from datetime import datetime
+
+from dateutil.parser import parse as parse_datetime
+
+
+def is_collection(obj):
+ """Tests if an object is a collection."""
+
+ col = getattr(obj, '__getitem__', False)
+ val = False if (not col) else True
+
+ if isinstance(obj, basestring):
+ val = False
+
+ return val
+
+
+def to_python(obj, in_dict, string_keys=None, date_keys=None, object_map=None, **kwargs):
+ """Extends a given object for API Consumption.
+
+ :param obj: Object to extend.
+ :param in_dict: Dict to extract data from.
+ :param string_keys: List of in_dict keys that will be extracted as strings.
+ :param date_keys: List of in_dict keys that will be extrad as datetimes.
+ :param object_map: Dict of {key, obj} map, for nested object results.
+ """
+
+ if string_keys:
+ for in_key in string_keys:
+ # print in_key
+ obj.__dict__[in_key] = in_dict.get(in_key)
+
+ if date_keys:
+ for in_key in date_keys:
+ in_date = in_dict.get(in_key)
+ try:
+ out_date = datetime.strptime(in_date, '%Y-%m-%d %H:%M:%S')
+ except TypeError:
+ out_date = None
+
+ obj.__dict__[in_key] = out_date
+
+ if object_map:
+
+ for (k, v) in object_map.items():
+ obj.__dict__[k] = v.new_from_dict(in_dict.get(k))
+
+ obj.__dict__.update(kwargs)
+
+ return obj
+
+
+def to_api(in_dict, int_keys=None, date_keys=None):
+ """Extends a given object for API Production."""
+
+ # Cast all int_keys to int()
+ if int_keys:
+ for in_key in int_keys:
+ if (in_key in in_dict) and (in_dict.get(in_key, None) is not None):
+ in_dict[in_key] = int(in_dict[in_key])
+
+ # Cast all date_keys to datetime.isoformat
+ if date_keys:
+ for in_key in date_keys:
+ if (in_key in in_dict) and (in_dict.get(in_key, None) is not None):
+
+ _from = in_dict[in_key]
+
+ if isinstance(_from, basestring):
+ dtime = parse_datetime(_from)
+
+ elif isinstance(_from, datetime):
+ dtime = _from
+
+ in_dict[in_key] = dtime.isoformat()
+
+ elif (in_key in in_dict) and in_dict.get(in_key, None) is None:
+ del in_dict[in_key]
+
+ # Remove all Nones
+ for k, v in in_dict.items():
+ if v is None:
+ del in_dict[k]
+
+ return in_dict