コード例 #1
0
ファイル: environment.py プロジェクト: pombredanne/old-webapp
def load_environment(global_conf, app_conf):
    """Configure the Pylons environment via the ``pylons.config`` object

    """

    # Pylons paths
    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    paths = dict(root=root,
                 controllers=os.path.join(root, 'controllers'),
                 static_files=os.path.join(root, 'public'),
                 templates=[os.path.join(root, 'templates')])

    # Initialize config with the basic options
    config.init_app(global_conf,
                    app_conf,
                    package='onlinelinguisticdatabase',
                    paths=paths)

    config['routes.map'] = make_map()
    config['pylons.app_globals'] = app_globals.Globals()
    config['pylons.h'] = onlinelinguisticdatabase.lib.helpers

    # Create the Mako TemplateLookup, with the default auto-escaping
    config['pylons.app_globals'].mako_lookup = TemplateLookup(
        directories=paths['templates'],
        error_handler=handle_mako_error,
        module_directory=os.path.join(app_conf['cache_dir'], 'templates'),
        input_encoding='utf-8',
        default_filters=['escape'],
        imports=['from webhelpers.html import escape'])

    # Setup the SQLAlchemy database engine
    #  Modification: check if SQLite is RDBMS and, if so,
    #  give the engine a SQLiteSetup listener which
    #  provides the regexp function missing from the SQLite dbapi
    #  (cf. http://groups.google.com/group/pylons-discuss/browse_thread/thread/8c82699e6b6a400c/5c5237c86202e2b8)
    SQLAlchemyURL = config['sqlalchemy.url']
    rdbms = SQLAlchemyURL.split(':')[0]
    if rdbms == 'sqlite':
        engine = engine_from_config(config,
                                    'sqlalchemy.',
                                    listeners=[SQLiteSetup()])
    else:
        engine = engine_from_config(config, 'sqlalchemy.')
    init_model(engine)

    # Put the application settings into the app_globals object
    #  This has the effect that when the app is restarted the globals like
    #  objectLanguageName, metalanguageName, etc. have the correct values
    #  Do the same for the variable app_globals attributes, e.g., sources list
    #  I HAD TO DISABLE THE FOLLOWING TWO COMMANDS BECAUSE IT WAS CAUSING
    #  setup-app TO CRASH BECAUSE application_settings WAS REQUESTED BEFORE THE
    #  TABLES EXISTED!  FIND ANOTHER WAY TO FIX THIS PROBLEM ...
    #applicationSettingsToAppGlobals(config['pylons.app_globals'])
    #updateSecondaryObjectsInAppGlobals(config['pylons.app_globals'])

    # CONFIGURATION OPTIONS HERE (note: all config options will override
    # any Pylons config options)
    config['pylons.strict_c'] = True
コード例 #2
0
ファイル: environment.py プロジェクト: jrwdunham/old-webapp
def load_environment(global_conf, app_conf):
    """Configure the Pylons environment via the ``pylons.config`` object

    """

    # Pylons paths
    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    paths = dict(root=root,
                 controllers=os.path.join(root, 'controllers'),
                 static_files=os.path.join(root, 'public'),
                 templates=[os.path.join(root, 'templates')])

    # Initialize config with the basic options
    config.init_app(
        global_conf, app_conf, package='onlinelinguisticdatabase', paths=paths
    )

    config['routes.map'] = make_map()
    config['pylons.app_globals'] = app_globals.Globals()
    config['pylons.h'] = onlinelinguisticdatabase.lib.helpers

    # Create the Mako TemplateLookup, with the default auto-escaping
    config['pylons.app_globals'].mako_lookup = TemplateLookup(
        directories=paths['templates'],
        error_handler=handle_mako_error,
        module_directory=os.path.join(app_conf['cache_dir'], 'templates'),
        input_encoding='utf-8', default_filters=['escape'],
        imports=['from webhelpers.html import escape'])

    # Setup the SQLAlchemy database engine
    #  Modification: check if SQLite is RDBMS and, if so,
    #  give the engine a SQLiteSetup listener which
    #  provides the regexp function missing from the SQLite dbapi
    #  (cf. http://groups.google.com/group/pylons-discuss/browse_thread/thread/8c82699e6b6a400c/5c5237c86202e2b8)
    SQLAlchemyURL = config['sqlalchemy.url']
    rdbms = SQLAlchemyURL.split(':')[0]
    if rdbms == 'sqlite':
        engine = engine_from_config(
            config, 'sqlalchemy.', listeners=[SQLiteSetup()])
    else:
        engine = engine_from_config(config, 'sqlalchemy.')
    init_model(engine)

    # Put the application settings into the app_globals object
    #  This has the effect that when the app is restarted the globals like
    #  objectLanguageName, metalanguageName, etc. have the correct values
    #  Do the same for the variable app_globals attributes, e.g., sources list
    #  I HAD TO DISABLE THE FOLLOWING TWO COMMANDS BECAUSE IT WAS CAUSING
    #  setup-app TO CRASH BECAUSE application_settings WAS REQUESTED BEFORE THE
    #  TABLES EXISTED!  FIND ANOTHER WAY TO FIX THIS PROBLEM ...
    #applicationSettingsToAppGlobals(config['pylons.app_globals'])
    #updateSecondaryObjectsInAppGlobals(config['pylons.app_globals'])

    # CONFIGURATION OPTIONS HERE (note: all config options will override
    # any Pylons config options)
    config['pylons.strict_c'] = True
コード例 #3
0
ファイル: environment.py プロジェクト: GracefulLemming/old
def load_environment(global_conf, app_conf):
    """Configure the Pylons environment via the ``pylons.config`` object.
    
    .. note::
    
        This is where the ``regexp`` operator for SQLite is defined and where
        the ``PRAGMA`` command is issued to make SQLite LIKE queries
        case-sensitive.

    """
    config = PylonsConfig()

    # Pylons paths
    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    paths = dict(root=root,
                 controllers=os.path.join(root, 'controllers'),
                 static_files=os.path.join(root, 'public'),
                 templates=[os.path.join(root, 'templates')])

    # Initialize config with the basic options
    config.init_app(global_conf,
                    app_conf,
                    package='onlinelinguisticdatabase',
                    paths=paths)

    config['routes.map'] = make_map(config)
    config['pylons.app_globals'] = app_globals.Globals(config)
    config['pylons.h'] = onlinelinguisticdatabase.lib.helpers

    # Setup cache object as early as possible
    import pylons
    pylons.cache._push_object(config['pylons.app_globals'].cache)

    # Create the Mako TemplateLookup, with the default auto-escaping
    config['pylons.app_globals'].mako_lookup = TemplateLookup(
        directories=paths['templates'],
        error_handler=handle_mako_error,
        module_directory=os.path.join(app_conf['cache_dir'], 'templates'),
        input_encoding='utf-8',
        default_filters=['escape'],
        imports=['from webhelpers.html import escape'])

    engine = engine_from_config(config, 'sqlalchemy.')

    # CONFIGURATION OPTIONS HERE (note: all config options will override
    # any Pylons config options)

    # Patch the SQLAlchemy database engine if SQLite is the RDBMS.  Add a REGEXP
    # function and make LIKE searches case-sensitive.
    RDBMSName = config['sqlalchemy.url'].split(':')[0]
    app_globals.RDBMSName = RDBMSName
    if RDBMSName == 'sqlite':
        # Try to use the event API of SQLA>=0.7; otherwise use a PoolListener a l SQLA ca. 0.5.8
        try:
            from sqlalchemy import event
            from sqlalchemy.engine import Engine

            @event.listens_for(Engine, 'connect')
            def sqlite_patches(dbapi_connection, connection_record):
                # Define a regexp function for SQLite,
                def regexp(expr, item):
                    """This is the Python re-based regexp function that we provide
                    for SQLite.  Note that searches will be case-sensitive by
                    default.  Such behaviour is assured in MySQL by inserting
                    COLLATE expressions into the query (cf. in SQLAQueryBuilder.py).
                    """
                    patt = re.compile(expr)
                    try:
                        return item and patt.search(item) is not None
                    # This will make regexp searches work on int, date & datetime fields.
                    except TypeError:
                        return item and patt.search(str(item)) is not None

                dbapi_connection.create_function('regexp', 2, regexp)
                # Make LIKE searches case-sensitive in SQLite.
                cursor = dbapi_connection.cursor()
                cursor.execute("PRAGMA case_sensitive_like=ON")
                cursor.close()
        except ImportError:
            from sqlalchemy.interfaces import PoolListener

            class SQLiteSetup(PoolListener):
                """A PoolListener used to provide the SQLite dbapi with a regexp function.
                """
                def connect(self, conn, conn_record):
                    conn.create_function('regexp', 2, self.regexp)

                def regexp(self, expr, item):
                    """This is the Python re-based regexp function that we provide for SQLite.
                    Note that searches will be case-sensitive by default, which may not be
                    the default for the MySQL regexp, depending on the collation."""
                    patt = re.compile(expr)
                    try:
                        return item and patt.search(item) is not None
                    # This will make regexp searches work on int, date & datetime fields.
                    # I think this is desirable ...
                    except TypeError:
                        return item and patt.search(str(item)) is not None

            engine = engine_from_config(config,
                                        'sqlalchemy.',
                                        listeners=[SQLiteSetup()])
            # Make LIKE searches case sensitive in SQLite
            engine.execute('PRAGMA case_sensitive_like=ON')

    init_model(engine)

    # start foma worker -- used for long-running tasks like FST compilation
    foma_worker = start_foma_worker()

    return config
コード例 #4
0
ファイル: info.py プロジェクト: jrwdunham/old
    def index(self):
        """Making a request to an OLD with no path in the URL should return
        information about that OLD. This method returns a JSON object with the
        following keys:

        - app = 'Online Linguistic Database'
        - version = the current version of the OLD
        - paths = an array of valid URL paths and HTTP methods that this OLD
          exposes, e.g., "GET /forms"

        .. warning::

            The 'version' key must be valuated with a valid version string,
            i.e., only digits and period characters. When ``python setup.py``
            is run, the 'version' key will be updated with the current version
            as specified in setup.py.

        """

        # Get OLD resources as a dict from resource names to lists of resource
        # attributes.
        resources = {}
        for rname in ['ApplicationSettings', 'Collection', 'CollectionBackup',
            'Corpus', 'CorpusBackup', 'ElicitationMethod', 'File', 'Form',
            'FormBackup', 'FormSearch', 'Keyboard', 'Language',
            'MorphemeLanguageModel', 'MorphemeLanguageModelBackup',
            'MorphologicalParser', 'MorphologicalParserBackup', 'Morphology',
            'MorphologyBackup', 'Orthography', 'Page', 'Phonology',
            'PhonologyBackup']:
            resources[rname] = []
            r_class = getattr(model, rname)
            for k in sorted(r_class.__dict__):
                if k.endswith('_id'): continue
                v = r_class.__dict__[k]
                if type(v) is InstrumentedAttribute:
                    resources[rname].append(k)
            resources[rname].sort()

        # Get the valid paths of this OLD, e.g., GET /forms or PUT
        # /syntacticcategories as a sorted list of strings.
        map = make_map(config)
        p1 = re.compile(':\((.+?)\)')
        p2 = re.compile('\{(.+?)\}')
        myroutes = []
        for r in map.matchlist:
            if ':(format)' not in r.routepath:
                if r.conditions:
                    if type(r.conditions['method']) is list:
                        method = '/'.join(sorted(r.conditions['method']))
                    else:
                        method = r.conditions['method']
                else:
                    method = 'GET'
                if method != 'OPTIONS':
                    path = p2.sub('<\\1>', p1.sub('<\\1>', r.routepath))
                    myroutes.append((path, method))
        meta = {
            'app': 'Online Lingusitic Database',
            'version': '2.0.0',
            'paths': ['%s %s' % (r[1], r[0]) for r in sorted(myroutes)],
            'resources': resources
        }
        return meta
コード例 #5
0
ファイル: environment.py プロジェクト: jrwdunham/old
def load_environment(global_conf, app_conf):
    """Configure the Pylons environment via the ``pylons.config`` object.
    
    .. note::
    
        This is where the ``regexp`` operator for SQLite is defined and where
        the ``PRAGMA`` command is issued to make SQLite LIKE queries
        case-sensitive.

    """
    config = PylonsConfig()

    # Pylons paths
    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    paths = dict(root=root,
                 controllers=os.path.join(root, 'controllers'),
                 static_files=os.path.join(root, 'public'),
                 templates=[os.path.join(root, 'templates')])

    # Initialize config with the basic options
    config.init_app(global_conf, app_conf, package='onlinelinguisticdatabase', paths=paths)

    config['routes.map'] = make_map(config)
    config['pylons.app_globals'] = app_globals.Globals(config)
    config['pylons.h'] = onlinelinguisticdatabase.lib.helpers
    
    # Setup cache object as early as possible
    import pylons
    pylons.cache._push_object(config['pylons.app_globals'].cache)

    # Create the Mako TemplateLookup, with the default auto-escaping
    config['pylons.app_globals'].mako_lookup = TemplateLookup(
        directories=paths['templates'],
        error_handler=handle_mako_error,
        module_directory=os.path.join(app_conf['cache_dir'], 'templates'),
        input_encoding='utf-8', default_filters=['escape'],
        imports=['from webhelpers.html import escape'])

    engine = engine_from_config(config, 'sqlalchemy.')

    # CONFIGURATION OPTIONS HERE (note: all config options will override
    # any Pylons config options)

    # Patch the SQLAlchemy database engine if SQLite is the RDBMS.  Add a REGEXP
    # function and make LIKE searches case-sensitive.
    RDBMSName = config['sqlalchemy.url'].split(':')[0]
    app_globals.RDBMSName = RDBMSName
    if RDBMSName == 'sqlite':
        # Try to use the event API of SQLA>=0.7; otherwise use a PoolListener a l SQLA ca. 0.5.8
        try:
            from sqlalchemy import event
            from sqlalchemy.engine import Engine
            @event.listens_for(Engine, 'connect')
            def sqlite_patches(dbapi_connection, connection_record):
                # Define a regexp function for SQLite,
                def regexp(expr, item):
                    """This is the Python re-based regexp function that we provide
                    for SQLite.  Note that searches will be case-sensitive by
                    default.  Such behaviour is assured in MySQL by inserting
                    COLLATE expressions into the query (cf. in SQLAQueryBuilder.py).
                    """
                    patt = re.compile(expr)
                    try:
                        return item and patt.search(item) is not None
                    # This will make regexp searches work on int, date & datetime fields.
                    except TypeError:
                        return item and patt.search(str(item)) is not None
                dbapi_connection.create_function('regexp', 2, regexp)
                # Make LIKE searches case-sensitive in SQLite.
                cursor = dbapi_connection.cursor()
                cursor.execute("PRAGMA case_sensitive_like=ON")
                cursor.close()
        except ImportError:
            from sqlalchemy.interfaces import PoolListener
            class SQLiteSetup(PoolListener):
                """A PoolListener used to provide the SQLite dbapi with a regexp function.
                """
                def connect(self, conn, conn_record):
                    conn.create_function('regexp', 2, self.regexp)

                def regexp(self, expr, item):
                    """This is the Python re-based regexp function that we provide for SQLite.
                    Note that searches will be case-sensitive by default, which may not be
                    the default for the MySQL regexp, depending on the collation."""
                    patt = re.compile(expr)
                    try:
                        return item and patt.search(item) is not None
                    # This will make regexp searches work on int, date & datetime fields.
                    # I think this is desirable ...
                    except TypeError:
                        return item and patt.search(str(item)) is not None
            engine = engine_from_config(
                config, 'sqlalchemy.', listeners=[SQLiteSetup()])
            # Make LIKE searches case sensitive in SQLite
            engine.execute('PRAGMA case_sensitive_like=ON')

    init_model(engine)

    # start foma worker -- used for long-running tasks like FST compilation
    foma_worker = start_foma_worker()

    return config
コード例 #6
0
ファイル: info.py プロジェクト: dativebase/old
    def index(self):
        """Making a request to an OLD with no path in the URL should return
        information about that OLD. This method returns a JSON object with the
        following keys:

        - app = 'Online Linguistic Database'
        - version = the current version of the OLD
        - paths = an array of valid URL paths and HTTP methods that this OLD
          exposes, e.g., "GET /forms"

        .. warning::

            The 'version' key must be valuated with a valid version string,
            i.e., only digits and period characters. When ``python setup.py``
            is run, the 'version' key will be updated with the current version
            as specified in setup.py.

        """

        # Get OLD resources as a dict from resource names to lists of resource
        # attributes.
        resources = {}
        for rname in [
                'ApplicationSettings', 'Collection', 'CollectionBackup',
                'Corpus', 'CorpusBackup', 'ElicitationMethod', 'File', 'Form',
                'FormBackup', 'FormSearch', 'Keyboard', 'Language',
                'MorphemeLanguageModel', 'MorphemeLanguageModelBackup',
                'MorphologicalParser', 'MorphologicalParserBackup',
                'Morphology', 'MorphologyBackup', 'Orthography', 'Page',
                'Phonology', 'PhonologyBackup'
        ]:
            resources[rname] = []
            r_class = getattr(model, rname)
            for k in sorted(r_class.__dict__):
                if k.endswith('_id'): continue
                v = r_class.__dict__[k]
                if type(v) is InstrumentedAttribute:
                    resources[rname].append(k)
            resources[rname].sort()

        # Get the valid paths of this OLD, e.g., GET /forms or PUT
        # /syntacticcategories as a sorted list of strings.
        map = make_map(config)
        p1 = re.compile(':\((.+?)\)')
        p2 = re.compile('\{(.+?)\}')
        myroutes = []
        for r in map.matchlist:
            if ':(format)' not in r.routepath:
                if r.conditions:
                    if type(r.conditions['method']) is list:
                        method = '/'.join(sorted(r.conditions['method']))
                    else:
                        method = r.conditions['method']
                else:
                    method = 'GET'
                if method != 'OPTIONS':
                    path = p2.sub('<\\1>', p1.sub('<\\1>', r.routepath))
                    myroutes.append((path, method))
        meta = {
            'app': 'Online Lingusitic Database',
            'version': '2.0.0',
            'paths': ['%s %s' % (r[1], r[0]) for r in sorted(myroutes)],
            'resources': resources
        }
        return meta