Skip to content

Commit

Permalink
style(linting): fix all old linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ElJocho committed Oct 16, 2023
1 parent f373f8e commit ed74d19
Show file tree
Hide file tree
Showing 23 changed files with 99 additions and 63 deletions.
13 changes: 10 additions & 3 deletions locust/locustfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def validate_uuid(uuid: str):
def monitor_resource_use(wait_time_sec: int = 1):
while True:
os.system(
'docker stats -a --no-stream --format "{{.Name}},{{.MemPerc}}" >> resource_log.csv'
'docker stats -a --no-stream --format "{{.Name}},{{.MemPerc}}" '
">> resource_log.csv"
)
time.sleep(wait_time_sec)

Expand All @@ -40,8 +41,14 @@ def create(self):
payload = {
"format": "A4",
"orientation": "landscape",
"bbox": "[964881.9316858181,6343777.762160674,967129.3396389671,6345658.909256095]",
"bboxWGS84": "[8.667681866041383,49.40450221498196,8.687870675181037,49.41549716938161]",
"bbox": (
"[964881.9316858181,6343777.762160674,"
"967129.3396389671,6345658.909256095]"
),
"bboxWGS84": (
"[8.667681866041383,49.40450221498196,"
"8.687870675181037,49.41549716938161]"
),
"size": '{"width":1716,"height":1436}',
"scale": "8391.039601898965",
}
Expand Down
4 changes: 2 additions & 2 deletions sketch_map_tool/database/client_celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from psycopg2.extensions import connection

from sketch_map_tool.config import get_config_value
from sketch_map_tool.exceptions import FileNotFoundError_
from sketch_map_tool.exceptions import CustomFileNotFoundError

db_conn: connection | None = None

Expand Down Expand Up @@ -58,7 +58,7 @@ def select_file(id_: int) -> bytes:
if raw:
return raw[0]
else:
raise FileNotFoundError_(
raise CustomFileNotFoundError(
"There is no file in the database with the id: " + str(id_)
)

Expand Down
10 changes: 5 additions & 5 deletions sketch_map_tool/database/client_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from sketch_map_tool.config import get_config_value
from sketch_map_tool.definitions import REQUEST_TYPES
from sketch_map_tool.exceptions import FileNotFoundError_, UUIDNotFoundError
from sketch_map_tool.exceptions import CustomFileNotFoundError, UUIDNotFoundError


def open_connection():
Expand Down Expand Up @@ -108,7 +108,7 @@ def select_file(id_: int) -> bytes:
if raw:
return raw[0]
else:
raise FileNotFoundError_(
raise CustomFileNotFoundError(
"There is no file in the database with the id: " + str(id_)
)

Expand All @@ -130,7 +130,7 @@ def select_file_name(id_: int) -> str:
if raw:
return raw[0]
else:
raise FileNotFoundError_(
raise CustomFileNotFoundError(
"There is no file in the database with the id: " + str(id_)
)

Expand All @@ -143,7 +143,7 @@ def select_map_frame(uuid: UUID) -> bytes:
try:
curs.execute(query, [str(uuid)])
except psycopg2.errors.UndefinedTable:
raise FileNotFoundError_(
raise CustomFileNotFoundError(
"In this Sketch Map Tool instance no sketch map "
"has been generated yet. You can only upload sketch"
" maps to the instance on which they have been created."
Expand All @@ -152,7 +152,7 @@ def select_map_frame(uuid: UUID) -> bytes:
if raw:
return raw[0]
else:
raise FileNotFoundError_(
raise CustomFileNotFoundError(
f"There is no map frame in the database with the uuid: {uuid}."
f" You can only upload sketch maps to the "
"instance on which they have been created."
Expand Down
2 changes: 1 addition & 1 deletion sketch_map_tool/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ class UUIDNotFoundError(DatabaseError):
pass


class FileNotFoundError_(DatabaseError):
class CustomFileNotFoundError(DatabaseError):
pass
13 changes: 7 additions & 6 deletions sketch_map_tool/map_generation/generate_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def generate_pdf( # noqa: C901
scale: float,
) -> Tuple[BytesIO, BytesIO]:
"""
Generate a sketch map pdf, i.e. a PDF containing the given map image as well as a code for
georeferencing, a scale, copyright information, and objects to help the feature detection
during the upload processing.
Generate a sketch map pdf, i.e. a PDF containing the given map image
as well as a code for georeferencing, a scale, copyright information,
and objects to help the feature detection during the upload processing.
Also generate template image (PNG) as Pillow object for later upload processing
Expand Down Expand Up @@ -68,7 +68,7 @@ def generate_pdf( # noqa: C901
column_origin_y = 0
column_margin = map_margin * cm

map_image_reportlab = PIL_image_to_image_reader(map_image_input)
map_image_reportlab = pil_image_to_image_reader(map_image_input)

# calculate m per px in map frame
cm_per_px = frame_width * scale / map_width_px
Expand Down Expand Up @@ -167,7 +167,8 @@ def draw_right_column(
qr_size = min(width, height) - margin
qr_code = resize_rlg_by_width(qr_code, qr_size)

# fills up the remaining space, placed between the TOP and the BOTTOM aligned elements
# fills up the remaining space, placed between
# the TOP and the BOTTOM aligned elements
space_filler = Spacer(width, 0) # height will be filled after list creation
# order all elements in column
flowables = [
Expand Down Expand Up @@ -216,7 +217,7 @@ def scale_style(format_: PaperFormat, style_name: str, factor: float) -> Paragra
return normal_style


def PIL_image_to_image_reader(map_image_input):
def pil_image_to_image_reader(map_image_input):
map_image_raw = io.BytesIO()
map_image_input.save(map_image_raw, format="png")
map_image_reportlab = ImageReader(map_image_raw)
Expand Down
5 changes: 4 additions & 1 deletion sketch_map_tool/map_generation/qr_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ def qr_code(


def _encode_data(uuid: str, bbox: Bbox, version_nr: str) -> str:
return f"{version_nr},{uuid},{bbox.lon_min},{bbox.lat_min},{bbox.lon_max},{bbox.lat_max}"
return (
f"{version_nr},{uuid},{bbox.lon_min},"
f"{bbox.lat_min},{bbox.lon_max},{bbox.lat_max}"
)


def _make_qr_code(data: str) -> BytesIO:
Expand Down
13 changes: 8 additions & 5 deletions sketch_map_tool/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
class Bbox:
"""Bounding Box in WGS 84 / Pseudo-Mercator (EPSG:3857)
Be aware that the argument order is relevant to the API and the JavaScript client. Keep the
order in sync with the client.
Be aware that the argument order is relevant to the API and the JavaScript client.
Keep the order in sync with the client.
"""

lon_min: float
Expand Down Expand Up @@ -43,9 +43,12 @@ class PaperFormat:
compass_scale: Scale factor of the compass
globe_scale: Scale factor of the globes
scale_height: Height of the scale [px].
scale_relative_xy: Position of the scale relative to the map frame width or height respectively
scale_background_params: (x relative to scale, y relative to scale, length additional to scale, height)
scale_distance_to_text: Distance from the scale bar to the text describing it [px]
scale_relative_xy: Position of the scale relative to the map frame width or
height respectively
scale_background_params: (x relative to scale, y relative to scale,
length additional to scale, height)
scale_distance_to_text: Distance from the scale bar
to the text describing it [px]
qr_y: Vertical distance from origin to the QR-code [cm]
indent: Indentation of the margin's content relative to the map area [cm]
qr_contents_distances_not_rotated: Tuple of distances [cm]
Expand Down
4 changes: 2 additions & 2 deletions sketch_map_tool/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from sketch_map_tool.database import client_flask as db_client_flask
from sketch_map_tool.definitions import REQUEST_TYPES
from sketch_map_tool.exceptions import (
FileNotFoundError_,
CustomFileNotFoundError,
MapGenerationError,
OQTReportError,
QRCodeError,
Expand Down Expand Up @@ -227,7 +227,7 @@ def download(uuid: str, type_: REQUEST_TYPES) -> Response:


@app.errorhandler(QRCodeError)
@app.errorhandler(FileNotFoundError_)
@app.errorhandler(CustomFileNotFoundError)
@app.errorhandler(UploadLimitsExceededError)
def handle_exception(error):
return render_template("error.html", error_msg=str(error)), 422
Expand Down
18 changes: 10 additions & 8 deletions sketch_map_tool/upload_processing/detect_markings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ def detect_markings(
threshold_bgr: float = 0.5,
) -> NDArray:
"""
Detect markings in the colours blue, green, red, pink, turquoise, white, and yellow.
Note that there must be a sufficient difference between the colour of the markings and the background. White and
yellow markings might therefore not be detected on many sketch maps.
Detect markings in the colours blue, green, red, pink, turquoise, white, and yellow
Note that there must be a sufficient difference between the colour of the markings
and the background. White and yellow markings might not be detected on sketch maps.
:param sketch_map_frame: TODO
:param threshold_bgr: Threshold for the colour detection. 0.5 means 50%, i.e. all BGR values above 50% * 255 will be
considered 255, all values below this threshold will be considered 0 for determining the
colour of the markings.
:param threshold_bgr: Threshold for the colour detection. 0.5 means 50%, i.e. all
BGR values above 50% * 255 will be considered 255,
all values below this threshold will be considered
0 for determining the colour of the markings.
"""
threshold_bgr_abs = threshold_bgr * 255

Expand Down Expand Up @@ -68,8 +69,9 @@ def prepare_img_for_markings(
"""
TODO pydoc
:param threshold_img_diff: Threshold for the marking detection concerning the absolute grayscale difference between
corresponding pixels in 'img_base' and 'img_markings'.
:param threshold_img_diff: Threshold for the marking detection concerning the
absolute grayscale difference between corresponding pixels
in 'img_base' and 'img_markings'.
"""
img_base_height, img_base_width, _ = img_base.shape
img_markings = cv2.resize(
Expand Down
8 changes: 4 additions & 4 deletions sketch_map_tool/upload_processing/georeference.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from sketch_map_tool.models import Bbox


def georeference(img: NDArray, bbox: Bbox, BGR: bool = True) -> BytesIO:
"""Create a GeoTIFF from a image (numpy array) and bounding box coordinates.
def georeference(img: NDArray, bbox: Bbox, bgr: bool = True) -> BytesIO:
"""Create a GeoTIFF from an image (numpy array) and bounding box coordinates.
The image (numpy array) has to be in BGR (3 channels).
The Bounding Box is in WGS 84 / Pseudo-Mercator.
Expand Down Expand Up @@ -41,10 +41,10 @@ def georeference(img: NDArray, bbox: Bbox, BGR: bool = True) -> BytesIO:
# set geo transform
# fmt: off
transform = [
bbox.lon_min, # x-coordinate of the upper-left corner of the upper-left pixel
bbox.lon_min, # x-coordinate of upper-left corner of upper-left pixel
pixel_width,
0, # row rotation (typically zero)
bbox.lat_max, # y-coordinate of the upper-left corner of the upper-left pixel
bbox.lat_max, # y-coordinate of upper-left corner of upper-left pixel
0, # column rotation (typically zero)
-pixel_height, # negative value for a north-up image
]
Expand Down
9 changes: 6 additions & 3 deletions sketch_map_tool/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ def validate_literature_reference(literatur_reference: LiteratureReference):
"""Validate literatur reference to not include empty strings."""
if literatur_reference.citation == "":
raise ValueError(
"Literature reference JSON fields should not contain empty strings as values."
"Literature reference JSON fields "
"should not contain empty strings as values."
)
if literatur_reference.img_src == "":
raise ValueError(
"Literature reference JSON fields should not contain empty strings as values."
"Literature reference JSON fields "
"should not contain empty strings as values."
)
if literatur_reference.url == "":
raise ValueError(
"Literature reference JSON fields should not contain empty strings as values."
"Literature reference JSON fields "
"should not contain empty strings as values."
)
3 changes: 2 additions & 1 deletion sketch_map_tool/wms/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def get_map_image(bbox: Bbox, size: Size) -> Response:
)
except ReadTimeout:
raise MapGenerationError(
"Map area couldn't be processed with the current resources. Please try again once."
"Map area couldn't be processed with the current resources. "
"Please try again once."
)


Expand Down
6 changes: 4 additions & 2 deletions tests/integration/test_celery_tasks.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from io import BytesIO

from uuid import uuid4

from sketch_map_tool import tasks


def test_generate_sketch_map(celery_worker, db_conn_celery, uuid, bbox, format_, size, scale):
def test_generate_sketch_map(
celery_worker, db_conn_celery, uuid, bbox, format_, size, scale
):

task = tasks.generate_sketch_map.apply_async(
args=(str(uuid4()), bbox, format_, "landscape", size, scale)
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_database_client_celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from psycopg2.extensions import connection

from sketch_map_tool.database import client_celery, client_flask
from sketch_map_tool.exceptions import FileNotFoundError_
from sketch_map_tool.exceptions import CustomFileNotFoundError


def test_open_connection():
Expand Down Expand Up @@ -46,6 +46,6 @@ def test_delete_map_frame(db_conn_celery, flask_app, map_frame_buffer):
with flask_app.app_context():
client_flask.select_map_frame(uuid) # Should not raise a FileNotFoundError_
client_celery.delete_map_frame(uuid)
with pytest.raises(FileNotFoundError_):
with pytest.raises(CustomFileNotFoundError):
with flask_app.app_context():
client_flask.select_map_frame(uuid)
6 changes: 3 additions & 3 deletions tests/integration/test_database_client_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from psycopg2.extensions import connection

from sketch_map_tool.database import client_flask
from sketch_map_tool.exceptions import FileNotFoundError_
from sketch_map_tool.exceptions import CustomFileNotFoundError


def test_open_close_connection(flask_app):
Expand Down Expand Up @@ -77,7 +77,7 @@ def test_select_file(file_ids):

def test_select_file_file_not_found(flask_app, files):
with flask_app.app_context():
with pytest.raises(FileNotFoundError_):
with pytest.raises(CustomFileNotFoundError):
client_flask.select_file(1000000)


Expand All @@ -94,5 +94,5 @@ def test_select_map_frame(flask_app, uuids):

def test_select_map_frame_file_not_found(flask_app):
with flask_app.app_context():
with pytest.raises(FileNotFoundError_):
with pytest.raises(CustomFileNotFoundError):
client_flask.select_map_frame(uuid4())
7 changes: 5 additions & 2 deletions tests/integration/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

import pytest

from sketch_map_tool.exceptions import FileNotFoundError_, UploadLimitsExceededError
from sketch_map_tool.exceptions import (
CustomFileNotFoundError,
UploadLimitsExceededError,
)


@mock.patch.dict(os.environ, {"SMT-MAX-NR-SIM-UPLOADS": "2"})
Expand Down Expand Up @@ -41,5 +44,5 @@ def test_allowed_nr_of_uploads(flask_client, sketch_map_markings_buffer_1):
# if we do not have a previous successful run this error will appear;
# uuid of test image not in database, but the exception shows
# that the uploads have been accepted and processed -> relevant code works
except FileNotFoundError_:
except CustomFileNotFoundError:
pass
9 changes: 6 additions & 3 deletions tests/unit/test_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ def test_get_literatur_references():
result[0].img_src
== "/static/assets/images/about/publications/ijgi-10-00130-g002.webp"
)
assert (
result[0].citation
== "Klonner, C., Hartmann, M., Dischl, R., Djami, L., Anderson, L., Raifer, M., Lima-Silva, F., Degrossi, L. C., Zipf, A., de Albuquerque, J. P. (2021): The Sketch Map Tool Facilitates the Assessment of OpenStreetMap Data for Participatory Mapping, 10(3): 130. ISPRS International Journal of Geo-Information. 10:130."
assert result[0].citation == (
"Klonner, C., Hartmann, M., Dischl, R., Djami, L., Anderson, L.,"
" Raifer, M., Lima-Silva, F., Degrossi, L. C., Zipf, A., de Albuquerque,"
" J. P. (2021): The Sketch Map Tool Facilitates the Assessment of "
"OpenStreetMap Data for Participatory Mapping, 10(3): 130."
" ISPRS International Journal of Geo-Information. 10:130."
)
assert result[0].url == "https://doi.org/10.3390/ijgi10030130"
2 changes: 1 addition & 1 deletion tests/unit/test_map_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_generate_pdf(
)
assert isinstance(sketch_map, BytesIO)
assert isinstance(sketch_map_template, BytesIO)
# if you want the maps to be saved for visual inspection, use the parameter --save-maps with pytest
# if you want the maps to be saved for visual inspection -> --save-maps with pytest
save_test_file(
request,
"--save-maps",
Expand Down
Loading

0 comments on commit ed74d19

Please sign in to comment.