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

ENH: enable more ruff rules + make appropriate changes #415

Merged
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
files: 'pyogrio\/'
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.1.13"
rev: "v0.5.2"
hooks:
- id: ruff-format
- id: ruff
- id: ruff
2 changes: 1 addition & 1 deletion environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ dependencies:
- pre-commit
- pytest
- versioneer
- ruff
- ruff==0.5.2
23 changes: 12 additions & 11 deletions pyogrio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
"""Vectorized vector I/O using OGR."""

try:
# we try importing shapely, to ensure it is imported (and it can load its
# own GEOS copy) before we load GDAL and its linked GEOS
import shapely # noqa
import shapely.geos # noqa
import shapely
import shapely.geos # noqa: F401
except Exception:
pass

from pyogrio._version import get_versions
from pyogrio.core import (
list_drivers,
__gdal_geos_version__,
__gdal_version__,
__gdal_version_string__,
detect_write_driver,
get_gdal_config_option,
get_gdal_data_path,
list_drivers,
list_layers,
read_bounds,
read_info,
set_gdal_config_options,
get_gdal_config_option,
get_gdal_data_path,
__gdal_version__,
__gdal_version_string__,
__gdal_geos_version__,
)
from pyogrio.raw import read_arrow, open_arrow, write_arrow
from pyogrio.geopandas import read_dataframe, write_dataframe
from pyogrio._version import get_versions

from pyogrio.raw import open_arrow, read_arrow, write_arrow

__version__ = get_versions()["version"]
del get_versions
Expand Down
2 changes: 1 addition & 1 deletion pyogrio/_compat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from packaging.version import Version

from pyogrio.core import __gdal_version__, __gdal_geos_version__
from pyogrio.core import __gdal_geos_version__, __gdal_version__

# detect optional dependencies
try:
Expand Down
7 changes: 3 additions & 4 deletions pyogrio/_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
# adapted from Fiona: https://github.com/Toblerity/Fiona/pull/875


from contextlib import contextmanager
import logging
import os
from pathlib import Path
import platform
import sys

from contextlib import contextmanager
from pathlib import Path

log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())
Expand All @@ -32,7 +31,7 @@
if platform.system() == "Windows" and sys.version_info >= (3, 8):
# if loading of extension modules fails, search for gdal dll directory
try:
import pyogrio._io # NOQA
import pyogrio._io # noqa: F401

except ImportError:
for path in os.getenv("PATH", "").split(os.pathsep):
Expand Down
52 changes: 35 additions & 17 deletions pyogrio/core.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
"""Core functions to interact with OGR data sources."""

from pyogrio._env import GDALEnv
from pyogrio.util import (
get_vsi_path_or_buffer,
_preprocess_options_key_value,
_mask_to_wkb,
_preprocess_options_key_value,
get_vsi_path_or_buffer,
)


with GDALEnv():
from pyogrio._err import _register_error_handler
from pyogrio._io import ogr_list_layers, ogr_read_bounds, ogr_read_info
from pyogrio._ogr import (
_get_drivers_for_path,
_register_drivers,
get_gdal_geos_version,
get_gdal_version,
get_gdal_version_string,
get_gdal_geos_version,
ogr_list_drivers,
set_gdal_config_options as _set_gdal_config_options,
)
theroggy marked this conversation as resolved.
Show resolved Hide resolved
from pyogrio._ogr import (
get_gdal_config_option as _get_gdal_config_option,
)
from pyogrio._ogr import (
get_gdal_data_path as _get_gdal_data_path,
)
from pyogrio._ogr import (
init_gdal_data as _init_gdal_data,
)
from pyogrio._ogr import (
init_proj_data as _init_proj_data,
_register_drivers,
_get_drivers_for_path,
)
from pyogrio._err import _register_error_handler
from pyogrio._io import ogr_list_layers, ogr_read_bounds, ogr_read_info
from pyogrio._ogr import (
set_gdal_config_options as _set_gdal_config_options,
)

_init_gdal_data()
_init_proj_data()
Expand All @@ -48,8 +59,8 @@ def list_drivers(read=False, write=False):
dict
Mapping of driver name to file mode capabilities: ``"r"``: read, ``"w"``: write.
Drivers that are available but with unknown support are marked with ``"?"``
"""

"""
drivers = ogr_list_drivers()

if read:
Expand All @@ -62,20 +73,23 @@ def list_drivers(read=False, write=False):


def detect_write_driver(path):
"""Attempt to infer the driver for a path by extension or prefix. Only
drivers that support write capabilities will be detected.
"""Attempt to infer the driver for a path by extension or prefix.

Only drivers that support write capabilities will be detected.

If the path cannot be resolved to a single driver, a ValueError will be
raised.

Parameters
----------
path : str
data source path

Returns
-------
str
name of the driver, if detected

"""
# try to infer driver from path
drivers = _get_drivers_for_path(path)
Expand Down Expand Up @@ -106,14 +120,15 @@ def list_layers(path_or_buffer, /):
Parameters
----------
path_or_buffer : str, pathlib.Path, bytes, or file-like
A dataset path or URI, raw buffer, or file-like object with a read method.

Returns
-------
ndarray shape (2, n)
array of pairs of [<layer name>, <layer geometry type>]
Note: geometry is `None` for nonspatial layers.
"""

"""
return ogr_list_layers(get_vsi_path_or_buffer(path_or_buffer))


Expand All @@ -136,6 +151,7 @@ def read_bounds(
Parameters
----------
path_or_buffer : str, pathlib.Path, bytes, or file-like
A dataset path or URI, raw buffer, or file-like object with a read method.
layer : int or str, optional (default: first layer)
If an integer is provided, it corresponds to the index of the layer
with the data source. If a string is provided, it must match the name
Expand Down Expand Up @@ -172,8 +188,8 @@ def read_bounds(
fids are global IDs read from the FID field of the dataset
bounds are ndarray of shape(4, n) containing ``xmin``, ``ymin``, ``xmax``,
``ymax``
"""

"""
return ogr_read_bounds(
get_vsi_path_or_buffer(path_or_buffer),
layer=layer,
Expand Down Expand Up @@ -222,6 +238,7 @@ def read_info(
Parameters
----------
path_or_buffer : str, pathlib.Path, bytes, or file-like
A dataset path or URI, raw buffer, or file-like object with a read method.
layer : [type], optional
Name or index of layer in data source. Reads the first layer by default.
encoding : [type], optional (default: None)
Expand Down Expand Up @@ -257,8 +274,8 @@ def read_info(
"dataset_metadata": "<dict of dataset metadata or None>"
"layer_metadata": "<dict of layer metadata or None>"
}
"""

"""
dataset_kwargs = _preprocess_options_key_value(kwargs) if kwargs else {}

return ogr_read_info(
Expand Down Expand Up @@ -288,8 +305,8 @@ def set_gdal_config_options(options):
configuration options. ``True`` / ``False`` are normalized to ``'ON'``
/ ``'OFF'``. A value of ``None`` for a config option can be used to clear out a
previously set value.
"""

"""
_set_gdal_config_options(options)


Expand All @@ -305,8 +322,8 @@ def get_gdal_config_option(name):
-------
value of the option or None if not set
``'ON'`` / ``'OFF'`` are normalized to ``True`` / ``False``.
"""

"""
return _get_gdal_config_option(name)


Expand All @@ -316,5 +333,6 @@ def get_gdal_data_path():
Returns
-------
str, or None if data directory was not found

"""
return _get_gdal_data_path()
25 changes: 9 additions & 16 deletions pyogrio/errors.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
class DataSourceError(RuntimeError):
"""Errors relating to opening or closing an OGRDataSource (with >= 1 layers)"""
"""Custom errors."""


pass
class DataSourceError(RuntimeError):
"""Errors relating to opening or closing an OGRDataSource (with >= 1 layers)."""


class DataLayerError(RuntimeError):
"""Errors relating to working with a single OGRLayer"""

pass
"""Errors relating to working with a single OGRLayer."""


class CRSError(DataLayerError):
"""Errors relating to getting or setting CRS values"""

pass
"""Errors relating to getting or setting CRS values."""


class FeatureError(DataLayerError):
"""Errors related to reading or writing a feature"""

pass
"""Errors related to reading or writing a feature."""


class GeometryError(DataLayerError):
"""Errors relating to getting or setting a geometry field"""

pass
"""Errors relating to getting or setting a geometry field."""


class FieldError(DataLayerError):
"""Errors relating to getting or setting a non-geometry field"""
"""Errors relating to getting or setting a non-geometry field."""
Loading
Loading