Commits

Jeffrey Goettsch committed 4b571d1

Replaced urllib2 with more efficient httplib2.

Comments (0)

Files changed (7)

 ------------
 
 If you have already downloaded pushnotify, run the following
-command inside this directory::
+commands inside this directory::
 
     $ python setup.py install
+    $ pip install -r requirements.txt
+
 
 If you have not yet downloaded pushnotify, it would be easier to do
 the following::
 
     $ pip install pushnotify
 
-Either method will install pushnotify into your current environment.
+Either method will install pushnotify into your current environment.

pushnotify/abstract.py

 
 """
 
+import httplib2
 import logging
 import urllib
-import urllib2
 
 
 class AbstractClient(object):
         self.application = application
         self.apikeys = {}
 
-        self._browser = urllib2.build_opener(urllib2.HTTPSHandler())
+        # as of 2012-10-06, the pushover server does not pass SSL validation
+        # because httplib2 uses its own certificate store
+        # see: http://code.google.com/p/httplib2/issues/detail?id=154
+
+        self._browser = httplib2.Http(
+            '.cache', disable_ssl_certificate_validation=True)
         self._last = {}
         self._urls = {'notify': '', 'verify': ''}
 
 
         self.logger.debug('_get requesting url: {0}'.format(url))
 
-        request = urllib2.Request(url)
-        try:
-            response_stream = self._browser.open(request)
-        except urllib2.HTTPError, exc:
-            return exc
-        else:
-            return response_stream
+        return self._browser.request(url, 'GET')
 
     def _post(self, url, data):
 
-        data = urllib.urlencode(data)
+        body = urllib.urlencode(data)
 
-        self.logger.debug('_post sending data: {0}'.format(data))
+        self.logger.debug('_post sending data: {0}'.format(body))
         self.logger.debug('_post sending to url: {0}'.format(url))
 
-        request = urllib2.Request(url, data)
-        try:
-            response_stream = self._browser.open(request)
-        except urllib2.HTTPError, exc:
-            return exc
-        else:
-            return response_stream
+        return self._browser.request(
+            url, 'POST', body,
+            headers={'Content-Type': 'application/x-www-form-urlencoded'})
 
     def add_key(self, apikey, device_key=''):
         """Add the given key to self.apikeys.

pushnotify/nma.py

         self._type = 'nma'
         self._urls = {'notify': NOTIFY_URL, 'verify': VERIFY_URL}
 
-    def _parse_response_stream(self, response_stream, verify=False):
+    def _parse_response(self, content, verify=False):
 
-        xmlresp = response_stream.read()
-        root = ElementTree.fromstring(xmlresp)
+        root = ElementTree.fromstring(content)
 
         self._last['type'] = root[0].tag.lower()
         self._last['code'] = root[0].attrib['code']
                         self._last['code'] != '401')):
                 self._raise_exception()
         else:
-            raise exceptions.UnrecognizedResponseError(xmlresp, -1)
+            raise exceptions.UnrecognizedResponseError(content, -1)
 
         return root
 
             if kwargs:
                 data.update(kwargs)
 
-            response_stream = self._post(self._urls['notify'], data)
-            self._parse_response_stream(response_stream)
+            response = self._post(self._urls['notify'], data)
+            self._parse_response(response[1])
 
         if not self.apikeys:
             self.logger.warn('notify called with no users set')
         if self.developerkey:
             data['developerkey'] = self.developerkey
 
-        response_stream = self._get(self._urls['verify'], data)
-        self._parse_response_stream(response_stream, True)
+        response = self._get(self._urls['verify'], data)
+        self._parse_response(response[1], True)
 
         return self._last['code'] == '200'
 

pushnotify/prowl.py

                       'retrieve_token': RETRIEVE_TOKEN_URL,
                       'retrieve_apikey': RETRIEVE_APIKEY_URL}
 
-    def _parse_response_stream(self, response_stream, verify=False):
+    def _parse_response(self, content, verify=False):
 
-        xmlresp = response_stream.read()
-        self.logger.info('received response: {0}'.format(xmlresp))
+        self.logger.info('received response: {0}'.format(content))
 
-        root = ElementTree.fromstring(xmlresp)
+        root = ElementTree.fromstring(content)
 
         self._last['type'] = root[0].tag.lower()
         self._last['code'] = root[0].attrib['code']
                         self._last['code'] != '401')):
                 self._raise_exception()
         else:
-            raise exceptions.UnrecognizedResponseError(xmlresp, -1)
+            raise exceptions.UnrecognizedResponseError(content, -1)
 
         if len(root) > 1:
             if root[1].tag.lower() == 'retrieve':
                     self._last['token_url'] = None
                     self._last['apikey'] = root[1].attrib['apikey']
                 else:
-                    raise exceptions.UnrecognizedResponseError(xmlresp, -1)
+                    raise exceptions.UnrecognizedResponseError(content, -1)
             else:
-                raise exceptions.UnrecognizedResponseError(xmlresp, -1)
+                raise exceptions.UnrecognizedResponseError(content, -1)
 
         return root
 
             if kwargs:
                 data.update(kwargs)
 
-            response_stream = self._post(self._urls['notify'], data)
-            self._parse_response_stream(response_stream)
+            response = self._post(self._urls['notify'], data)
+            self._parse_response(response[1])
 
         if not self.apikeys:
             self.logger.warn('notify called with no users set')
         data = {'providerkey': self.developerkey,
                 'token': reg_token}
 
-        response_stream = self._get(self._urls['retrieve_apikey'], data)
-        self._parse_response_stream(response_stream)
+        response = self._get(self._urls['retrieve_apikey'], data)
+        self._parse_response(response[1])
 
         return self._last['apikey']
 
 
         data = {'providerkey': self.developerkey}
 
-        response_stream = self._get(self._urls['retrieve_token'], data)
-        self._parse_response_stream(response_stream)
+        response = self._get(self._urls['retrieve_token'], data)
+        self._parse_response(response[1])
 
         return self._last['token'], self._last['token_url']
 
         if self.developerkey:
             data['providerkey'] = self.developerkey
 
-        response_stream = self._get(self._urls['verify'], data)
-        self._parse_response_stream(response_stream, True)
+        response = self._get(self._urls['verify'], data)
+        self._parse_response(response[1], True)
 
         return self._last['code'] == '200'
 

pushnotify/pushover.py

         self._type = 'pushover'
         self._urls = {'notify': NOTIFY_URL, 'verify': VERIFY_URL}
 
-    def _parse_response_stream(self, stream, verify=False):
+    def _parse_response(self, response, content, verify=False):
 
-        response = stream.read()
-        self.logger.info('received response: {0}'.format(response))
+        self.logger.info('received response: {0}'.format(content))
 
-        response = json.loads(response)
+        content = json.loads(content)
 
-        self._last['code'] = stream.code
-        self._last['device'] = response.get('device', None)
-        self._last['errors'] = response.get('errors', None)
-        self._last['status'] = response.get('status', None)
-        self._last['token'] = response.get('token', None)
-        self._last['user'] = response.get('user', None)
+        self._last['code'] = response
+        self._last['device'] = content.get('device', None)
+        self._last['errors'] = content.get('errors', None)
+        self._last['status'] = content.get('status', None)
+        self._last['token'] = content.get('token', None)
+        self._last['user'] = content.get('user', None)
 
         return self._last['status']
 
                 if kwargs:
                     data.update(kwargs)
 
-                response_stream = self._post(self._urls['notify'], data)
-                this_successful = self._parse_response_stream(response_stream)
+                response, content = self._post(self._urls['notify'], data)
+                this_successful = self._parse_response(response, content)
 
                 all_successful = all_successful and this_successful
 
 
         data = {'token': self.developerkey, 'user': apikey}
 
-        response_stream = self._post(self._urls['verify'], data)
+        response, content = self._post(self._urls['verify'], data)
 
-        self._parse_response_stream(response_stream, True)
+        self._parse_response(response, content, True)
 
         return self._last['status']
 
         data = {'token': self.developerkey, 'user': apikey,
                 'device': device_key}
 
-        response_stream = self._post(self._urls['verify'], data)
+        response, content = self._post(self._urls['verify'], data)
 
-        self._parse_response_stream(response_stream, True)
+        self._parse_response(response, content, True)
 
         if self._last['user'] and 'invalid' in self._last['user'].lower():
             self._raise_exception()
+httplib2==0.7.6
 #!/usr/bin/env python
 # vim: set fileencoding=utf-8
 
+import re
 from setuptools import setup
 
 
+def parse_requirements():
+
+    requirements = []
+
+    with open('requirements.txt') as fh:
+        for line in fh.read().split('\n'):
+            if re.match(r'(\s*#)|(\s*$)', line):
+                continue
+            if re.match(r'\s*-e\s+', line):
+                requirements.append(
+                    re.sub(r'\s*-e\s+.*#egg=(.*)$', r'\1', line))
+            elif re.match(r'\s*-f\s+', line):
+                pass
+            else:
+                requirements.append(line)
+
+    return requirements
+
 version = '0.5p'
 
 with open('README.rst') as fh:
 setup(
     name='pushnotify',
     packages=['pushnotify', 'pushnotify.tests'],
+    install_requires=parse_requirements(),
     version=version,
     author='Jeffrey Goettsch',
     author_email='jgoettsch+pypushnotify@gmail.com',
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.