toplevel.rst 3.3 KB

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