Diff for "API/Examples"

Not logged in - Log In / Register

Differences between revisions 10 and 24 (spanning 14 versions)
Revision 10 as of 2009-11-25 20:38:33
Size: 3042
Editor: 70-8-217-239
Comment: Make the first example be how to find out your launchpadlib version.
Revision 24 as of 2010-07-24 13:14:13
Size: 8860
Editor: 5351F837
Comment:
Deletions are marked like this. Additions are marked like this.
Line 12: Line 12:
   import launchpadlib
   print launchpadlib.__version__
}}}

'''1.5.1''' or above is the answer you're looking for; almost all subsequent examples assume you have at least that recent a launchpadlib.

== Hello Launchpad! ==

Before an API client can do anything, it needs to login to Launchpad. There are three possibilities:

 * "Anonymous login", which does not require user interaction but of course gives you only read-only access to public data
 * Access level requested by the client: the user must confirm they want to allow that level of access
 * Access level specified by the user. This may be confusing if the client program expects to be able to write but it's not allowed.

Ever wanted to have Launchpad greet you by your own name? Now you can, in the comfort of your own home.

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

The `hello-world` bit is the name of the application and 'edge' means connect to the edge server.

If your app is only going to read public data from launchpad, and not write it you can make the process
more user-friendly by telling Launchpad that you only care about reading public data.

{{{
   from launchpadlib.launchpad import Launchpad
   launchpad = Launchpad.login_anonymously('just testing', 'edge')
}}}

You can change the argument to be any combination of

  * READ_PUBLIC
  * READ_PRIVATE
  * WRITE_PUBLIC
  * WRITE_PRIVATE

that your want to offer the users. Each one you leave out will be one less option presented to
the user, and one less way that things could go wrong if they happen to choose it.

This will save you from having to do something like telling the user "go the web page
that just opened and click the 'Read public' button", as they will only have the
choice between doing that or not granting any access at all.

See also [[API/launchpadlib#Getting started|guide to app login]].

== Does a bug have a release target? ==

{{{
   from launchpadlib.launchpad import Launchpad
   
   def has_target(bug, series):
       series_url = str(series)
       for task in bug.bug_tasks:
           if str(task).startswith(series_url):
               return True
       return False
   
   launchpad = Launchpad.login_with(
       'hello-world', 'edge')
   b = launchpad.bugs[324614]
   ubuntu = launchpad.distributions["ubuntu"]
   jaunty = ubuntu.getSeries(name_or_version="jaunty")
   has_target(b, jaunty)
   ### ==> should evalute to True
}}}

== Listing the current package versions in a particular distroseries ==

{{{
   from launchpadlib.launchpad import Launchpad
   
   launchpad = Launchpad.login_with(
       'hello-world', 'edge')
   ubuntu = launchpad.distributions["ubuntu"]
   archive = ubuntu.main_archive
   series = ubuntu.current_series
   archive.getPublishedSources(exact_match=True, source_name="apport", distro_series=series)[0].source_package_version
   ### ==> should return u'0.123'
}}}

== Get dsc-files for sources in an archive ==

{{{
   import re
   import urlparse
   
   ### See previous examples for how to get an archive.
                   
   def create_webroot_url_from_self_link(self_link):
       scheme, netloc, _, _, _ = urlparse.urlsplit(self_link)
       netloc = netloc.lstrip("api.")
       return u"%s://%s/" %(scheme, netloc)
       
   def get_dsc(archive):
       re_version = re.compile(r"^\d+\:")
       x = archive.getPublishedSources()
       webroot = create_webroot_url_from_self_link(archive.self_link)
       for i in x:
           version = i.source_package_version
           version = re_version.sub("", version, 1)
           yield "%s~%s/+archive/+files/%s_%s.dsc" \
               %(webroot, archive.owner.name, i.source_package_name, version)
}}}

== Cache Launchpad credentials per application ==

''This one is for older launchpadlibs. If you are using a current version, just replace the code below with `Launchpad.login_with`.''

From <https://launchpad.net/hydrazine> - use your own application name.

{{{
   def create_session():
       lplib_cachedir = os.path.expanduser("~/.cache/launchpadlib/")
       hydrazine_cachedir = os.path.expanduser("~/.cache/hydrazine/")
       rrd_dir = os.path.expanduser("~/.cache/hydrazine/rrd")
       for d in [lplib_cachedir, hydrazine_cachedir, rrd_dir]:
           if not os.path.isdir(d):
               os.makedirs(d, mode=0700)
   
   
       hydrazine_credentials_filename = os.path.join(hydrazine_cachedir,
           'credentials')
       if os.path.exists(hydrazine_credentials_filename):
           credentials = Credentials()
           credentials.load(file(
               os.path.expanduser("~/.cache/hydrazine/credentials"),
               "r"))
           trace('loaded existing credentials')
           return Launchpad(credentials, service_root,
               lplib_cachedir)
           # TODO: handle the case of having credentials that have expired etc
       else:
           launchpad = Launchpad.get_token_and_login(
               'Hydrazine',
               service_root,
               lplib_cachedir)
           trace('saving credentials...')
           launchpad.credentials.save(file(
               hydrazine_credentials_filename,
               "w"))
           return launchpad
}}}

== Get date a user joined a team ==

This is an example of using team_membership details

{{{
def get_join_date(team, user):
    team = launchpad.people[team]
    members = team.members_details
    for member in members:
        if member.member.name == user:
            return member.date_joined
    return None

print get_join_date("zeitgeist", "thekorn")
### ==> should return a datetime.datetime object like 2009-06-14 18:01:10.511369+00:00
}}}

== Turn on debugging output ==

{{{
import httplib2
httplib2.debuglevel = 1
}}}

This enables detailed traces of requests launchpadlib makes. This can be worthwhile for debugging issues or optimizing performance. (See also [[http://launchpad.net/bugs/520219|bug 520219]] asking for better logging here.)

== Get a useful error message from launchpadlib ==

''Recent versions of launchpadlib include useful information in the str() of the exception object, so you don't need to do this''

Because launchpadlib is just a simple wrapper for an HTTP API, when the Launchpad server raises an error, this appears on the client side as an HTTP error. However, there is useful information to be had!

{{{
Line 13: Line 192:
import launchpadlib
print launchpadlib.__version__
}}}

'''1.5.1''' or above is the answer you're looking for; all subsequent examples assume you have at least that recent a launchpadlib.

== Hello Launchpad! ==

Ever wanted to have Launchpad greet you by your own name? Now you can, in the comfort of your own home.
try:
    do_something_errorful()
except HTTPError, e:
    # e.content has the actual Launchpad error.
    print e.content
}}}


== Fetching an object's raw JSON ==

Launchpadlib provides a nice Python wrapper around JSON objects, but it does allow you to directly access the JSON itself. Each launchpadlib object has a `self_link` property which you can use to view the JSON in a regular web brower, but you can not do this while using launchpadlib's access permissions. This recipe shows you how to fetch the JSON for an object with the same permissions as the currently running script.

We can use the semi-private `_browser` member of the current `Launchpad` object to grab the raw JSON using the current authentication. We can pass a launchpadlib object's `self_link` URL to the browser, the same as launchpadlib itself does.
Line 25: Line 208:
from launchpadlib.launchpad import Launchpad, EDGE_SERVICE_ROOT
launchpad = Launchpad.login_with(
    'hello-world', EDGE_SERVICE_ROOT)
print 'Hello, %s!' % launchpad.me.display_name
}}}

The `hello-world` bit is the name of the application and EDGE_SERVICE_ROOT means connect to the edge server.

== Does a bug have a release target? ==

{{{
#!python
from launchpadlib.launchpad import Launchpad, EDGE_SERVICE_ROOT

def has_target(bug, series):
    series_url = str(series)
    for task in bug.bug_tasks:
        if str(task).startswith(series_url):
            return True
    return False

launchpad = Launchpad.login_with(
    'hello-world', EDGE_SERVICE_ROOT)
b = launchpad.bugs[324614]
ubuntu = launchpad.distributions["ubuntu"]
jaunty = ubuntu.getSeries(name_or_version="jaunty")
has_target(b, jaunty)
### ==> should evalute to True
}}}

== Listing the current package versions in a particular distroseries ==

{{{
#!python
Line 61: Line 211:
launchpad = Launchpad.login_with(
    'hello-world', 'production')
ubuntu = launchpad.distributions["ubuntu"]
archive = ubuntu.main_archive
series = ubuntu.current_series
archive.getPublishedSources(exact_match=True, source_name="apport", distro_series=series)[0].source_package_version
### ==> should return u'0.123'
}}}

== Get dsc-files for sources in an archive ==

{{{
#!python
import re
import urlparse

### See previous examples for how to get an archive.
                
def create_webroot_url_from_self_link(self_link):
    scheme, netloc, _, _, _ = urlparse.urlsplit(self_link)
    netloc = netloc.lstrip("api.")
    return u"%s://%s/" %(scheme, netloc)
launchpad = Launchpad.login_with('lplib.cookbook.json_fetcher', 'edge', '.lplib-json_fetcher-cache')

# Our authenticated browser object
browser = launchpad._browser

def get_person_as_json(person_name):
    person = launchpad.people[person_name]
    if not person:
        # Oops, this person does not exist.
        return None
Line 84: Line 222:
def get_dsc(archive):
    re_version = re.compile(r"^\d+\:")
    x = archive.getPublishedSources()
    webroot = create_webroot_url_from_self_link(archive.self_link)
    for i in x:
        version = i.source_package_version
        version = re_version.sub("", version, 1)
        yield "%s~%s/+archive/+files/%s_%s.dsc" \
            %(webroot, archive.owner.name, i.source_package_name, version)
}}}
    return browser.get(person.self_link)
}}}

== Get the type of requested code review ==

The trick here ([[https://bugs.launchpad.net/bugs/526362|bug 526362]]) is that the review type is actually an attribute of the pending review, and the pending review is recorded as a 'vote' with no vote or comment. So you need to iterate the {{{votes}}} attribute of the merge proposal.

Launchpad Help > API > Examples

This page has a bunch of examples of how to use launchpadlib and the Python APIs. Think of it like a cookbook that you can add your favourite recipe to.

If this duplicates launchpadlib or API/Uses too much, then please merge or edit pages as needed.

Find out if your launchpadlib version is recent enough (>= 1.5.1)

   import launchpadlib
   print launchpadlib.__version__

1.5.1 or above is the answer you're looking for; almost all subsequent examples assume you have at least that recent a launchpadlib.

Hello Launchpad!

Before an API client can do anything, it needs to login to Launchpad. There are three possibilities:

  • "Anonymous login", which does not require user interaction but of course gives you only read-only access to public data
  • Access level requested by the client: the user must confirm they want to allow that level of access
  • Access level specified by the user. This may be confusing if the client program expects to be able to write but it's not allowed.

Ever wanted to have Launchpad greet you by your own name? Now you can, in the comfort of your own home.

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

The hello-world bit is the name of the application and 'edge' means connect to the edge server.

If your app is only going to read public data from launchpad, and not write it you can make the process more user-friendly by telling Launchpad that you only care about reading public data.

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

You can change the argument to be any combination of

  • READ_PUBLIC
  • READ_PRIVATE
  • WRITE_PUBLIC
  • WRITE_PRIVATE

that your want to offer the users. Each one you leave out will be one less option presented to the user, and one less way that things could go wrong if they happen to choose it.

This will save you from having to do something like telling the user "go the web page that just opened and click the 'Read public' button", as they will only have the choice between doing that or not granting any access at all.

See also guide to app login.

Does a bug have a release target?

   from launchpadlib.launchpad import Launchpad
   
   def has_target(bug, series):
       series_url = str(series)
       for task in bug.bug_tasks:
           if str(task).startswith(series_url):
               return True
       return False
   
   launchpad = Launchpad.login_with(
       'hello-world', 'edge')
   b = launchpad.bugs[324614]
   ubuntu = launchpad.distributions["ubuntu"]
   jaunty = ubuntu.getSeries(name_or_version="jaunty")
   has_target(b, jaunty)
   ### ==> should evalute to True

Listing the current package versions in a particular distroseries

   from launchpadlib.launchpad import Launchpad
   
   launchpad = Launchpad.login_with(
       'hello-world', 'edge')
   ubuntu = launchpad.distributions["ubuntu"]
   archive = ubuntu.main_archive
   series = ubuntu.current_series
   archive.getPublishedSources(exact_match=True, source_name="apport", distro_series=series)[0].source_package_version
   ### ==> should return u'0.123'

Get dsc-files for sources in an archive

   import re
   import urlparse
   
   ### See previous examples for how to get an archive.
                   
   def create_webroot_url_from_self_link(self_link):
       scheme, netloc, _, _, _ = urlparse.urlsplit(self_link)
       netloc = netloc.lstrip("api.")
       return u"%s://%s/" %(scheme, netloc)
       
   def get_dsc(archive):
       re_version = re.compile(r"^\d+\:")
       x = archive.getPublishedSources()
       webroot = create_webroot_url_from_self_link(archive.self_link)
       for i in x:
           version = i.source_package_version
           version = re_version.sub("", version, 1)
           yield "%s~%s/+archive/+files/%s_%s.dsc" \
               %(webroot, archive.owner.name, i.source_package_name, version)

Cache Launchpad credentials per application

This one is for older launchpadlibs. If you are using a current version, just replace the code below with Launchpad.login_with.

From <https://launchpad.net/hydrazine> - use your own application name.

   def create_session():
       lplib_cachedir = os.path.expanduser("~/.cache/launchpadlib/")
       hydrazine_cachedir = os.path.expanduser("~/.cache/hydrazine/")
       rrd_dir = os.path.expanduser("~/.cache/hydrazine/rrd")
       for d in [lplib_cachedir, hydrazine_cachedir, rrd_dir]:
           if not os.path.isdir(d):
               os.makedirs(d, mode=0700)
   
   
       hydrazine_credentials_filename = os.path.join(hydrazine_cachedir,
           'credentials')
       if os.path.exists(hydrazine_credentials_filename):
           credentials = Credentials()
           credentials.load(file(
               os.path.expanduser("~/.cache/hydrazine/credentials"),
               "r"))
           trace('loaded existing credentials')
           return Launchpad(credentials, service_root,
               lplib_cachedir)
           # TODO: handle the case of having credentials that have expired etc
       else:
           launchpad = Launchpad.get_token_and_login(
               'Hydrazine',
               service_root,
               lplib_cachedir)
           trace('saving credentials...')
           launchpad.credentials.save(file(
               hydrazine_credentials_filename,
               "w"))
           return launchpad

Get date a user joined a team

This is an example of using team_membership details

def get_join_date(team, user):
    team = launchpad.people[team]
    members = team.members_details
    for member in members:
        if member.member.name == user:
            return member.date_joined
    return None

print get_join_date("zeitgeist", "thekorn")
### ==> should return a datetime.datetime object like  2009-06-14 18:01:10.511369+00:00

Turn on debugging output

import httplib2
httplib2.debuglevel = 1

This enables detailed traces of requests launchpadlib makes. This can be worthwhile for debugging issues or optimizing performance. (See also bug 520219 asking for better logging here.)

Get a useful error message from launchpadlib

Recent versions of launchpadlib include useful information in the str() of the exception object, so you don't need to do this

Because launchpadlib is just a simple wrapper for an HTTP API, when the Launchpad server raises an error, this appears on the client side as an HTTP error. However, there is useful information to be had!

   1 try:
   2     do_something_errorful()
   3 except HTTPError, e:
   4     # e.content has the actual Launchpad error.
   5     print e.content

Fetching an object's raw JSON

Launchpadlib provides a nice Python wrapper around JSON objects, but it does allow you to directly access the JSON itself. Each launchpadlib object has a self_link property which you can use to view the JSON in a regular web brower, but you can not do this while using launchpadlib's access permissions. This recipe shows you how to fetch the JSON for an object with the same permissions as the currently running script.

We can use the semi-private _browser member of the current Launchpad object to grab the raw JSON using the current authentication. We can pass a launchpadlib object's self_link URL to the browser, the same as launchpadlib itself does.

   1 from launchpadlib.launchpad import Launchpad
   2 
   3 launchpad = Launchpad.login_with('lplib.cookbook.json_fetcher', 'edge', '.lplib-json_fetcher-cache')
   4 
   5 # Our authenticated browser object
   6 browser = launchpad._browser
   7 
   8 def get_person_as_json(person_name):
   9     person = launchpad.people[person_name]
  10     if not person:
  11         # Oops, this person does not exist.
  12         return None
  13     
  14     return browser.get(person.self_link)

Get the type of requested code review

The trick here (bug 526362) is that the review type is actually an attribute of the pending review, and the pending review is recorded as a 'vote' with no vote or comment. So you need to iterate the votes attribute of the merge proposal.

API/Examples (last edited 2024-06-03 10:18:34 by ruinedyourlife)