def setup_sqlalchemy(self): #from tg import config sqlalchemy_url = os.getenv('BISQUE_DBURL', None) or config.get('sqlalchemy.url') has_database = asbool(config.get('bisque.has_database', True)) if not has_database or not sqlalchemy_url: config['use_transaction_manager'] = False config['has_database'] = False log.info("NO DATABASE is configured") return log.info("DATABASE %s", sqlalchemy_url) config['bisque.has_database'] = True self.has_database = True if not sqlalchemy_url.startswith('sqlite://'): return super(BisqueAppConfig, self).setup_sqlalchemy() log.info("SQLLite special handling NullPool timoout") from sqlalchemy.pool import NullPool from sqlalchemy import engine_from_config engine = engine_from_config( config, 'sqlalchemy.', poolclass=NullPool, connect_args={'timeout': 30000}, ) config['pylons.app_globals'].sa_engine = engine # Pass the engine to initmodel, to be able to introspect tables self.package.model.init_model(engine) self.register_hook('controller_wrapper', transaction_retry_wrapper)
def __init__(self, template='', content_type=None, exclude_names=None, custom_format=None, render_params=None, inherit=False): if exclude_names is None: exclude_names = [] if template in config.get('renderers', []): engine, template = template, '' elif ':' in template: engine, template = template.split(':', 1) elif template: # Use the default templating engine from the config engine = config.get('default_renderer') else: engine, template = None, None if content_type is None: if engine == 'json': content_type = 'application/json' else: content_type = 'text/html' if engine in ('json', 'amf') and 'tmpl_context' not in exclude_names: exclude_names.append('tmpl_context') self.engine = engine self.template = template self.content_type = content_type self.exclude_names = exclude_names self.custom_format = custom_format self.render_params = render_params self.inherit = inherit
def add_auth_middleware(self, app, skip_authentication): """ Configure authentication and authorization. :param app: The TG2 application. :param skip_authentication: Should authentication be skipped if explicitly requested? (used by repoze.who-testutil) :type skip_authentication: bool """ if asbool(config.get('loom.profile', False)): log.debug('Setting up profiling middleware') app = AccumulatingProfileMiddleware( app, log_filename='/home/volkmuth/profile/app.log', cachegrind_filename='/home/volkmuth/profile/cachegrind.out.app', discard_first_request=False, flush_at_shutdown=True, path='/__profile__') parser = WhoConfig(config['here']) parser.parse(open(config.get('who.config_file', 'who.ini'))) app = make_middleware(skip_authentication, app, parser.identifiers, parser.authenticators, parser.challengers, parser.mdproviders, parser.request_classifier, parser.challenge_decider, remote_user_key=parser.remote_user_key) #app = make_who_with_config(app, config, config.get('who.config_file','who.ini'), # config.get('who.log_file','stdout'), config.get('who.log_level','debug'), # skip_authentication # ) app = ModuleMiddleware(app) return app
def register_template_engine(self, content_type, engine, template, exclude_names, render_params): """Registers an engine on the controller. Multiple engines can be registered, but only one engine per content_type. If no content type is specified the engine is registered at */* which is the default, and will be used whenever no content type is specified. exclude_names keeps track of a list of keys which will be removed from the controller's dictionary before it is loaded into the template. This allows you to exclude some information from JSONification, and other 'automatic' engines which don't require a template. render_params registers extra parameters which will be sent to the rendering method. This allows you to influence things like the rendering method or the injected doctype. """ default_renderer = config.get('default_renderer') available_renderers = config.get('renderers', []) if engine and not available_renderers: log.warning( 'Renderers not registered yet while exposing template %s for engine %s, ' 'skipping engine availability check', template, engine) if engine and available_renderers and engine not in available_renderers: log.debug( 'Registering template %s for engine %s not available. Skipping it', template, engine) return content_type = content_type or '*/*' try: current_content_type_engine = self.engines[content_type][0] except (KeyError, IndexError): current_content_type_engine = None if current_content_type_engine is not None and engine != default_renderer: # Avoid overwriting the default renderer when there is already a template registered return self.engines[content_type] = (engine, template, exclude_names or [], render_params or {}) # Avoid engine lookup if we have only one engine registered if len(self.engines) == 1: self.default_engine = content_type else: self.default_engine = None # This is a hack to make text/html prominent in respect to other common choices # when they have the same weight for webob.acceptparse.Accept.best_match(). # It uses the fact that the most common content types are all alphabetically # precedent to text/html, and so sorting engine keys alphabetically reversed # should make text/html the first choice when no other better choices are available. self.engines_keys = sorted(self.engines, reverse=True)
def register_template_engine(self, content_type, engine, template, exclude_names, render_params): """Registers an engine on the controller. Multiple engines can be registered, but only one engine per content_type. If no content type is specified the engine is registered at */* which is the default, and will be used whenever no content type is specified. exclude_names keeps track of a list of keys which will be removed from the controller's dictionary before it is loaded into the template. This allows you to exclude some information from JSONification, and other 'automatic' engines which don't require a template. render_params registers extra parameters which will be sent to the rendering method. This allows you to influence things like the rendering method or the injected doctype. """ default_renderer = config.get('default_renderer') available_renderers = config.get('renderers', []) if engine and not available_renderers: log.warning('Renderers not registered yet while exposing template %s for engine %s, ' 'skipping engine availability check', template, engine) if engine and available_renderers and engine not in available_renderers: log.debug('Registering template %s for engine %s not available. Skipping it', template, engine) return content_type = content_type or '*/*' try: current_content_type_engine = self.engines[content_type][0] except (KeyError, IndexError): current_content_type_engine = None if current_content_type_engine is not None and engine != default_renderer: # Avoid overwriting the default renderer when there is already a template registered return self.engines[content_type] = (engine, template, exclude_names or [], render_params or {}) # Avoid engine lookup if we have only one engine registered if len(self.engines) == 1: self.default_engine = content_type else: self.default_engine = None # This is a hack to make text/html prominent in respect to other common choices # when they have the same weight for webob.acceptparse.Accept.best_match(). # It uses the fact that the most common content types are all alphabetically # precedent to text/html, and so sorting engine keys alphabetically reversed # should make text/html the first choice when no other better choices are available. self.engines_keys = sorted(self.engines, reverse=True)
def __init__(self): self.path = os.path.join(FEATURES_TABLES_FILE_DIR, self.name) #set cache in site.cfg self.cache = str2bool( config.get('bisque.feature.%s.cache' % self.name, None) #checks for specific feature or config.get('bisque.feature.default.cache', None) #checks the default or 'False') #sets a default if nothing is specified
def register_template_engine(self, content_type, engine, template, exclude_names, render_params): """Registers an engine on the controller. Multiple engines can be registered, but only one engine per content_type. If no content type is specified the engine is registered at */* which is the default, and will be used whenever no content type is specified. exclude_names keeps track of a list of keys which will be removed from the controller's dictionary before it is loaded into the template. This allows you to exclude some information from JSONification, and other 'automatic' engines which don't require a template. render_params registers extra parameters which will be sent to the rendering method. This allows you to influence things like the rendering method or the injected doctype. """ default_renderer = config.get('default_renderer') available_renderers = config.get('renderers', []) if engine and not available_renderers: log.warn( 'Renderers not registered yet while exposing template %s for engine %s, ' 'skipping engine availability check', template, engine) if engine and available_renderers and engine not in available_renderers: log.debug( 'Registering template %s for engine %s not available. Skipping it', template, engine) return content_type = content_type or '*/*' if content_type in self.engines and engine != default_renderer: #Avoid overwriting the default renderer when there is already a template registered return self.engines[content_type] = (engine, template, exclude_names, render_params or {}) #Avoid engine lookup if we have only one engine registered if len(self.engines) == 1: self.default_engine = content_type else: self.default_engine = None # this is a work-around to make text/html prominent in respect # to other common choices when they have the same weight for # paste.util.mimeparse.best_match. self.engines_keys = sorted(self.engines, reverse=True)
def register_template_engine(self, content_type, engine, template, exclude_names, render_params): """Registers an engine on the controller. Multiple engines can be registered, but only one engine per content_type. If no content type is specified the engine is registered at */* which is the default, and will be used whenever no content type is specified. exclude_names keeps track of a list of keys which will be removed from the controller's dictionary before it is loaded into the template. This allows you to exclude some information from JSONification, and other 'automatic' engines which don't require a template. render_params registers extra parameters which will be sent to the rendering method. This allows you to influence things like the rendering method or the injected doctype. """ default_renderer = config.get('default_renderer') available_renderers = config.get('renderers', []) if engine and not available_renderers: log.warn('Renderers not registered yet while exposing template %s for engine %s, ' 'skipping engine availability check', template, engine) if engine and available_renderers and engine not in available_renderers: log.debug('Registering template %s for engine %s not available. Skipping it', template, engine) return content_type = content_type or '*/*' if content_type in self.engines and engine != default_renderer: #Avoid overwriting the default renderer when there is already a template registered return self.engines[content_type] = (engine, template, exclude_names, render_params or {}) #Avoid engine lookup if we have only one engine registered if len(self.engines) == 1: self.default_engine = content_type else: self.default_engine = None # this is a work-around to make text/html prominent in respect # to other common choices when they have the same weight for # paste.util.mimeparse.best_match. self.engines_keys = sorted(self.engines, reverse=True)
def _setup_bytecode_cache(self): cache_type = config.get('jinja_bytecode_cache_type') bcc = None try: if cache_type == 'memcached' and config.get('memcached_host'): import pylibmc from jinja2 import MemcachedBytecodeCache client = pylibmc.Client([config['memcached_host']]) bcc = MemcachedBytecodeCache(client) elif cache_type == 'filesystem': from jinja2 import FileSystemBytecodeCache bcc = FileSystemBytecodeCache() except: log.exception("Error encountered while setting up a" + " %s-backed bytecode cache for Jinja" % cache_type) return bcc
def _setup_bytecode_cache(cls): cache_type = config.get('jinja_bytecode_cache_type') bcc = None try: if cache_type == 'memcached' and config.get('memcached_host'): import pylibmc from jinja2 import MemcachedBytecodeCache client = pylibmc.Client([config['memcached_host']]) bcc = MemcachedBytecodeCache(client, prefix='jinja2/{}/'.format(jinja2.__version__)) elif cache_type == 'filesystem': from jinja2 import FileSystemBytecodeCache bcc = FileSystemBytecodeCache(pattern='__jinja2_{}_%s.cache'.format(jinja2.__version__)) except: log.exception("Error encountered while setting up a" + " %s-backed bytecode cache for Jinja" % cache_type) return bcc
def add_static_file_middleware(self, app): #from tg import config log.info("ADDING STATIC MIDDLEWARE") global public_file_filter static_app = public_file_filter app = DirectCascade([static_app, app]) if asbool(config.get('bisque.static_files', True)): # used by engine to add module specific static files # Add services static files log.info("LOADING STATICS") #static_app.add_path (config['pylons.paths']['static_files'], # config['pylons.paths']['static_files'] # ) if config.get('bisque.js_environment', 'production') == 'production': static_app.add_path( '', config.get('bisque.paths.public', './public')) else: ###staticfilters = [] for x in pkg_resources.iter_entry_points("bisque.services"): try: log.info('found static service: ' + str(x)) service = x.load() if not hasattr(service, 'get_static_dirs'): continue staticdirs = service.get_static_dirs() for d, r in staticdirs: log.debug("adding static: %s %s" % (d, r)) static_app.add_path(d, r, "/%s" % x.name) except Exception: log.exception("Couldn't load bisque service %s" % x) continue # static_app = BQStaticURLParser(d) # staticfilters.append (static_app) #cascade = staticfilters + [app] #print ("CASCADE", cascade) log.info("END STATICS: discovered %s static files " % len(static_app.files.keys())) else: log.info("NO STATICS") return app
def _setup_bytecode_cache(cls): cache_type = config.get('jinja_bytecode_cache_type') bcc = None try: if cache_type == 'memcached' and config.get('memcached_host'): import pylibmc from jinja2 import MemcachedBytecodeCache client = pylibmc.Client([config['memcached_host']]) bcc_prefix = 'jinja2/{}/'.format(jinja2.__version__) if six.PY3: bcc_prefix += 'py{}{}/'.format(sys.version_info.major, sys.version_info.minor) bcc = MemcachedBytecodeCache(client, prefix=bcc_prefix) elif cache_type == 'filesystem': from jinja2 import FileSystemBytecodeCache bcc = FileSystemBytecodeCache(pattern='__jinja2_{}_%s.cache'.format(jinja2.__version__)) except Exception: log.exception("Error encountered while setting up a" + " %s-backed bytecode cache for Jinja" % cache_type) return bcc
def make_plugin(secret=None, cookie_name='auth_tkt', secure=False, include_ip=False): if secret is None: try: secret=config.get('beaker.session.secret') except KeyError: raise ValueError('secret must not be None') log.debug('secure is ' + str(secure)) plugin = InnAuthTktCookiePlugin(DBSession, secret, cookie_name, _bool(secure), _bool(include_ip)) return plugin
def _resolve_options(self): """This resolves exposition options that depend on configuration steps that might not have already happened. It's automatically called by _apply when required """ if self.engine is not None: return exclude_names = self.exclude_names template = self.template content_type = self.content_type if exclude_names is None: exclude_names = [] if template in config.get('renderers', []): engine, template = template, '' elif ':' in template: engine, template = template.split(':', 1) elif template: # Use the default templating engine from the config engine = config.get('default_renderer') else: engine, template = None, None if content_type is None: all_engines_options = config.get('rendering_engines_options', {}) engine_options = all_engines_options.get(engine, {}) content_type = engine_options.get('content_type', 'text/html') engines_without_vars = config.get('rendering_engines_without_vars', []) if engine in engines_without_vars and 'tmpl_context' not in exclude_names: exclude_names.append('tmpl_context') self.engine = engine self.template = template self.content_type = content_type self.exclude_names = exclude_names
def setup_routes(self): """Setup the default TG2 routes Override this and setup your own routes maps if you want to use custom routes. It is recommended that you keep the existing application routing in tact, and just add new connections to the mapper above the routes_placeholder connection. Lets say you want to add a pylons controller SamplesController, inside the controllers/samples.py file of your application. You would augment the app_cfg.py in the following way:: from routes import Mapper from tg.configuration import AppConfig class MyAppConfig(AppConfig): def setup_routes(self): map = Mapper(directory=config['pylons.paths']['controllers'], always_scan=config['debug']) # Add a Samples route map.connect('/samples/', controller='samples', action=index) # Setup a default route for the root of object dispatch map.connect('*url', controller='root', action='routes_placeholder') config['routes.map'] = map base_config = MyAppConfig() """ from tg.configuration import config from routes.mapper import Mapper map_ = Mapper(directory=config['pylons.paths']['controllers'], always_scan=config['debug']) # Setup a default route for the root of object dispatch controller_ = 'root' root_folder = config.get('app.root_folder') if root_folder: controller_ = '%s/root' % root_folder map_.connect('*url', controller=controller_, action='routes_placeholder') config['routes.map'] = map_
def create(cls, config, app_globals): # this has evolved over the age of allura, and upgrades of TG # the parent JinjaRenderer logic is different, some may be better and hasn't been incorporated into ours yet bcc = cls._setup_bytecode_cache() jinja2_env = jinja2.Environment( loader=PackagePathLoader(), auto_reload=config['auto_reload_templates'], autoescape=True, bytecode_cache=bcc, cache_size=asint(config.get('jinja_cache_size', -1)), extensions=['jinja2.ext.do', 'jinja2.ext.i18n']) jinja2_env.install_gettext_translations(tg.i18n) jinja2_env.filters['datetimeformat'] = helpers.datetimeformat jinja2_env.filters['filter'] = lambda s, t=None: list(filter(t and jinja2_env.tests[t], s)) jinja2_env.filters['nl2br'] = helpers.nl2br_jinja_filter jinja2_env.globals.update({'hasattr': hasattr}) config['tg.app_globals'].jinja2_env = jinja2_env # TG doesn't need this, but we use g.jinja2_env a lot return {'jinja': cls(jinja2_env)}
def setup_jinja_renderer(self): bcc = self._setup_bytecode_cache() jinja2_env = jinja2.Environment( loader=PackagePathLoader(), auto_reload=config.auto_reload_templates, autoescape=True, bytecode_cache=bcc, cache_size=config.get('jinja_cache_size', -1), extensions=['jinja2.ext.do', 'jinja2.ext.i18n']) jinja2_env.install_gettext_translations(pylons.i18n) jinja2_env.filters['filesizeformat'] = helpers.do_filesizeformat jinja2_env.filters['datetimeformat'] = helpers.datetimeformat jinja2_env.filters['filter'] = lambda s,t=None: filter(t and jinja2_env.tests[t], s) jinja2_env.filters['map'] = helpers.map_jinja_filter jinja2_env.globals.update({'hasattr': hasattr}) config['pylons.app_globals'].jinja2_env = jinja2_env # Jinja's unable to request c's attributes without strict_c config['pylons.strict_c'] = True self.render_functions.jinja = tg.render.render_jinja
def setup_jinja_renderer(self): bcc = self._setup_bytecode_cache() jinja2_env = jinja2.Environment( loader=PackagePathLoader(), auto_reload=config.auto_reload_templates, autoescape=True, bytecode_cache=bcc, cache_size=config.get('jinja_cache_size', -1), extensions=['jinja2.ext.do', 'jinja2.ext.i18n']) jinja2_env.install_gettext_translations(pylons.i18n) jinja2_env.filters['filesizeformat'] = helpers.do_filesizeformat jinja2_env.filters['datetimeformat'] = helpers.datetimeformat jinja2_env.filters['filter'] = lambda s,t=None: filter(t and jinja2_env.tests[t], s) jinja2_env.filters['map'] = helpers.map_jinja_filter jinja2_env.filters['nl2br'] = helpers.nl2br_jinja_filter jinja2_env.globals.update({'hasattr': hasattr}) config['pylons.app_globals'].jinja2_env = jinja2_env # Jinja's unable to request c's attributes without strict_c config['pylons.strict_c'] = True self.render_functions.jinja = tg.render.render_jinja
def create(cls, config, app_globals): # this has evolved over the age of allura, and upgrades of TG # the parent JinjaRenderer logic is different, some may be better and hasn't been incorporated into ours yet bcc = cls._setup_bytecode_cache() jinja2_env = jinja2.Environment( loader=PackagePathLoader(), auto_reload=config['auto_reload_templates'], autoescape=True, bytecode_cache=bcc, cache_size=config.get('jinja_cache_size', -1), extensions=['jinja2.ext.do', 'jinja2.ext.i18n']) jinja2_env.install_gettext_translations(tg.i18n) jinja2_env.filters['filesizeformat'] = helpers.do_filesizeformat jinja2_env.filters['datetimeformat'] = helpers.datetimeformat jinja2_env.filters['filter'] = lambda s, t=None: filter(t and jinja2_env.tests[t], s) jinja2_env.filters['nl2br'] = helpers.nl2br_jinja_filter jinja2_env.globals.update({'hasattr': hasattr}) config['tg.app_globals'].jinja2_env = jinja2_env # TG doesn't need this, but we use g.jinja2_env a lot return {'jinja': cls(jinja2_env)}
from tg.configuration import config #from tg.controllers import CUSTOM_CONTENT_TYPE from bq.core import identity from bq.core.service import ServiceController #from bq.exceptions import RequestError from bq.util.paths import data_path from bq.util.converters import asbool #from bq.util.copylink import copy_link from bq.util.io_misc import dolink #from bq.util.xmldict import d2xml from .formats import find_inputer, find_formatter log = logging.getLogger("bq.data_service.resource") CACHING = asbool(config.get('bisque.data_service.caching', True)) ETAGS = asbool(config.get('bisque.data_service.etags', True)) #SERVER_CACHE = asbool(config.get('bisque.data_service.server_cache', True)) CACHEDIR = config.get('bisque.data_service.server_cache', data_path('server_cache')) URI = re.compile(r"^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?") def parse_uri(uri): """Parses a URI using the regex given in Appendix B of RFC 3986. (scheme, authority, path, query, fragment) = parse_uri(uri) """ groups = URI.match(uri).groups() return (groups[1], groups[3], groups[4], groups[6], groups[8])
def add_auth_middleware(self, app, skip_authentication): """ """ log_stream = config.get('who.log_stream', 'stdout') log_stream = LOG_STREAMS.get(log_stream, log_stream) if isinstance(log_stream, basestring): log_stream = logging.getLogger(log_stream) log_level = LOG_LEVELS.get(config['who.log_level'], logging.ERROR) log.debug("LOG_STREAM %s LOG_LEVEL %s", str(log_stream), str(log_level)) if 'who.config_file' in config and asbool( config.get('bisque.has_database')): parser = WhoConfig(config['here']) parser.parse(open(config['who.config_file'])) if not asbool(skip_authentication): app = PluggableAuthenticationMiddleware( app, parser.identifiers, parser.authenticators, parser.challengers, parser.mdproviders, parser.request_classifier, parser.challenge_decider, log_stream=log_stream, log_level=log_level, remote_user_key=parser.remote_user_key, ) else: app = AuthenticationForgerMiddleware( app, parser.identifiers, parser.authenticators, parser.challengers, parser.mdproviders, parser.request_classifier, parser.challenge_decider, log_stream=log_stream, log_level=log_level, remote_user_key=parser.remote_user_key, ) else: log.info("MEX auth only") # Add mex only authentication from repoze.who.plugins.basicauth import BasicAuthPlugin from bq.core.lib.mex_auth import make_plugin from repoze.who.classifiers import default_request_classifier from repoze.who.classifiers import default_challenge_decider basicauth = BasicAuthPlugin('repoze.who') mexauth = make_plugin() identifiers = [('mexauth', mexauth)] authenticators = [('mexauth', mexauth)] challengers = [] mdproviders = [] app = PluggableAuthenticationMiddleware( app, identifiers, authenticators, challengers, mdproviders, default_request_classifier, default_challenge_decider, log_stream=log_stream, log_level=log_level, ) return app
def __init__(self): super(StudyFileManager, self).__init__() path=os.path.join(config.get('loom.filestorage.userdata',os.path.join(os.path.dirname(__file__), '..','..','..','..','local')), 'studies') self.setRoot(path)