## page was renamed from BugHelper/Dev/python-launchpad-bugs/API changes/BugListExample ## page was renamed from BugHelper/Dev/python-launchpad-bugs/API changes/BugList example ||<>|| = BugList - tutorial = '' This is still work in progress '' == General workflow == This is how to use python-launchpad-bugs to get a list of bugs {{{#!python >>> from launchpadbugs.connector import ConnectBugList >>> BugList = ConnectBugList() # using the html mode >>> bl = BugList("https://bugs.launchpad.net/ubuntu/+source/bughelper/+bugs") }}} If you need a faster method to fetch the bug list but can abdicate additional information like status and importance of a bugreport it is possible to use the text mode of launchpad: {{{#!python start=2 >>> BugList = ConnectBugList("text") # using the text mode }}} In some cases, e.g. if the resulting lists should contain private bugreports, authentication is necessary {{{#!python start=4 >>> BugList.authentication="cookie.txt" #for text-based mozilla cookie files or >>> BugList.authentication="cookie.sql" #for sql mozilla cookie files }}} It is also possible to do authentication via email-login and password. Note: This takes much longer than using cookie files, therefore python-launchpad-bugs provides a method to save the resulting cookie into a file {{{#!python start=7 >>> BugList.authentication={"email": "login@email.com", "passwort": "YourPassword"} >>> BugList.connection.save_cookie("/home/markus/.lpcookie") }}} There are currently different versions of launchpad, the stable bugs.launchpad.net and the testing environment bugs.edge.launchpad.net. Sometimes it makes sense to explicitly choose one of these versions to work with. In python-launchpad-bugs it is possible to define which version to use. {{{#!python >>> from launchpadbugs.lpconstants import HTTPCONNECTION >>> BugList.set_connection_mode(HTTPCONNECTION.MODE.EDGE) # or >>> BugList.set_connection_mode(HTTPCONNECTION.MODE.STABLE) }}} == Bug list objects == `BugList` objects are set-like objects, each member is an instance of `LPBugInfo`. {{{#!python >>> bl >>> print bl BugList([,,]) >>> len(bl) 3 }}} === LPBugInfo === A `LPBugInfo` object has this attribute: ||'''Attribute'''||'''Description'''||'''Example'''|| || .bugnumber || number of a bugreport (type of int) || || || .url || url to this bugreport (task specific) || || || .summary || one-line title of a bugreport || || || .importance || importance of a bugreport || || || .status || status of a bugreport || || || .sourcepackage || returns affected package within a distribution or an empty string|| || {{{#!python >>> b = bl.pop() >>> b >>> print b [Bug 239007 : Confirmed/Undecided] >>> int(b) 239007 }}} === Adding some (upstream) bugs to the set === It is easy to merge two `BugList`-objects {{{#!python >>> bl >>> bl += BugList("https://bugs.launchpad.net/bughelper/+bugs") >>> len(bl) 14 }}} === Filtering bug lists === With python-launchpad-bugs 0.3 there is a new way of filtering bug lists. First of all: there are two different ways of filtering * Filtering while fetching the list * Filtering the actual object The way mentioned by the last item can easily be done by using python's buildin `filter()` function and does not change the actual object.<
> In contrast the first way is irreversible. It is possible to use URL-based filter options (options provided by `advanced-search` of launchpad) or functions to filter elements. This functions take an element of the bug list as the only argument and return either `False` or an instance of `LPBugInfo` or `LPBug`. If `False` is returned the related element is filtered out of the list. As a very basic example let's have a look at all bugs in the bughelper project which have an even bugnumber and which importance is either "Medium" or "Low" {{{#!python >>> from launchpadbugs.basebuglistfilter import URLBugListFilter >>> bug_filter = URLBugListFilter() >>> bug_filter.add_option("importance", ("Medium", "Low")) >>> bug_filter.functions.append(lambda x: not int(x) % 2 and x) >>> bl = BugList(bug_filter("https://bugs.launchpad.net/bughelper/+bugs")) >>> print bl BugList([,]) }}} === Sorting the list of bugs === `LPBugList` objects do also have a `.sort()` method which returns a sorted list. `` could be nr, status or importance with an optional leading `-` for reverse order. {{{#!python >>> bl = BugList("https://bugs.launchpad.net/ubuntu/+source/bughelper/+bugs") >>> print [str(i) for i in bl] ['[Bug 239007 : Confirmed/Undecided]', '[Bug 129341 : New/Wishlist]', '[Bug 88102 : Confirmed/Undecided]', '[Bug 150887 : In Progress/Wishlist]'] >>> print [str(i) for i in bl.sort("-status")] ['[Bug 129341 : New/Wishlist]', '[Bug 239007 : Confirmed/Undecided]', '[Bug 88102 : Confirmed/Undecided]', '[Bug 150887 : In Progress/Wishlist]'] }}} == Raised Exeptions == If the url given as an argument to the buglist is not valid in launchpad a `LaunchpadURLError`-Error will be raised {{{#!python >>> from launchpadbugs.exceptions import LaunchpadURLError >>> try: ... bl = BugList("https://bugs.launchpad.net/ubuntu/+source/bughelpcxcxcver/+bugs") ... except LaunchpadURLError, e: ... print e ... * message: Page not found * url: https://bugs.launchpad.net/ubuntu/+source/bughelpcxcxcver/+bugs }}} New to python-launchpad-bugs 0.3 is that this types of errors always return the url in question. == visualize fetching progress == With python-launchpad-bugs 0.3 it is possible to visualize the progress of getting the bug lists from launchpad. This is useful for GUI based applications. {{{#!python >>> from launchpadbugs.connector import ConnectBugList >>> BugList = ConnectBugList() >>> def progress_hook(counter, batchsize, result_length): ... print counter, "/", batchsize, "/", result_length ... >>> BugList.set_progress_hook(progress_hook) >>> BugList("https://bugs.edge.launchpad.net/python-launchpad-bugs/+bugs") 1 / 25 / 30 2 / 25 / 30 >>> }}} == More on custom filter functions == Another new feature of python-launchpad-bugs 0.3 are filter functions which are able to stop the entire fetching process. As a very abstract example let's try to get a list of the first 5 bugs in ubuntu with the word 'ubuntu' in the summary. {{{#!python >>> from launchpadbugs.connector import ConnectBugList >>> from launchpadbugs.basebuglistfilter import URLBugListFilter, StopFiltering >>> class myfilter(object): ... def __init__(self): ... self.counter = 0 ... def __call__(self, bug): ... if self.counter > 5: ... raise StopFiltering ... if "ubuntu" in bug.summary.lower(): ... self.counter += 1 ... return bug ... else: ... return False ... >>> BugList = ConnectBugList() >>> bug_filter = URLBugListFilter() >>> bug_filter.functions.append(myfilter()) >>> BugList(bug_filter("https://bugs.edge.launchpad.net/ubuntu/+bugs")) BugList([,,,,,]) }}} Some more useful examples can be found in [[http://bazaar.launchpad.net/~bughelper-dev/python-launchpad-bugs/main/annotate/head:/launchpadbugs/basebuglistfilter.py|basebuglistfilter.py]], one of them is the `datereported` filter function which allows getting a list of all bugs reported on a given date or in a given time period. ---- Go Back to '''[[BugHelper/Dev]]'''.<
> CategoryBugHelper