From 58dc47d93a78f2881916af0d0beba1cf8e7ef409 Mon Sep 17 00:00:00 2001 From: Antonio Valentino Date: Mon, 14 Oct 2024 09:01:43 +0200 Subject: [PATCH 1/2] Do not use the deprecated pkg-resources package Usage of pkg-resources is replaced with importlib. --- docs/api.rst | 15 ++++++++------- docs/cli.rst | 4 ++-- setup.py | 2 +- stac_check/cli.py | 4 ++-- stac_check/lint.py | 7 ++++--- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index 40a7680..08b26f9 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -30,7 +30,7 @@ API Reference Expand source code -
import pkg_resources
+        

         from stac_validator.validate import StacValidate
         from stac_validator.utilities import is_valid_url
         import json
@@ -40,7 +40,8 @@ API Reference
         import requests
         from typing import Optional, Union, Dict, Any, List
         from dotenv import load_dotenv
-        import pkg_resources
+        import importlib.metadata
+	import importlib.resources
 
         load_dotenv()
 
@@ -162,7 +163,7 @@ API Reference
                 self.config = self.parse_config(self.config_file)
                 self.asset_type = self.message["asset_type"] if "asset_type" in self.message else ""
                 self.version = self.message["version"] if "version" in self.message else ""
-                self.validator_version = pkg_resources.require("stac-validator")[0].version
+                self.validator_version = importlib.metadata.distribution("stac-validator").version
                 self.validate_all = self.recursive_validation(self.item)
                 self.valid_stac = self.message["valid_stac"]
                 self.error_type = self.check_error_type()
@@ -205,7 +206,7 @@ API Reference
                     with open(default_config_file) as f:
                         default_config = yaml.load(f, Loader=yaml.FullLoader)
                 else:
-                    with pkg_resources.resource_stream(__name__, "stac-check.config.yml") as f:
+                    with importlib.resources.open_text(__name__, "stac-check.config.yml") as f:
                         default_config = yaml.load(f, Loader=yaml.FullLoader)
                 if config_file:
                     with open(config_file) as f:
@@ -866,7 +867,7 @@ API Reference
                 self.config = self.parse_config(self.config_file)
                 self.asset_type = self.message["asset_type"] if "asset_type" in self.message else ""
                 self.version = self.message["version"] if "version" in self.message else ""
-                self.validator_version = pkg_resources.require("stac-validator")[0].version
+                self.validator_version = importlib.metadata.distribution("stac-validator")[0].version
                 self.validate_all = self.recursive_validation(self.item)
                 self.valid_stac = self.message["valid_stac"]
                 self.error_type = self.check_error_type()
@@ -909,7 +910,7 @@ API Reference
                     with open(default_config_file) as f:
                         default_config = yaml.load(f, Loader=yaml.FullLoader)
                 else:
-                    with pkg_resources.resource_stream(__name__, "stac-check.config.yml") as f:
+                    with importlib.resources.open_text(__name__, "stac-check.config.yml") as f:
                         default_config = yaml.load(f, Loader=yaml.FullLoader)
                 if config_file:
                     with open(config_file) as f:
@@ -1416,7 +1417,7 @@ API Reference
                 with open(default_config_file) as f:
                     default_config = yaml.load(f, Loader=yaml.FullLoader)
             else:
-                with pkg_resources.resource_stream(__name__, "stac-check.config.yml") as f:
+                with importlib.resources.open_text(__name__, "stac-check.config.yml") as f:
                     default_config = yaml.load(f, Loader=yaml.FullLoader)
             if config_file:
                 with open(config_file) as f:
diff --git a/docs/cli.rst b/docs/cli.rst
index 1e335c4..e17c909 100644
--- a/docs/cli.rst
+++ b/docs/cli.rst
@@ -32,7 +32,7 @@ CLI Reference
     
     
import click
     from .lint import Linter
-    import pkg_resources
+    import importlib.matadata
 
     def link_asset_message(link_list:list, type: str, format: str) -> None:
         """Prints a list of links or assets and any errors associated with them.
@@ -205,7 +205,7 @@ CLI Reference
     )
     @click.command()
     @click.argument('file')
-    @click.version_option(version=pkg_resources.require("stac-check")[0].version)
+    @click.version_option(version=importlib.metadata.distribution("stac-check").version)
     def main(file, recursive, max_depth, assets, links):
         linter = Linter(file, assets=assets, links=links, recursive=recursive, max_depth=max_depth)
         intro_message(linter)
diff --git a/setup.py b/setup.py
index a830685..c531b39 100644
--- a/setup.py
+++ b/setup.py
@@ -15,6 +15,7 @@
     url="https://github.com/stac-utils/stac-check",
     packages=find_packages(exclude=("tests",)),
     include_package_data=True,
+    setup_requires=["setuptools"],
     install_requires=[
         "click>=8.0.0",
         "requests>=2.19.1",
@@ -22,7 +23,6 @@
         "stac-validator>=3.4.0",
         "PyYAML",
         "python-dotenv",
-        "setuptools",
     ],
     extras_require={
         "dev": [
diff --git a/stac_check/cli.py b/stac_check/cli.py
index 491ceee..89e9ab6 100644
--- a/stac_check/cli.py
+++ b/stac_check/cli.py
@@ -1,5 +1,5 @@
 import click
-import pkg_resources
+import importlib.metadata
 
 from .lint import Linter
 from .logo import logo
@@ -177,7 +177,7 @@ def cli_message(linter: Linter) -> None:
 )
 @click.command()
 @click.argument("file")
-@click.version_option(version=pkg_resources.require("stac-check")[0].version)
+@click.version_option(version=importlib.metadata.distribution("stac-check").version)
 def main(file, recursive, max_depth, assets, links):
     linter = Linter(
         file, assets=assets, links=links, recursive=recursive, max_depth=max_depth
diff --git a/stac_check/lint.py b/stac_check/lint.py
index 0db65b0..ab63095 100644
--- a/stac_check/lint.py
+++ b/stac_check/lint.py
@@ -3,7 +3,8 @@
 from dataclasses import dataclass
 from typing import Any, Dict, List, Optional, Union
 
-import pkg_resources
+import importlib.metadata
+import importlib.resources
 import requests
 import yaml
 from dotenv import load_dotenv
@@ -134,7 +135,7 @@ def __post_init__(self):
             self.message["asset_type"] if "asset_type" in self.message else ""
         )
         self.version = self.message["version"] if "version" in self.message else ""
-        self.validator_version = pkg_resources.require("stac-validator")[0].version
+        self.validator_version = importlib.metadata.distribution("stac-validator").version
         self.validate_all = self.recursive_validation(self.item)
         self.valid_stac = self.message["valid_stac"]
         self.error_type = self.check_error_type()
@@ -185,7 +186,7 @@ def parse_config(config_file: Optional[str] = None) -> Dict:
             with open(default_config_file) as f:
                 default_config = yaml.load(f, Loader=yaml.FullLoader)
         else:
-            with pkg_resources.resource_stream(__name__, "stac-check.config.yml") as f:
+            with importlib.resources.open_text(__name__, "stac-check.config.yml") as f:
                 default_config = yaml.load(f, Loader=yaml.FullLoader)
         if config_file:
             with open(config_file) as f:

From 1edd8af4959cefe1d46de56e7a1195be5cff4992 Mon Sep 17 00:00:00 2001
From: Antonio Valentino 
Date: Mon, 14 Oct 2024 09:11:59 +0200
Subject: [PATCH 2/2] Linting

---
 stac_check/cli.py  | 7 +++++--
 stac_check/lint.py | 8 +++++---
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/stac_check/cli.py b/stac_check/cli.py
index 89e9ab6..ffb8577 100644
--- a/stac_check/cli.py
+++ b/stac_check/cli.py
@@ -1,6 +1,7 @@
-import click
 import importlib.metadata
 
+import click
+
 from .lint import Linter
 from .logo import logo
 
@@ -38,7 +39,9 @@ def recursive_message(linter: Linter) -> None:
     click.secho(f"Max-depth = {linter.max_depth}")
     click.secho("-------------------------")
     for count, msg in enumerate(linter.validate_all):
-        click.secho(f"Asset {count+1} Validated: {msg['path']}", bg="white", fg="black")
+        click.secho(
+            f"Asset {count + 1} Validated: {msg['path']}", bg="white", fg="black"
+        )
         click.secho()
         if msg["valid_stac"] == True:
             recursive_linter = Linter(msg["path"], recursive=True)
diff --git a/stac_check/lint.py b/stac_check/lint.py
index ab63095..fec7818 100644
--- a/stac_check/lint.py
+++ b/stac_check/lint.py
@@ -1,10 +1,10 @@
+import importlib.metadata
+import importlib.resources
 import json
 import os
 from dataclasses import dataclass
 from typing import Any, Dict, List, Optional, Union
 
-import importlib.metadata
-import importlib.resources
 import requests
 import yaml
 from dotenv import load_dotenv
@@ -135,7 +135,9 @@ def __post_init__(self):
             self.message["asset_type"] if "asset_type" in self.message else ""
         )
         self.version = self.message["version"] if "version" in self.message else ""
-        self.validator_version = importlib.metadata.distribution("stac-validator").version
+        self.validator_version = importlib.metadata.distribution(
+            "stac-validator"
+        ).version
         self.validate_all = self.recursive_validation(self.item)
         self.valid_stac = self.message["valid_stac"]
         self.error_type = self.check_error_type()