Test Failed
Push — master ( fa6286...b729ab )
by Dominik
01:40
created

_generate_triggers()   A

Complexity

Conditions 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 19
rs 9.4285

1 Method

Rating   Name   Duplication   Size   Complexity  
A element_loaded() 0 12 2
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
""" Trigger code for live OSMAlchemy/Overpass integration. """
29
30
import datetime
31
from sqlalchemy import inspect
32
from sqlalchemy.event import listens_for
33
34
from .online import _get_single_element_by_id
35
from .util import _import_osm_xml
36
37
def _generate_triggers(osmalchemy, maxage=60*60*24):
38
    """ Generates the triggers for online functionality.
39
40
      osmalchemy - reference to the OSMAlchemy instance to be configured
41
      maxage - maximum age of objects before they are updated online, in seconds
42
    """
43
44
    @listens_for(osmalchemy.Element, "load")
45
    def element_loaded(element, context):
0 ignored issues
show
Coding Style introduced by
This function 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...
Unused Code introduced by
The argument context seems to be unused.
Loading history...
Unused Code introduced by
The variable element_loaded seems to be unused.
Loading history...
46
        # Check the age of the element
47
        updated = element.updated
48
        now = datetime.datetime.now()
49
        age = (updated-now).total_seconds()
50
51
        # Should we update?
52
        if age > maxage:
53
            # Retrieve element with id from API
54
            xml = _get_single_element_by_id(osmalchemy._overpass, element.type, element.id)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _overpass was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
55
            _import_osm_xml(osmalchemy, inspect(element).session, xml)
56