Adding warnings when we do downloads so 1) we can know how bad our performance is...
[doneit] / src / rtmapi.py
index 0525805..d513fc8 100644 (file)
@@ -78,18 +78,25 @@ class RTMapi(object):
                pairs = ''.join(['%s%s' % (k, v) for (k, v) in sortedItems(params)])
                return md5(self._secret+pairs).hexdigest()
 
+       @staticmethod
+       def open_url(url, queryArgs=None):
+               if queryArgs:
+                       url += '?' + urllib.urlencode(queryArgs)
+               warnings.warn("Performing download of %s" % url, stacklevel=5)
+               return urllib.urlopen(url)
+
        def get(self, **params):
                "Get the XML response for the passed `params`."
                params['api_key'] = self._apiKey
                params['format'] = 'json'
                params['api_sig'] = self._sign(params)
 
-               json = openURL(SERVICE_URL, params).read()
+               json = self.open_url(SERVICE_URL, params).read()
 
                if _use_simplejson:
-                       data = dottedDict('ROOT', simplejson.loads(json))
+                       data = DottedDict('ROOT', simplejson.loads(json))
                else:
-                       data = dottedJSON(json)
+                       data = DottedDict('ROOT', safer_eval(json))
                rsp = data.rsp
 
                if rsp.stat == 'fail':
@@ -168,13 +175,7 @@ def sortedItems(dictionary):
                yield key, dictionary[key]
 
 
-def openURL(url, queryArgs=None):
-       if queryArgs:
-               url = url + '?' + urllib.urlencode(queryArgs)
-       return urllib.urlopen(url)
-
-
-class dottedDict(object):
+class DottedDict(object):
        "Make dictionary items accessible via the object-dot notation."
 
        def __init__(self, name, dictionary):
@@ -183,9 +184,9 @@ class dottedDict(object):
                if isinstance(dictionary, dict):
                        for key, value in dictionary.items():
                                if isinstance(value, dict):
-                                       value = dottedDict(key, value)
+                                       value = DottedDict(key, value)
                                elif isinstance(value, (list, tuple)):
-                                       value = [dottedDict('%s_%d' % (key, i), item)
+                                       value = [DottedDict('%s_%d' % (key, i), item)
                                                         for i, item in enumerate(value)]
                                setattr(self, key, value)
 
@@ -205,14 +206,10 @@ class dottedDict(object):
                )
 
 
-def safeEval(string):
+def safer_eval(string):
        return eval(string, {}, {})
 
 
-def dottedJSON(json):
-       return dottedDict('ROOT', safeEval(json))
-
-
 API = {
        'auth': {
                'checkToken':
@@ -221,15 +218,15 @@ API = {
                        [(), ()],
                'getToken':
                        [('frob'), ()]
-               },
+       },
        'contacts': {
                'add':
                        [('timeline', 'contact'), ()],
                'delete':
                        [('timeline', 'contact_id'), ()],
                'getList':
-                       [(), ()]
-               },
+                       [(), ()],
+       },
        'groups': {
                'add':
                        [('timeline', 'group'), ()],
@@ -241,7 +238,7 @@ API = {
                        [(), ()],
                'removeContact':
                        [('timeline', 'group_id', 'contact_id'), ()],
-               },
+       },
        'lists': {
                'add':
                        [('timeline', 'name'), ('filter'), ()],
@@ -257,21 +254,21 @@ API = {
                        [('timeline', 'list_id', 'name'), ()],
                'unarchive':
                        [('timeline'), ('list_id'), ()],
-               },
+       },
        'locations': {
                'getList':
-                       [(), ()]
-               },
+                       [(), ()],
+       },
        'reflection': {
                'getMethodInfo':
                        [('methodName',), ()],
                'getMethods':
-                       [(), ()]
-               },
+                       [(), ()],
+       },
        'settings': {
                'getList':
-                       [(), ()]
-               },
+                       [(), ()],
+       },
        'tasks': {
                'add':
                        [('timeline', 'name',), ('list_id', 'parse',)],
@@ -324,37 +321,37 @@ API = {
                'uncomplete':
                        [('timeline', 'list_id', 'taskseries_id', 'task_id'),
                         ()],
-               },
+       },
        'tasksNotes': {
                'add':
                        [('timeline', 'list_id', 'taskseries_id', 'task_id', 'note_title', 'note_text'), ()],
                'delete':
                        [('timeline', 'note_id'), ()],
                'edit':
-                       [('timeline', 'note_id', 'note_title', 'note_text'), ()]
-               },
+                       [('timeline', 'note_id', 'note_title', 'note_text'), ()],
+       },
        'test': {
                'echo':
                        [(), ()],
                'login':
-                       [(), ()]
-               },
+                       [(), ()],
+       },
        'time': {
                'convert':
                        [('to_timezone',), ('from_timezone', 'to_timezone', 'time')],
                'parse':
-                       [('text',), ('timezone', 'dateformat')]
-               },
+                       [('text',), ('timezone', 'dateformat')],
+       },
        'timelines': {
                'create':
-                       [(), ()]
-               },
+                       [(), ()],
+       },
        'timezones': {
                'getList':
-                       [(), ()]
-               },
+                       [(), ()],
+       },
        'transactions': {
                'undo':
-                       [('timeline', 'transaction_id'), ()]
-               },
-       }
+                       [('timeline', 'transaction_id'), ()],
+       },
+}