Passed
Push — master ( 7cc42f...cb04cf )
by Dominik
29:15 queued 12:34
created

_query_compiling()   B

Complexity

Conditions 6

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 6
c 4
b 0
f 0
dl 0
loc 29
ccs 0
cts 14
cp 0
crap 42
rs 7.5384
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
""" Trigger code for live OSMAlchemy/Overpass integration. """
29
30 1
import datetime
0 ignored issues
show
Unused Code introduced by
The import datetime seems to be unused.
Loading history...
31 1
from sqlalchemy import inspect
0 ignored issues
show
Unused Code introduced by
Unused inspect imported from sqlalchemy
Loading history...
32 1
from sqlalchemy.event import listens_for
33 1
from sqlalchemy.orm import Query
34 1
from weakref import WeakSet
0 ignored issues
show
introduced by
standard import "from weakref import WeakSet" comes before "from sqlalchemy import inspect"
Loading history...
35
36 1
from .online import _get_single_element_by_id
0 ignored issues
show
Unused Code introduced by
Unused _get_single_element_by_id imported from online
Loading history...
37 1
from .util import _analyse_clause, _import_osm_xml
0 ignored issues
show
Unused Code introduced by
Unused _import_osm_xml imported from util
Loading history...
38
39 1
def _generate_triggers(osmalchemy, maxage=60*60*24):
0 ignored issues
show
Unused Code introduced by
The argument maxage seems to be unused.
Loading history...
40
    """ Generates the triggers for online functionality.
41
42
      osmalchemy - reference to the OSMAlchemy instance to be configured
43
      maxage - maximum age of objects before they are updated online, in seconds
44
    """
45
46
    _visited_queries = WeakSet()
47
48
    @listens_for(Query, "before_compile")
49
    def _query_compiling(query):
0 ignored issues
show
Unused Code introduced by
The variable _query_compiling seems to be unused.
Loading history...
50
        # Get the session associated with the query:
51
        session = query.session
0 ignored issues
show
Unused Code introduced by
The variable session seems to be unused.
Loading history...
52
53
        # Prevent recursion by skipping already-seen queries
54
        if query in _visited_queries:
55
            return
56
        else:
57
            _visited_queries.add(query)
58
59
        # Check whether this query affects our model
60
        affected_models = set([c["type"] for c in query.column_descriptions])
61
        our_models = set([osmalchemy.node, osmalchemy.way,  osmalchemy.relation,
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
Loading history...
62
                          osmalchemy.element])
63
        if affected_models.isdisjoint(our_models):
64
            # None of our models is affected
65
            return
66
67
        # Check whether this query filters elements
68
        # Online update will only run on a specified set, not all data
69
        if query.whereclause is None:
70
            # No filters
71
            return
72
73
        # Analyse where clause looking for all looked-up fields
74
        tree = {}
75
        for target in our_models.intersection(affected_models):
76
            tree[target.__name__] = _analyse_clause(query.whereclause, target)
77