Skip to content

Commit 99a1cf3

Browse files
committed
Replace deprecated use of pkg_resources.
Replace with importlib. Signed-off-by: Chris PeBenito <[email protected]>
1 parent 90562e6 commit 99a1cf3

File tree

4 files changed

+35
-19
lines changed

4 files changed

+35
-19
lines changed

setools/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
# SPDX-License-Identifier: LGPL-2.1-only
66
#
77
try:
8-
import pkg_resources
8+
from importlib.metadata import distribution
99
# pylint: disable=no-member
10-
__version__ = pkg_resources.get_distribution("setools").version
10+
__version__ = distribution("setools").version
1111
except ImportError: # pragma: no cover
1212
__version__ = "unknown"
1313

setools/permmap.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
from collections import OrderedDict
88
from contextlib import suppress
99
from dataclasses import dataclass
10-
from typing import cast, Dict, Final, Iterable, Optional, Union
11-
12-
import pkg_resources
10+
from importlib import resources as pkg_resources
11+
import pathlib
12+
from typing import cast, Dict, Final, Iterable, Union
1313

1414
from . import exception
1515
from .descriptors import PermissionMapDescriptor
1616
from .mixins import TupleCompat
1717
from .policyrep import AVRule, SELinuxPolicy, TERuletype
1818

19+
# This is the filename in the Python package
20+
DEFAULT_PERM_MAP: Final[str] = "perm_map"
21+
1922
INFOFLOW_DIRECTIONS: Final = ("r", "w", "b", "n", "u")
2023
MIN_WEIGHT: Final[int] = 1
2124
MAX_WEIGHT: Final[int] = 10
@@ -98,25 +101,24 @@ class PermissionMap:
98101
MIN_WEIGHT: Final[int] = MIN_WEIGHT
99102
MAX_WEIGHT: Final[int] = MAX_WEIGHT
100103

101-
def __init__(self, permmapfile: Optional[str] = None) -> None:
104+
def __init__(self, permmapfile: str | pathlib.Path | None = None) -> None:
102105
"""
103106
Parameter:
104107
permmapfile The path to the permission map to load.
105108
"""
106109
self.log = logging.getLogger(__name__)
107110
self._permmap: MapStruct = OrderedDict()
108-
self._permmapfile: str
111+
self._permmapfile: pathlib.Path
109112

110113
if permmapfile:
111114
self.load(permmapfile)
112115
else:
113-
distro = pkg_resources.get_distribution("setools")
114-
# pylint: disable=no-member
115-
path = "{0}/setools/perm_map".format(distro.location)
116-
self.load(path)
116+
package_location = pkg_resources.files("setools")
117+
with pkg_resources.as_file(package_location / DEFAULT_PERM_MAP) as path:
118+
self.load(path)
117119

118120
def __str__(self) -> str:
119-
return self._permmapfile
121+
return str(self._permmapfile)
120122

121123
def __deepcopy__(self, memo) -> 'PermissionMap':
122124
newobj = PermissionMap.__new__(PermissionMap)
@@ -131,7 +133,7 @@ def __iter__(self) -> Iterable[Mapping]:
131133
for mapping in self.perms(cls):
132134
yield mapping
133135

134-
def load(self, permmapfile: str) -> None:
136+
def load(self, permmapfile: str | pathlib.Path) -> None:
135137
"""
136138
Parameter:
137139
permmapfile The path to the permission map to load.
@@ -239,7 +241,7 @@ def load(self, permmapfile: str) -> None:
239241
if perm_count >= num_perms:
240242
state = 2
241243

242-
self._permmapfile = permmapfile
244+
self._permmapfile = pathlib.Path(permmapfile)
243245
self.log.info("Successfully opened permission map \"{0}\"".format(permmapfile))
244246
self.log.debug("Read {0} classes and {1} total permissions.".format(
245247
class_count, total_perms))

setoolsgui/apol.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
from collections import defaultdict
55
from contextlib import suppress
66
from functools import partial
7+
from importlib import resources as pkg_resources
78
import json
89
import logging
910
import os
1011
import sys
1112
import typing
1213

13-
import pkg_resources
1414
from PyQt6 import QtCore, QtGui, QtWidgets
1515
import setools
1616

@@ -1067,9 +1067,10 @@ def run_apol(policy: str | None = None) -> int:
10671067
app = QtWidgets.QApplication(sys.argv)
10681068

10691069
# load apol stylesheet
1070-
distro = pkg_resources.get_distribution("setools")
1071-
with open(f"{distro.location}/setoolsgui/{STYLESHEET}", "r", encoding="utf-8") as fd:
1072-
app.setStyleSheet(fd.read())
1070+
package_location = pkg_resources.files("setoolsgui")
1071+
with pkg_resources.as_file(package_location / STYLESHEET) as path:
1072+
with open(path, "r", encoding="utf-8") as fd:
1073+
app.setStyleSheet(fd.read())
10731074

10741075
#
10751076
# Create main window

setoolsgui/widgets/helpdialog.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# SPDX-License-Identifier: LGPL-2.1-only
22

3+
from importlib import resources as pkg_resources
4+
35
from PyQt6 import QtCore, QtWidgets
46

57
__all__ = ("HtmlHelpDialog",)
@@ -40,6 +42,17 @@ def from_file(cls, title: str, filename: str, /,
4042

4143
return cls(title, html, parent)
4244

45+
@classmethod
46+
def from_package_file(cls, title: str, filename: str, /,
47+
parent: QtWidgets.QWidget | None = None) -> "HtmlHelpDialog":
48+
49+
"""Load HTML from a file in the package and return a new HtmlHelpDialog instance."""
50+
51+
package_location = pkg_resources.files("setoolsgui")
52+
with pkg_resources.as_file(package_location / filename) as path:
53+
with open(path, "r", encoding="utf-8") as fd:
54+
return cls(title, fd.read(), parent)
55+
4356

4457
if __name__ == '__main__':
4558
import sys
@@ -51,7 +64,7 @@ def from_file(cls, title: str, filename: str, /,
5164
warnings.simplefilter("default")
5265

5366
app = QtWidgets.QApplication(sys.argv)
54-
widget = HtmlHelpDialog.from_file("Test help window", "setoolsgui/apol.html")
67+
widget = HtmlHelpDialog.from_package_file("Test help window", "apol.html")
5568
widget.resize(1024, 768)
5669
widget.show()
5770
rc = app.exec()

0 commit comments

Comments
 (0)