def create_app(**kwargs): """ create a WSGI application that can be used in the modules. """ from flask import Flask from pyggi.lib.config import config import logging static_base = '/static' if config.has_option('general', 'static_url_base'): static_base = config.get('general', 'static_url_base') app = Flask(__name__, static_url_path=static_base) # get logging settings config_settings = dict(level=logging.WARNING, format="[%(asctime)s] %(levelname)s: %(message)s", datefmt="%Y-%m-%d %H:%M:%S") if config.has_option('log', 'file'): config_settings['filename'] = config.get('log', 'file') # activate logging logging.basicConfig(**config_settings) # register modules to application. the modules come # from the configuration file. there is a list of # modules to be imported. an item in this list has the # form ("module:name","prefix") where name is the name of the module # object that should be imported for module_desc in config.items('modules'): module, prefix = module_desc module, attribute = module.rsplit('.', 1) # now we try to import the module and register it # in our application. otherwise ignore and continue try: _import = __import__(module, globals(), locals(), [attribute], -1) prefix = "" if prefix.strip('/') == "" else "/" + prefix.strip('/') app.register_blueprint(getattr(_import, attribute), url_prefix=prefix) except Exception as e: logging.critical("could not load module '%s': %s", module_desc[0], e) # register some filters import lib.filters app.jinja_env.filters['dateformat'] = lib.filters.format_datetime app.jinja_env.filters['diffformat'] = lib.filters.format_diff app.jinja_env.filters['timesince'] = lib.filters.humanize_timesince app.jinja_env.filters['force_unicode'] = lib.filters.force_unicode app.jinja_env.filters['first_line'] = lib.filters.first_line app.jinja_env.tests['text'] = lib.filters.is_text app.context_processor( lambda: dict(static_url_for=lib.filters.static_url_for)) return app
def create_app(**kwargs): """ create a WSGI application that can be used in the modules. """ from flask import Flask from pyggi.lib.config import config import logging static_base = '/static' if config.has_option('general', 'static_url_base'): static_base = config.get('general', 'static_url_base') app = Flask(__name__, static_url_path=static_base) # get logging settings config_settings = dict( level=logging.WARNING, format="[%(asctime)s] %(levelname)s: %(message)s", datefmt="%Y-%m-%d %H:%M:%S" ) if config.has_option('log', 'file'): config_settings['filename'] = config.get('log', 'file') # activate logging logging.basicConfig(**config_settings) # register modules to application. the modules come # from the configuration file. there is a list of # modules to be imported. an item in this list has the # form ("module:name","prefix") where name is the name of the module # object that should be imported for module_desc in config.items('modules'): module, prefix = module_desc module, attribute = module.rsplit('.', 1) # now we try to import the module and register it # in our application. otherwise ignore and continue try: _import = __import__(module, globals(), locals(), [attribute], -1) prefix = "" if prefix.strip('/') == "" else "/" + prefix.strip('/') app.register_blueprint(getattr(_import, attribute), url_prefix=prefix) except Exception as e: logging.critical("could not load module '%s': %s", module_desc[0], e) # register some filters import lib.filters app.jinja_env.filters['dateformat'] = lib.filters.format_datetime app.jinja_env.filters['diffformat'] = lib.filters.format_diff app.jinja_env.filters['timesince'] = lib.filters.humanize_timesince app.jinja_env.filters['force_unicode'] = lib.filters.force_unicode app.jinja_env.filters['first_line'] = lib.filters.first_line app.jinja_env.tests['text'] = lib.filters.is_text app.context_processor(lambda: dict(static_url_for=lib.filters.static_url_for)) return app
def template_function(*args, **kwargs): # error when no template is given if template is None: raise Exception("no template given") # get the context from the executed function context = None context = f(*args, **kwargs) if context is None: context = {} elif not isinstance(context, dict): return context # add the page name into the context from pyggi.lib.config import config context['page_name'] = ( request.environ.get('pyggi.site_name') or request.environ.get('wsgiorg.routing_args', (None, {}))[1].get('site_name') or config.get('general', 'name') ) # add pygglet object into the context from pyggi.lib.pygglet import __pygglets__ context['pygglets'] = __pygglets__ # render the context using given template response = render_template(template, **context) return response
def template_function(*args, **kwargs): # error when no template is given if template is None: raise Exception("no template given") # get the context from the executed function context = None context = f(*args, **kwargs) if context is None: context = {} elif not isinstance(context, dict): return context # add the page name into the context from pyggi.lib.config import config context['page_name'] = (request.environ.get('pyggi.site_name') or request.environ.get( 'wsgiorg.routing_args', (None, {}))[1].get('site_name') or config.get('general', 'name')) # add pygglet object into the context from pyggi.lib.pygglet import __pygglets__ context['pygglets'] = __pygglets__ # render the context using given template response = render_template(template, **context) return response
def get_repository_base(): from flask import request return request.environ.get('wsgiorg.routing_args', (None, {}))[1].get('repository_base') or config.get('general', 'git_repositories')
class DummyCache(object): def get(*args, **kwargs): pass def set(*args, **kwargs): pass if not (config.has_option('cache', 'enabled') and config.getboolean('cache', 'enabled')): cache = DummyCache() else: if current_app.debug: from werkzeug.contrib.cache import SimpleCache cache = SimpleCache() else: try: from werkzeug.contrib.cache import MemcachedCache cache = MemcachedCache([x.strip() for x in config.get('cache', 'uris').split(",") if not len(x.strip()) == 0]) except: cache = DummyCache() logging.critical("could not connect to memcache daemon - disabling caching") def get_clone_urls(): from flask import request return request.environ.get('wsgiorg.routing_args', (None, {}))[1].get('repository_clone_urls') or dict(config.items('clone')) def get_repository_base(): from flask import request return request.environ.get('wsgiorg.routing_args', (None, {}))[1].get('repository_base') or config.get('general', 'git_repositories') def get_repository_path(name): import os return os.path.join(get_repository_base(), name)
def get_repository_base(): from flask import request return request.environ.get( 'wsgiorg.routing_args', (None, {}))[1].get('repository_base') or config.get( 'general', 'git_repositories')
def set(*args, **kwargs): pass if not (config.has_option('cache', 'enabled') and config.getboolean('cache', 'enabled')): cache = DummyCache() else: if current_app.debug: from werkzeug.contrib.cache import SimpleCache cache = SimpleCache() else: try: from werkzeug.contrib.cache import MemcachedCache cache = MemcachedCache([ x.strip() for x in config.get('cache', 'uris').split(",") if not len(x.strip()) == 0 ]) except: cache = DummyCache() logging.critical( "could not connect to memcache daemon - disabling caching") def get_clone_urls(): from flask import request return request.environ.get( 'wsgiorg.routing_args', (None, {}))[1].get('repository_clone_urls') or dict( config.items('clone'))