def test_with_text_star_accept(): _check_webob_dependency() environ = test_environ() environ['PATH_INFO'] = '/test.iscool' environ['HTTP_ACCEPT'] = 'text/*' m = MIMETypes(environ) eq_(m.mimetype('text/html'), 'text/html')
def init_defaults(self, global_conf, app_conf, package=None, paths=None): conf = global_conf.copy() conf.update(app_conf) conf.update(dict(app_conf=app_conf, global_conf=global_conf)) conf.update(self.pop('environment_load', {})) if paths: conf['pyblox.paths'] = paths conf['pyblox.package'] = package conf['debug'] = asbool(conf.get('debug')) # Load the MIMETypes with its default types MIMETypes.init() for key, val in copy.deepcopy(self.defaults).iteritems(): conf.setdefault(key, val) if 'cache_dir' in conf: conf.setdefault('beaker.session.data_dir', os.path.join(conf['cache_dir'], 'sessions')) conf.setdefault('beaker.cache.data_dir', os.path.join(conf['cache_dir'], 'cache')) conf['pyblox.cache_dir'] = conf.pop('cache_dir', conf['app_conf'].get('cache_dir')) # Save our errorware values # Pylons did this nicely... still thinking about it. # conf['pyblox.errorware'] = errorware self.update(conf)
def test_with_star_star_accept(): _check_webob_dependency() environ = test_environ() environ['PATH_INFO'] = '/test.iscool' environ['HTTP_ACCEPT'] = '*/*' m = MIMETypes(environ) eq_(m.mimetype('application/xml'), 'application/xml')
def test_root_path(): _check_webob_dependency() environ = test_environ() environ['PATH_INFO'] = '/' environ['HTTP_ACCEPT'] = 'text/html, application/xml' m = MIMETypes(environ) eq_(m.mimetype('text/html'), 'text/html')
def init_defaults(self, global_conf, app_conf, package=None, paths=None): conf = global_conf.copy() conf.update(app_conf) conf.update(dict(app_conf=app_conf, global_conf=global_conf)) conf.update(self.pop('environment_load', {})) if paths: conf['bootstrappy.paths'] = paths conf['bootstrappy.package'] = package conf['debug'] = asbool(conf.get('debug')) # Load the MIMETypes with its default types MIMETypes.init() for key, val in copy.deepcopy(self.defaults).iteritems(): conf.setdefault(key, val) if 'cache_dir' in conf: conf.setdefault('beaker.session.data_dir', os.path.join(conf['cache_dir'], 'sessions')) conf.setdefault('beaker.cache.data_dir', os.path.join(conf['cache_dir'], 'cache')) conf['bootstrappy.cache_dir'] = conf.pop( 'cache_dir', conf['app_conf'].get('cache_dir')) # Save our errorware values # Pylons did this nicely... still thinking about it. # conf['bootstrappy.errorware'] = errorware self.update(conf)
def test_with_no_extention(): _check_webob_dependency() environ = test_environ() environ['PATH_INFO'] = '/test' environ['HTTP_ACCEPT'] = 'application/xml' m = MIMETypes(environ) eq_(m.mimetype('text/html'), False) eq_(m.mimetype('application/xml'), 'application/xml')
def init_app(self, global_conf, app_conf, package=None, paths=None): """Initialize configuration for the application .. note This *must* be called at least once, as soon as possible to setup all the configuration options. ``global_conf`` Several options are expected to be set for a Pylons web application. They will be loaded from the global_config which has the main Paste options. If ``debug`` is not enabled as a global config option, the following option *must* be set: * error_to - The email address to send the debug error to The optional config options in this case are: * smtp_server - The SMTP server to use, defaults to 'localhost' * error_log - A logfile to write the error to * error_subject_prefix - The prefix of the error email subject * from_address - Whom the error email should be from ``app_conf`` Defaults supplied via the [app:main] section from the Paste config file. ``load_config`` only cares about whether a 'prefix' option is set, if so it will update Routes to ensure URL's take that into account. ``package`` The name of the application package, to be stored in the app_conf. .. versionchanged:: 1.0 ``template_engine`` option is no longer supported. """ log.debug("Initializing configuration, package: '%s'", package) conf = global_conf.copy() conf.update(app_conf) conf.update(dict(app_conf=app_conf, global_conf=global_conf)) conf.update(self.pop('environment_load', {})) if paths: conf['pylons.paths'] = paths conf['pylons.package'] = package conf['debug'] = asbool(conf.get('debug')) # Load the MIMETypes with its default types MIMETypes.init() # Ensure all the keys from defaults are present, load them if not for key, val in copy.deepcopy(PylonsConfig.defaults).iteritems(): conf.setdefault(key, val) # Load the errorware configuration from the Paste configuration file # These all have defaults, and emails are only sent if configured and # if this application is running in production mode errorware = {} errorware['debug'] = conf['debug'] if not errorware['debug']: errorware['debug'] = False errorware['error_email'] = conf.get('email_to') errorware['error_log'] = conf.get('error_log', None) errorware['smtp_server'] = conf.get('smtp_server', 'localhost') errorware['error_subject_prefix'] = conf.get( 'error_subject_prefix', 'WebApp Error: ') errorware['from_address'] = conf.get( 'from_address', conf.get('error_email_from', '*****@*****.**')) errorware['error_message'] = conf.get('error_message', 'An internal server error occurred') # Copy in some defaults if 'cache_dir' in conf: conf.setdefault('beaker.session.data_dir', os.path.join(conf['cache_dir'], 'sessions')) conf.setdefault('beaker.cache.data_dir', os.path.join(conf['cache_dir'], 'cache')) conf['pylons.cache_dir'] = conf.pop('cache_dir', conf['app_conf'].get('cache_dir')) # Save our errorware values conf['pylons.errorware'] = errorware # Load conf dict into self self.update(conf)
def test_with_no_extention_and_no_accept(): _check_webob_dependency() environ = test_environ() environ['PATH_INFO'] = '/test' m = MIMETypes(environ) eq_(m.mimetype('html'), 'text/html')
def test_usage(): _check_webob_dependency() environ = test_environ() environ['PATH_INFO'] = '/test.html' m = MIMETypes(environ) eq_(m.mimetype('html'), 'text/html')
def test_register_alias(): MIMETypes.add_alias('html', 'text/html') eq_(MIMETypes.aliases['html'], 'text/html')
def set_defaults(self, template_engine=None): conf = self # Load the MIMETypes with its default types MIMETypes.init() # Ensure all the keys from defaults are present, load them if not for key, val in copy.deepcopy(PylonsConfig.defaults).iteritems(): conf.setdefault(key, val) # Setup the prefix to override the routes if necessary. prefix = conf.get('prefix') if prefix: warnings.warn(pylons.legacy.prefix_warning % prefix, DeprecationWarning, 3) map = conf.get('routes.map') if map: map.prefix = prefix map._created_regs = False # Load the errorware configuration from the Paste configuration file # These all have defaults, and emails are only sent if configured and # if this application is running in production mode errorware = {} errorware['debug'] = asbool(conf.get('debug')) if not errorware['debug']: errorware['debug'] = False errorware['error_email'] = conf.get('email_to') errorware['error_log'] = conf.get('error_log', None) errorware['smtp_server'] = conf.get('smtp_server', 'localhost') errorware['error_subject_prefix'] = conf.get( 'error_subject_prefix', 'WebApp Error: ') errorware['from_address'] = conf.get( 'from_address', conf.get('error_email_from', '*****@*****.**')) errorware['error_message'] = conf.get('error_message', 'An internal server error occurred') # Standard Pylons configuration directives for Myghty myghty_defaults = {} # Raise a complete error for the error middleware to catch myghty_defaults['raise_error'] = True myghty_defaults['output_encoding'] = \ conf['pylons.response_options']['charset'] myghty_defaults['component_root'] = [{os.path.basename(path): path} \ for path in conf['pylons.paths']['templates']] # Merge additional globals myghty_defaults.setdefault('allow_globals', []).extend(pylons.templating.PYLONS_VARS) myghty_template_options = {} if 'myghty_data_dir' in conf: warnings.warn("Old config option found in ini file, replace " "'myghty_data_dir' option with 'data_dir'", DeprecationWarning, 3) myghty_defaults['data_dir'] = conf['myghty_data_dir'] elif 'cache_dir' in conf: myghty_defaults['data_dir'] = os.path.join(conf['cache_dir'], 'templates') # Copy in some defaults if 'cache_dir' in conf: conf.setdefault('beaker.session.data_dir', os.path.join(conf['cache_dir'], 'sessions')) conf.setdefault('beaker.cache.data_dir', os.path.join(conf['cache_dir'], 'cache')) # Copy Myghty defaults and options into template options for k, v in myghty_defaults.iteritems(): myghty_template_options['myghty.'+k] = v # Legacy copy of session and cache settings into conf if k.startswith('session_') or k.startswith('cache_'): conf[k] = v # Copy old session/cache config to new keys for Beaker 0.7+ for key, val in conf.items(): if key.startswith('cache_'): conf['cache.'+key[6:]] = val elif key.startswith('session_'): conf['session.'+key[8:]] = val # Setup the main template options dict conf['buffet.template_options'].update(myghty_template_options) # Setup several defaults for various template languages defaults = {} # Rearrange template options as default for Mako defaults['mako.directories'] = conf['pylons.paths']['templates'] defaults['mako.filesystem_checks'] = True defaults['mako.output_encoding'] = \ conf['pylons.response_options']['charset'] if 'cache_dir' in conf: defaults['mako.module_directory'] = \ os.path.join(conf['cache_dir'], 'templates') # Setup kid defaults defaults['kid.assume_encoding'] = 'utf-8' defaults['kid.encoding'] = conf['pylons.response_options']['charset'] # Merge template options into defaults defaults.update(conf['buffet.template_options']) conf['buffet.template_options'] = defaults # Prepare our default template engine if template_engine == 'pylonsmyghty': self.add_template_engine('pylonsmyghty', None, myghty_template_options) elif template_engine == 'mako': self.add_template_engine('mako', '') elif template_engine in ['genshi', 'kid']: self.add_template_engine(template_engine, conf['pylons.package'] + '.templates') elif template_engine == 'cheetah': self.add_template_engine(template_engine, '%s.templates' % conf['pylons.package']) log.debug("Loaded %s template engine as the default template " "renderer", template_engine) conf['pylons.cache_dir'] = conf.pop('cache_dir', conf['app_conf'].get('cache_dir')) # Save our errorware values conf['pylons.errorware'] = errorware
def init_app(self, global_conf, app_conf, package=None, paths=None): """Initialize configuration for the application .. note This *must* be called at least once, as soon as possible to setup all the configuration options. ``global_conf`` Several options are expected to be set for a Pylons web application. They will be loaded from the global_config which has the main Paste options. If ``debug`` is not enabled as a global config option, the following option *must* be set: * error_to - The email address to send the debug error to The optional config options in this case are: * smtp_server - The SMTP server to use, defaults to 'localhost' * error_log - A logfile to write the error to * error_subject_prefix - The prefix of the error email subject * from_address - Whom the error email should be from ``app_conf`` Defaults supplied via the [app:main] section from the Paste config file. ``load_config`` only cares about whether a 'prefix' option is set, if so it will update Routes to ensure URL's take that into account. ``package`` The name of the application package, to be stored in the app_conf. .. versionchanged:: 1.0 ``template_engine`` option is no longer supported. """ log.debug("Initializing configuration, package: '%s'", package) conf = global_conf.copy() conf.update(app_conf) conf.update(dict(app_conf=app_conf, global_conf=global_conf)) conf.update(self.pop('environment_load', {})) if paths: conf['pylons.paths'] = paths conf['pylons.package'] = package conf['debug'] = asbool(conf.get('debug')) # Load the MIMETypes with its default types MIMETypes.init() # Ensure all the keys from defaults are present, load them if not for key, val in copy.deepcopy(PylonsConfig.defaults).iteritems(): conf.setdefault(key, val) # Load the errorware configuration from the Paste configuration file # These all have defaults, and emails are only sent if configured and # if this application is running in production mode errorware = {} errorware['debug'] = conf['debug'] if not errorware['debug']: errorware['debug'] = False errorware['error_email'] = conf.get('email_to') errorware['error_log'] = conf.get('error_log', None) errorware['smtp_server'] = conf.get('smtp_server', 'localhost') errorware['error_subject_prefix'] = conf.get( 'error_subject_prefix', 'WebApp Error: ') errorware['from_address'] = conf.get( 'from_address', conf.get('error_email_from', '*****@*****.**')) errorware['error_message'] = conf.get( 'error_message', 'An internal server error occurred') # Copy in some defaults if 'cache_dir' in conf: conf.setdefault('beaker.session.data_dir', os.path.join(conf['cache_dir'], 'sessions')) conf.setdefault('beaker.cache.data_dir', os.path.join(conf['cache_dir'], 'cache')) conf['pylons.cache_dir'] = conf.pop('cache_dir', conf['app_conf'].get('cache_dir')) # Save our errorware values conf['pylons.errorware'] = errorware # Load conf dict into self self.update(conf)
def setup(): MIMETypes.init() mimetypes.add_type('application/xml', '.xml', True)
def set_defaults(self, template_engine): conf = self.current_conf() # Load the MIMETypes with its default types MIMETypes.init() # Ensure all the keys from defaults are present, load them if not for key, val in copy.deepcopy(PylonsConfig.defaults).iteritems(): conf.setdefault(key, val) # Setup the prefix to override the routes if necessary. prefix = conf.get('prefix') if prefix: warnings.warn(pylons.legacy.prefix_warning % prefix, DeprecationWarning, 3) map = conf.get('routes.map') if map: map.prefix = prefix map._created_regs = False # Load the errorware configuration from the Paste configuration file # These all have defaults, and emails are only sent if configured and # if this application is running in production mode errorware = {} errorware['debug'] = asbool(conf.get('debug')) if not errorware['debug']: errorware['debug'] = False errorware['error_email'] = conf.get('email_to') errorware['error_log'] = conf.get('error_log', None) errorware['smtp_server'] = conf.get('smtp_server', 'localhost') errorware['error_subject_prefix'] = conf.get( 'error_subject_prefix', 'WebApp Error: ') errorware['from_address'] = conf.get( 'from_address', conf.get('error_email_from', '*****@*****.**')) errorware['error_message'] = conf.get( 'error_message', 'An internal server error occurred') # Standard Pylons configuration directives for Myghty myghty_defaults = {} # Raise a complete error for the error middleware to catch myghty_defaults['raise_error'] = True myghty_defaults['output_encoding'] = \ conf['pylons.response_options']['charset'] myghty_defaults['component_root'] = [{os.path.basename(path): path} \ for path in conf['pylons.paths']['templates']] # Merge additional globals myghty_defaults.setdefault('allow_globals', []).extend(pylons.templating.PYLONS_VARS) myghty_template_options = {} if 'myghty_data_dir' in conf: warnings.warn( "Old config option found in ini file, replace " "'myghty_data_dir' option with 'data_dir'", DeprecationWarning, 3) myghty_defaults['data_dir'] = conf['myghty_data_dir'] elif 'cache_dir' in conf: myghty_defaults['data_dir'] = os.path.join(conf['cache_dir'], 'templates') # Copy in some defaults if 'cache_dir' in conf: conf.setdefault('beaker.session.data_dir', os.path.join(conf['cache_dir'], 'sessions')) conf.setdefault('beaker.cache.data_dir', os.path.join(conf['cache_dir'], 'cache')) # Copy Myghty defaults and options into template options for k, v in myghty_defaults.iteritems(): myghty_template_options['myghty.' + k] = v # Legacy copy of session and cache settings into conf if k.startswith('session_') or k.startswith('cache_'): conf[k] = v # Copy old session/cache config to new keys for Beaker 0.7+ for key, val in conf.items(): if key.startswith('cache_'): conf['cache.' + key[6:]] = val elif key.startswith('session_'): conf['session.' + key[8:]] = val # Setup the main template options dict conf['buffet.template_options'].update(myghty_template_options) # Setup several defaults for various template languages defaults = {} # Rearrange template options as default for Mako defaults['mako.directories'] = conf['pylons.paths']['templates'] defaults['mako.filesystem_checks'] = True defaults['mako.output_encoding'] = \ conf['pylons.response_options']['charset'] if 'cache_dir' in conf: defaults['mako.module_directory'] = \ os.path.join(conf['cache_dir'], 'templates') # Setup kid defaults defaults['kid.assume_encoding'] = 'utf-8' defaults['kid.encoding'] = conf['pylons.response_options']['charset'] # Merge template options into defaults defaults.update(conf['buffet.template_options']) conf['buffet.template_options'] = defaults # Prepare our default template engine if template_engine == 'pylonsmyghty': self.add_template_engine('pylonsmyghty', None, myghty_template_options) elif template_engine == 'mako': self.add_template_engine('mako', '') elif template_engine in ['genshi', 'kid']: self.add_template_engine(template_engine, conf['pylons.package'] + '.templates') elif template_engine == 'cheetah': self.add_template_engine(template_engine, '%s.templates' % conf['pylons.package']) log.debug( "Loaded %s template engine as the default template " "renderer", template_engine) conf['pylons.cache_dir'] = conf.pop('cache_dir', conf['app_conf'].get('cache_dir')) # Save our errorware values conf['pylons.errorware'] = errorware