Skip to content

Commit

Permalink
Replace usage of pkg_resources.resource_(listdir|isdir)
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf committed Feb 20, 2024
1 parent e340388 commit 35b449c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 26 deletions.
22 changes: 9 additions & 13 deletions python/nav/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,12 @@
import pwd
import stat
import configparser
import pkg_resources
from pathlib import Path

from nav.errors import GeneralException
from nav.util import files, resource_bytes
from . import buildconf

try:
from importlib.resources import files
except ImportError: # Python 3.7!
from importlib_resources import files


_logger = logging.getLogger(__name__)

# Potential locations to find configuration files
Expand Down Expand Up @@ -254,13 +248,15 @@ def _config_resource_walk(source=''):
from available nav package resources. All paths returned will be relative to
the etc top directory.
"""
current_path = os.path.join('etc', source)
for name in pkg_resources.resource_listdir('nav', current_path):
full_name = os.path.join(current_path, name)
relative_name = os.path.join(source, name)
if pkg_resources.resource_isdir('nav', full_name):
source = Path(source)
current_path = Path('etc') / source
for path in files('nav').joinpath(current_path).iterdir():
name = path.name
full_name = current_path / name
relative_name = str(source / name)
if files('nav').joinpath(full_name).is_dir():
for path in _config_resource_walk(source=relative_name):
yield path
yield str(path)
else:
yield relative_name

Expand Down
21 changes: 8 additions & 13 deletions python/nav/pgsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,12 @@
import subprocess
from textwrap import wrap
from errno import ENOENT, EACCES
from pathlib import Path
import psycopg2
from pkg_resources import resource_listdir

from nav.db import ConnectionParameters
from nav.colors import colorize, print_color
from nav.colors import COLOR_CYAN, COLOR_YELLOW, COLOR_RED, COLOR_GREEN

try:
from importlib.resources import files
except ImportError: # Python 3.7!
from importlib_resources import files

from nav.util import files, resource_bytes


Expand Down Expand Up @@ -569,12 +563,13 @@ def __init__(self, resource_module):
self._find_change_scripts()

def _find_change_scripts(self):
changes_dir = 'sql/changes'
scripts = [
os.path.join(changes_dir, f)
for f in resource_listdir(self.resource_module, changes_dir)
if self.script_pattern.match(f)
]
changes_dir = Path('sql/changes')
scripts = []
sql_path = files(self.resource_module).joinpath(changes_dir)
for path in sql_path.iterdir():
filename = path.name
if self.script_pattern.match(str(filename)):
scripts.append(str(changes_dir / filename))
self[:] = scripts

def get_missing_changes(self, versions):
Expand Down

0 comments on commit 35b449c

Please sign in to comment.