Skip to content

Commit

Permalink
Replace Flake8 with Ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Feb 4, 2024
1 parent 51f2827 commit 189d892
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 37 deletions.
2 changes: 0 additions & 2 deletions .flake8

This file was deleted.

34 changes: 9 additions & 25 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.0
hooks:
- id: pyupgrade
args: [--py38-plus]
- id: ruff
args: [--fix, --exit-non-zero-on-fix]

- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.12.1
rev: 24.1.1
hooks:
- id: black
exclude: ^html/

- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort

- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
hooks:
- id: flake8
additional_dependencies: [flake8-2020, flake8-implicit-str-concat]
exclude: ^html/

- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.10.0
hooks:
- id: python-check-blanket-noqa
- id: python-no-log-warn

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
Expand All @@ -37,16 +19,18 @@ repos:
- id: check-json
- id: check-toml
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
- id: trailing-whitespace

- repo: https://github.com/tox-dev/pyproject-fmt
rev: 1.5.3
rev: 1.7.0
hooks:
- id: pyproject-fmt
additional_dependencies: [tox]

- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.15
rev: v0.16
hooks:
- id: validate-pyproject

Expand Down
2 changes: 2 additions & 0 deletions examples/custom_animations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
on the map.
"""

from __future__ import annotations

import pygame

from osmviz.animation import Simulation, SimViz, TrackingViz
Expand Down
2 changes: 2 additions & 0 deletions examples/multiple_trackvizs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
on a map using TrackingViz objects.
"""

from __future__ import annotations

from osmviz.animation import Simulation, TrackingViz

# The goal is to show 10 trains racing eastward across the US.
Expand Down
2 changes: 2 additions & 0 deletions examples/pil_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
of OSM tiles patched together.
"""

from __future__ import annotations

from PIL import Image

from osmviz.manager import OSMManager, PILImageManager
Expand Down
26 changes: 26 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,31 @@ version.source = "vcs"
[tool.hatch.version.raw-options]
local_scheme = "no-local-version"

[tool.ruff.lint]
select = [
"C4", # flake8-comprehensions
"E", # pycodestyle errors
"EM", # flake8-errmsg
"F", # pyflakes errors
"I", # isort
"ISC", # flake8-implicit-str-concat
"LOG", # flake8-logging
"PGH", # pygrep-hooks
"RUF100", # unused noqa (yesqa)
"UP", # pyupgrade
"W", # pycodestyle warnings
"YTT", # flake8-2020
]
extend-ignore = [
"E203", # Whitespace before ':'
"E221", # Multiple spaces before operator
"E226", # Missing whitespace around arithmetic operator
"E241", # Multiple spaces after ','
]

[tool.ruff.lint.isort]
known-first-party = ["osmviz"]
required-imports = ["from __future__ import annotations"]

[tool.pytest.ini_options]
addopts = "-W error::DeprecationWarning"
3 changes: 2 additions & 1 deletion src/osmviz/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
Keyboard input is accepted to control the speed of the simulation.
"""

# Copyright (c) 2010 Colin Bick, Robert Damphousse

# Permission is hereby granted, free of charge, to any person obtaining a copy
Expand All @@ -48,7 +49,7 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import annotations

import time
from functools import reduce
Expand Down
28 changes: 19 additions & 9 deletions src/osmviz/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from __future__ import annotations

import hashlib
import math
Expand Down Expand Up @@ -88,7 +89,8 @@ def prepare_image(self, width, height):
are those specified by width and height.
"""
if self.image:
raise Exception("Image already prepared.")
msg = "Image already prepared."
raise Exception(msg)
self.image = self.create_image(width, height)

def destroy_image(self):
Expand All @@ -107,12 +109,14 @@ def paste_image_file(self, image_file, xy):
of that image, pastes the image into this object's internal image.
"""
if not self.image:
raise Exception("Image not prepared")
msg = "Image not prepared"
raise Exception(msg)

try:
img = self.load_image_file(image_file)
except Exception as e:
raise Exception(f"Could not load image {image_file}\n{e}")
msg = f"Could not load image {image_file}\n{e}"
raise Exception(msg)

self.paste_image(img, xy)
del img
Expand All @@ -135,7 +139,8 @@ def __init__(self):
try:
import pygame
except ImportError:
raise Exception("Pygame could not be imported!")
msg = "Pygame could not be imported!"
raise Exception(msg)
self.pygame = pygame

def create_image(self, width, height):
Expand Down Expand Up @@ -164,7 +169,8 @@ def __init__(self, mode):
try:
import PIL.Image
except ImportError:
raise Exception("PIL could not be imported!")
msg = "PIL could not be imported!"
raise Exception(msg)
self.PILImage = PIL.Image

def create_image(self, width, height):
Expand Down Expand Up @@ -243,7 +249,8 @@ def __init__(self, **kwargs):
print(f"WARNING: Using {self.cache} to cache map tiles.")
if not os.access(self.cache, os.R_OK | os.W_OK):
print(f" ERROR: Insufficient access to {self.cache}.")
raise Exception("Unable to find/create/use map tile cache directory.")
msg = "Unable to find/create/use map tile cache directory."
raise Exception(msg)

# Get URL template, which supports the following fields:
# * {z}: tile zoom level
Expand Down Expand Up @@ -279,7 +286,8 @@ def __init__(self, **kwargs):
if mgr: # Assume it's a valid manager
self.manager = mgr
else:
raise Exception("OSMManager.__init__ requires argument image_manager")
msg = "OSMManager.__init__ requires argument image_manager"
raise Exception(msg)

def get_tile_coord(self, lon_deg, lat_deg, zoom):
"""
Expand Down Expand Up @@ -327,7 +335,8 @@ def retrieve_tile_image(self, tile_coord, zoom):
try:
urlretrieve(url, filename=filename)
except Exception as e:
raise Exception(f"Unable to retrieve URL: {url}\n{e}")
msg = f"Unable to retrieve URL: {url}\n{e}"
raise Exception(msg)
return filename

def tile_nw_lat_lon(self, tile_coord, zoom):
Expand All @@ -354,7 +363,8 @@ def create_osm_image(self, bounds, zoom):
"""
(min_lat, max_lat, min_lon, max_lon) = bounds
if not self.manager:
raise Exception("No ImageManager was specified, cannot create image.")
msg = "No ImageManager was specified, cannot create image."
raise Exception(msg)

topleft = min_x, min_y = self.get_tile_coord(min_lon, max_lat, zoom)
max_x, max_y = self.get_tile_coord(max_lon, min_lat, zoom)
Expand Down
2 changes: 2 additions & 0 deletions test/functional/test_animation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from osmviz.animation import Simulation, TrackingViz

Inf = float("inf")
Expand Down
2 changes: 2 additions & 0 deletions test/functional/test_manager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import PIL.Image as Image

from osmviz.manager import OSMManager, PILImageManager
Expand Down
3 changes: 3 additions & 0 deletions test/unit/test_image_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""
Unit tests for ImageManager
"""

from __future__ import annotations

import pytest

from osmviz.manager import ImageManager
Expand Down
3 changes: 3 additions & 0 deletions test/unit/test_osm_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""
Unit tests PILImageManager
"""

from __future__ import annotations

import pytest

from osmviz.manager import OSMManager, PILImageManager
Expand Down
3 changes: 3 additions & 0 deletions test/unit/test_pil_image_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""
Unit tests for PILImageManager
"""

from __future__ import annotations

import pytest

from osmviz.manager import PILImageManager
Expand Down

0 comments on commit 189d892

Please sign in to comment.