Skip to content

Commit

Permalink
feat: add ruff, update files
Browse files Browse the repository at this point in the history
No functional changes
  • Loading branch information
gadomski committed Mar 23, 2023
1 parent d002c5e commit e842d47
Show file tree
Hide file tree
Showing 32 changed files with 143 additions and 125 deletions.
12 changes: 0 additions & 12 deletions .flake8

This file was deleted.

2 changes: 0 additions & 2 deletions .isort.cfg

This file was deleted.

19 changes: 7 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
# Please run `pre-commit run --all-files` when adding or changing entries.

repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.258
hooks:
- id: ruff
- repo: https://github.com/psf/black
rev: 22.12.0
rev: 23.1.0
hooks:
- id: black
- repo: https://github.com/codespell-project/codespell
rev: v2.2.2
rev: v2.2.4
hooks:
- id: codespell
args: [--ignore-words=.codespellignore]
Expand All @@ -16,12 +20,8 @@ repos:
rev: v1.1.1
hooks:
- id: doc8
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
- id: flake8
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.991
rev: v1.1.1
hooks:
- id: mypy
# TODO lint test and scripts too
Expand All @@ -35,8 +35,3 @@ repos:
- pyproj
- pystac
- types-requests
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort (python)
2 changes: 1 addition & 1 deletion .readthedocs.environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies:
- black
- codespell
- coverage
- flake8
- ruff
- ipython
- jupyter
- lxml-stubs
Expand Down
6 changes: 6 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ Raster footprint generation
.. automodule:: stactools.core.utils.raster_footprint
:members:

Geometry
~~~~~~~~

.. automodule:: stactools.core.geometry
:members:

Testing
-------

Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

[tool.ruff]
line-length = 88
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
black
codespell
flake8
importlib-metadata
ipython
jupyter
Expand All @@ -13,6 +12,7 @@ pydata-sphinx-theme
pylint
pytest
pytest-cov
ruff
sphinx < 6
sphinx-autobuild
sphinx-click
Expand Down
2 changes: 1 addition & 1 deletion scripts/check_minimum_requirements
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ for package_requirement in package_requirements:
if package_requirement.marker is not None:
continue
min_requirement = min_requirements[package_requirement.name]
for (package_specifier, min_specifier) in zip(
for package_specifier, min_specifier in zip(
package_requirement.specifier, min_requirement.specifier
):
if (
Expand Down
1 change: 0 additions & 1 deletion scripts/format
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
usage
else
pre-commit run black --all-files
pre-commit run isort --all-files
fi
fi
2 changes: 1 addition & 1 deletion scripts/lint
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
else
pre-commit run codespell --all-files
pre-commit run doc8 --all-files
pre-commit run flake8 --all-files
pre-commit run ruff --all-files
pre-commit run mypy --all-files
fi
fi
6 changes: 3 additions & 3 deletions src/stactools/cli/commands/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ def copy_command(
copy_assets: bool,
publish_location: Optional[str],
) -> None:
"""Copy a STAC Catalog or Collection at SRC to the directory
at DST.
"""Copy a STAC Catalog or Collection at SRC to the directory at DST.
Note: Copying a catalog will upgrade it to the latest version of STAC."""
Note: Copying a catalog will upgrade it to the latest version of STAC.
"""
source_catalog = pystac.read_file(make_absolute_href(src))
if not isinstance(source_catalog, pystac.Catalog):
raise click.BadArgumentUsage(f"{src} is not a STAC Catalog")
Expand Down
5 changes: 4 additions & 1 deletion src/stactools/cli/commands/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ def create_summary_command(cli: click.Group) -> click.Command:
"--inplace",
is_flag=True,
default=False,
help="If updating, update the collection in-place, instead of printing it to stdout.",
help=(
"If updating, update the collection in-place, "
"instead of printing it to stdout."
),
)
def summary_command(
href: str, fields: Optional[str], update: bool, inplace: bool
Expand Down
8 changes: 6 additions & 2 deletions src/stactools/cli/commands/update_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def create_update_geometry_command(cli: Group) -> Command:
help=(
"The names of the assets to try for footprint extraction. "
"The first successful footprint will be used. "
"If no assets are provided, all assets will be tried until one is successful."
"If no assets are provided, all assets will be tried until one is "
"successful."
),
)
@click.option(
Expand All @@ -41,7 +42,10 @@ def create_update_geometry_command(cli: Group) -> Command:
"-i",
"--densification-distance",
type=float,
help="The distance interval at which to increase point density within the polygon",
help=(
"The distance interval at which to increase point density within the "
"polygon"
),
)
@click.option(
"-s",
Expand Down
2 changes: 1 addition & 1 deletion src/stactools/cli/commands/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def create_version_command(cli: Group) -> Command:
@cli.command("version", short_help="Display version info.")
def version_command() -> None:
"""Display version info"""
"""Display version info."""
echo(f"stactools version {__version__}")
echo(f"PySTAC version {pystac.__version__}")
echo(f"STAC version {get_stac_version()}")
Expand Down
4 changes: 3 additions & 1 deletion src/stactools/cli/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ def get_create_subcommand_functions(self) -> List[Callable[[Group], Command]]:

def load_plugins(self) -> None:
"""Discover all plugins and register their resources.
Import each Python module within the stactools namespace package
and call the register_plugin function at its root (if it exists).
and call the register_plugin function at its root (if it
exists).
"""
import importlib
import pkgutil
Expand Down
3 changes: 2 additions & 1 deletion src/stactools/core/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ def add_item(
do_move_assets(item_copy, copy=False)
else:
raise ValueError(
f"Cannot add Item {source_item.id} because {target_catalog} does not have a self href."
f"Cannot add Item {source_item.id} because {target_catalog} does "
"not have a self href."
)
2 changes: 1 addition & 1 deletion src/stactools/core/add_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def add_raster_to_item(item: Item) -> Item:
def _read_bands(href: str) -> List[RasterBand]:
bands = []
with rasterio.open(href) as dataset:
for (i, index) in enumerate(dataset.indexes):
for i, index in enumerate(dataset.indexes):
data = dataset.read(index, masked=True)
band = RasterBand.create()
band.nodata = dataset.nodatavals[i]
Expand Down
3 changes: 2 additions & 1 deletion src/stactools/core/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ def move_assets(
abs_asset_href = asset.get_absolute_href()
if abs_asset_href is None:
raise ValueError(
f"Asset {asset.title} HREF is not available for item {item.id}. This operation "
f"Asset {asset.title} HREF is not available for item {item.id}. "
"This operation "
"requires that the Asset HREFs are available."
)

Expand Down
8 changes: 4 additions & 4 deletions src/stactools/core/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ def bounding_box(geom: Dict[str, Any]) -> List[float]:
"""Extracts and returns the bounding box of a GeoJSON geometry.
Args:
geom (dict): A GeoJSON Feature, GeoJSON FeatureCollection, GeoJSON geometry, STAC Item,
or STAC ItemCollection.
geom (dict): A GeoJSON Feature, GeoJSON FeatureCollection, GeoJSON geometry,
STAC Item, or STAC ItemCollection.
Returns:
list: A list of float values containing the bounding box of the GeoJSON geometry in the
format [min X, min Y, max X, max Y]
list: A list of float values containing the bounding box of the GeoJSON
geometry in the format [min X, min Y, max X, max Y]
"""
return list(rasterio.features.bounds(geom))
39 changes: 24 additions & 15 deletions src/stactools/core/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
ReadHrefModifier = Callable[[str], str]
"""Type alias for a function parameter that allows users to manipulate HREFs.
Used for reading, e.g. appending an Azure SAS Token or translating to a signed URL.
Used for reading, e.g. appending an Azure SAS Token or translating to a
signed URL.
"""


Expand Down Expand Up @@ -46,21 +47,25 @@ def read_text(


class FsspecStacIO(StacIO):
"""A subclass of :py:class:`pystac.DefaultStacIO` that uses `fsspec
<https://filesystem-spec.readthedocs.io/en/latest/>`_ for reads and writes.
"""
"""A subclass of :py:class:`pystac.DefaultStacIO` that uses
`fsspec <https://filesystem-spec.readthedocs.io/en/latest/>`_
for reads and writes."""

def read_text(self, source: HREF, *args: Any, **kwargs: Any) -> str:
"""A concrete implementation of :meth:`StacIO.read_text
<pystac.StacIO.read_text>`. Converts the ``source`` argument to a string (if it
is not already) and delegates to :meth:`FsspecStacIO.read_text_from_href` for
opening and reading the file."""
"""A concrete implementation of
:meth:`StacIO.read_text <pystac.StacIO.read_text>`.
Converts the ``source`` argument to
a string (if it is not already) and delegates to
:meth:`FsspecStacIO.read_text_from_href` for opening and reading
the file.
"""
href = str(os.fspath(source))
return self.read_text_from_href(href, **kwargs)

def read_text_from_href(self, href: str, **kwargs: Any) -> str:
"""Reads a file as a utf-8 string using `fsspec
<https://filesystem-spec.readthedocs.io/en/latest/>`_
"""Reads a file as a utf-8 string using
`fsspec <https://filesystem-spec.readthedocs.io/en/latest/>`_.
Args:
href (str): The href to read.
Expand All @@ -79,10 +84,13 @@ def read_text_from_href(self, href: str, **kwargs: Any) -> str:
raise ValueError(f"Unable to decode data loaded from HREF: {href}")

def write_text(self, dest: HREF, txt: str, *args: Any, **kwargs: Any) -> None:
"""A concrete implementation of :meth:`StacIO.write_text
<pystac.StacIO.write_text>`. Converts the ``dest`` argument to a string (if it
is not already) and delegates to :meth:`FsspecStacIO.write_text_from_href` for
opening and reading the file."""
"""A concrete implementation of :meth:`StacIO.write_text <pystac.StacIO.write_text>`.
Converts the ``dest`` argument to a
string (if it is not already) and delegates to
:meth:`FsspecStacIO.write_text_from_href` for opening and
reading the file.
""" # noqa: E501
href = str(os.fspath(dest))
return self.write_text_to_href(href, txt, **kwargs)

Expand All @@ -107,5 +115,6 @@ def write_text_to_href(self, href: str, txt: str, **kwargs: Any) -> None:


def use_fsspec() -> None:
"""Sets the default :py:class:`pystac.StacIO` to :py:class:`FsspecStacIO`."""
"""Sets the default :py:class:`pystac.StacIO` to
:py:class:`FsspecStacIO`."""
StacIO.set_default(FsspecStacIO)
7 changes: 4 additions & 3 deletions src/stactools/core/io/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def find(self, xpath: str) -> Optional["XmlElement"]:
def find_or_throw(
self, xpath: str, get_exception: Callable[[str], Exception]
) -> "XmlElement":
"""Find a child ``XmlElement`` by xpath, or throw an exception if not found.
"""Find a child ``XmlElement`` by xpath, or throw an exception if not
found.
Args:
xpath (str): The xpath to use for search.
Expand Down Expand Up @@ -145,7 +146,7 @@ def text(self) -> Optional[str]:

@lru_cache(maxsize=100)
def get_attr(self, attr: str) -> Optional[str]:
"""Returns the value of a given attribute of this element
"""Returns the value of a given attribute of this element.
Args:
attr (str): The name of the attribute.
Expand All @@ -171,7 +172,7 @@ def from_file(
Args:
href (str): The href to read.
read_href_modifier (Optional[:py:class:`stactools.core.io.ReadHrefModifier`]):
read_href_modifier (Optional[:class:`stactools.core.io.ReadHrefModifier`]):
An optional callable that will be used to modify the href.
Defaults to None.
Expand Down
4 changes: 2 additions & 2 deletions src/stactools/core/projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def reproject_geom(
geom: Dict[str, Any],
precision: Optional[int] = None,
) -> Dict[str, Any]:
"""Reprojects a geometry represented as GeoJSON from the src_crs to the dest
crs.
"""Reprojects a geometry represented as GeoJSON from the src_crs to the
dest crs.
Args:
src_crs (pyproj.crs.CRS, rasterio.crs.CRS, or str): Projection of input data.
Expand Down
3 changes: 1 addition & 2 deletions src/stactools/core/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ def deprecate(from_: str, to: str, version: str) -> None:
@contextmanager
def ignore_not_georeferenced() -> Generator[None, None, None]:
"""Suppress rasterio's warning when opening a dataset that contains no
georeferencing information.
"""
georeferencing information."""
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=NotGeoreferencedWarning)
yield
Loading

0 comments on commit e842d47

Please sign in to comment.