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 / exceptions.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  10.4 KB  |  258 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import urllib2
  5. import libxml2
  6. from lphelper import unicode_for_libxml2
  7.  
  8. def noerr(ctx, str):
  9.     pass
  10.  
  11. libxml2.registerErrorHandler(noerr, None)
  12.  
  13. class LaunchpadError(Exception):
  14.     
  15.     def __str__(self):
  16.         return 'Something went wrong'
  17.  
  18.     
  19.     def __repr__(self):
  20.         t = self.__class__.__name__
  21.         if t == 'LaunchpadError':
  22.             t = ''
  23.         else:
  24.             t = ' %s' % t
  25.         return '<LaunchpadError%s>' % t
  26.  
  27.     
  28.     def value(self):
  29.         
  30.         try:
  31.             return self.msg
  32.         except:
  33.             return None
  34.  
  35.  
  36.     value = property(value)
  37.  
  38.  
  39. class LaunchpadURLError(LaunchpadError, urllib2.URLError):
  40.     
  41.     def __init__(self, msg, url = None):
  42.         self.msg = msg
  43.         self.url = url
  44.  
  45.     
  46.     def __str__(self):
  47.         if not self.url:
  48.             pass
  49.         return '\n    * message: %s\n    * url: %s' % (self.msg, 'unknown')
  50.  
  51.  
  52.  
  53. class LaunchpadInternalServerError(LaunchpadError):
  54.     
  55.     def __init__(self, url):
  56.         self.url = url
  57.  
  58.     
  59.     def __str__(self):
  60.         return '\n    * message: An internal server error occurred. Please try again later.\n    * url: %s' % self.url
  61.  
  62.  
  63.  
  64. class LaunchpadLoginError(LaunchpadError):
  65.     
  66.     def __init__(self, url, msg = ''):
  67.         self.url = url
  68.         self.msg = msg
  69.  
  70.     
  71.     def __str__(self):
  72.         if self.msg:
  73.             m = ' %s' % self.msg.strip()
  74.         else:
  75.             m = ''
  76.         return '\n    * message: To continue, you must log in to Launchpad.%s\n    * url: %s' % (m, self.url)
  77.  
  78.  
  79.  
  80. class LaunchpadLoginFailed(LaunchpadLoginError):
  81.     
  82.     def __init__(self, url):
  83.         self.url = url
  84.  
  85.     
  86.     def __str__(self):
  87.         return 'Login failed: The email address and password do not match.\n    * url: %s' % self.url
  88.  
  89.  
  90.  
  91. class LaunchpadNotAllowedError(LaunchpadLoginError):
  92.     pass
  93.  
  94.  
  95. class PythonLaunchpadBugsError(Exception):
  96.     
  97.     def __str__(self):
  98.         return 'Something went wrong'
  99.  
  100.     
  101.     def __repr__(self):
  102.         t = self.__class__.__name__
  103.         if t == 'PythonLaunchpadBugsError':
  104.             t = ''
  105.         else:
  106.             t = ' %s' % t
  107.         return '<PythonLaunchpadBugsError%s>' % t
  108.  
  109.  
  110.  
  111. class PythonLaunchpadBugsValueError(PythonLaunchpadBugsError, ValueError):
  112.     
  113.     def __init__(self, values = { }, url = '', msg = ''):
  114.         self.values = values
  115.         if not url:
  116.             pass
  117.         self.url = 'unknown'
  118.         self.msg = msg
  119.  
  120.     
  121.     def __str__(self):
  122.         errors = ''
  123.         msg = ''
  124.         if self.values:
  125.             l = len(self.values)
  126.             if l == 1:
  127.                 errors = '\nThere is %s error\n' % l
  128.             else:
  129.                 errors = '\nThere are %s errors\n' % len(self.values)
  130.             for i, k in self.values.iteritems():
  131.                 errors += '    * %s: %s\n' % (i, k)
  132.             
  133.             errors.rstrip('\n')
  134.         
  135.         if self.msg:
  136.             msg = '\n%s' % self.msg
  137.         
  138.         r = 'There is a problem with the information you entered.  Please fix it and try again.%s%s' % (errors, msg)
  139.         return r.rstrip('\n')
  140.  
  141.  
  142.  
  143. class PythonLaunchpadBugsIOError(PythonLaunchpadBugsError, IOError):
  144.     
  145.     def __init__(self, msg):
  146.         self.msg = msg
  147.  
  148.     
  149.     def __str__(self):
  150.         return self.msg
  151.  
  152.  
  153.  
  154. class PythonLaunchpadBugsRuntimeError(PythonLaunchpadBugsError, RuntimeError):
  155.     
  156.     def __init__(self, msg):
  157.         self.msg = msg
  158.  
  159.     
  160.     def __str__(self):
  161.         msg = '    * %s' % self.msg.lstrip(' *')
  162.         return 'The following error occured while using python-launchpad-bugs. Please report this error with as much information as possible.\n\t%s' % msg
  163.  
  164.  
  165.  
  166. class PythonLaunchpadBugsParsingError(PythonLaunchpadBugsError, AssertionError):
  167.     
  168.     def __init__(self, path, url = None):
  169.         self.path = path
  170.         if not url:
  171.             pass
  172.         self.url = 'unknown url'
  173.  
  174.     
  175.     def __str__(self):
  176.         return 'Error while parsing %s (%s)' % (self.path, self.url)
  177.  
  178.  
  179.  
  180. class PythonLaunchpadBugsXMLParsingError(PythonLaunchpadBugsParsingError):
  181.     
  182.     def __init__(self, path, xml, url = None):
  183.         PythonLaunchpadBugsParsingError.__init__(self, path, url)
  184.         self.xml = xml
  185.  
  186.     
  187.     def __str__(self):
  188.         return 'Wrong XPath-Expr while parsing %s (%s)' % (self.path, self.url)
  189.  
  190.  
  191.  
  192. def choose_LaunchpadError(value, url):
  193.     if value == 'login failed':
  194.         return LaunchpadLoginError(url)
  195.     if value == 500:
  196.         return LaunchpadInternalServerError(url)
  197.     if value == 404:
  198.         return LaunchpadURLError('Page not found', url)
  199.     if value == 403:
  200.         return LaunchpadNotAllowedError(url)
  201.     return LaunchpadURLError(value, url)
  202.  
  203. (UNKNOWNERROR, VALUEERROR, RUNTIMEERROR) = range(3)
  204.  
  205. def _parse_page(text):
  206.     guess_type = UNKNOWNERROR
  207.     errors = { }
  208.     t = libxml2.htmlParseDoc(unicode_for_libxml2(text), 'UTF-8')
  209.     e = t.xpathEval('//tr[@class="error"]')
  210.     for i in e:
  211.         msg = ''
  212.         m = i.xpathEval('td/div[@class="message"]')
  213.         if m:
  214.             msg = m[0].content
  215.         
  216.         m = i.xpathEval('td/div/input | td/div/textarea')
  217.         if not m:
  218.             raise AssertionError
  219.         val = m[0].prop('name')
  220.         errors[val] = msg
  221.     
  222.     return (guess_type, errors)
  223.  
  224.  
  225. def choose_pylpbugsError(error_type = UNKNOWNERROR, values = { }, text = '', url = ''):
  226.     errors = { }
  227.     if text:
  228.         (guess_type, errors) = _parse_page(text)
  229.         if not error_type:
  230.             error_type = guess_type
  231.         
  232.     
  233.     values.update(errors)
  234.     if error_type == VALUEERROR:
  235.         return PythonLaunchpadBugsValueError(values, url)
  236.     if error_type == RUNTIMEERROR and msg:
  237.         return PythonLaunchpadBugsRuntimeError(msg)
  238.     return PythonLaunchpadBugsError
  239.  
  240.  
  241. def parse_error(cond, path, xml = None, url = '', msg = '', error_type = UNKNOWNERROR):
  242.     if not cond:
  243.         if error_type:
  244.             if error_type == VALUEERROR:
  245.                 raise PythonLaunchpadBugsValueError(url = url, msg = msg)
  246.             error_type == VALUEERROR
  247.             if error_type == RUNTIMEERROR and msg:
  248.                 raise PythonLaunchpadBugsRuntimeError(msg)
  249.             msg
  250.             raise PythonLaunchpadBugsError
  251.         error_type
  252.         if xml is None:
  253.             raise PythonLaunchpadBugsParsingError(path, url)
  254.         xml is None
  255.         raise PythonLaunchpadBugsXMLParsingError(path, xml, url)
  256.     cond
  257.  
  258.