home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / launchpadbugs / text_buglist.py < prev    next >
Encoding:
Python Source  |  2008-08-05  |  2.7 KB  |  77 lines

  1. """
  2. TODO:
  3.   * implement BugList.sort()
  4. """
  5. import urlparse
  6.  
  7. from bugbase import LPBugInfo, Bug as LPBug
  8. from buglistbase import LPBugList, LPBugPage
  9. from utils import valid_lp_url
  10. from lpconstants import BASEURL
  11. from lphelper import sort
  12.  
  13. class BugInfo(LPBugInfo):
  14.     # TODO: use same attribute names like Bug.Bug
  15.     def __init__(self, nr, all_tasks):
  16.         LPBugInfo.__init__(self, nr, None, None, None, None, None, all_tasks)
  17.         
  18.         
  19. class BugPage(LPBugPage):
  20.     """
  21.     grab content of a single bug-table    
  22.     """
  23.     @staticmethod
  24.     def find_parse_function(connection, url, all_tasks):
  25.         """ this can extended to parse other listtypes (like
  26.             https://edge.launchpad.net/ubuntu/+milestone/gutsy-updates"""
  27.         url = valid_lp_url(url, BASEURL.BUGPAGE)
  28.         u = urlparse.urlsplit(url)
  29.         if u[2].endswith("+bugs-text"):
  30.             pass
  31.         elif u[2].endswith("+bugs"):
  32.             url = urlparse.urlunsplit((u[0],u[1],"%s-text"%u[2],u[3],u[4]))
  33.         else:
  34.             url = urlparse.urlunsplit((u[0],u[1],"%s/+bugs-text"%u[2],u[3],u[4]))
  35.         lp_content = connection.get(url)
  36.         result = BugPage.parse_text_bugpage(lp_content.text, all_tasks, url)
  37.         return result
  38.         
  39.     @staticmethod
  40.     def parse_text_bugpage(text, all_tasks, url):
  41.         bugs = text.split("\n")
  42.         def _parse():
  43.             for i in bugs:
  44.                 if i:
  45.                     yield BugInfo(i, all_tasks)
  46.         return _parse(), False, len(bugs), len(bugs)
  47.  
  48.  
  49. class BugList(LPBugList):
  50.     """
  51.     returns a SET of BugInfo objects
  52.     searches baseurl and its following pages
  53.     """
  54.     def __init__(self, baseurl, connection=None, all_tasks=False, progress_hook=None):
  55.         if hasattr(baseurl, "baseurl"):
  56.             baseurl.baseurl = valid_lp_url(baseurl.baseurl, BASEURL.BUGLIST)
  57.         else:
  58.             baseurl = valid_lp_url(baseurl, BASEURL.BUGLIST)
  59.         LPBugList.__init__(self, baseurl, connection, all_tasks,
  60.                     BugPage, progress_hook)
  61.         
  62.     def add(self, item):
  63.         assert isinstance(item, (LPBugInfo, LPBug))
  64.         LPBugList.add(self, item)
  65.     
  66.     def sort(self, optsort):
  67.         """ returns a list of bug objects sorted by optsort
  68.         if one of the element in the list is an instance of LPBugInfo
  69.         the list can only be sorted by the bugnumber """
  70.         attribute = optsort.strip("-")
  71.         m = filter(lambda x: isinstance(x, LPBugInfo), self)
  72.         if m and not attribute == "nr":
  73.             raise TypeError, "text buglists containing LPBugInfo objects can only be sorted by nr"
  74.         cmp_func = lambda x, y: sort(x, y, attribute)
  75.         isreverse = optsort.startswith("-")
  76.         return sorted(self, cmp=cmp_func, reverse=isreverse)
  77.