home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1486 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  8.5 KB  |  248 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import warnings
  6. import cherrypy
  7.  
  8. class Checker(object):
  9.     on = True
  10.     
  11.     def __init__(self):
  12.         self._populate_known_types()
  13.  
  14.     
  15.     def __call__(self):
  16.         if self.on:
  17.             oldformatwarning = warnings.formatwarning
  18.             warnings.formatwarning = self.formatwarning
  19.             
  20.             try:
  21.                 for name in dir(self):
  22.                     if name.startswith('check_'):
  23.                         method = getattr(self, name)
  24.                         if method and callable(method):
  25.                             method()
  26.                         
  27.                     callable(method)
  28.             finally:
  29.                 warnings.formatwarning = oldformatwarning
  30.  
  31.         
  32.  
  33.     
  34.     def formatwarning(self, message, category, filename, lineno, line = None):
  35.         return 'CherryPy Checker:\n%s\n\n' % message
  36.  
  37.     global_config_contained_paths = False
  38.     
  39.     def check_skipped_app_config(self):
  40.         for sn, app in cherrypy.tree.apps.iteritems():
  41.             if not isinstance(app, cherrypy.Application):
  42.                 continue
  43.             
  44.             if not app.config:
  45.                 msg = 'The Application mounted at %r has an empty config.' % sn
  46.                 if self.global_config_contained_paths:
  47.                     msg += ' It looks like the config you passed to cherrypy.config.update() contains application-specific sections. You must explicitly pass application config via cherrypy.tree.mount(..., config=app_config)'
  48.                 
  49.                 warnings.warn(msg)
  50.                 return None
  51.         
  52.  
  53.     
  54.     def check_static_paths(self):
  55.         request = cherrypy.request
  56.         for sn, app in cherrypy.tree.apps.iteritems():
  57.             if not isinstance(app, cherrypy.Application):
  58.                 continue
  59.             
  60.             request.app = app
  61.             for section in app.config:
  62.                 request.get_resource(section + '/dummy.html')
  63.                 conf = request.config.get
  64.                 if conf('tools.staticdir.on', False):
  65.                     msg = ''
  66.                     root = conf('tools.staticdir.root')
  67.                     dir = conf('tools.staticdir.dir')
  68.                     if dir is None:
  69.                         msg = 'tools.staticdir.dir is not set.'
  70.                     else:
  71.                         fulldir = ''
  72.                         if os.path.isabs(dir):
  73.                             fulldir = dir
  74.                             if root:
  75.                                 msg = 'dir is an absolute path, even though a root is provided.'
  76.                                 testdir = os.path.join(root, dir[1:])
  77.                                 if os.path.exists(testdir):
  78.                                     msg += '\nIf you meant to serve the filesystem folder at %r, remove the leading slash from dir.' % testdir
  79.                                 
  80.                             
  81.                         elif not root:
  82.                             msg = 'dir is a relative path and no root provided.'
  83.                         else:
  84.                             fulldir = os.path.join(root, dir)
  85.                             if not os.path.isabs(fulldir):
  86.                                 msg = '%r is not an absolute path.' % fulldir
  87.                             
  88.                         if fulldir and not os.path.exists(fulldir):
  89.                             if msg:
  90.                                 msg += '\n'
  91.                             
  92.                             msg += '%r (root + dir) is not an existing filesystem path.' % fulldir
  93.                         
  94.                     if msg:
  95.                         warnings.warn('%s\nsection: [%s]\nroot: %r\ndir: %r' % (msg, section, root, dir))
  96.                     
  97.                 msg
  98.             
  99.         
  100.  
  101.     obsolete = {
  102.         'server.default_content_type': 'tools.response_headers.headers',
  103.         'log_access_file': 'log.access_file',
  104.         'log_config_options': None,
  105.         'log_file': 'log.error_file',
  106.         'log_file_not_found': None,
  107.         'log_request_headers': 'tools.log_headers.on',
  108.         'log_to_screen': 'log.screen',
  109.         'show_tracebacks': 'request.show_tracebacks',
  110.         'throw_errors': 'request.throw_errors',
  111.         'profiler.on': 'cherrypy.tree.mount(profiler.make_app(cherrypy.Application(Root())))' }
  112.     deprecated = { }
  113.     
  114.     def _compat(self, config):
  115.         for section, conf in config.iteritems():
  116.             if isinstance(conf, dict):
  117.                 for k, v in conf.iteritems():
  118.                     if k in self.obsolete:
  119.                         warnings.warn('%r is obsolete. Use %r instead.\nsection: [%s]' % (k, self.obsolete[k], section))
  120.                         continue
  121.                     if k in self.deprecated:
  122.                         warnings.warn('%r is deprecated. Use %r instead.\nsection: [%s]' % (k, self.deprecated[k], section))
  123.                         continue
  124.                 
  125.             if section in self.obsolete:
  126.                 warnings.warn('%r is obsolete. Use %r instead.' % (section, self.obsolete[section]))
  127.                 continue
  128.             if section in self.deprecated:
  129.                 warnings.warn('%r is deprecated. Use %r instead.' % (section, self.deprecated[section]))
  130.                 continue
  131.         
  132.  
  133.     
  134.     def check_compatibility(self):
  135.         self._compat(cherrypy.config)
  136.         for sn, app in cherrypy.tree.apps.iteritems():
  137.             if not isinstance(app, cherrypy.Application):
  138.                 continue
  139.             
  140.             self._compat(app.config)
  141.         
  142.  
  143.     extra_config_namespaces = []
  144.     
  145.     def _known_ns(self, app):
  146.         ns = [
  147.             'wsgi']
  148.         ns.extend(app.toolboxes.keys())
  149.         ns.extend(app.namespaces.keys())
  150.         ns.extend(app.request_class.namespaces.keys())
  151.         ns.extend(cherrypy.config.namespaces.keys())
  152.         ns += self.extra_config_namespaces
  153.         for section, conf in app.config.iteritems():
  154.             is_path_section = section.startswith('/')
  155.             if is_path_section and isinstance(conf, dict):
  156.                 for k, v in conf.iteritems():
  157.                     atoms = k.split('.')
  158.                     if len(atoms) > 1:
  159.                         if atoms[0] not in ns:
  160.                             if atoms[0] == 'cherrypy' and atoms[1] in ns:
  161.                                 msg = 'The config entry %r is invalid; try %r instead.\nsection: [%s]' % (k, '.'.join(atoms[1:]), section)
  162.                             else:
  163.                                 msg = 'The config entry %r is invalid, because the %r config namespace is unknown.\nsection: [%s]' % (k, atoms[0], section)
  164.                             warnings.warn(msg)
  165.                         elif atoms[0] == 'tools':
  166.                             if atoms[1] not in dir(cherrypy.tools):
  167.                                 msg = 'The config entry %r may be invalid, because the %r tool was not found.\nsection: [%s]' % (k, atoms[1], section)
  168.                                 warnings.warn(msg)
  169.                             
  170.                         
  171.                     atoms[0] not in ns
  172.                 
  173.         
  174.  
  175.     
  176.     def check_config_namespaces(self):
  177.         for sn, app in cherrypy.tree.apps.iteritems():
  178.             if not isinstance(app, cherrypy.Application):
  179.                 continue
  180.             
  181.             self._known_ns(app)
  182.         
  183.  
  184.     known_config_types = { }
  185.     
  186.     def _populate_known_types(self):
  187.         import __builtin__
  188.         builtins = _[1]
  189.         
  190.         def traverse(obj, namespace):
  191.             for name in dir(obj):
  192.                 vtype = type(getattr(obj, name, None))
  193.                 if vtype in builtins:
  194.                     self.known_config_types[namespace + '.' + name] = vtype
  195.                     continue
  196.             
  197.  
  198.         traverse(cherrypy.request, 'request')
  199.         traverse(cherrypy.response, 'response')
  200.         traverse(cherrypy.server, 'server')
  201.         traverse(cherrypy.engine, 'engine')
  202.         traverse(cherrypy.log, 'log')
  203.  
  204.     
  205.     def _known_types(self, config):
  206.         msg = 'The config entry %r in section %r is of type %r, which does not match the expected type %r.'
  207.         for section, conf in config.iteritems():
  208.             if isinstance(conf, dict):
  209.                 for k, v in conf.iteritems():
  210.                     if v is not None:
  211.                         expected_type = self.known_config_types.get(k, None)
  212.                         vtype = type(v)
  213.                         if expected_type and vtype != expected_type:
  214.                             warnings.warn(msg % (k, section, vtype.__name__, expected_type.__name__))
  215.                         
  216.                     vtype != expected_type
  217.                 
  218.             k = section
  219.             v = conf
  220.             if v is not None:
  221.                 expected_type = self.known_config_types.get(k, None)
  222.                 vtype = type(v)
  223.                 if expected_type and vtype != expected_type:
  224.                     warnings.warn(msg % (k, section, vtype.__name__, expected_type.__name__))
  225.                 
  226.             vtype != expected_type
  227.         
  228.  
  229.     
  230.     def check_config_types(self):
  231.         self._known_types(cherrypy.config)
  232.         for sn, app in cherrypy.tree.apps.iteritems():
  233.             if not isinstance(app, cherrypy.Application):
  234.                 continue
  235.             
  236.             self._known_types(app.config)
  237.         
  238.  
  239.     
  240.     def check_localhost(self):
  241.         for k, v in cherrypy.config.iteritems():
  242.             if k == 'server.socket_host' and v == 'localhost':
  243.                 warnings.warn("The use of 'localhost' as a socket host can cause problems on newer systems, since 'localhost' can map to either an IPv4 or an IPv6 address. You should use '127.0.0.1' or '[::1]' instead.")
  244.                 continue
  245.         
  246.  
  247.  
  248.