Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ruff to replace black and isort #43

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
venv*/
.venv/
.vscode/
.pytest_cache/
__pycache__/
Expand All @@ -15,3 +16,5 @@ coverage.xml
.hatch/
dist/
.*_cache/
tests/data/
work/
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[![Python: 3.8, 3.9, 3.10, 3.11, 3.12](https://img.shields.io/badge/python-3.8%20%7C%203.9%20%7C%203.10%20%7C%203.11%20%7C%203.12-blue)](https://www.python.org)
![GitHub](https://img.shields.io/github/license/agrenott/pyhgtmap)
![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/agrenott/pyhgtmap/pythonpackage.yaml)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Hatch project](https://img.shields.io/badge/%F0%9F%A5%9A-Hatch-4051b5.svg)](https://github.com/pypa/hatch)
[![linting - Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v0.json)](https://github.com/charliermarsh/ruff)
[![types - Mypy](https://img.shields.io/badge/types-Mypy-blue.svg)](https://github.com/python/mypy)
Expand Down
31 changes: 14 additions & 17 deletions pyhgtmap/hgt/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ def parsePolygon(filename):
http://download.geofabrik.de/clipbounds/clipbounds.tgz
and returns it as list of (<lon>, <lat>) tuples.
"""
lines = [
line.strip().lower()
for line in open(filename).read().split("\n")
if line.strip()
]
with open(filename) as polygon_file:
lines = [
line.strip().lower()
for line in polygon_file.read().split("\n")
if line.strip()
]
polygons = []
curPolygon = []
for line in lines:
Expand Down Expand Up @@ -98,7 +99,7 @@ def parseHgtFilename(
minLat = -1 * int(latValue)
else:
raise filenameError(
"something wrong with latitude coding in" " filename {0:s}".format(filename)
"something wrong with latitude coding in filename {0:s}".format(filename)
)
maxLat = minLat + 1
if lonSwitch == "E" and lonValue.isdigit():
Expand All @@ -107,8 +108,7 @@ def parseHgtFilename(
minLon = -1 * int(lonValue)
else:
raise filenameError(
"something wrong with longitude coding in"
" filename {0:s}".format(filename)
"something wrong with longitude coding in filename {0:s}".format(filename)
)
maxLon = minLon + 1
return minLon + corrx, minLat + corry, maxLon + corrx, maxLat + corry
Expand All @@ -118,7 +118,7 @@ def getTransform(o, reverse=False) -> Optional[TransformFunType]:
try:
from osgeo import osr
except ModuleNotFoundError:
raise ImportError(GEOTIFF_ERROR)
raise ImportError(GEOTIFF_ERROR) from None

n = osr.SpatialReference()
n.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
Expand All @@ -134,7 +134,7 @@ def getTransform(o, reverse=False) -> Optional[TransformFunType]:
t = osr.CoordinateTransformation(o, n)

def transform(
points: Iterable[Tuple[float, float]]
points: Iterable[Tuple[float, float]],
) -> Iterable[Tuple[float, float]]:
return [
p[:2]
Expand All @@ -151,7 +151,7 @@ def parseGeotiffBbox(
try:
from osgeo import gdal, osr
except ModuleNotFoundError:
raise ImportError(GEOTIFF_ERROR)
raise ImportError(GEOTIFF_ERROR) from None
try:
g = gdal.Open(filename)
geoTransform = g.GetGeoTransform()
Expand All @@ -168,7 +168,7 @@ def parseGeotiffBbox(
numOfCols = g.RasterXSize
numOfRows = g.RasterYSize
except Exception:
raise hgtError("Can't handle geotiff file {!s}".format(filename))
raise hgtError("Can't handle geotiff file {!s}".format(filename)) from None
lonIncrement = geoTransform[1]
latIncrement = geoTransform[5]
minLon = geoTransform[0] + 0.5 * lonIncrement
Expand Down Expand Up @@ -415,7 +415,7 @@ def initAsGeotiff(
try:
from osgeo import gdal, osr
except ModuleNotFoundError:
raise ImportError(GEOTIFF_ERROR)
raise ImportError(GEOTIFF_ERROR) from None

try:
g = gdal.Open(self.fullFilename)
Expand Down Expand Up @@ -571,10 +571,7 @@ def tooManyNodes(data):
"""
if maxNodes == 0:
return False
if estimNumOfNodes(data) > maxNodes:
return True
else:
return False
return estimNumOfNodes(data) > maxNodes

def getChops(unchoppedData, unchoppedBbox):
"""returns a data chop and the according bbox. This function is
Expand Down
18 changes: 11 additions & 7 deletions pyhgtmap/hgt/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,18 @@ def plotData(self, plotPrefix="heightPlot") -> None:
+ ".xyz"
)
try:
plotFile = open(filename, "w")
with open(filename, "w") as plotFile:
for latIndex, row in enumerate(self.zData):
lat = self.maxLat - latIndex * self.latIncrement
for lonIndex, height in enumerate(row):
lon = self.minLon + lonIndex * self.lonIncrement
plotFile.write(
"{0:.7f} {1:.7f} {2:d}\n".format(lon, lat, height)
)
except Exception:
raise IOError("could not open plot file {0:s} for writing".format(filename))
for latIndex, row in enumerate(self.zData):
lat = self.maxLat - latIndex * self.latIncrement
for lonIndex, height in enumerate(row):
lon = self.minLon + lonIndex * self.lonIncrement
plotFile.write("{0:.7f} {1:.7f} {2:d}\n".format(lon, lat, height))
raise IOError(
"could not open plot file {0:s} for writing".format(filename)
) from None

def _get_contours(
self,
Expand Down
6 changes: 2 additions & 4 deletions pyhgtmap/output/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ def make_osm_filename(
"""generate a filename for the output osm file. This is done using the bbox
of the current hgt file.
"""
if opts.outputPrefix:
prefix = "{0:s}_".format(opts.outputPrefix)
else:
prefix = ""

prefix = "{0:s}_".format(opts.outputPrefix) if opts.outputPrefix else ""
srcNameMiddles = [
os.path.split(os.path.split(srcName)[0])[1].lower()
for srcName in input_files_names
Expand Down
2 changes: 1 addition & 1 deletion pyhgtmap/output/o5mUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(
writeTimestamp=False,
) -> None:
super().__init__()
self.outf = open(filename, "wb")
self.outf = open(filename, "wb") # noqa: SIM115 # TODO: use context handler
self.bbox = bbox
self.elevClassifier = elevClassifier
self.stringTable = StringTable()
Expand Down
7 changes: 2 additions & 5 deletions pyhgtmap/output/osmUtil.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(

self.outF = Gzip.open(fName, "wb", gzip)
else:
self.outF = open(fName, "wb")
self.outF = open(fName, "wb") # noqa: SIM115 # TODO: use context handler
self.osmVersion = "{0:.1f}".format(osmVersion)
if osmVersion > 0.5:
self.versionString = ' version="1"'
Expand Down Expand Up @@ -175,10 +175,7 @@ def writeXML(
<opts> are the options coming from pyhgtmap.
"""
IDCounter = pyhgtmap.output.Id(start_node_id)
if osm_version > 0.5:
versionString = ' version="1"'
else:
versionString = ""
versionString = ' version="1"' if osm_version > 0.5 else ""
ways = []
for elevation, contour_list in tile_contours.contours.items():
if not contour_list:
Expand Down
2 changes: 1 addition & 1 deletion pyhgtmap/sources/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_source(self, nickname: str) -> Source:

def available_sources(self) -> Generator[str, None, None]:
"""Returns available sources' nicknames."""
return (str(key) for key in self._inner_registry.keys())
return (str(key) for key in self._inner_registry)


# Force import of all implementations to register them in the pool
Expand Down
12 changes: 7 additions & 5 deletions pyhgtmap/sources/sonny.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ def download_missing_file(

# Actually download and extract file on the fly
zipped_buffer = files[0].GetContentIOBuffer(remove_bom=True)
with ZipFile(io.BytesIO(cast(bytes, zipped_buffer.read()))) as zip_archive:
# We expect the name to match the archive & area one
with zip_archive.open(f"{area}.hgt") as hgt_file_in:
with open(output_file_name, mode="wb") as hgt_file_out:
hgt_file_out.write(hgt_file_in.read())
# We expect the name to match the archive & area one
with ZipFile(
io.BytesIO(cast(bytes, zipped_buffer.read()))
) as zip_archive, zip_archive.open(f"{area}.hgt") as hgt_file_in, open(
output_file_name, mode="wb"
) as hgt_file_out:
hgt_file_out.write(hgt_file_in.read())
16 changes: 6 additions & 10 deletions pyhgtmap/sources/viewfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import os
from pathlib import Path, PurePath
from typing import Dict, List
from urllib.request import urlopen
from zipfile import ZipFile

from bs4 import BeautifulSoup
from urllib.request import urlopen


from . import Source

Expand Down Expand Up @@ -40,14 +39,11 @@ def inner_areas(coord_tag: str) -> List[str]:
names: List[str] = []
for lon in range(west, east):
for lat in range(south, north):
if lon < 0:
lon_name = "W{0:0>3d}".format(-lon)
else:
lon_name = "E{0:0>3d}".format(lon)
if south < 0:
lat_name = "S{0:0>2d}".format(-lat)
else:
lat_name = "N{0:0>2d}".format(lat)
lon_name = "W{0:0>3d}".format(-lon) if lon < 0 else "E{0:0>3d}".format(lon)
lat_name = (
"S{0:0>2d}".format(-lat) if south < 0 else "N{0:0>2d}".format(lat)
)

name = "".join([lat_name, lon_name])
names.append(name)
return names
Expand Down
21 changes: 6 additions & 15 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ dependencies = [
"pytest-mpl~=0.16.1",
"types-beautifulsoup4>=4",
"coverage~=7.2.1",
"black>=23.1.0",
"mypy>=1.0.1",
"mypy-extensions~=1.0.0",
"ruff>=0.0.259",
Expand All @@ -74,15 +73,16 @@ dependencies = [
[tool.hatch.envs.default.scripts]
all = ["style", "typing", "test_cov"]
fmt = [
"black {args:pyhgtmap tests tools}",
"ruff --fix {args:pyhgtmap tests tools}",
# Sort imports - https://docs.astral.sh/ruff/formatter/#sorting-imports
"ruff check --select I --fix {args:pyhgtmap tests tools}",
"ruff format {args:pyhgtmap tests tools}",
"style",
]
style = [
"ruff {args:pyhgtmap tests tools}",
"black --check --diff {args:pyhgtmap tests tools}",
"ruff check {args:pyhgtmap tests tools}",
"ruff format --check --diff {args:pyhgtmap tests tools}",
]
test = "pytest {args}"
test = "pytest {args:tests}"
test_cov = [
"coverage run -m pytest --mpl",
"coverage combine",
Expand All @@ -99,15 +99,6 @@ python = ["3.8", "3.9", "3.10", "3.11", "3.12"]
# To allow using system's GDAL, which is painful to install
system-packages = true

[tool.black]

[tool.isort]
atomic = true
known_first_party = ["pyhgtmap"]
line_length = 88
profile = "black"
skip_gitignore = true

[tool.mypy]
files = ["tests", "pyhgtmap", "tools"]
ignore_missing_imports = true
Expand Down
44 changes: 20 additions & 24 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@

ignore = [
# black auto-formats lines, trust it
# ruff/black auto-formats lines, trust it
"E501",
]

select = [
# "A",
# "B",
# "C",
# "DTZ",
"E", # "EM",
"F", # "FBT",
# "I",
# "ICN",
# "ISC",
# "N",
# "PLC",
# "PLE",
# "PLR",
# "PLW",
# "Q",
# "RUF",
# "S",
# "SIM",
# "T",
# "TID",
# "UP",
# "W",
# "YTT",
# pycodestyle
"E",
# Pyflakes
"F",
# pyupgrade
#"UP",
# flake8-bugbear
"B",
# flake8-simplify
"SIM",
# isort
"I",
]
target-version = "py39"
# Ignore files not refactored yet
exclude = ["NASASRTMUtil.py", "configUtil.py"]


# Same as black
line-length = 88

[lint.isort]
known-first-party = ["pyhgtmap"]
4 changes: 2 additions & 2 deletions tests/hgt/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def handle_optional_geotiff_support() -> Generator[None, None, None]:
raise
# GDAL not available, ensure the proper errror message is raised
assert (
"GeoTiff optional support not enabled; please install with 'pip install pyhgtmap[geotiff]'"
== ex.msg
ex.msg
== "GeoTiff optional support not enabled; please install with 'pip install pyhgtmap[geotiff]'"
)


Expand Down
27 changes: 15 additions & 12 deletions tests/hgt/test_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ def _test_process_files(nb_jobs: int, options) -> None:
(os.path.join(TEST_DATA_PATH, "N43E006.hgt"), False)
]
# Instrument method without changing its behavior
processor.process_tile_internal = Mock(side_effect=processor.process_tile_internal) # type: ignore
processor.process_tile_internal = Mock( # type: ignore
side_effect=processor.process_tile_internal
)
processor.process_files(files_list)
out_files_names: List[str] = sorted(glob.glob("*.osm.pbf"))
# We may have more files generated (eg. .coverage ones)
Expand Down Expand Up @@ -251,7 +253,9 @@ def _test_process_files_single_output(nb_jobs: int, options) -> None:
# Increase step size to speed up test case
options.contourStepSize = 500
# Instrument method without changing its behavior
processor.process_tile_internal = Mock(side_effect=processor.process_tile_internal) # type: ignore
processor.process_tile_internal = Mock( # type: ignore
side_effect=processor.process_tile_internal
)
processor.process_files(files_list)
out_files_names: List[str] = sorted(glob.glob("*.osm.pbf"))
# We may have more files generated (eg. .coverage ones)
Expand Down Expand Up @@ -343,13 +347,12 @@ def test_process_tile_internal_empty_contour(
tile_mock = MagicMock()
tile_mock.get_contours.return_value = tile_contours
tile_mock.__str__.return_value = "Tile (28.00, 42.50, 29.00, 43.00)" # type: ignore
with tempfile.TemporaryDirectory() as tempdir_name:
with cwd(tempdir_name):
caplog.set_level(logging.INFO, logger="pyhgtmap.hgt.processor")
processor.process_tile_internal("empty.pbf", tile_mock)
# NO file must be generated
assert not os.path.exists("empty.pbf")
assert (
"Tile (28.00, 42.50, 29.00, 43.00) doesn't contain any node, skipping."
in caplog.text
)
with tempfile.TemporaryDirectory() as tempdir_name, cwd(tempdir_name):
caplog.set_level(logging.INFO, logger="pyhgtmap.hgt.processor")
processor.process_tile_internal("empty.pbf", tile_mock)
# NO file must be generated
assert not os.path.exists("empty.pbf")
assert (
"Tile (28.00, 42.50, 29.00, 43.00) doesn't contain any node, skipping."
in caplog.text
)
Loading
Loading