home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / launchpadbugs / connector.py < prev    next >
Encoding:
Python Source  |  2008-09-02  |  6.4 KB  |  177 lines

  1. from lpconstants import CONNECTOR, _MODE_WRAPPER
  2. from exceptions import LaunchpadError
  3.  
  4. #
  5. # factory 
  6. #
  7.  
  8. class LaunchpadConnector(object):
  9.     def __init__(self, cls, method=CONNECTOR.MODES.DEFAULT):
  10.         if isinstance(method, str):
  11.             method = method.upper()
  12.             try:
  13.                 method = getattr(CONNECTOR.MODES, method)
  14.             except AttributeError:
  15.                 raise NotImplementedError, "Connection method '%s' not implemented" %method
  16.         self.__method = method
  17.         
  18.         if not isinstance(self.method, _MODE_WRAPPER):
  19.             raise ValueError
  20.         
  21.         try:            
  22.             cls_id = CONNECTOR.CONNECTIONS[self.method][cls][0]
  23.         except:
  24.             raise ValueError        
  25.         try:
  26.             self.__module = __import__(cls_id, globals(), locals(), [], -1)
  27.         except TypeError:
  28.             self.__module = __import__(cls_id, globals(), locals(), [])
  29.         self.__cls = getattr(self.module, cls)
  30.         
  31.         try:
  32.             connection_id = CONNECTOR.CONNECTIONS[self.method][cls][1]
  33.         except:
  34.             ValueError
  35.         try:
  36.             mod_connection = __import__(connection_id[0], globals(), locals(), [], -1)
  37.         except TypeError:
  38.             mod_connection = __import__(connection_id[0], globals(), locals(), [])
  39.         self.__connection = getattr(mod_connection, connection_id[1])()
  40.         
  41.     @property
  42.     def connection(self):
  43.         return self.__connection
  44.     
  45.     def set_connection_mode(self, mode):
  46.         self.connection.set_mode(mode)
  47.        
  48.     def get_auth(self):
  49.         return self.connection.get_auth()
  50.         
  51.     def set_auth(self, auth):
  52.         self.connection.set_auth(auth)
  53.     authentication = property(get_auth, set_auth)
  54.         
  55.     @property
  56.     def cls(self):
  57.         return self.__cls
  58.         
  59.     @property
  60.     def method(self):
  61.         return self.__method
  62.         
  63.     @property
  64.     def module(self):
  65.         return self.__module
  66.         
  67.     @property
  68.     def needs_login(self):
  69.         return self.connection.needs_login()
  70.     
  71.     #wrapper around old exception class
  72.     class Error:
  73.         LPUrlError = LaunchpadError
  74.         
  75.  
  76. class ConnectBug(LaunchpadConnector):
  77.     def __init__(self, method=CONNECTOR.MODES.DEFAULT):
  78.         LaunchpadConnector.__init__(self, "Bug", method)
  79.         
  80.     def __call__(self, bug=None, url=None):
  81.         return self.cls(bug, url, connection=self.connection)
  82.     
  83.     @property
  84.     def content_types(self):
  85.         return self.connection.content_types
  86.     
  87.     def NewAttachment(self, *args, **kwargs):
  88.         """ returns a Attachment-class """
  89.         if self.method == CONNECTOR.MODES.TEXT:
  90.             raise NotImplementedError, "It is impossible to add attachments in the text-mode"
  91.         return getattr(self.module, "Attachment")(connection=self.connection, *args, **kwargs)
  92.     
  93.     def NewComment(self, *args, **kwargs):
  94.         """ returns a Comment-object """
  95.         if self.method == CONNECTOR.MODES.TEXT:
  96.             raise NotImplementedError, "It is impossible to add comments in the text-mode"
  97.         return getattr(self.module, "Comment")(*args, **kwargs)
  98.     
  99.     def NewTask(self, task_type, *args, **kwargs):
  100.         """ returns a Comment-object """
  101.         if self.method == CONNECTOR.MODES.TEXT:
  102.             raise NotImplementedError, "It is impossible to add a task in the text-mode"
  103.         try:
  104.             return getattr(self.module, "create_%s_task" %task_type.lower())(*args, **kwargs)
  105.         except AttributeError:
  106.             raise NotImplementedError, "creating of new '%s'-tasks is not implemented" %task_type
  107.         
  108.     def New(self, *args, **kwargs):
  109.         """ returns a Bug-object """
  110.         if self.method == CONNECTOR.MODES.TEXT:
  111.             raise NotImplementedError, "It is impossible to create bugreports in the text-mode"
  112.         return getattr(self.module, "create_new_bugreport")(connection=self.connection, *args, **kwargs)
  113.  
  114.  
  115. class ConnectBugList(LaunchpadConnector):
  116.     def __init__(self, method=CONNECTOR.MODES.DEFAULT, all_tasks=False):
  117.         LaunchpadConnector.__init__(self, "BugList", method)
  118.         self.__all_tasks = all_tasks
  119.         self.__progress_hook = None
  120.         
  121.     def __call__(self, baseurl=None):
  122.         return self.cls(baseurl, connection=self.connection,
  123.                         all_tasks=self.__all_tasks,
  124.                         progress_hook=self.__progress_hook)
  125.     
  126.     def set_progress_hook(self, progress_hook, blocksize=25):
  127.         self.__progress_hook = self.cls._create_progress_hook(progress_hook, blocksize)
  128.     
  129.     
  130. def ConnectTaskList(method=CONNECTOR.MODES.DEFAULT):
  131.     return ConnectBugList(method, all_tasks=True)
  132.     
  133.     
  134. class ConnectBlueprint(LaunchpadConnector):
  135.     def __init__(self, method=CONNECTOR.MODES.DEFAULT):
  136.         LaunchpadConnector.__init__(self, "Blueprint", method)
  137.         
  138.     def __call__(self, url):
  139.         return self.cls(url, self.connection)
  140.     
  141.  
  142. class ConnectBlueprintList(LaunchpadConnector):
  143.     def __init__(self, method=CONNECTOR.MODES.DEFAULT):
  144.         LaunchpadConnector.__init__(self, "BlueprintList", method)
  145.         self.__progress_hook = None
  146.         
  147.     def __call__(self, baseurl=None):
  148.         return self.cls(baseurl, connection=self.connection,
  149.                         progress_hook=self.__progress_hook)
  150.  
  151.     def set_progress_hook(self, progress_hook, blocksize=25):
  152.         self.__progress_hook = self.cls._create_progress_hook(progress_hook, blocksize)
  153.  
  154.  
  155. class ConnectProjectList(LaunchpadConnector):
  156.     def __init__(self,method=CONNECTOR.MODES.DEFAULT):
  157.         LaunchpadConnector.__init__(self,"ProjectList",method)
  158.         self.__progress_hook=None
  159.         
  160.     def __call__(self,baseurl=None):
  161.         return self.cls(baseurl,connection=self.connection,all_tasks=False,progress_hook=self.__progress_hook)
  162.     
  163.     def set_progress_hook(self, progress_hook, blocksize=25):
  164.         self.__progress_hook=self.cls._create_progress_hook(progress_hook,blocksize)
  165.  
  166.         
  167. class ConnectProjectPackageList(LaunchpadConnector):
  168.     def __init__(self,method=CONNECTOR.MODES.DEFAULT):
  169.         LaunchpadConnector.__init__(self,"ProjectPackageList",method)
  170.         self.__progress_hook=None
  171.         
  172.     def __call__(self,baseurl=None):
  173.         return self.cls(baseurl,connection=self.connection,all_tasks=False,progress_hook=self.__progress_hook)
  174.     
  175.     def set_progress_hook(self,progress_hook,blocksize=25):
  176.         self.__progress_hook=self.cls._create_progress_hook(progress_hook,blocksize)
  177.