Exemple #1
0
def boostrap_from_config(config_file_name):
    """
    just a small wrapper that does the same as uwsgi with the ``*.ini`` file, via ``setup.py``
    take the given filename, bootstrap an app and configure lgging for it

    see http://stackoverflow.com/a/19058365

    :param config_file_name:
    :return:
    """
    assert config_file_name is not None
    if not os.path.exists(config_file_name):
        raise RuntimeError('file does not exist: %s' % config_file_name)
    config_file_name = os.path.realpath(config_file_name)

    # monkeypatching psycopg2 compatibility
    # https://github.com/chtd/psycopg2cffi
    from psycopg2cffi import compat
    compat.register()

    # dont use paste, but pyramid to also configure logging http://stackoverflow.com/a/19058365
    # from paste.deploy import loadapp
    # loadapp("config:%s" % config_file)
    pyramid.paster.setup_logging(config_file_name)
    app = pyramid.paster.get_app(config_file_name)

    log.debug('used configfile: %s', config_file_name)
    return app
Exemple #2
0
def hack_psycopg2cffi():  # pragma: nocover
    try:
        from psycopg2cffi import compat
    except ImportError:
        pass
    else:
        compat.register()
    finally:
        import psycopg2
    return
Exemple #3
0
def patch_db_conn():
    """
    We use psycopg2cffi if under pypy, and we need to "greenify"
    it when under either an eventlet or a gevent worker
    """
    if INTERPRETER == "pypy3":
        from psycopg2cffi import compat
        logging.info("Patching PsycoPG2")
        compat.register()

    if WORKER_TYPE == "eventlet":
        import eventlet
        logging.info("Patching for Eventlet")
        eventlet.monkey_patch()

    elif WORKER_TYPE == "gevent" and DB_CONNECTOR == "psycopg":
        import psycogreen.gevent
        logging.info("Patching for Gevent")
        psycogreen.gevent.patch_psycopg()
Exemple #4
0
def make_wsgi_application(global_config, **local_conf):
    """
    generic function to create a wsgi application out of a given paster config

    :param global_config:
    :param local_conf:
    :return:
    """

    # monkeypatching psycopg2 compatibility
    # https://github.com/chtd/psycopg2cffi
    from psycopg2cffi import compat
    compat.register()

    # Do something before importing Django and your settings have been applied.
    if global_config['__file__'] not in __apps_by_ini_configuration__:
        log.debug('create django wsgi app from ini-file %s...', global_config['__file__'])
        app = get_configured_django_wsgi_app(global_config, **local_conf)

        # Do something right after your application has been set up (e.g., add WSGI middleware).
        # http://rhodesmill.org/brandon/2011/wsgi-under-cherrypy/

        # non-standard customization: we want to be able to switch a function via ini-configuration
        from django.conf import settings as django_settings

        # TODO amb: tiny hack: cookie-domain must be a string. dunno where this becomes a unicode
        django_settings.SESSION_COOKIE_DOMAIN = str(django_settings.SESSION_COOKIE_DOMAIN)

        log.debug('FIXTURE_DIRS:    %s', getattr(django_settings, 'FIXTURE_DIRS', None))

        debug_flag = global_config['debug'] == 'true' or False
        if debug_flag:
            log.debug('add `TransLogger` to see all requests')
            app = TransLogger(app)
        __apps_by_ini_configuration__[global_config['__file__']] = app
    else:
        log.warn('reuse django wsgi app from ini-file %s to avoid ``django.conf`` collisions',
                 global_config['__file__'])
    return __apps_by_ini_configuration__[global_config['__file__']]
Exemple #5
0
def greenify():
    """Patch threading and psycopg2 modules for green threads."""
    # don't greenify twice.
    if _GREEN:
        return
    _GREEN[True] = True

    from gevent.monkey import patch_all, saved
    if ('threading' in sys.modules) and ('threading' not in saved):
        raise Exception('threading module loaded before patching!')
    patch_all()

    from psycogreen.gevent import patch_psycopg
    patch_psycopg()

    try:
        # Use psycopg2 by default
        import psycopg2
        del psycopg2
    except ImportError:
        # Fallback to psycopg2cffi if required (eg: pypy)
        from psycopg2cffi import compat
        compat.register()
Exemple #6
0
def greenify():
    """Patch threading and psycopg2 modules for green threads."""
    # don't greenify twice.
    if _GREEN:
        return
    _GREEN[True] = True

    from gevent.monkey import patch_all, saved
    if ('threading' in sys.modules) and ('threading' not in saved):
        raise Exception('threading module loaded before patching!')
    patch_all()

    from psycogreen.gevent import patch_psycopg
    patch_psycopg()

    try:
        # Use psycopg2 by default
        import psycopg2
        del psycopg2
    except ImportError:
        # Fallback to psycopg2cffi if required (eg: pypy)
        from psycopg2cffi import compat
        compat.register()
Exemple #7
0
def init_sa(config, **connect_kwargs):
    settings = config.registry.settings

    pc_name = settings.pop('sqlalchemy.poolclass', '')
    if pc_name:
        resolver = DottedNameResolver()
        pool_class = resolver.maybe_resolve(pc_name)
        settings['sqlalchemy.poolclass'] = pool_class

    # http://stackoverflow.com/a/15691283
    engine = engine_from_config(settings, 'sqlalchemy.',
                                connect_args=connect_kwargs)

    db_session = get_db_session(None, settings)
    db_session.configure(bind=engine)
    model_paths = config.registry.setdefault('bowab.models', set())
    for emp in model_paths:
        config.scan(emp)

    if '__pypy__' in sys.builtin_module_names:
        from psycopg2cffi import compat
        compat.register()

    return db_session
Exemple #8
0
import re
import sys
import os.path
import dj_database_url

try:
    from psycopg2cffi import compat

    compat.register()
except ImportError:
    pass

import django
import six

django.utils.six = six

BASE_DIR = os.path.dirname(os.path.abspath(__file__))


def get_bool(name, default):
    return os.getenv(name, str(default)).lower() == "true"


def get_intOrNone(name, default):
    """Parses the env variable, accepts ints and literal None"""
    value = os.getenv(name, str(default))
    if value.lower() == "none":
        return None
    return int(value)
# Flask-Restless is distributed under both the GNU Affero General Public
# License version 3 and under the 3-clause BSD license. For more
# information, see LICENSE.AGPL and LICENSE.BSD.
"""Unit tests for filtering resources in client requests."""
from datetime import date
from datetime import datetime
from datetime import time
from operator import itemgetter

# This import is unused but is required for testing on PyPy. CPython can
# use psycopg2, but PyPy can only use psycopg2cffi.
try:
    import psycopg2
except ImportError:
    from psycopg2cffi import compat
    compat.register()
from sqlalchemy import Column
from sqlalchemy import Date
from sqlalchemy import DateTime
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
from sqlalchemy import Time
from sqlalchemy import Unicode
from sqlalchemy.dialects.postgresql import INET
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.orm import backref
from sqlalchemy.orm import relationship
from testing.postgresql import PostgresqlFactory as PGFactory

from .helpers import check_sole_error
from .helpers import dumps
Exemple #10
0
def app_factory():
    """Factory function for the WSGI application object.

    Reads config, initializes Flask extensions that are part of the core,
    loads pg-config drivers and extensions, and configures core views.
    """
    # Use psycopg2cffi if PYPY
    if _compat.PYPY:  # pragma: no cover
        pg2cfficompat.register()

    # Flask application
    app = flask.Flask('pg-discuss')
    app.root_path = os.path.abspath(os.path.dirname(__file__))

    # Load default config values from pg_discuss.config module
    app.config.from_object(config)

    # Load custom config from user-defined PG_DISCUSS_SETTINGS_FILE
    custom_settings = os.environ.get('PG_DISCUSS_SETTINGS_FILE',
                                     '/opt/pg-discuss/local_settings.py')
    if custom_settings and os.path.isfile(custom_settings):
        app.config.from_pyfile(custom_settings)

    # Set the recursion limit
    sys.setrecursionlimit(app.config['PYTHON_RECURSION_LIMIT'])

    # Flask-SQLAlchemy
    db.init_app(app)

    # Flask-Migrate
    app.migrate = flask_migrate.Migrate(app, db)

    # Flask-Script
    app.script_manager = flask_script.Manager(app)
    app.script_manager.add_command('db', flask_migrate.MigrateCommand)
    app.script_manager.add_command('createadminuser',
                                   auth_forms.CreateAdminUser)

    # Flask-Login, for Admin users.
    app.admin_login_manager = flask_login.LoginManager(app)
    # Set up callback to load user objects`
    app.admin_login_manager.user_loader(
        lambda user_id: db.session.query(models.AdminUser).get(user_id))

    # Use stevedore to load drivers/extensions.
    # Discover all drivers/extensions, but do not load any.
    # Used for logging found extensions.
    app.ext_mgr_all = stevedore.ExtensionManager(namespace='pg_discuss.ext')

    # Load configured IdentityPolicy driver
    app.identity_policy_loader = stevedore.DriverManager(
        namespace='pg_discuss.ext',
        name=app.config['DRIVER_IDENTITY_POLICY'],
    )
    # Initialize IdentityPolicyManager with configured IdentityManager
    app.identity_policy_mgr = identity.IdentityPolicyManager(
        app,
        app.identity_policy_loader.driver,
    )

    # Load configured CommentRenderer driver
    app.comment_renderer_loader = stevedore.DriverManager(
        namespace='pg_discuss.ext',
        name=app.config['DRIVER_COMMENT_RENDERER'],
    )
    app.comment_renderer = app.comment_renderer_loader.driver(app)

    # Load configured JSONEncoder driver
    app.json_encoder_loader = stevedore.DriverManager(
        namespace='pg_discuss.ext',
        name=app.config['DRIVER_JSON_ENCODER'],
    )
    app.json_encoder = app.json_encoder_loader.driver

    # Exempt public read-only views from IdentityPolicy
    app.identity_policy_mgr.exempt(views.fetch)
    app.identity_policy_mgr.exempt(views.view)

    # Default routes. Other routes must be added through App extensions.
    # Default routes are set up before app extensions are loaded so extensions
    # can introspect/modify view functions.
    app.route('/threads/<thread_cid>/comments', methods=['GET'])(views.fetch)
    app.route('/threads/<thread_cid>/comments', methods=['POST'])(views.new)
    app.route('/comments/<int:comment_id>', methods=['GET'])(views.view)
    app.route('/comments/<int:comment_id>', methods=['PATCH'])(views.edit)
    app.route('/comments/<int:comment_id>', methods=['DELETE'])(views.delete)
    app.route('/login', methods=['GET', 'POST'])(views.admin_login)
    app.route('/logout', methods=['GET'])(views.admin_logout)

    # Load all extensions explicitly enabled via `ENABLE_EXT_*` parameters.
    app.ext_mgr = stevedore.NamedExtensionManager(
        namespace='pg_discuss.ext',
        names=config.get_enabled_extensions(app.config),
        name_order=True,
        invoke_on_load=True,
        invoke_kwds={'app': app},
        on_load_failure_callback=ext.fail_on_ext_load,
        propagate_map_exceptions=True,
    )
    # Create hook map
    app.hook_map = ext.get_hook_map(app.ext_mgr.extensions, ext.hook_classes())

    # Run the `init_app` hooks.
    ext.exec_init_app(app)

    # Add a route to the landing page at the root, '/'. Ignore if an extension
    # has already set up a route for the root.
    try:
        app.route('/', methods=['GET'])(views.landing)
    except:
        pass

    return app
Exemple #11
0
import enum
import os

try:
    from psycopg2cffi.compat import register
except ImportError:
    pass
else:
    register()
from pytest import fixture, yield_fixture
from sqlalchemy.engine import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import NullPool
from sqlalchemy.schema import Column
from sqlalchemy.sql import text
from sqlalchemy.types import Integer

from sqlalchemy_enum34 import Enum, EnumType


Base = declarative_base()
Session = sessionmaker()


class Color(enum.Enum):

    red = 'r'
    green = 'g'
    blue = 'b'
Exemple #12
0
class PostgreSQLStore(SQLStore):
    """
    This is a PostgreSQL-based specialization of C{L{SQLStore}}.

    To create an instance, see C{L{SQLStore.__init__}}.  To create the
    tables it will use, see C{L{SQLStore.createTables}}.

    All other methods are implementation details.
    """

    try:
        import psycopg2
    except ImportError:
        from psycopg2cffi import compat
        compat.register()

    exceptions = None

    create_nonce_sql = """
    CREATE TABLE %(nonces)s (
        server_url VARCHAR(2047) NOT NULL,
        timestamp INTEGER NOT NULL,
        salt CHAR(40) NOT NULL,
        PRIMARY KEY (server_url, timestamp, salt)
    );
    """

    create_assoc_sql = """
    CREATE TABLE %(associations)s
    (
        server_url VARCHAR(2047) NOT NULL,
        handle VARCHAR(255) NOT NULL,
        secret BYTEA NOT NULL,
        issued INTEGER NOT NULL,
        lifetime INTEGER NOT NULL,
        assoc_type VARCHAR(64) NOT NULL,
        PRIMARY KEY (server_url, handle),
        CONSTRAINT secret_length_constraint CHECK (LENGTH(secret) <= 128)
    );
    """

    def db_set_assoc(self, server_url, handle, secret, issued, lifetime,
                     assoc_type):
        """
        Set an association.  This is implemented as a method because
        REPLACE INTO is not supported by PostgreSQL (and is not
        standard SQL).
        """
        result = self.db_get_assoc(server_url, handle)
        rows = self.cur.fetchall()
        if len(rows):
            # Update the table since this associations already exists.
            return self.db_update_assoc(secret, issued, lifetime, assoc_type,
                                        server_url, handle)
        else:
            # Insert a new record because this association wasn't
            # found.
            return self.db_new_assoc(server_url, handle, secret, issued,
                                     lifetime, assoc_type)

    new_assoc_sql = ('INSERT INTO %(associations)s '
                     'VALUES (%%s, %%s, %%s, %%s, %%s, %%s);')
    update_assoc_sql = ('UPDATE %(associations)s SET '
                        'secret = %%s, issued = %%s, '
                        'lifetime = %%s, assoc_type = %%s '
                        'WHERE server_url = %%s AND handle = %%s;')
    get_assocs_sql = ('SELECT handle, secret, issued, lifetime, assoc_type'
                      ' FROM %(associations)s WHERE server_url = %%s;')
    get_expired_sql = ('SELECT server_url '
                       'FROM %(associations)s WHERE issued + lifetime < %%s;')

    get_assoc_sql = (
        'SELECT handle, secret, issued, lifetime, assoc_type'
        ' FROM %(associations)s WHERE server_url = %%s AND handle = %%s;')
    remove_assoc_sql = ('DELETE FROM %(associations)s '
                        'WHERE server_url = %%s AND handle = %%s;')

    clean_assoc_sql = 'DELETE FROM %(associations)s WHERE issued + lifetime < %%s;'

    add_nonce_sql = 'INSERT INTO %(nonces)s VALUES (%%s, %%s, %%s);'

    clean_nonce_sql = 'DELETE FROM %(nonces)s WHERE timestamp < %%s;'

    def blobEncode(self, blob):
        from psycopg2 import Binary

        return Binary(blob)

    def blobDecode(self, blob):
        return blob.tobytes()
def patch_psycopg2():
    from psycopg2cffi import compat

    compat.register()
Exemple #14
0
VERSION = '0.1.0.dev1'

try:
    from psycopg2cffi import compat as _compat
except ImportError:
    pass
else:
    _compat.register()
Exemple #15
0
""":mod:`favien` --- Favien
~~~~~~~~~~~~~~~~~~~~~~~~~~~

"""
import platform


if platform.python_implementation() == 'PyPy':
    # Monkey patches for PyPy
    from psycopg2cffi.compat import register
    register()