o ;a u@sdZeZdgZddlZddlZzddlmZWney&ddl mZYnwddl Z zddl m Z WneyEddl m Z e jZ YnwddlmZmZmZmZddlmZdd lmZdd lmZmZmZmZmZmZmZm Z m!Z!dd l"m#Z#dd l$m%Z%m&Z&d Z'GdddeZ(GdddeZ)GdddeZ*Gddde*Z+Gddde*Z,Gddde*Z-GdddeZ.GdddeZ/dS)zRoot Launchpad API class. LaunchpadN)urlsplit)proxy_info_from_environment) ProxyInfo)CollectionWithKeyBasedLookup HostedFile ScalarValue ServiceRoot)SystemWideConsumer) RestfulHttp) AccessTokenAnonymousAccessToken AuthorizeRequestTokenWithBrowserAuthorizeRequestTokenWithURLConsumer CredentialsMemoryCredentialStoreKeyringCredentialStoreUnencryptedFileCredentialStore)uris)STAGING_SERVICE_ROOTEDGE_SERVICE_ROOTzhttps://api.launchpad.netc@eZdZdZddZdZdS) PersonSetz7A custom subclass capable of person lookup by username.cCt|jjdt|S)z7Transform a username into the URL to a person resource.~str_root _root_uri ensureSlashselfkeyr$8/usr/lib/python3/dist-packages/launchpadlib/launchpad.py_get_url_from_idJzPersonSet._get_url_from_idteamN__name__ __module__ __qualname____doc__r& collection_ofr$r$r$r%rGsrc@r)BugSetz2A custom subclass capable of bug lookup by bug ID.cCr)z2Transform a bug ID into the URL to a bug resource.zbugs/rr!r$r$r%r&Wr'zBugSet._get_url_from_idbugNr)r$r$r$r%r/Tsr/c@seZdZdZddZdZdS) PillarSetzzA custom subclass capable of lookup by pillar name. Projects, project groups, and distributions are all pillars. cCst|jjt|S)zr?args __class__r$r%rAsz LaunchpadOAuthAwareHttp.__init__cCs(|jdko|dp|dp|dS)z=Helper method to detect an error caused by a bad OAuth token.is Expired tokens Invalid tokensUnknown access token)status startswith)r"responsecontentr$r$r%_bad_oauth_tokens  z(LaunchpadOAuthAwareHttp._bad_oauth_tokencs(tt|j|\}}|j||g|RSr=)r@r<_requestretry_on_bad_token)r"rBrGrHrCr$r%rJs z LaunchpadOAuthAwareHttp._requestcGsF|||r|jdurd|jj_||jj|jj|j|S||fS)zxIf the response indicates a bad token, get a new token and retry. Otherwise, just return the response. N)rIr?r> credentials access_tokencredential_storerJ)r"rGrHrBr$r$r%rKs     z*LaunchpadOAuthAwareHttp.retry_on_bad_token) r*r+r,r-rArIrJrK __classcell__r$r$rCr%r<s   r<cs<eZdZdZdZeeeee dZ e e j e jddeeffdd ZddZed d Zed d Zed dZee jddedddddef ddZee jddedgdddef ddZeddZee jddeefddZede jddeddddedddf ddZeddZe d ddZed!ddZZS)"rzRoot Launchpad API class. :ivar credentials: The credentials instance used to access Launchpad. :type credentials: `Credentials` z1.0)bugs distributionspeopleproject_groupsprojectsNc s\t|}||s||drd|} t| ||_||_tt|||||||dS)arRoot 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 /zIt 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.N) rlookup_service_rootendswith ValueErrorrNr?r@rrA) r"rLr?rN service_rootcachetimeout proxy_infoversionerrorrCr$r%rAs   zLaunchpad.__init__cCst||j||||Sr=)r<r?)r"rLrZr[r\r$r$r% httpFactoryszLaunchpad.httpFactorycCshdttj@S)N>SUDO_GIDSUDO_UID SUDO_USER)setosenvironkeys)clsr$r$r%_is_sudoszLaunchpad._is_sudocGs|rt|St|Sr=)rhrr)rgrBr$r$r%authorization_engine_factorysz&Launchpad.authorization_engine_factorycCs|rt|St|ddS)NT)fallback)rhrr)rgcredential_save_failedr$r$r%credential_store_factorys z"Launchpad.credential_store_factoryc Cs^|dt||}t||d}|dur|||| }| dur$|| } |||| ||||| S)aConvenience method for setting up access credentials. When all three pieces of credential information (the consumer name, the access token and the access secret) are available, this method can be used to quickly log into the service root. This method is deprecated as of launchpadlib version 1.9.0. You should use Launchpad.login_anonymously() for anonymous access, and Launchpad.login_with() for all other purposes. :param consumer_name: the application name. :type consumer_name: string :param token_string: the access token, as appropriate for the `AccessToken` constructor :type token_string: string :param access_secret: the access token's secret, as appropriate for the `AccessToken` constructor :type access_secret: string :param service_root: The URL to the root of the web service. :type service_root: string :param authorization_engine: See `Launchpad.__init__`. If you don't provide an authorization engine, a default engine will be constructed using your values for `service_root` and `credential_save_failed`. :param allow_access_levels: This argument is ignored, and only present to preserve backwards compatibility. :param max_failed_attempts: This argument is ignored, and only present to preserve backwards compatibility. :return: The web service root :rtype: `Launchpad` login) consumer_namerMN) _warn_of_deprecated_login_methodr rrirl)rgrn token_string access_secretrYrZr[r\r?allow_access_levelsmax_failed_attemptsrNrkr]rMrLr$r$r%rm s. 1 zLaunchpad.loginc Cs&|d||||||||| | | S)aGet credentials from Launchpad and log into the service root. This method is deprecated as of launchpadlib version 1.9.0. You should use Launchpad.login_anonymously() for anonymous access and Launchpad.login_with() for all other purposes. :param consumer_name: Either a consumer name, as appropriate for the `Consumer` constructor, or a premade Consumer object. :type consumer_name: string :param service_root: The URL to the root of the web service. :type service_root: string :param authorization_engine: See `Launchpad.__init__`. If you don't provide an authorization engine, a default engine will be constructed using your values for `service_root` and `credential_save_failed`. :param allow_access_levels: This argument is ignored, and only present to preserve backwards compatibility. :return: The web service root :rtype: `Launchpad` get_token_and_login)ro_authorize_token_and_login) rgrnrYrZr[r\r?rrrsrNrkr]r$r$r%rtUs $zLaunchpad.get_token_and_loginc  Cst|tr|} nt|} td} | | _|dur|||d|}|dur)|| }n |d| |jd| |j } | durB|| |} n| } |j | j_ || ||||||| S)aEAuthorize a request token. Log in with the resulting access token. This is the private, non-deprecated implementation of the deprecated method get_token_and_login(). Once get_token_and_login() is removed, this code can be streamlined and moved into its other call site, login_with(). NrkrN) isinstancerr rconsumerrirl"_assert_login_argument_consistencyrkloadunique_consumer_idapplication_name)rgrnrYrZr[r\r?rrrNrkr]rwrLcached_credentialsr$r$r%rusJ  z$Launchpad._authorize_token_and_loginc Cs>|||\}}}}t} t|| d} || dd|||||dS)z:Get access to Launchpad without providing any credentials.)rMN)rYrZr[r\r]) _get_pathsr r) rgrnrYlaunchpadlib_dirr[r\r] cache_pathservice_root_dirtokenrLr$r$r%login_anonymouslys$  zLaunchpad.login_anonymouslyc Cs|||\}}}}|dur| dur|durtd| dur$tjd} | dur0| dur0td| durD| dur>t| | } n|| } n |d| | jd| } |dur\| ||| |}n|d||j |d| |j j |d ||j ||j ||||||| | | S) aLog in to Launchpad, possibly acquiring and storing credentials. Use this method to get a `Launchpad` object. If the end-user has no cached Launchpad credential, their browser will open and they'll be asked to log in and authorize a desktop integration. The authorized Launchpad credential will be stored securely: in the GNOME keyring, the KDE Wallet, or in an encrypted file on disk. The next time your program (or any other program run by that user on the same computer) invokes this method, the end-user will be prompted to unlock their keyring (or equivalent), and the credential will be retrieved from local storage and reused. You can customize this behavior in three ways: 1. Pass in a filename to `credentials_file`. The end-user's credential will be written to that file, and on subsequent runs read from that file. Alternatively the filename can be given in the LP_CREDENTIALS_FILE environment variable. 2. Subclass `CredentialStore` and pass in an instance of the subclass as `credential_store`. This lets you change how the end-user's credential is stored and retrieved locally. 3. Subclass `RequestTokenAuthorizationEngine` and pass in an instance of the subclass as `authorization_engine`. This lets you change change what happens when the end-user needs to authorize the Launchpad credential. :param application_name: The application name. This is *not* the OAuth consumer name. Unless a consumer_name is also provided, the OAuth consumer will be a system-wide consumer representing the end-user's computer as a whole. :type application_name: string :param service_root: The URL to the root of the web service. :type service_root: string. Can either be the full URL to a service or one of the short service names. :param launchpadlib_dir: The directory used to store cached data obtained from Launchpad. The cache is shared by all consumers, and each Launchpad service root has its own cache. :type launchpadlib_dir: string :param authorization_engine: A strategy for getting the end-user to authorize an OAuth request token, for exchanging the request token for an access token, and for storing the access token locally so that it can be reused. By default, launchpadlib will open the end-user's web browser to have them authorize the request token. :type authorization_engine: `RequestTokenAuthorizationEngine` :param allow_access_levels: The acceptable access levels for this application. This argument is used to construct the default `authorization_engine`, so if you pass in your own `authorization_engine` any value for this argument will be ignored. This argument will also be ignored unless you also specify `consumer_name`. :type allow_access_levels: list of strings :param max_failed_attempts: Ignored; only present for backwards compatibility. :param credentials_file: The path to a file in which to store this user's OAuth access token. :param version: The version of the Launchpad web service to use. :param consumer_name: The consumer name, as appropriate for the `Consumer` constructor. You probably don't want to provide this, since providing it will prevent you from taking advantage of desktop-wide integration. :type consumer_name: string :param credential_save_failed: a callback that is called upon a failure to save the credentials locally. This argument is used to construct the default `credential_store`, so if you pass in your own `credential_store` any value for this argument will be ignored. :type credential_save_failed: A callable :param credential_store: A strategy for storing an OAuth access token locally. By default, tokens are stored in the GNOME keyring (or equivalent). If `credentials_file` is provided, then tokens are stored unencrypted in that file. :type credential_store: `CredentialStore` :return: A web service root authorized as the end-user. :rtype: `Launchpad` NzZAt least one of application_name, consumer_name, or authorization_engine must be provided.LP_CREDENTIALS_FILEzFAt most one of credentials_file and credential_store must be provided.rkrNr{rnrr)r}rXrdregetrrlrxrkrir{rwr#rrru)rgr{rYr~r[r\r?rrrscredentials_filer]rnrkrNrrr$r$r% login_withs w  zLaunchpad.login_withcCstd|tdS)NzThe Launchpad.%s() method is deprecated. You should use Launchpad.login_anonymous() for anonymous access and Launchpad.login_with() for all other purposes.)warningswarnDeprecationWarning)rgnamer$r$r%ros z*Launchpad._warn_of_deprecated_login_methodauthorization enginecCs4d}|dur||krt|||||||fdSdS)aHelper to find conflicting values passed into the login methods. Many of the arguments to login_with are used to build other objects--the authorization engine or the credential store. If these objects are provided directly, many of the arguments become redundant. We'll allow redundant arguments through, but if a argument *conflicts* with the corresponding value in the provided object, we raise an error. zInconsistent values given for %s: (%r passed in, versus %r in %s). You don't need to pass in %s if you pass in %s, so just omit that argument.N)rX)rg argument_nameargument_value object_value object_nameinconsistent_value_messager$r$r%rxsz,Launchpad._assert_login_argument_consistencyc Cs|dur tjdd}tj|}|dddkrtdzt|dWnty>}z |jtjkr4WYd}~nd}~wwt |dt |}t |\}}}}}tj||} tj| d} zt| dWnty}z |jtjkrxWYd}~nd}~ww||| | fS)aLocate 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) Nrz .launchpadlibzSMust set $HOME or pass 'launchpadlib_dir' to indicate location to store cached datairZ) rdpathjoin expanduserrXmakedirsOSErrorerrnoEEXISTchmodrrVr) rgrYr~errscheme host_namerqueryfragmentrrr$r$r%r}s:        zLaunchpad._get_paths)rr=)r*r+r,r-DEFAULT_VERSIONr/r:rr7r3RESOURCE_TYPE_CLASSESupdater rrrrAr_ classmethodrhrirlrmrtrurrrorxr}rOr$r$rCr%rs 0     H 1 S  P   #)0r-type __metaclass____all__rrd urllib.parser ImportErrorurlparserhttplib2rrfrom_environmentlazr.restfulclient.resourcerrrr "lazr.restfulclient.authorize.oauthr lazr.restfulclient._browserr launchpadlib.credentialsr r rrrrrrr launchpadlibrlaunchpadlib.urisrr OAUTH_REALMrr/r1r3r7r:r<rr$r$r$r%sB      ,   )