Passed
Branch master (dbe909)
by Dominik
16:02 queued 40s
created

OSMAlchemy.__init__()   B

Complexity

Conditions 5

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.7283

Importance

Changes 14
Bugs 0 Features 0
Metric Value
cc 5
c 14
b 0
f 0
dl 0
loc 37
ccs 9
cts 13
cp 0.6923
crap 5.7283
rs 8.0894
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 1
""" Module that holds the main OSMAlchemy class.
29
30
The classe encapsulates the model and accompanying logic.
31
"""
32
33 1
from .model import _generate_model
34 1
from .online import _generate_overpass_api
35 1
from .util import _import_osm_file
36 1
from .triggers import _generate_triggers
37
38 1
class OSMAlchemy(object):
39
    """ Wrapper class for the OSMAlchemy model and logic
40
41
    This class holds all the SQLAlchemy classes and logic that make up
42
    OSMAlchemy. It is contained in a separate class because it is a
43
    template that can be modified as needed by users, e.g. by using a
44
    different table prefix or a different declarative base.
45
    """
46
47 1
    def __init__(self, base=None, prefix="osm_", overpass=None, maxage=60*60*24):
48
        """ Initialise the table definitions in the wrapper object
49
50
        This function generates the OSM element classes as SQLAlchemy table
51
        declaratives. If called without an argument, it uses a newly created
52
        declarative base.
53
54
        The base argument, if provided, can be either a declarative base or
55
        a Flask-SQLAlchemy object.
56
        """
57
58
        # Check what we got as declarative base
59 1
        if base is None:
60
            # Nothing, so create one
61
            self._base = declarative_base()
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'declarative_base'
Loading history...
62 1
        elif hasattr(base, "Model"):
63
            # Unwrap Flask-SQLAlchemy object if we got one
64
            self._base = base.Model
65
        else:
66 1
            self._base = base
67
68
        # Store prefix
69 1
        self._prefix = prefix
70
71
        # Store API endpoint for Overpass
72 1
        if overpass is not None:
73
            self._overpass = _generate_overpass_api(overpass)
74
        else:
75 1
            self._overpass = None
76
77
        # Generate model and store as instance members
78 1
        self.Node, self.Way, self.Relation, self.Element = _generate_model(self._base,
0 ignored issues
show
Coding Style Naming introduced by
The name Node does not conform to the attribute naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name Element does not conform to the attribute naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name Relation does not conform to the attribute naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name Way does not conform to the attribute naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
79
                                                                           self._prefix)
80
81
        # Add triggers if online functionality is enabled
82 1
        if self._overpass is not None:
83
            _generate_triggers(self, maxage)
84
85 1
    def import_osm_file(self, session, path):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
86
        _import_osm_file(self, session, path)
87