From 615edb730a66b01845ce3f8b151dfcf6811b7e6f Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Fri, 12 Jul 2013 12:47:46 +0200 Subject: [PATCH] `people.sephandle()` raises `InvalidHandleError`, `Connection()` has new `schema` parameter, `Changelog` updated --- Changelog.markdown | 16 ++++++++++++++++ diaspy/connection.py | 11 ++++++----- diaspy/errors.py | 6 ++++++ diaspy/people.py | 2 +- 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/Changelog.markdown b/Changelog.markdown index 433da8f..46746ad 100644 --- a/Changelog.markdown +++ b/Changelog.markdown @@ -19,6 +19,22 @@ up-to-date than manual and if conflicts appear they should follow the order: *docstrings* -> *docs/* -> *manual/* +---- + +Version `0.3.1` (2013-07-12): + +* __upd__: `diaspy.people.sephandle()` raises `InvalidHandleError` instead of `UserError` +* __new__: `connection.Connection` has new parameter in `__init__()`: it's `schema` + + +The new parameter in `connection.Connection` is useful when operating with handles. +As handle does not contain schema (`http`, `https`, etc.) `_setlogin()` would raise an +unhandled exception -- `requests.exceptions.MissingSchema`. +Now, however, `Connection` will catch the exception, add missing schema and try once more. +This parameter is provided to give programmers ability to manipulate it. + +Also, now you can pass just `pod.example.com` as `pod` parameter. Less typing! + ---- Version `0.3.0` (2013-07-07): diff --git a/diaspy/connection.py b/diaspy/connection.py index 90a2e8b..28b5f8d 100644 --- a/diaspy/connection.py +++ b/diaspy/connection.py @@ -25,7 +25,7 @@ class Connection(): _token_regex = re.compile(r'content="(.*?)"\s+name="csrf-token') _userinfo_regex = re.compile(r'window.current_user_attributes = ({.*})') - def __init__(self, pod, username='', password=''): + def __init__(self, pod, username='', password='', schema='https'): """ :param pod: The complete url of the diaspora pod to use. :type pod: str @@ -38,10 +38,11 @@ class Connection(): self.session = requests.Session() self.login_data = {} self.token = '' - try: - self._setlogin(username, password) - except Exception as e: - raise LoginError('cannot create login data (caused by: {0}'.format(e)) + try: self._setlogin(username, password) + except request.exceptions.MissingSchema: self.pod = '{0}://{1}'.format(schema, self.pod) + finally: pass + try: self._setlogin() + except Exception as e: raise LoginError('cannot create login data (caused by: {0})'.format(e)) def __repr__(self): """Returns token string. diff --git a/diaspy/errors.py b/diaspy/errors.py index 35f1cf9..d4eac5f 100644 --- a/diaspy/errors.py +++ b/diaspy/errors.py @@ -22,6 +22,12 @@ class UserError(DiaspyError): pass +class InvalidHandleError(DiaspyError): + """Raised when invalid handle is found. + """ + pass + + class SearchError(DiaspyError): """Exception raised when something related to search goes wrong. """ diff --git a/diaspy/people.py b/diaspy/people.py index 9eaaccd..1ea8eca 100644 --- a/diaspy/people.py +++ b/diaspy/people.py @@ -11,7 +11,7 @@ def sephandle(handle): :returns: two-tuple (pod, user) """ if re.match('^[a-zA-Z]+[a-zA-Z0-9_-]*@[a-z0-9.]+\.[a-z]+$', handle) is None: - raise errors.UserError('invalid handle: {0}'.format(handle)) + raise errors.InvalidHandleError('{0}'.format(handle)) handle = handle.split('@') pod, user = handle[1], handle[0] return (pod, user) -- 2.25.1