diff --git a/app/__init__.py b/app/__init__.py index 2cb3fad..16f19e4 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -9,8 +9,10 @@ from app.flask_celery import make_celery bootstrap = Bootstrap() db = SQLAlchemy() +migrate = Migrate() cache = Cache() + def create_app(config_name='development'): # Initialize Flask app = Flask(__name__) @@ -25,14 +27,12 @@ def create_app(config_name='development'): # Initialize other things bootstrap.init_app(app) db.init_app(app) + migrate.init_app(app, db) cache.init_app(app) - - #migrate = Migrate(app, db) - #celery = make_celery(app) - + + # celery = make_celery(app) + from app.main import bp as bp_main app.register_blueprint(bp_main) - #from app import commands - return app diff --git a/app/backend/liveglidernet.py b/app/backend/liveglidernet.py index e3c43ba..3dd7a77 100644 --- a/app/backend/liveglidernet.py +++ b/app/backend/liveglidernet.py @@ -1,9 +1,7 @@ -from datetime import datetime, timedelta, timezone, date - -from app.model import AircraftBeacon, Device, Receiver +from datetime import datetime, timezone +from app.model import AircraftBeacon, ReceiverBeacon from app import db -from app.model import ReceiverBeacon def utc_to_local(utc_dt): @@ -28,7 +26,7 @@ def rec(min_timestamp, min_online_timestamp): lines.append("") lines.append('') for receiver_beacon in last_seen_query: - if receiver_beacon.location == None or receiver_beacon.name.startswith("FNB"): + if receiver_beacon.location is None or receiver_beacon.name.startswith("FNB"): continue lines.append( ''.format( diff --git a/app/collect/database.py b/app/collect/database.py index df1e661..6193a92 100644 --- a/app/collect/database.py +++ b/app/collect/database.py @@ -1,10 +1,8 @@ -from sqlalchemy import distinct -from sqlalchemy.sql import null, and_, func, not_, case -from sqlalchemy.dialects import postgresql +from sqlalchemy.sql import null, and_, func, case from sqlalchemy.dialects.postgresql import insert from flask import current_app -from app.model import Country, DeviceInfo, DeviceInfoOrigin, AircraftBeacon, ReceiverBeacon, Device, Receiver +from app.model import Country, DeviceInfo, DeviceInfoOrigin, Receiver from app.utils import get_ddb, get_flarmnet diff --git a/app/collect/logbook.py b/app/collect/logbook.py index a4774b7..86ae1ad 100644 --- a/app/collect/logbook.py +++ b/app/collect/logbook.py @@ -171,7 +171,7 @@ def update_max_altitudes(session, date, logger=None): logger.info("Update logbook max altitude.") if session is None: - session = app.session + session = current_app.session (start, end) = date_to_timestamps(date) diff --git a/app/commands/__init__.py b/app/commands/__init__.py index 7d6777b..3e442f0 100644 --- a/app/commands/__init__.py +++ b/app/commands/__init__.py @@ -5,6 +5,7 @@ from .gateway import user_cli as gateway_cli from .logbook import user_cli as logbook_cli from .stats import user_cli as stats_cli + def register(app): app.cli.add_command(database_cli) app.cli.add_command(export_cli) diff --git a/app/commands/database.py b/app/commands/database.py index fa054de..96440b8 100644 --- a/app/commands/database.py +++ b/app/commands/database.py @@ -2,11 +2,11 @@ from flask import current_app from flask.cli import AppGroup import click -from datetime import datetime, timedelta +from datetime import datetime from sqlalchemy.sql import func from app.collect.database import update_device_infos, update_country_code -from app.model import * +from app.model import AircraftBeacon, DeviceInfoOrigin from app.utils import get_airports, get_days from app import db diff --git a/app/commands/export.py b/app/commands/export.py index 86fcc30..11f0756 100644 --- a/app/commands/export.py +++ b/app/commands/export.py @@ -6,7 +6,7 @@ import re import csv from aerofiles.igc import Writer -from app.model import * +from app.model import AircraftBeacon, Device from app import db user_cli = AppGroup("export") @@ -64,7 +64,7 @@ def igc(address, date): print("Address {} not valid.".format(address)) return - if not re.match("\d{4}-\d{2}-\d{2}", date): + if not re.match(r"\d{4}-\d{2}-\d{2}", date): print("Date {} not valid.".format(date)) return diff --git a/app/commands/gateway.py b/app/commands/gateway.py index e67bb51..7d942e3 100644 --- a/app/commands/gateway.py +++ b/app/commands/gateway.py @@ -1,6 +1,5 @@ from flask import current_app from flask.cli import AppGroup -import click from ogn.client import AprsClient from app.gateway.bulkimport import ContinuousDbFeeder diff --git a/app/commands/stats.py b/app/commands/stats.py index 91ec5ee..227aa8e 100644 --- a/app/commands/stats.py +++ b/app/commands/stats.py @@ -18,6 +18,7 @@ from app.collect.stats import ( ) from app.collect.ognrange import update_entries as update_receiver_coverages +from app.model import Device from app import db @@ -58,9 +59,6 @@ def create_country(start, end): result = create_country_stats(session=db.session, date=single_date) -from app.model import * - - @user_cli.command("update_devices_name") def update_devices_name(): """Update Devices name.""" diff --git a/app/gateway/bulkimport.py b/app/gateway/bulkimport.py index 872d193..6c05d72 100644 --- a/app/gateway/bulkimport.py +++ b/app/gateway/bulkimport.py @@ -11,7 +11,7 @@ from ogn.parser import parse, ParseError from app.model import AircraftBeacon, ReceiverBeacon, Location from app.utils import open_file -from app.gateway.process_tools import * +from app.gateway.process_tools import create_indices, add_missing_devices, add_missing_receivers, update_aircraft_beacons, update_receiver_beacons, update_receiver_location, transfer_aircraft_beacons, transfer_receiver_beacons, delete_aircraft_beacons, delete_receiver_beacons, update_aircraft_beacons_bigdata, update_receiver_beacons_bigdata, create_tables from app import db @@ -264,26 +264,28 @@ def get_aircraft_beacons_postfixes(): """Get the postfixes from imported aircraft_beacons logs.""" postfixes = db.session.execute( - """ + r""" SELECT DISTINCT(RIGHT(tablename, 8)) FROM pg_catalog.pg_tables WHERE schemaname = 'public' AND tablename LIKE 'aircraft\_beacons\_20______' ORDER BY RIGHT(tablename, 10); - """ + """ ).fetchall() return [postfix for postfix in postfixes] def export_to_path(postfix): - import os, gzip + import os + import gzip - aircraft_beacons_file = os.path.join(path, "aircraft_beacons_{0}.csv.gz".format(postfix)) - with gzip.open(aircraft_beacons_file, "wt", encoding="utf-8") as gzip_file: - self.cur.copy_expert("COPY ({}) TO STDOUT WITH (DELIMITER ',', FORMAT CSV, HEADER, ENCODING 'UTF-8');".format(self.get_merged_aircraft_beacons_subquery()), gzip_file) - receiver_beacons_file = os.path.join(path, "receiver_beacons_{0}.csv.gz".format(postfix)) - with gzip.open(receiver_beacons_file, "wt") as gzip_file: - self.cur.copy_expert("COPY ({}) TO STDOUT WITH (DELIMITER ',', FORMAT CSV, HEADER, ENCODING 'UTF-8');".format(self.get_merged_receiver_beacons_subquery()), gzip_file) + pass # wtf is this? + # aircraft_beacons_file = os.path.join(path, "aircraft_beacons_{0}.csv.gz".format(postfix)) + # with gzip.open(aircraft_beacons_file, "wt", encoding="utf-8") as gzip_file: + # self.cur.copy_expert("COPY ({}) TO STDOUT WITH (DELIMITER ',', FORMAT CSV, HEADER, ENCODING 'UTF-8');".format(self.get_merged_aircraft_beacons_subquery()), gzip_file) + # receiver_beacons_file = os.path.join(path, "receiver_beacons_{0}.csv.gz".format(postfix)) + # with gzip.open(receiver_beacons_file, "wt") as gzip_file: + # self.cur.copy_expert("COPY ({}) TO STDOUT WITH (DELIMITER ',', FORMAT CSV, HEADER, ENCODING 'UTF-8');".format(self.get_merged_receiver_beacons_subquery()), gzip_file) def convert(sourcefile, datestr, saver): @@ -315,7 +317,8 @@ def convert(sourcefile, datestr, saver): if message is None: continue - dictfilt = lambda x, y: dict([(i, x[i]) for i in x if i in set(y)]) + def dictfilt(x, y): + return dict([(i, x[i]) for i in x if i in set(y)]) try: if message["beacon_type"] in AIRCRAFT_BEACON_TYPES: @@ -385,11 +388,11 @@ def file_import(path): results = list() for (root, dirs, files) in os.walk(path): for file in sorted(files): - match = re.match("OGN_log\.txt_([0-9]{4}\-[0-9]{2}\-[0-9]{2})\.gz$", file) + match = re.match(r"OGN_log\.txt_([0-9]{4}\-[0-9]{2}\-[0-9]{2})\.gz$", file) if match: results.append({"filepath": os.path.join(root, file), "datestr": match.group(1)}) - with LogfileDbSaver() as saver: + with LogfileDbSaver() as saver: # noqa: F821 already_imported = saver.get_datestrs() results = list(filter(lambda x: x["datestr"] not in already_imported, results)) diff --git a/app/main/__init__.py b/app/main/__init__.py index 4206cc6..7e35f16 100644 --- a/app/main/__init__.py +++ b/app/main/__init__.py @@ -2,4 +2,4 @@ from flask import Blueprint bp = Blueprint("main", __name__) -import app.main.routes \ No newline at end of file +import app.main.routes diff --git a/app/main/live_routes.py b/app/main/live_routes.py index 9a78393..9c6a621 100644 --- a/app/main/live_routes.py +++ b/app/main/live_routes.py @@ -1,12 +1,10 @@ -from flask import request, render_template, make_response, send_file, current_app +from flask import request, render_template, current_app from flask_cors import cross_origin from app.backend.liveglidernet import rec, lxml -from app import db -from app import cache - from app.main import bp + @bp.route("/live.html") @cross_origin() def live(): diff --git a/app/main/routes.py b/app/main/routes.py index 867ced8..9c1af6f 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -4,7 +4,7 @@ from flask import request, render_template, send_file from app import db from app import cache -from app.model import * +from app.model import Airport, Country, Device, Logbook, Receiver, ReceiverStats from app.main import bp diff --git a/app/model/aircraft_beacon.py b/app/model/aircraft_beacon.py index 313100f..4fd0f26 100644 --- a/app/model/aircraft_beacon.py +++ b/app/model/aircraft_beacon.py @@ -80,8 +80,8 @@ class AircraftBeacon(Beacon): "timestamp", "track", "ground_speed", - #'raw_message', - #'reference_timestamp', + # 'raw_message', + # 'reference_timestamp', "address_type", "aircraft_type", "stealth", diff --git a/app/model/logbook.py b/app/model/logbook.py index ce1376d..3c61772 100644 --- a/app/model/logbook.py +++ b/app/model/logbook.py @@ -1,5 +1,5 @@ from sqlalchemy.ext.hybrid import hybrid_property - +from sqlalchemy.sql import null, case from app import db diff --git a/ogn_python.py b/ogn_python.py index 901f0b8..2ae3c03 100644 --- a/ogn_python.py +++ b/ogn_python.py @@ -1,13 +1,10 @@ import os -from flask_migrate import Migrate -from app import create_app, db -from app.commands import register +from app import create_app, db, commands app = create_app(os.getenv('FLASK_CONFIG') or 'default') -migrate = Migrate(app, db) -register(app) +commands.register(app) @app.shell_context_processor def make_shell_context(): - return dict(app=app, db=db) \ No newline at end of file + return dict(app=app, db=db) diff --git a/setup.cfg b/setup.cfg index 24c26dc..8d174ac 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,2 +1,2 @@ [flake8] -ignore = E501, E126 +ignore = F401, F841, E402, E501, E126