launchpadlib API not fully updated for Python3.5

Bug #1759192 reported by Yushan Zhang
This bug report is a duplicate of:  Bug #1672458: oauth import doesn't work on python3. Edit Remove
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
launchpadlib
New
Undecided
Unassigned

Bug Description

When running the copy-paste example from reference page: (the package I use is installed with `pip install launchpadlib` which has a version of 1.10.6)

````
   from launchpadlib.launchpad import Launchpad
   launchpad = Launchpad.login_with('hello-world', 'production')
   print('Hello, %s!' % launchpad.me.display_name)
````

got an exception:

````
  File "/home/zhangysh1995/.local/lib/python3.5/site-packages/lazr/restfulclient/authorize/oauth.py", line 33, in <module>
    oauth = __import__('oauth.oauth', {}).oauth
  File "/home/zhangysh1995/.local/lib/python3.5/site-packages/oauth/oauth.py", line 29, in <module>
    import urlparse
ImportError: No module named 'urlparse'
````

This caused by the migration of this module to `from urllib.parse import urlparse`. After fixed this, got another exception for the second example:

````
   from launchpadlib.launchpad import Launchpad
   launchpad = Launchpad.login_anonymously('just testing', 'production')

````

````
    sig = '%s&' % escape(consumer.secret)
  File "/home/zhangysh1995/.local/lib/python3.5/site-packages/oauth/oauth.py", line 50, in escape
    return urllib.quote(s, safe='~')
AttributeError: module 'urllib' has no attribute 'quote'
````

This is caused by the same reason. And another:

````
  File "/home/zhangysh1995/.local/lib/python3.5/site-packages/lazr/restfulclient/authorize/oauth.py", line 248, in authorizeRequest
    headers.update(oauth_request.to_header(self.oauth_realm))
  File "/home/zhangysh1995/.local/lib/python3.5/site-packages/oauth/oauth.py", line 207, in to_header
    for k, v in self.parameters.iteritems():
AttributeError: 'dict' object has no attribute 'iteritems'
````

If it is possible, I could do the fix found for Python 3.5 when exploring the APIs.

description: updated
affects: launchpad → launchpadlib
Revision history for this message
Yushan Zhang (zhangysh1995) wrote :
Download full text (22.6 KiB)

"""
The MIT License

Copyright (c) 2007 Leah Culver

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import cgi
import urllib
import time
import random
from urllib.parse import urlparse, quote
import hmac
import binascii

VERSION = '1.0' # Hi Blaine!
HTTP_METHOD = 'GET'
SIGNATURE_METHOD = 'PLAINTEXT'

class OAuthError(RuntimeError):
    """Generic exception class."""
    def __init__(self, message='OAuth error occured.'):
        self.message = message

def build_authenticate_header(realm=''):
    """Optional WWW-Authenticate header (401 error)"""
    return {'WWW-Authenticate': 'OAuth realm="%s"' % realm}

def escape(s):
    """Escape a URL including any /."""
    return quote(s, safe='~')

def _utf8_str(s):
    """Convert unicode to utf-8."""
    if isinstance(s, unicode):
        return s.encode("utf-8")
    else:
        return str(s)

def generate_timestamp():
    """Get seconds since epoch (UTC)."""
    return int(time.time())

def generate_nonce(length=8):
    """Generate pseudorandom number."""
    return ''.join([str(random.randint(0, 9)) for i in range(length)])

def generate_verifier(length=8):
    """Generate pseudorandom number."""
    return ''.join([str(random.randint(0, 9)) for i in range(length)])

class OAuthConsumer(object):
    """Consumer of OAuth authentication.

    OAuthConsumer is a data type that represents the identity of the Consumer
    via its shared secret with the Service Provider.

    """
    key = None
    secret = None

    def __init__(self, key, secret):
        self.key = key
        self.secret = secret

class OAuthToken(object):
    """OAuthToken is a data type that represents an End User via either an access
    or request token.

    key -- the token
    secret -- the token secret

    """
    key = None
    secret = None
    callback = None
    callback_confirmed = None
    verifier = None

    def __init__(self, key, secret):
        self.key = key
        self.secret = secret

    def set_callback(self, callback):
        self.callback = callback
        self.callback_confirmed = 'true'

    def set_verifier(self, verifier=None):
        if verifier is not None:
            self.verifier = verifier
        else:
            self...

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.