1
# -*- coding: utf8 - *-
2 2
"""Cihai core functionality."""
3 2
from __future__ import absolute_import, print_function, unicode_literals
4

5 2
from sqlalchemy import MetaData, create_engine
6 2
from sqlalchemy.ext.automap import automap_base
7 2
from sqlalchemy.orm import Session
8

9

10 2
class Database(object):
11
    """
12
    Cihai SQLAlchemy instance
13
    """
14

15 2
    def __init__(self, config):
16 2
        self.engine = create_engine(config['database']['url'])
17

18 2
        self.metadata = MetaData()
19 2
        self.metadata.bind = self.engine
20 2
        self.reflect_db()
21

22 2
        self.session = Session(self.engine)
23

24 2
    def reflect_db(self):
25
        """
26
        No-op to reflect db info.
27

28
        This is available as a method so the database can be reflected
29
        outside initialization (such bootstrapping unihan during CLI usage).
30
        """
31 2
        self.metadata.reflect(views=True, extend_existing=True)
32 2
        self.base = automap_base(metadata=self.metadata)
33 2
        self.base.prepare()
34

35
    #: :class:`sqlalchemy.engine.Engine` instance.
36 2
    engine = None
37

38
    #: :class:`sqlalchemy.schema.MetaData` instance.
39 2
    metadata = None
40

41
    #: :class:`sqlalchemy.orm.session.Session` instance.
42 2
    session = None
43

44
    #: :class:`sqlalchemy.ext.automap.AutomapBase` instance.
45 2
    base = None

Read our documentation on viewing source code .

Loading