| 1 |
********************* |
| 2 |
Top-level collections |
| 3 |
********************* |
| 4 |
|
| 5 |
The launchpad web service's top-level collections provide access to |
| 6 |
Launchpad-wide objects like projects and people. |
| 7 |
|
| 8 |
>>> import httplib2 |
| 9 |
>>> httplib2.debuglevel = 1 |
| 10 |
|
| 11 |
>>> from launchpadlib.testing.helpers import salgado_with_full_permissions |
| 12 |
>>> launchpad = salgado_with_full_permissions.login() |
| 13 |
send: ... |
| 14 |
... |
| 15 |
|
| 16 |
It's possible to do key-based lookups on the top-level |
| 17 |
collections. The bug collection does lookups by bug ID. |
| 18 |
|
| 19 |
>>> bug = launchpad.bugs[1] |
| 20 |
send: b'GET /.../bugs/1 ...' |
| 21 |
... |
| 22 |
|
| 23 |
To avoid triggering an HTTP request when simply looking up an object, |
| 24 |
you can use a different syntax: |
| 25 |
|
| 26 |
>>> bug = launchpad.bugs(1) |
| 27 |
|
| 28 |
The HTTP request will happen when you need information that can only |
| 29 |
be obtained from the web service. |
| 30 |
|
| 31 |
>>> print(bug.id) |
| 32 |
send: b'GET /.../bugs/1 ...' |
| 33 |
... |
| 34 |
1 |
| 35 |
|
| 36 |
Let's look at some more collections. The project collection does |
| 37 |
lookups by project name. |
| 38 |
|
| 39 |
>>> project = launchpad.projects('firefox') |
| 40 |
>>> print(project.name) |
| 41 |
send: b'GET /.../firefox ...' |
| 42 |
... |
| 43 |
firefox |
| 44 |
|
| 45 |
The project group collection does lookups by project group name. |
| 46 |
|
| 47 |
>>> group = launchpad.project_groups('gnome') |
| 48 |
>>> print(group.name) |
| 49 |
send: b'GET /.../gnome ...' |
| 50 |
... |
| 51 |
gnome |
| 52 |
|
| 53 |
The distribution collection does lookups by distribution name. |
| 54 |
|
| 55 |
>>> distribution = launchpad.distributions('ubuntu') |
| 56 |
>>> print(distribution.name) |
| 57 |
send: b'GET /.../ubuntu ...' |
| 58 |
... |
| 59 |
ubuntu |
| 60 |
|
| 61 |
The person collection does lookups by a person's Launchpad |
| 62 |
name. |
| 63 |
|
| 64 |
>>> person = launchpad.people('salgado') |
| 65 |
>>> print(person.name) |
| 66 |
send: b'GET /.../~salgado ...' |
| 67 |
... |
| 68 |
salgado |
| 69 |
|
| 70 |
>>> team = launchpad.people('rosetta-admins') |
| 71 |
>>> print(team.name) |
| 72 |
send: b'GET /1.0/~rosetta-admins ...' |
| 73 |
... |
| 74 |
rosetta-admins |
| 75 |
|
| 76 |
How does launchpadlib know that 'salgado' is a person and |
| 77 |
'rosetta-admins' is a team? |
| 78 |
|
| 79 |
>>> print(person.resource_type_link) |
| 80 |
http://.../1.0/#person |
| 81 |
>>> 'default_membership_period' in person.lp_attributes |
| 82 |
False |
| 83 |
|
| 84 |
>>> print(team.resource_type_link) |
| 85 |
http://.../1.0/#team |
| 86 |
>>> 'default_membership_period' in team.lp_attributes |
| 87 |
True |
| 88 |
|
| 89 |
The truth is that it doesn't know, not before making that HTTP |
| 90 |
request. Until an HTTP request is made, launchpadlib assumes |
| 91 |
everything in launchpad.people[] is a team (since a team has strictly |
| 92 |
more capabilities than a person). |
| 93 |
|
| 94 |
>>> person2 = launchpad.people('salgado') |
| 95 |
>>> 'default_membership_period' in person2.lp_attributes |
| 96 |
True |
| 97 |
|
| 98 |
But accessing any attribute of an object--even trying to see what kind |
| 99 |
of object 'salgado' is--will trigger the HTTP request that will |
| 100 |
determine that 'salgado' is actually a person. |
| 101 |
|
| 102 |
>>> print(person2.resource_type_link) |
| 103 |
send: b'GET /.../~salgado ...' |
| 104 |
... |
| 105 |
http://.../1.0/#person |
| 106 |
|
| 107 |
>>> 'default_membership_period' in person2.lp_attributes |
| 108 |
False |
| 109 |
|
| 110 |
Accessing an attribute of an object that might be a team will trigger |
| 111 |
the HTTP request, and then cause an error if the object turns out not |
| 112 |
to be a team. |
| 113 |
|
| 114 |
>>> person3 = launchpad.people('salgado') |
| 115 |
>>> person3.default_membership_period |
| 116 |
Traceback (most recent call last): |
| 117 |
AttributeError: ...api.launchpad.../~salgado object has no attribute 'default_membership_period' |
| 118 |
|
| 119 |
Cleanup. |
| 120 |
|
| 121 |
>>> httplib2.debuglevel = 0 |