home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / launchpadbugs / buglistbase.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  9.3 KB  |  241 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''TODO:
  5.  * move urlcheck to subclasses'''
  6. import copy
  7. import itertools
  8. from exceptions import LaunchpadError
  9.  
  10. class LPBugPage(object):
  11.     """ Base-class which represents one batch of bugs
  12.     
  13.     Each subclass must implement a staticmethod 'find_parse_function'
  14.     which delegates the actual parsing.
  15.     A LPBugPage-object is iterable and has aditional attributes
  16.       - .parser: generator over elements of the page, this generator
  17.           is also accesible directly via __iter__
  18.       - .following_page: False or url of next batch
  19.       - .batchsize: number of items on this bugpage
  20.       - .length: overall size of the search result
  21.     """
  22.     
  23.     def find_parse_function(connection, url, all_tasks):
  24.         raise NotImplementedError, 'this method must be implemented by a concrete subclass'
  25.  
  26.     find_parse_function = staticmethod(find_parse_function)
  27.     
  28.     def __init__(self, url, connection, all_tasks = False):
  29.         if not hasattr(connection, 'get'):
  30.             raise AssertionError, 'Connection object needed'
  31.         (self.parser, self.following_page, self.batchsize, self.length) = self.find_parse_function(connection, url, all_tasks)
  32.  
  33.     
  34.     def __iter__(self):
  35.         return self.parser
  36.  
  37.  
  38.  
  39. class _ProgressWrapper(object):
  40.     ''' helper-class to fake the behavoiur of the progress_hook for
  41.     the connections object.
  42.     '''
  43.     
  44.     def __init__(self, hook, blocksize):
  45.         self.counter = 0
  46.         self.ticks = 0
  47.         self.hook = hook
  48.         self.blocksize = blocksize
  49.  
  50.     
  51.     def reset(self):
  52.         self.counter = 0
  53.         self.ticks = 0
  54.  
  55.     
  56.     def __call__(self, size, blocksize = None):
  57.         self.counter += 1
  58.  
  59.  
  60.  
  61. class LPBugList(set):
  62.     ''' set-like representation of a launchpad query
  63.     
  64.     TODO:
  65.         * on-demand parsing -thekorn: DONE, we are using generators now, that should be enough
  66.         * documentation
  67.     '''
  68.     
  69.     def _create_progress_hook(hook_func, blocksize = 25):
  70.         if not blocksize:
  71.             raise AssertionError, 'blocksize needs to be an integer greater than 0'
  72.         if not callable(hook_func):
  73.             raise AssertionError, 'hook_func needs to be callable with three arguments'
  74.         return _ProgressWrapper(hook_func, blocksize)
  75.  
  76.     _create_progress_hook = staticmethod(_create_progress_hook)
  77.     
  78.     def __new__(cls, *args, **kwargs):
  79.         ''' this is necessary to allow keyword-arguments in python2.4 '''
  80.         obj = set.__new__(cls)
  81.         return obj
  82.  
  83.     
  84.     def __init__(self, baseurl, connection = None, all_tasks = False, helper_bugpage = None, progress_hook = None):
  85.         self._LPBugList__args = locals().copy()
  86.         del self._LPBugList__args['self']
  87.         if not connection:
  88.             raise AssertionError, 'Connection object needed'
  89.         self._LPBugList__connection = connection
  90.         self._LPBugList__all_tasks = all_tasks
  91.         self.set_progress_hook(progress_hook)
  92.         if hasattr(baseurl, 'baseurl') and hasattr(baseurl, 'functions'):
  93.             self.baseurl = baseurl.baseurl
  94.             self._LPBugList__filter = baseurl.functions
  95.         else:
  96.             self.baseurl = baseurl
  97.             self._LPBugList__filter = set()
  98.         self.class_helper_bugpage = helper_bugpage
  99.         set.__init__(self)
  100.         self._add(self.baseurl)
  101.  
  102.     
  103.     def __repr__(self):
  104.         return '<BugList %s>' % self.baseurl.split('?')[0]
  105.  
  106.     
  107.     def __str__(self):
  108.         return 'BugList([%s])' % ','.join((lambda .0: for i in .0:
  109. repr(i))(self))
  110.  
  111.     
  112.     def __copy__(self):
  113.         return self.__class__(**self._LPBugList__args)
  114.  
  115.     
  116.     def copy(self):
  117.         return copy.copy(self)
  118.  
  119.     
  120.     def __iadd__(self, other):
  121.         ''' let l be an instance of BugList,
  122.         l += BugList(<LP-URL>) will add bugs to l
  123.         l.add(<LP-URL>) is still possible
  124.         '''
  125.         for bug in other:
  126.             self.add(bug)
  127.         
  128.         return self
  129.  
  130.     
  131.     def sort(self, optsort):
  132.         ''' returns a LIST of bugs sorted by optsort '''
  133.         raise NotImplementedError
  134.  
  135.     
  136.     def _fetch(self, url):
  137.         """
  138.         searches given bugpage for bugs and 'next'-page and returns both values
  139.         
  140.         if calling <url> returns an LaunchpadError, this error will be
  141.         ignored if there are still bugs in the buglist, otherwise raised
  142.         again
  143.         """
  144.         if self.class_helper_bugpage is None:
  145.             raise NotImplementedError, 'this method must be implemented by a concrete subclass'
  146.         self.class_helper_bugpage is None
  147.         
  148.         try:
  149.             bugpage = self.class_helper_bugpage(url, connection = self._LPBugList__connection, all_tasks = self._LPBugList__all_tasks)
  150.         except LaunchpadError:
  151.             bugpage = None
  152.             if not self:
  153.                 raise 
  154.             self
  155.  
  156.         return bugpage
  157.  
  158.     
  159.     def _add(self, url, follow = True):
  160.         ''' adds bugs to the buglist '''
  161.         follow_page = True
  162.         bugpage = None
  163.         if self._LPBugList__progress_hook is not None:
  164.             self._LPBugList__progress_hook.reset()
  165.         
  166.         while url and follow_page:
  167.             bugpage = self._fetch(url)
  168.             if bugpage is None:
  169.                 break
  170.             
  171.             if self._LPBugList__filter:
  172.                 b = self._stoppable_filter(bugpage)
  173.             elif self._LPBugList__progress_hook is None:
  174.                 b = bugpage
  175.             else:
  176.                 b = (None, itertools.imap)((lambda x: if not self._LPBugList__progress_hook(bugpage.length, bugpage.batchsize):
  177. passx), bugpage)
  178.             self += b
  179.             url = bugpage.following_page
  180.             if follow:
  181.                 pass
  182.             follow_page = bool(url)
  183.         if bugpage is not None and self._LPBugList__progress_hook is not None:
  184.             self._LPBugList__progress_hook(bugpage.length)
  185.         
  186.  
  187.     
  188.     def _stoppable_filter(self, bugpage):
  189.         for bug in bugpage:
  190.             if self._LPBugList__progress_hook is not None:
  191.                 self._LPBugList__progress_hook(bugpage.length, bugpage.batchsize)
  192.             
  193.             
  194.             try:
  195.                 for func in self._LPBugList__filter:
  196.                     bug = func(bug)
  197.                     if not bug:
  198.                         break
  199.                         continue
  200.                 else:
  201.                     yield bug
  202.             continue
  203.             except StopIteration:
  204.                 bugpage.following_page = False
  205.                 break
  206.                 continue
  207.             
  208.  
  209.         
  210.  
  211.     
  212.     def _get_class_helper_bugpage(self):
  213.         if not self._LPBugList__class_helper_bugpage:
  214.             raise NotImplementedError, 'this method must be implemented by a concrete subclass'
  215.         self._LPBugList__class_helper_bugpage
  216.         return self._LPBugList__class_helper_bugpage
  217.  
  218.     
  219.     def _set_class_helper_bugpage(self, value):
  220.         if not value is None or issubclass(value, LPBugPage):
  221.             raise TypeError, 'BugList.class_helper_bugpage needs to be instance of LPBugPage'
  222.         issubclass(value, LPBugPage)
  223.         self._LPBugList__class_helper_bugpage = value
  224.  
  225.     class_helper_bugpage = property(_get_class_helper_bugpage, _set_class_helper_bugpage)
  226.     
  227.     def get_bugs(self):
  228.         return self
  229.  
  230.     bugs = property(fget = get_bugs, doc = 'get list of bugs')
  231.     
  232.     def set_progress_hook(self, hook_func, blocksize = 25):
  233.         if hook_func is None:
  234.             self._LPBugList__progress_hook = None
  235.         elif isinstance(hook_func, _ProgressWrapper):
  236.             self._LPBugList__progress_hook = hook_func
  237.         else:
  238.             self._LPBugList__progress_hook = self._create_progress_hook(hook_func, blocksize)
  239.  
  240.  
  241.