__init__.py 2.25 KB
Newer Older
Gerion Entrup's avatar
Gerion Entrup committed
1
2
3
4
5
6
7
8
"""Abstraction of the retrieval and database function needed for adding new
entries to the database.

This module takes a structure of operations and then handles all the database
and web retrieval tasks in a generic way.

It was written mainly to avoid doubling code and provide a clean interface.

9
The structure has to be a dict with an ORM table class as key and an Entity
Gerion Entrup's avatar
Gerion Entrup committed
10
11
12
13
14
15
as value. See the documentation of Entity for furthor information.

To use the module, the first call must be init() with the the structure and
a session class (not an instance). Then several create() calls could be made.
The last action is commit(), to commit all to the database.
"""
Gerion Entrup's avatar
Gerion Entrup committed
16
from retrieval.fetcher import get_table_by_id
17
from retrieval.entity import Entity, Filter
Gerion Entrup's avatar
Gerion Entrup committed
18
19
20
from retrieval.helper import map_quality, fake_id
from retrieval.retrieval import Retrieval

21
__all__ = ['get_table_by_id', 'Entity', 'Filter', 'create', 'init', 'commit',
Gerion Entrup's avatar
Gerion Entrup committed
22
23
24
25
26
27
           'map_quality', 'fake_id']

_retrieval = False


def init(structure, session_fac):
Gerion Entrup's avatar
Gerion Entrup committed
28
29
30
31
32
33
34
35
36
37
    """Take a structure and a sqlalchemy session instance and initializes
    the module.

    This function have to be called before doing anything else with the module.

    Arguments:
    structure   -- dict of operations, see the documention of the module to get
                   details of the format
    session_fac -- a SQLAlchemy Session class (not an instance)
    """
Gerion Entrup's avatar
Gerion Entrup committed
38
39
40
41
42
43
    global _retrieval
    if not _retrieval:
        _retrieval = Retrieval(structure, session_fac)


def create(table, *args):
Gerion Entrup's avatar
Gerion Entrup committed
44
45
46
    """Create a table object (row in the database).

    Arguments:
47
48
    table -- The type of the table entry that should be created. Has to be an
             ORM table class.
Gerion Entrup's avatar
Gerion Entrup committed
49
50
51
52
53
    *args -- All arguments necessary for the specific table.

    This function return a valid table object. If the object is in the cache
    or in the database, this would be returned, otherwise it creates a new one.
    """
Gerion Entrup's avatar
Gerion Entrup committed
54
55
56
57
58
59
60
    global _retrieval
    if not _retrieval:
        raise("You have to call init before using create.")
    return _retrieval.create(table, *args)


def commit():
Gerion Entrup's avatar
Gerion Entrup committed
61
    """Add all objects to the session and commit it to the database."""
Gerion Entrup's avatar
Gerion Entrup committed
62
63
64
65
    global _retrieval
    if not _retrieval:
        raise("You have to call init before using create.")
    return _retrieval.commit()