~leonardr/launchpadlib/use-1.0

« back to all changes in this revision

Viewing changes to src/launchpadlib/launchpad.py

  • Committer: Leonard Richardson
  • Date: 2009-12-17 13:25:11 UTC
  • mfrom: (75.1.3 anonymous-access)
  • Revision ID: leonard.richardson@canonical.com-20091217132511-ng8mk8alfawu1qt9
[r=salgado] Added helper code for gaining anonymous access to Launchpad.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
from lazr.restfulclient.resource import (
33
33
    CollectionWithKeyBasedLookup, HostedFile, ServiceRoot)
34
34
from launchpadlib.credentials import (
35
 
    AccessToken, Credentials, AuthorizeRequestTokenWithBrowser)
 
35
    AccessToken, AnonymousAccessToken, Credentials,
 
36
    AuthorizeRequestTokenWithBrowser)
36
37
from oauth.oauth import OAuthRequest, OAuthSignatureMethod_PLAINTEXT
37
38
from launchpadlib import uris
38
39
 
170
171
        return cls(credentials, service_root, cache, timeout, proxy_info)
171
172
 
172
173
    @classmethod
 
174
    def login_anonymously(
 
175
        cls, consumer_name, service_root=uris.STAGING_SERVICE_ROOT,
 
176
        launchpadlib_dir=None, timeout=None, proxy_info=None):
 
177
        """Get access to Launchpad without providing any credentials."""
 
178
        service_root_dir, cache_path = cls._get_paths(
 
179
            service_root, launchpadlib_dir)
 
180
        token = AnonymousAccessToken()
 
181
        credentials = Credentials(consumer_name, access_token=token)
 
182
        return cls(credentials, service_root, cache_path, timeout, proxy_info)
 
183
 
 
184
    @classmethod
173
185
    def login_with(cls, consumer_name,
174
186
                   service_root=uris.STAGING_SERVICE_ROOT,
175
187
                   launchpadlib_dir=None, timeout=None, proxy_info=None,
210
222
        :rtype: `Launchpad`
211
223
 
212
224
        """
213
 
        if launchpadlib_dir is None:
214
 
            home_dir = os.environ['HOME']
215
 
            launchpadlib_dir = os.path.join(home_dir, '.launchpadlib')
216
 
        launchpadlib_dir = os.path.expanduser(launchpadlib_dir)
217
 
        if not os.path.exists(launchpadlib_dir):
218
 
            os.makedirs(launchpadlib_dir,0700)
219
 
        os.chmod(launchpadlib_dir,0700)
220
 
        # Determine the real service root.
221
 
        service_root = uris.lookup_service_root(service_root)
222
 
        # Each service root has its own cache and credential dirs.
223
 
        scheme, host_name, path, query, fragment = urlparse.urlsplit(
224
 
            service_root)
225
 
        service_root_dir = os.path.join(launchpadlib_dir, host_name)
226
 
        cache_path = os.path.join(service_root_dir, 'cache')
227
 
        if not os.path.exists(cache_path):
228
 
            os.makedirs(cache_path)
 
225
        service_root_dir, cache_path = cls._get_paths(
 
226
            service_root, launchpadlib_dir)
229
227
        credentials_path = os.path.join(service_root_dir, 'credentials')
230
228
        if not os.path.exists(credentials_path):
231
229
            os.makedirs(credentials_path)
232
230
        if credentials_file is None:
233
 
            consumer_credentials_path = os.path.join(credentials_path, 
 
231
            consumer_credentials_path = os.path.join(credentials_path,
234
232
                consumer_name)
235
233
        else:
236
234
            consumer_credentials_path = credentials_file
250
248
            launchpad.credentials.save_to_path(consumer_credentials_path)
251
249
            os.chmod(consumer_credentials_path, stat.S_IREAD | stat.S_IWRITE)
252
250
        return launchpad
 
251
 
 
252
    @classmethod
 
253
    def _get_paths(cls, service_root, launchpadlib_dir=None):
 
254
        """Locate launchpadlib-related user paths and ensure they exist.
 
255
 
 
256
        This is a helper function used by login_with() and
 
257
        login_anonymously().
 
258
 
 
259
        :param service_root: The service root the user wants to connect to.
 
260
        :param launchpadlib_dir: The user's base launchpadlib directory,
 
261
            if known.
 
262
        :return: A 2-tuple: (cache_dir, service_root_dir)
 
263
        """
 
264
        if launchpadlib_dir is None:
 
265
            home_dir = os.environ['HOME']
 
266
            launchpadlib_dir = os.path.join(home_dir, '.launchpadlib')
 
267
        launchpadlib_dir = os.path.expanduser(launchpadlib_dir)
 
268
        if not os.path.exists(launchpadlib_dir):
 
269
            os.makedirs(launchpadlib_dir,0700)
 
270
        os.chmod(launchpadlib_dir,0700)
 
271
        # Determine the real service root.
 
272
        service_root = uris.lookup_service_root(service_root)
 
273
        # Each service root has its own cache and credential dirs.
 
274
        scheme, host_name, path, query, fragment = urlparse.urlsplit(
 
275
            service_root)
 
276
        service_root_dir = os.path.join(launchpadlib_dir, host_name)
 
277
        cache_path = os.path.join(service_root_dir, 'cache')
 
278
        if not os.path.exists(cache_path):
 
279
            os.makedirs(cache_path)
 
280
        return (cache_path, service_root_dir)