# Path: pypi_portal/application.py
# def create_app(config_obj, no_sql=False):
#     """Flask application factory. Initializes and returns the Flask application.
# 
#     Blueprints are registered in here.
# 
#     Modeled after: http://flask.pocoo.org/docs/patterns/appfactories/
# 
#     Positional arguments:
#     config_obj -- configuration object to load into app.config.
# 
#     Keyword arguments:
#     no_sql -- does not run init_app() for the SQLAlchemy instance. For Celery compatibility.
# 
#     Returns:
#     The initialized Flask application.
#     """
#     # Initialize app. Flatten config_obj to dictionary (resolve properties).
#     app = Flask(__name__, template_folder=TEMPLATE_FOLDER, static_folder=STATIC_FOLDER)
#     config_dict = dict([(k, getattr(config_obj, k)) for k in dir(config_obj) if not k.startswith('_')])
#     app.config.update(config_dict)
# 
#     # Import DB models. Flask-SQLAlchemy doesn't do this automatically like Celery does.
#     with app.app_context():
#         for module in app.config.get('DB_MODELS_IMPORTS', list()):
#             import_module(module)
# 
#     # Setup redirects and register blueprints.
#     app.add_url_rule('/favicon.ico', 'favicon', lambda: app.send_static_file('favicon.ico'))
#     for bp in all_blueprints:
#         import_module(bp.import_name)
#         app.register_blueprint(bp)
# 
#     # Initialize extensions/add-ons/plugins.
#     if not no_sql:
#         db.init_app(app)
#     Statics(app)  # Enable Flask-Statics-Helper features.
#     redis.init_app(app)
#     celery.init_app(app)
#     mail.init_app(app)
# 
#     # Activate middleware.
#     locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')  # For filters inside the middleware file.
#     with app.app_context():
#         import_module('pypi_portal.middleware')
# 
#     # Return the application instance.
#     return app
# 
# def get_config(config_class_string, yaml_files=None):
#     """Load the Flask config from a class.
# 
#     Positional arguments:
#     config_class_string -- string representation of a configuration class that will be loaded (e.g.
#         'pypi_portal.config.Production').
#     yaml_files -- List of YAML files to load. This is for testing, leave None in dev/production.
# 
#     Returns:
#     A class object to be fed into app.config.from_object().
#     """
#     config_module, config_class = config_class_string.rsplit('.', 1)
#     config_class_object = getattr(import_module(config_module), config_class)
#     config_obj = config_class_object()
# 
#     # Expand some options.
#     celery_fmt = 'pypi_portal.tasks.{}'
#     db_fmt = 'pypi_portal.models.{}'
#     if getattr(config_obj, 'CELERY_IMPORTS', False):
#         config_obj.CELERY_IMPORTS = [celery_fmt.format(m) for m in config_obj.CELERY_IMPORTS]
#     for definition in getattr(config_obj, 'CELERYBEAT_SCHEDULE', dict()).values():
#         definition.update(task=celery_fmt.format(definition['task']))
#     if getattr(config_obj, 'DB_MODELS_IMPORTS', False):
#         config_obj.DB_MODELS_IMPORTS = [db_fmt.format(m) for m in config_obj.DB_MODELS_IMPORTS]
#     #for script_name, script_file in getattr(config_obj, 'REDIS_SCRIPTS', dict()).items():
#     #    config_obj.REDIS_SCRIPTS[script_name] = os.path.join(REDIS_SCRIPTS_FOLDER, script_file)
# 
#     # Load additional configuration settings.
#     yaml_files = yaml_files or [f for f in [
#         os.path.join('/', 'etc', 'pypi_portal', 'config.yml'),
#         os.path.abspath(os.path.join(APP_ROOT_FOLDER, '..', 'config.yml')),
#         os.path.join(APP_ROOT_FOLDER, 'config.yml'),
#     ] if os.path.exists(f)]
#     additional_dict = dict()
#     for y in yaml_files:
#         with open(y) as f:
#             additional_dict.update(load(f.read()))
# 
#     # Merge the rest into the Flask app config.
#     for key, value in additional_dict.items():
#         setattr(config_obj, key, value)
# 
#     return config_obj
# 
# Path: pypi_portal/extensions.py
# LOG = getLogger(__name__)
# def _on_connect(dbapi_connection, **_):


from functools import wraps
from celery.app.log import Logging
from celery.bin.celery import main as celery_main
from docopt import docopt
from flask.ext.script import Shell
from tornado import httpserver, ioloop, web, wsgi
from pypi_portal.application import create_app, get_config
from pypi_portal.extensions import db
import logging
import logging.handlers
import os
import signal
import sys
import flask
    celery_args = ['celery', 'worker', '-B', '-s', '/tmp/celery.db', '--concurrency=5']
    with app.app_context():
        return celery_main(celery_args)


@command
def celerybeat():
    setup_logging('celerybeat')
    app = create_app(parse_options(), no_sql=True)
    Logging._setup = True
    celery_args = ['celery', 'beat', '-C', '--pidfile', OPTIONS['--pid'], '-s', OPTIONS['--schedule']]
    with app.app_context():
        return celery_main(celery_args)


@command
def celeryworker():
    setup_logging('celeryworker{}'.format(OPTIONS['--name']))
    app = create_app(parse_options(), no_sql=True)
    Logging._setup = True
    celery_args = ['celery', 'worker', '-n', OPTIONS['--name'], '-C', '--autoscale=10,1', '--without-gossip']
    with app.app_context():
        return celery_main(celery_args)


@command
def shell():
    setup_logging('shell')
    app = create_app(parse_options())
    app.app_context().push()

# Path: pypi_portal/application.py
# def create_app(config_obj, no_sql=False):
#     """Flask application factory. Initializes and returns the Flask application.
# 
#     Blueprints are registered in here.
# 
#     Modeled after: http://flask.pocoo.org/docs/patterns/appfactories/
# 
#     Positional arguments:
#     config_obj -- configuration object to load into app.config.
# 
#     Keyword arguments:
#     no_sql -- does not run init_app() for the SQLAlchemy instance. For Celery compatibility.
# 
#     Returns:
#     The initialized Flask application.
#     """
#     # Initialize app. Flatten config_obj to dictionary (resolve properties).
#     app = Flask(__name__, template_folder=TEMPLATE_FOLDER, static_folder=STATIC_FOLDER)
#     config_dict = dict([(k, getattr(config_obj, k)) for k in dir(config_obj) if not k.startswith('_')])
#     app.config.update(config_dict)
# 
#     # Import DB models. Flask-SQLAlchemy doesn't do this automatically like Celery does.
#     with app.app_context():
#         for module in app.config.get('DB_MODELS_IMPORTS', list()):
#             import_module(module)
# 
#     # Setup redirects and register blueprints.
#     app.add_url_rule('/favicon.ico', 'favicon', lambda: app.send_static_file('favicon.ico'))
#     for bp in all_blueprints:
#         import_module(bp.import_name)
#         app.register_blueprint(bp)
# 
#     # Initialize extensions/add-ons/plugins.
#     if not no_sql:
#         db.init_app(app)
#     Statics(app)  # Enable Flask-Statics-Helper features.
#     redis.init_app(app)
#     celery.init_app(app)
#     mail.init_app(app)
# 
#     # Activate middleware.
#     locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')  # For filters inside the middleware file.
#     with app.app_context():
#         import_module('pypi_portal.middleware')
# 
#     # Return the application instance.
#     return app
# 
# def get_config(config_class_string, yaml_files=None):
#     """Load the Flask config from a class.
# 
#     Positional arguments:
#     config_class_string -- string representation of a configuration class that will be loaded (e.g.
#         'pypi_portal.config.Production').
#     yaml_files -- List of YAML files to load. This is for testing, leave None in dev/production.
# 
#     Returns:
#     A class object to be fed into app.config.from_object().
#     """
#     config_module, config_class = config_class_string.rsplit('.', 1)
#     config_class_object = getattr(import_module(config_module), config_class)
#     config_obj = config_class_object()
# 
#     # Expand some options.
#     celery_fmt = 'pypi_portal.tasks.{}'
#     db_fmt = 'pypi_portal.models.{}'
#     if getattr(config_obj, 'CELERY_IMPORTS', False):
#         config_obj.CELERY_IMPORTS = [celery_fmt.format(m) for m in config_obj.CELERY_IMPORTS]
#     for definition in getattr(config_obj, 'CELERYBEAT_SCHEDULE', dict()).values():
#         definition.update(task=celery_fmt.format(definition['task']))
#     if getattr(config_obj, 'DB_MODELS_IMPORTS', False):
#         config_obj.DB_MODELS_IMPORTS = [db_fmt.format(m) for m in config_obj.DB_MODELS_IMPORTS]
#     #for script_name, script_file in getattr(config_obj, 'REDIS_SCRIPTS', dict()).items():
#     #    config_obj.REDIS_SCRIPTS[script_name] = os.path.join(REDIS_SCRIPTS_FOLDER, script_file)
# 
#     # Load additional configuration settings.
#     yaml_files = yaml_files or [f for f in [
#         os.path.join('/', 'etc', 'pypi_portal', 'config.yml'),
#         os.path.abspath(os.path.join(APP_ROOT_FOLDER, '..', 'config.yml')),
#         os.path.join(APP_ROOT_FOLDER, 'config.yml'),
#     ] if os.path.exists(f)]
#     additional_dict = dict()
#     for y in yaml_files:
#         with open(y) as f:
#             additional_dict.update(load(f.read()))
# 
#     # Merge the rest into the Flask app config.
#     for key, value in additional_dict.items():
#         setattr(config_obj, key, value)
# 
#     return config_obj
# 
# Path: pypi_portal/extensions.py
# LOG = getLogger(__name__)
# def _on_connect(dbapi_connection, **_):


# Path: manage.py
from functools import wraps
from celery.app.log import Logging
from celery.bin.celery import main as celery_main
from docopt import docopt
from flask.ext.script import Shell
from tornado import httpserver, ioloop, web, wsgi
from pypi_portal.application import create_app, get_config
from pypi_portal.extensions import db
import logging
import logging.handlers
import os
import signal
import sys
import flask

    celery_args = ['celery', 'worker', '-B', '-s', '/tmp/celery.db', '--concurrency=5']
    with app.app_context():
        return celery_main(celery_args)


@command
def celerybeat():
    setup_logging('celerybeat')
    app = create_app(parse_options(), no_sql=True)
    Logging._setup = True
    celery_args = ['celery', 'beat', '-C', '--pidfile', OPTIONS['--pid'], '-s', OPTIONS['--schedule']]
    with app.app_context():
        return celery_main(celery_args)


@command
def celeryworker():
    setup_logging('celeryworker{}'.format(OPTIONS['--name']))
    app = create_app(parse_options(), no_sql=True)
    Logging._setup = True
    celery_args = ['celery', 'worker', '-n', OPTIONS['--name'], '-C', '--autoscale=10,1', '--without-gossip']
    with app.app_context():
        return celery_main(celery_args)


@command
def shell():
    setup_logging('shell')
    app = create_app(parse_options())
    app.app_context().push()

    Shell(make_context=lambda: dict(app=app, db=db)).run(no_ipython=False, no_bpython=False)