def test_lookups(self): """Ensure that short service names turn into long service names.""" # If the service name is a known alias, lookup methods convert # it to a URL. with self.edge_deprecation_error(): for alias in self.aliases: self.assertEqual( uris.lookup_service_root(alias), uris.service_roots[alias]) with self.edge_deprecation_error(): for alias in self.aliases: self.assertEqual( uris.lookup_web_root(alias), uris.web_roots[alias]) # If the service name is a valid URL, lookup methods let it # through. other_root = "http://some-other-server.com" self.assertEqual(uris.lookup_service_root(other_root), other_root) self.assertEqual(uris.lookup_web_root(other_root), other_root) # Otherwise, lookup methods raise an exception. not_a_url = "not-a-url" self.assertRaises(ValueError, uris.lookup_service_root, not_a_url) self.assertRaises(ValueError, uris.lookup_web_root, not_a_url)
def lookup_service_root(service_root): try: return uris.lookup_service_root(service_root) except ValueError: if service_root != 'qastaging': raise staging_root = uris.lookup_service_root('staging') return staging_root.replace('staging', 'qastaging')
def _get_paths(cls, service_root, launchpadlib_dir=None): """Locate launchpadlib-related user paths and ensure they exist. This is a helper function used by login_with() and login_anonymously(). :param service_root: The service root the user wants to connect to. This may be an alias (which will be dereferenced to a URL and returned) or a URL (which will be returned as is). :param launchpadlib_dir: The user's base launchpadlib directory, if known. This may be modified, expanded, or determined from the environment if missing. A definitive value will be returned. :return: A 4-tuple: (service_root_uri, launchpadlib_dir, cache_dir, service_root_dir) """ if launchpadlib_dir is None: home_dir = os.environ['HOME'] launchpadlib_dir = os.path.join(home_dir, '.launchpadlib') launchpadlib_dir = os.path.expanduser(launchpadlib_dir) if not os.path.exists(launchpadlib_dir): os.makedirs(launchpadlib_dir,0700) os.chmod(launchpadlib_dir,0700) # Determine the real service root. service_root = uris.lookup_service_root(service_root) # Each service root has its own cache and credential dirs. scheme, host_name, path, query, fragment = urlparse.urlsplit( service_root) service_root_dir = os.path.join(launchpadlib_dir, host_name) cache_path = os.path.join(service_root_dir, 'cache') if not os.path.exists(cache_path): os.makedirs(cache_path) return (service_root, launchpadlib_dir, cache_path, service_root_dir)
def __init__(self, service_root, application_name=None, consumer_name=None, allow_access_levels=None): """Base class initialization. :param service_root: The root of the Launchpad instance being used. :param application_name: The name of the application that wants to use launchpadlib. This is used in conjunction with a desktop-wide integration. If you specify this argument, your values for consumer_name and allow_access_levels are ignored. :param consumer_name: The OAuth consumer name, for an application that wants its own point of integration into Launchpad. In almost all cases, you want to specify application_name instead and do a desktop-wide integration. The exception is when you're integrating a third-party website into Launchpad. :param allow_access_levels: A list of the Launchpad access levels to present to the user. ('READ_PUBLIC' and so on.) Your value for this argument will be ignored during a desktop-wide integration. :type allow_access_levels: A list of strings. """ self.service_root = uris.lookup_service_root(service_root) self.web_root = uris.web_root_for_service_root(service_root) if application_name is None and consumer_name is None: raise ValueError( "You must provide either application_name or consumer_name.") if application_name is not None and consumer_name is not None: raise ValueError( "You must provide only one of application_name and " "consumer_name. (You provided %r and %r.)" % (application_name, consumer_name)) if consumer_name is None: # System-wide integration. Create a system-wide consumer # and identify the application using a separate # application name. allow_access_levels = ["DESKTOP_INTEGRATION"] consumer = SystemWideConsumer(application_name) else: # Application-specific integration. Use the provided # consumer name to create a consumer automatically. consumer = Consumer(consumer_name) application_name = consumer_name self.consumer = consumer self.application_name = application_name self.allow_access_levels = allow_access_levels or []
def __init__(self, service_root, application_name=None, consumer_name=None, allow_access_levels=None): """Base class initialization. :param service_root: The root of the Launchpad instance being used. :param application_name: The name of the application that wants to use launchpadlib. This is used in conjunction with a desktop-wide integration. If you specify this argument, your values for consumer_name and allow_access_levels are ignored. :param consumer_name: The OAuth consumer name, for an application that wants its own point of integration into Launchpad. In almost all cases, you want to specify application_name instead and do a desktop-wide integration. The exception is when you're integrating a third-party website into Launchpad. :param allow_access_levels: A list of the Launchpad access levels to present to the user. ('READ_PUBLIC' and so on.) Your value for this argument will be ignored during a desktop-wide integration. :type allow_access_levels: A list of strings. """ self.service_root = uris.lookup_service_root(service_root) self.web_root = uris.web_root_for_service_root(service_root) if application_name is None and consumer_name is None: raise ValueError( "You must provide either application_name or consumer_name.") if application_name is not None and consumer_name is not None: raise ValueError( "You must provide only one of application_name and " "consumer_name. (You provided %r and %r.)" % ( application_name, consumer_name)) if consumer_name is None: # System-wide integration. Create a system-wide consumer # and identify the application using a separate # application name. allow_access_levels = ["DESKTOP_INTEGRATION"] consumer = SystemWideConsumer(application_name) else: # Application-specific integration. Use the provided # consumer name to create a consumer automatically. consumer = Consumer(consumer_name) application_name = consumer_name self.consumer = consumer self.application_name = application_name self.allow_access_levels = allow_access_levels or []
def __init__(self, credentials, service_root=STAGING_SERVICE_ROOT, cache=None, timeout=None, proxy_info=None, version=DEFAULT_VERSION): service_root = lookup_service_root(service_root) if (service_root.endswith(version) or service_root.endswith(version + '/')): error = ("It looks like you're using a service root that " "incorporates the name of the web service version " '("%s"). Please use one of the constants from ' "launchpadlib.uris instead, or at least remove " "the version name from the root URI." % version) raise ValueError(error) super(SimpleLaunchpad, self).__init__( credentials, service_root, cache, timeout, proxy_info, version)
def get_token_and_login(cls, consumer_name, service_root=uris.STAGING_SERVICE_ROOT, cache=None, timeout=None, proxy_info=None, authorizer_class=AuthorizeRequestTokenWithBrowser, allow_access_levels=[], max_failed_attempts=3, version=DEFAULT_VERSION): """Get credentials from Launchpad and log into the service root. This is a convenience method which will open up the user's preferred web browser and thus should not be used by most applications. Applications should, instead, use Credentials.get_request_token() to obtain the authorization URL and Credentials.exchange_request_token_for_access_token() to obtain the actual OAuth access token. This method will negotiate an OAuth access token with the service provider, but to complete it we will need the user to log into Launchpad and authorize us, so we'll open the authorization page in a web browser and ask the user to come back here and tell us when they finished the authorization process. :param consumer_name: The consumer name, as appropriate for the `Consumer` constructor :type consumer_name: string :param service_root: The URL to the root of the web service. :type service_root: string :return: The web service root :rtype: `Launchpad` """ credentials = Credentials(consumer_name) service_root = uris.lookup_service_root(service_root) web_root_uri = URI(service_root) web_root_uri.path = "" web_root_uri.host = web_root_uri.host.replace("api.", "", 1) web_root = str(web_root_uri.ensureSlash()) authorization_json = credentials.get_request_token( web_root=web_root, token_format=Credentials.DICT_TOKEN_FORMAT) authorizer = authorizer_class( web_root, authorization_json['oauth_token_consumer'], authorization_json['oauth_token'], allow_access_levels, max_failed_attempts) authorizer() credentials.exchange_request_token_for_access_token(web_root) return cls(credentials, service_root, cache, timeout, proxy_info, version)
def __init__(self, credentials, service_root=STAGING_SERVICE_ROOT, cache=None, timeout=None, proxy_info=None, version=DEFAULT_VERSION): service_root = lookup_service_root(service_root) if (service_root.endswith(version) or service_root.endswith(version + '/')): error = ("It looks like you're using a service root that " "incorporates the name of the web service version " '("%s"). Please use one of the constants from ' "launchpadlib.uris instead, or at least remove " "the version name from the root URI." % version) raise ValueError(error) super(SimpleLaunchpad, self).__init__(credentials, service_root, cache, timeout, proxy_info, version)
def _get_paths(cls, service_root, launchpadlib_dir=None): if launchpadlib_dir is None: launchpadlib_dir = os.path.join('~', '.launchpadlib') launchpadlib_dir = os.path.expanduser(launchpadlib_dir) if launchpadlib_dir[:1] == '~': raise ValueError("Must set $HOME or pass 'launchpadlib_dir' to " "indicate location to store cached data") if not os.path.exists(launchpadlib_dir): os.makedirs(launchpadlib_dir, 0700) os.chmod(launchpadlib_dir, 0700) # Determine the real service root. service_root = lookup_service_root(service_root) # Each service root has its own cache and credential dirs. scheme, host_name, path, query, fragment = urlsplit(service_root) service_root_dir = os.path.join(launchpadlib_dir, host_name) cache_path = os.path.join(service_root_dir, 'cache') if not os.path.exists(cache_path): os.makedirs(cache_path, 0700) return (service_root, launchpadlib_dir, cache_path, service_root_dir)
def _get_paths(cls, service_root, launchpadlib_dir=None): """Locate launchpadlib-related user paths and ensure they exist. This is a helper function used by login_with() and login_anonymously(). :param service_root: The service root the user wants to connect to. This may be an alias (which will be dereferenced to a URL and returned) or a URL (which will be returned as is). :param launchpadlib_dir: The user's base launchpadlib directory, if known. This may be modified, expanded, or determined from the environment if missing. A definitive value will be returned. :return: A 4-tuple: (service_root_uri, launchpadlib_dir, cache_dir, service_root_dir) """ if launchpadlib_dir is None: launchpadlib_dir = os.path.join('~', '.launchpadlib') launchpadlib_dir = os.path.expanduser(launchpadlib_dir) if launchpadlib_dir[:1] == '~': raise ValueError("Must set $HOME or pass 'launchpadlib_dir' to " "indicate location to store cached data") try: os.makedirs(launchpadlib_dir, 0o700) except OSError as err: if err.errno != errno.EEXIST: raise os.chmod(launchpadlib_dir, 0o700) # Determine the real service root. service_root = uris.lookup_service_root(service_root) # Each service root has its own cache and credential dirs. scheme, host_name, path, query, fragment = urlsplit(service_root) service_root_dir = os.path.join(launchpadlib_dir, host_name) cache_path = os.path.join(service_root_dir, 'cache') try: os.makedirs(cache_path, 0o700) except OSError as err: if err.errno != errno.EEXIST: raise return (service_root, launchpadlib_dir, cache_path, service_root_dir)
def _get_paths(cls, service_root, launchpadlib_dir=None): if launchpadlib_dir is None: launchpadlib_dir = os.path.join('~', '.launchpadlib') launchpadlib_dir = os.path.expanduser(launchpadlib_dir) if launchpadlib_dir[:1] == '~': raise ValueError("Must set $HOME or pass 'launchpadlib_dir' to " "indicate location to store cached data") if not os.path.exists(launchpadlib_dir): os.makedirs(launchpadlib_dir, 0700) os.chmod(launchpadlib_dir, 0700) # Determine the real service root. service_root = lookup_service_root(service_root) # Each service root has its own cache and credential dirs. scheme, host_name, path, query, fragment = urlsplit( service_root) service_root_dir = os.path.join(launchpadlib_dir, host_name) cache_path = os.path.join(service_root_dir, 'cache') if not os.path.exists(cache_path): os.makedirs(cache_path, 0700) return (service_root, launchpadlib_dir, cache_path, service_root_dir)
def __init__(self, credentials, authorization_engine, credential_store, service_root=uris.STAGING_SERVICE_ROOT, cache=None, timeout=None, proxy_info=None, version=DEFAULT_VERSION): """Root access to the Launchpad API. :param credentials: The credentials used to access Launchpad. :type credentials: `Credentials` :param authorization_engine: The object used to get end-user input for authorizing OAuth request tokens. Used when an OAuth access token expires or becomes invalid during a session, or is discovered to be invalid once launchpadlib starts up. :type authorization_engine: `RequestTokenAuthorizationEngine` :param service_root: The URL to the root of the web service. :type service_root: string """ service_root = uris.lookup_service_root(service_root) if (service_root.endswith(version) or service_root.endswith(version + '/')): error = ("It looks like you're using a service root that " "incorporates the name of the web service version " '("%s"). Please use one of the constants from ' "launchpadlib.uris instead, or at least remove " "the version name from the root URI." % version) raise ValueError(error) self.credential_store = credential_store # We already have an access token, but it might expire or # become invalid during use. Store the authorization engine in # case we need to authorize a new token during use. self.authorization_engine = authorization_engine super(Launchpad, self).__init__(credentials, service_root, cache, timeout, proxy_info, version)
def __init__(self, credentials, service_root=uris.STAGING_SERVICE_ROOT, cache=None, timeout=None, proxy_info=None, version=DEFAULT_VERSION): """Root access to the Launchpad API. :param credentials: The credentials used to access Launchpad. :type credentials: `Credentials` :param service_root: The URL to the root of the web service. :type service_root: string """ service_root = uris.lookup_service_root(service_root) if (service_root.endswith(version) or service_root.endswith(version + '/')): error = ("It looks like you're using a service root that " "incorporates the name of the web service version " '("%s"). Please use one of the constants from ' "launchpadlib.uris instead, or at least remove " "the version name from the root URI." % version) raise ValueError(error) super(Launchpad, self).__init__( credentials, service_root, cache, timeout, proxy_info, version)
def __init__(self, credentials, authorization_engine, credential_store, service_root=uris.STAGING_SERVICE_ROOT, cache=None, timeout=None, proxy_info=None, version=DEFAULT_VERSION): """Root access to the Launchpad API. :param credentials: The credentials used to access Launchpad. :type credentials: `Credentials` :param authorization_engine: The object used to get end-user input for authorizing OAuth request tokens. Used when an OAuth access token expires or becomes invalid during a session, or is discovered to be invalid once launchpadlib starts up. :type authorization_engine: `RequestTokenAuthorizationEngine` :param service_root: The URL to the root of the web service. :type service_root: string """ service_root = uris.lookup_service_root(service_root) if (service_root.endswith(version) or service_root.endswith(version + '/')): error = ("It looks like you're using a service root that " "incorporates the name of the web service version " '("%s"). Please use one of the constants from ' "launchpadlib.uris instead, or at least remove " "the version name from the root URI." % version) raise ValueError(error) self.credential_store = credential_store # We already have an access token, but it might expire or # become invalid during use. Store the authorization engine in # case we need to authorize a new token during use. self.authorization_engine = authorization_engine super(Launchpad, self).__init__( credentials, service_root, cache, timeout, proxy_info, version)
def test_edge_server_equivalent_string_becomes_production(self): with self.edge_deprecation_error(): self.assertEqual( uris.lookup_service_root('https://api.edge.launchpad.net/'), uris.lookup_service_root('production'))
def test_top_level_edge_constant_becomes_production(self): with self.edge_deprecation_error(): self.assertEqual(uris.lookup_service_root(uris.EDGE_SERVICE_ROOT), uris.lookup_service_root('production'))
def test_edge_service_root_is_production(self): # The edge server no longer exists, so if the client wants # edge we give them production. with self.edge_deprecation_error(): self.assertEqual(uris.lookup_service_root('edge'), uris.lookup_service_root('production'))