pgcontents/query.py
def _generate_notebooks(table, timestamp_column,
                        engine, crypto_factory, min_dt, max_dt, logger):
    """
    See docstrings for `generate_files` and `generate_checkpoints`.

    Parameters
    ----------
    table : SQLAlchemy.Table
        Table to fetch notebooks from, `files` or `remote_checkpoints.
    timestamp_column : SQLAlchemy.Column
        `table`'s column storing timestamps, `created_at` or `last_modified`.
    engine : SQLAlchemy.engine
        Engine encapsulating database connections.
    crypto_factory : function[str -> Any]
        A function from user_id to an object providing the interface required
        by PostgresContentsManager.crypto.  Results of this will be used for
        decryption of the selected notebooks.
    min_dt : datetime.datetime
        Minimum last modified datetime at which a file will be included.
    max_dt : datetime.datetime
        Last modified datetime at and after which a file will be excluded.
    logger : Logger
    """
    where_conds = []
    if min_dt is not None:
        where_conds.append(timestamp_column >= min_dt)
    if max_dt is not None:
        where_conds.append(timestamp_column < max_dt)
    if table is files:
        # Only select files that are notebooks
        where_conds.append(files.c.name.like(u'%.ipynb'))

    # Query for notebooks satisfying the conditions.
    query = select([table]).order_by(timestamp_column)
    for cond in where_conds:
        query = query.where(cond)
    result = engine.execute(query)

    # Decrypt each notebook and yield the result.
    for nb_row in result:
        try:
            # The decrypt function depends on the user
            user_id = nb_row['user_id']
            decrypt_func = crypto_factory(user_id).decrypt

            nb_dict = to_dict_with_content(table.c, nb_row, decrypt_func)
            if table is files:
                # Correct for files schema differing somewhat from checkpoints.
                nb_dict['path'] = nb_dict['parent_name'] + nb_dict['name']
                nb_dict['last_modified'] = nb_dict['created_at']

            # For 'content', we use `reads_base64` directly. If the db content
            # format is changed from base64, the decoding should be changed
            # here as well.
            yield {
                'id': nb_dict['id'],
                'user_id': user_id,
                'path': to_api_path(nb_dict['path']),
                'last_modified': nb_dict['last_modified'],
                'content': reads_base64(nb_dict['content']),
            }
        except CorruptedFile:
            if logger is not None:
                logger.warning(
                    'Corrupted file with id %d in table %s.'
                    % (nb_row['id'], table.name)
                )
def _generate_notebooks(table, timestamp_column,
                        engine, crypto_factory, min_dt, max_dt, logger):
    """
    See docstrings for `generate_files` and `generate_checkpoints`.

    Parameters
    ----------
    table : SQLAlchemy.Table
        Table to fetch notebooks from, `files` or `remote_checkpoints.
    timestamp_column : SQLAlchemy.Column
        `table`'s column storing timestamps, `created_at` or `last_modified`.
    engine : SQLAlchemy.engine
        Engine encapsulating database connections.
    crypto_factory : function[str -> Any]
        A function from user_id to an object providing the interface required
        by PostgresContentsManager.crypto.  Results of this will be used for
        decryption of the selected notebooks.
    min_dt : datetime.datetime
        Minimum last modified datetime at which a file will be included.
    max_dt : datetime.datetime
        Last modified datetime at and after which a file will be excluded.
    logger : Logger
    """
    where_conds = []
    if min_dt is not None:
        where_conds.append(timestamp_column >= min_dt)
    if max_dt is not None:
        where_conds.append(timestamp_column < max_dt)
    if table is files:
        # Only select files that are notebooks
        where_conds.append(files.c.name.like(u'%.ipynb'))

    # Query for notebooks satisfying the conditions.
    query = select([table]).order_by(timestamp_column)
    for cond in where_conds:
        query = query.where(cond)
    result = engine.execute(query)

    # Decrypt each notebook and yield the result.
    for nb_row in result:
        try:
            # The decrypt function depends on the user
            user_id = nb_row['user_id']
            decrypt_func = crypto_factory(user_id).decrypt

            nb_dict = to_dict_with_content(table.c, nb_row, decrypt_func)
            if table is files:
                # Correct for files schema differing somewhat from checkpoints.
                nb_dict['path'] = nb_dict['parent_name'] + nb_dict['name']
                nb_dict['last_modified'] = nb_dict['created_at']

            # For 'content', we use `reads_base64` directly. If the db content
            # format is changed from base64, the decoding should be changed
            # here as well.
            yield {
                'id': nb_dict['id'],
                'user_id': user_id,
                'path': to_api_path(nb_dict['path']),
                'last_modified': nb_dict['last_modified'],
                'content': reads_base64(nb_dict['content']),
            }
        except CorruptedFile:
            if logger is not None:
                logger.warning(
                    'Corrupted file with id %d in table %s.'
                    % (nb_row['id'], table.name)
                )
See docstrings for `generate_files` and `generate_checkpoints`.

    Parameters
    ----------
    table : SQLAlchemy.Table
        Table to fetch notebooks from, `files` or `remote_checkpoints.
    timestamp_column : SQLAlchemy.Column
        `table`'s column storing timestamps, `created_at` or `last_modified`.
    engine : SQLAlchemy.engine
        Engine encapsulating database connections.
    crypto_factory : function[str -> Any]
        A function from user_id to an object providing the interface required
        by PostgresContentsManager.crypto.  Results of this will be used for
        decryption of the selected notebooks.
    min_dt : datetime.datetime
        Minimum last modified datetime at which a file will be included.
    max_dt : datetime.datetime
        Last modified datetime at and after which a file will be excluded.
    logger : Logger