Skip to content

Commit

Permalink
Merge 85b4bb4 into 5599444
Browse files Browse the repository at this point in the history
  • Loading branch information
wuan committed Oct 26, 2018
2 parents 5599444 + 85b4bb4 commit 409cc5c
Show file tree
Hide file tree
Showing 25 changed files with 129 additions and 123 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
@@ -1,9 +1,10 @@
language: python

python:
- 2.7
- 3.3
- 3.4
- 3.5
- 3.6

install:
# install deps
Expand Down
2 changes: 1 addition & 1 deletion blitzortung/__init__.py
Expand Up @@ -53,7 +53,7 @@ class Error(Exception):
from . import db

INJECTOR = injector.Injector(
[config.ConfigModule(), db.DbModule()])
[config.ConfigModule(), db.DbModule()])

__all__ = [

Expand Down
4 changes: 2 additions & 2 deletions blitzortung/builder/raw_signal.py
Expand Up @@ -94,8 +94,8 @@ class RawWaveformEvent(Event):
class for building of raw signal objects
"""

@inject(channel_builder=ChannelWaveform)
def __init__(self, channel_builder):
@inject
def __init__(self, channel_builder: ChannelWaveform):
super(RawWaveformEvent, self).__init__()
self.altitude = 0
self.channels = []
Expand Down
1 change: 1 addition & 0 deletions blitzortung/cache.py
Expand Up @@ -19,6 +19,7 @@
"""

from __future__ import division

import time


Expand Down
10 changes: 5 additions & 5 deletions blitzortung/config.py
Expand Up @@ -25,13 +25,13 @@
except ImportError:
import ConfigParser as configparser

from injector import Module, singleton, provides, inject
from injector import Module, singleton, inject, provider


@singleton
class Config(object):
@inject(config_parser=configparser.ConfigParser)
def __init__(self, config_parser):
@inject
def __init__(self, config_parser: configparser.ConfigParser):
self.config_parser = config_parser

def get_username(self):
Expand Down Expand Up @@ -69,8 +69,8 @@ def config():

class ConfigModule(Module):
@singleton
@provides(configparser.ConfigParser)
def provide_config_parser(self):
@provider
def provide_config_parser(self) -> configparser.ConfigParser:
config_parser = configparser.ConfigParser()
config_parser.read('/etc/blitzortung.conf')
return config_parser
33 changes: 19 additions & 14 deletions blitzortung/data.py
Expand Up @@ -21,31 +21,31 @@
from __future__ import unicode_literals, generators

import datetime
import math

import pytz
import six

from . import types
import math
from blitzortung.geom import GridElement
from . import typedef


class Timestamp(types.EqualityAndHash):
class Timestamp(typedef.EqualityAndHash):
timestamp_string_minimal_fractional_seconds_length = 20
timestamp_string_microseconds_length = 26

__slots__ = ['datetime', 'nanosecond']

def __init__(self, date_time=datetime.datetime.utcnow().replace(tzinfo=pytz.UTC), nanosecond=0):
if type(date_time) == str or type(date_time) == unicode:
def __init__(self, date_time=datetime.datetime.utcnow().replace(tzinfo=pytz.UTC), nanosecond: int = 0):
if type(date_time) == str:
date_time, date_time_nanosecond = Timestamp.from_timestamp(date_time)
nanosecond += date_time_nanosecond
elif type(date_time) == int:
date_time, date_time_nanosecond = Timestamp.from_nanoseconds(date_time)
nanosecond += date_time_nanosecond

if nanosecond < 0 or nanosecond > 999:
microdelta = nanosecond / 1000
microdelta = nanosecond // 1000
date_time += datetime.timedelta(microseconds=microdelta)
nanosecond -= microdelta * 1000

Expand Down Expand Up @@ -130,25 +130,29 @@ def __lt__(self, other):
if type(other) == datetime.datetime:
return self.datetime < other
else:
return self.datetime < other.datetime or (self.datetime == other.datetime and self.nanosecond < other.nanosecond)
return self.datetime < other.datetime or (
self.datetime == other.datetime and self.nanosecond < other.nanosecond)

def __le__(self, other):
if type(other) == datetime.datetime:
return self.datetime <= other
else:
return self.datetime < other.datetime or (self.datetime == other.datetime and self.nanosecond <= other.nanosecond)
return self.datetime < other.datetime or (
self.datetime == other.datetime and self.nanosecond <= other.nanosecond)

def __gt__(self, other):
if type(other) == datetime.datetime:
return self.datetime > other
else:
return self.datetime > other.datetime or (self.datetime == other.datetime and self.nanosecond > other.nanosecond)
return self.datetime > other.datetime or (
self.datetime == other.datetime and self.nanosecond > other.nanosecond)

def __ge__(self, other):
if type(other) == datetime.datetime:
return self.datetime >= other
else:
return self.datetime > other.datetime or (self.datetime == other.datetime and self.nanosecond >= other.nanosecond)
return self.datetime > other.datetime or (
self.datetime == other.datetime and self.nanosecond >= other.nanosecond)

def __add__(self, other):
if type(other) == Timedelta:
Expand Down Expand Up @@ -185,10 +189,10 @@ def __repr__(self):
NaT = Timestamp(None)


class Timedelta(types.EqualityAndHash):
class Timedelta(typedef.EqualityAndHash):
def __init__(self, timedelta=datetime.timedelta(), nanodelta=0):
if nanodelta < 0 or nanodelta > 999:
microdelta = nanodelta / 1000
microdelta = nanodelta // 1000
timedelta += datetime.timedelta(microseconds=microdelta)
nanodelta -= microdelta * 1000
self.timedelta = timedelta
Expand All @@ -206,7 +210,7 @@ def __repr__(self):
return "Timedelta({}, {})".format(self.timedelta, self.nanodelta)


class Event(types.Point):
class Event(typedef.Point):
time_format = '%Y-%m-%d %H:%M:%S'
time_format_fractional_seconds = time_format + '.%f'

Expand Down Expand Up @@ -298,7 +302,8 @@ def __str__(self):
offline_since = self.timestamp
status_char = "*" if offline_since is None else "-"
try:
status_text = "" if offline_since is None else " offline since " + offline_since.strftime("%Y-%m-%d %H:%M %Z")
status_text = "" if offline_since is None else " offline since " + offline_since.strftime(
"%Y-%m-%d %H:%M %Z")
except ValueError:
status_text = 'n/a'
return u"%s%3d/%3d '%s' '%s' (%.4f, %.4f)%s" % (
Expand Down
9 changes: 3 additions & 6 deletions blitzortung/dataimport/base.py
Expand Up @@ -24,10 +24,7 @@
import logging
import datetime

try:
from html.parser import HTMLParser
except ImportError:
from HTMLParser import HTMLParser
from html.parser import HTMLParser

from injector import inject

Expand Down Expand Up @@ -60,8 +57,8 @@ class HttpFileTransport(FileTransport):
logger = logging.getLogger(__name__)
html_parser = HTMLParser()

@inject(configuration=config.Config)
def __init__(self, configuration, session=None):
@inject
def __init__(self, configuration: config.Config, session=None):
self.config = configuration
self.session = session if session else Session()

Expand Down
5 changes: 2 additions & 3 deletions blitzortung/dataimport/raw_signal.py
Expand Up @@ -31,9 +31,8 @@
class RawSignalsBlitzortungDataProvider(object):
logger = logging.getLogger(__name__)

@inject(data_transport=HttpFileTransport, data_url=BlitzortungDataPath,
url_path_generator=BlitzortungDataPathGenerator, waveform_builder=builder.RawWaveformEvent)
def __init__(self, data_transport, data_url, url_path_generator, waveform_builder):
@inject
def __init__(self, data_transport: HttpFileTransport, data_url: BlitzortungDataPath, url_path_generator: BlitzortungDataPathGenerator, waveform_builder: builder.RawWaveformEvent):
self.data_transport = data_transport
self.data_url = data_url
self.url_path_generator = url_path_generator
Expand Down
5 changes: 2 additions & 3 deletions blitzortung/dataimport/station.py
Expand Up @@ -31,9 +31,8 @@
class StationsBlitzortungDataProvider(object):
logger = logging.getLogger(__name__)

@inject(data_transport=HttpFileTransport, data_url=BlitzortungDataPath,
station_builder=builder.Station)
def __init__(self, data_transport, data_url, station_builder):
@inject
def __init__(self, data_transport:HttpFileTransport, data_url:BlitzortungDataPath, station_builder:builder.Station):
self.data_transport = data_transport
self.data_url = data_url
self.station_builder = station_builder
Expand Down
9 changes: 5 additions & 4 deletions blitzortung/dataimport/strike.py
Expand Up @@ -29,13 +29,14 @@
from .base import HttpFileTransport, BlitzortungDataPath, BlitzortungDataPathGenerator
from .. import builder


@singleton
class StrikesBlitzortungDataProvider(object):
logger = logging.getLogger(__name__)

@inject(data_transport=HttpFileTransport, data_url=BlitzortungDataPath,
url_path_generator=BlitzortungDataPathGenerator, strike_builder=builder.Strike)
def __init__(self, data_transport, data_url, url_path_generator, strike_builder):
@inject
def __init__(self, data_transport: HttpFileTransport, data_url: BlitzortungDataPath,
url_path_generator: BlitzortungDataPathGenerator, strike_builder: builder.Strike):
self.data_transport = data_transport
self.data_url = data_url
self.url_path_generator = url_path_generator
Expand All @@ -54,7 +55,7 @@ def get_strikes_since(self, latest_strike=None, region=1):
try:
strike = self.strike_builder.from_line(strike_line).build()
except builder.BuilderError as e:
self.logger.warn("%s: %s (%s)" % (e.__class__, e.args, strike_line))
self.logger.warning("%s: %s (%s)" % (e.__class__, e.args, strike_line))
continue
except Exception as e:
self.logger.error("%s: %s (%s)" % (e.__class__, e.args, strike_line))
Expand Down
8 changes: 4 additions & 4 deletions blitzortung/db/__init__.py
Expand Up @@ -18,7 +18,7 @@
"""

from injector import Module, provides, singleton, inject
from injector import Module, provider, singleton, inject
import atexit


Expand Down Expand Up @@ -51,9 +51,9 @@ def cleanup(connection_pool):
connection_pool.closeall()

@singleton
@provides(psycopg2.pool.ThreadedConnectionPool)
@inject(config=config.Config)
def provide_psycopg2_connection_pool(self, config):
@provider
@inject
def provide_psycopg2_connection_pool(self, config: config.Config) -> psycopg2.pool.ThreadedConnectionPool:
connection_pool = psycopg2.pool.ThreadedConnectionPool(4, 50, config.get_db_connection_string())
atexit.register(self.cleanup, connection_pool)
return connection_pool
Expand Down
14 changes: 7 additions & 7 deletions blitzortung/db/mapper.py
Expand Up @@ -23,7 +23,7 @@
import pytz
import shapely.wkb

import blitzortung.builder
from .. import builder


class ObjectMapper(object):
Expand All @@ -42,8 +42,8 @@ def convert_to_timezone(timestamp, target_timezone=None):


class Strike(ObjectMapper):
@inject(strike_builder=blitzortung.builder.Strike)
def __init__(self, strike_builder):
@inject
def __init__(self, strike_builder: builder.Strike):
self.strike_builder = strike_builder

def create_object(self, result, **kwargs):
Expand All @@ -64,8 +64,8 @@ def create_object(self, result, **kwargs):


class Station(ObjectMapper):
@inject(station_builder=blitzortung.builder.Station)
def __init__(self, station_builder):
@inject
def __init__(self, station_builder: builder.Station):
self.station_builder = station_builder

def create_object(self, result, **kwargs):
Expand All @@ -85,8 +85,8 @@ def create_object(self, result, **kwargs):


class StationOffline(ObjectMapper):
@inject(station_offline_builder=blitzortung.builder.StationOffline)
def __init__(self, station_offline_builder):
@inject
def __init__(self, station_offline_builder: builder.StationOffline):
self.station_offline_builder = station_offline_builder

def create_object(self, result, **kwargs):
Expand Down
34 changes: 16 additions & 18 deletions blitzortung/db/table.py
Expand Up @@ -232,9 +232,8 @@ class Strike(Base):

TABLE_NAME = 'strikes'

@inject(db_connection_pool=psycopg2.pool.ThreadedConnectionPool, query_builder_=query_builder.Strike,
strike_mapper=mapper.Strike)
def __init__(self, db_connection_pool, query_builder_, strike_mapper):
@inject
def __init__(self, db_connection_pool: psycopg2.pool.ThreadedConnectionPool, query_builder_:query_builder.Strike, strike_mapper: mapper.Strike):
super(Strike, self).__init__(db_connection_pool)

self.query_builder = query_builder_
Expand Down Expand Up @@ -338,8 +337,8 @@ class Station(Base):
ALTER SEQUENCE stations_id_seq RESTART 1;
"""

@inject(db_connection_pool=psycopg2.pool.ThreadedConnectionPool, station_mapper=mapper.Station)
def __init__(self, db_connection_pool, station_mapper):
@inject
def __init__(self, db_connection_pool: psycopg2.pool.ThreadedConnectionPool, station_mapper: mapper.Station):
super(Station, self).__init__(db_connection_pool)

self.table_name = 'stations'
Expand Down Expand Up @@ -394,9 +393,8 @@ class StationOffline(Base):
ALTER SEQUENCE stations_offline_id_seq RESTART 1;
"""

@inject(db_connection_pool=psycopg2.pool.ThreadedConnectionPool,
station_offline_mapper=mapper.StationOffline)
def __init__(self, db_connection_pool, station_offline_mapper):
@inject
def __init__(self, db_connection_pool: psycopg2.pool.ThreadedConnectionPool, station_offline_mapper: mapper.StationOffline):
super(StationOffline, self).__init__(db_connection_pool)

self.table_name = 'stations_offline'
Expand Down Expand Up @@ -441,8 +439,8 @@ class Location(Base):
"""

@inject(db_connection_pool=psycopg2.pool.ThreadedConnectionPool)
def __init__(self, db_connection_pool):
@inject
def __init__(self, db_connection_pool: psycopg2.pool.ThreadedConnectionPool):
super(Location, self).__init__(db_connection_pool)
self.schema_name = 'geo'
self.table_name = 'geonames'
Expand Down Expand Up @@ -588,8 +586,8 @@ class ServiceLogTotal(ServiceLogBase):
CREATE INDEX servicelog_total_timestamp ON servicelog_total USING btree("timestamp");
"""

@inject(db_connection_pool=psycopg2.pool.ThreadedConnectionPool)
def __init__(self, db_connection_pool):
@inject
def __init__(self, db_connection_pool: psycopg2.pool.ThreadedConnectionPool):
super(ServiceLogTotal, self).__init__(db_connection_pool)

self.table_name = 'servicelog_total'
Expand All @@ -614,8 +612,8 @@ class ServiceLogCountry(ServiceLogBase):
CREATE INDEX servicelog_country_timestamp ON servicelog_country USING btree("timestamp");
"""

@inject(db_connection_pool=psycopg2.pool.ThreadedConnectionPool)
def __init__(self, db_connection_pool):
@inject
def __init__(self, db_connection_pool: psycopg2.pool.ThreadedConnectionPool):
super(ServiceLogCountry, self).__init__(db_connection_pool)

self.table_name = 'servicelog_country'
Expand All @@ -641,8 +639,8 @@ class ServiceLogVersion(ServiceLogBase):
CREATE INDEX servicelog_version_timestamp ON servicelog_version USING btree("timestamp");
"""

@inject(db_connection_pool=psycopg2.pool.ThreadedConnectionPool)
def __init__(self, db_connection_pool):
@inject
def __init__(self, db_connection_pool: psycopg2.pool.ThreadedConnectionPool):
super(ServiceLogVersion, self).__init__(db_connection_pool)

self.table_name = 'servicelog_version'
Expand All @@ -668,8 +666,8 @@ class ServiceLogParameters(ServiceLogBase):
CREATE INDEX servicelog_parameters_timestamp ON servicelog_parameters USING btree("timestamp");
"""

@inject(db_connection_pool=psycopg2.pool.ThreadedConnectionPool)
def __init__(self, db_connection_pool):
@inject
def __init__(self, db_connection_pool: psycopg2.pool.ThreadedConnectionPool):
super(ServiceLogParameters, self).__init__(db_connection_pool)

self.table_name = 'servicelog_parameters'
Expand Down

0 comments on commit 409cc5c

Please sign in to comment.