Test Failed
Push — master ( bdf1d5...583804 )
by Dominik
02:32
created

OSMAlchemy.__init__()   B

Complexity

Conditions 3

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 12
Bugs 0 Features 0
Metric Value
c 12
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 3
1
# ~*~ coding: utf-8 ~*~
2
#-
3
# OSMAlchemy - OpenStreetMap to SQLAlchemy bridge
4
# Copyright (c) 2016 Dominik George <[email protected]>
5
#
6
# Permission is hereby granted, free of charge, to any person obtaining a copy
7
# of this software and associated documentation files (the "Software"), to deal
8
# in the Software without restriction, including without limitation the rights
9
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
# copies of the Software, and to permit persons to whom the Software is
11
# furnished to do so, subject to the following conditions:
12
#
13
# The above copyright notice and this permission notice shall be included in all
14
# copies or substantial portions of the Software.
15
#
16
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
# SOFTWARE.
23
#
24
# Alternatively, you are free to use OSMAlchemy under Simplified BSD, The
25
# MirOS Licence, GPL-2+, LGPL-2.1+, AGPL-3+ or the same terms as Python
26
# itself.
27
28
""" Module that holds the main OSMAlchemy class.
29
30
The classe encapsulates the model and accompanying logic.
31
"""
32
33
from .model import _generate_model
34
35
class OSMAlchemy(object):
36
    """ Wrapper class for the OSMAlchemy model and logic
37
38
    This class holds all the SQLAlchemy classes and logic that make up
39
    OSMAlchemy. It is contained in a separate class because it is a
40
    template that can be modified as needed by users, e.g. by using a
41
    different table prefix or a different declarative base.
42
    """
43
44
    def __init__(self, base=None, prefix="osm_"):
45
        """ Initialise the table definitions in the wrapper object
46
47
        This function generates the OSM element classes as SQLAlchemy table
48
        declaratives. If called without an argument, it uses a newly created
49
        declarative base.
50
51
        The base argument, if provided, can be either a declarative base or
52
        a Flask-SQLAlchemy object.
53
        """
54
55
        # Check what we got as declarative base
56
        if base is None:
57
            # Nothing, so create one
58
            self._base = declarative_base()
59
        elif hasattr(base, "Model"):
60
            # Unwrap Flask-SQLAlchemy object if we got one
61
            self._base = base.Model
62
        else:
63
            self._base = base
64
65
        # Store prefix
66
        self._prefix = prefix
67
68
        # Generate model and store as instance members
69
        self.Node, self.Way, self.Relation = _generate_model(self._base, self._prefix)
70