Skip to content

Commit

Permalink
Merge pull request #262 from jaik03/Remove-pkg_resources
Browse files Browse the repository at this point in the history
Remove pkg resources
  • Loading branch information
kedder authored Jan 14, 2024
2 parents 2e1414a + 653f990 commit b5f7861
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ pytest-cov = "*"
mock = "*"
black = "*"
mypy = "*"
types-pkg-resources = "*"
types-mock = "*"
exceptiongroup = {markers="python_version < '3.11'"}
tomli = {markers="python_version < '3.11'"}

[packages]
ofxstatement = {editable = true,path = "."}
exceptiongroup = "*"
importlib_metadata = {version = ">=3.8", markers="python_version < '3.10'"}
28 changes: 19 additions & 9 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ packages = find:
python_requires = >=3.8, <4
install_requires =
appdirs>=1.3.0
importlib_metadata>=3.8;python_version<'3.10'
data_files =

[options.packages.find]
Expand Down
16 changes: 10 additions & 6 deletions src/ofxstatement/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,33 @@
"""
from typing import List, Tuple, Type
from collections.abc import MutableMapping
import pkg_resources
import sys

if sys.version_info < (3, 10):
from importlib_metadata import entry_points
else:
from importlib.metadata import entry_points

from ofxstatement.ui import UI
from ofxstatement.parser import AbstractStatementParser


def get_plugin(name: str, ui: UI, settings: MutableMapping) -> "Plugin":
plugins = list(pkg_resources.iter_entry_points("ofxstatement", name))
plugins = entry_points(name=name)
if not plugins:
raise PluginNotRegistered(name)
if len(plugins) > 1:
raise PluginNameConflict(plugins)
pcls = plugins[0].load()
plugin = pcls(ui, settings)
return plugin
plugin = plugins[0].load() # type: ignore[index] # index requires a int but class expects a string
return plugin(ui, settings)


def list_plugins() -> List[Tuple[str, Type["Plugin"]]]:
"""Return list of all plugin classes registered as a list of tuples:
[(name, plugin_class)]
"""
plugin_eps = pkg_resources.iter_entry_points("ofxstatement")
plugin_eps = entry_points(group="ofxstatement")
return sorted((ep.name, ep.load()) for ep in plugin_eps)


Expand Down
6 changes: 2 additions & 4 deletions src/ofxstatement/tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ def get_parser(self):

ep = mock.Mock()
ep.load.return_value = SamplePlugin

ep_patch = mock.patch("pkg_resources.iter_entry_points", return_value=[ep])

ep_patch = mock.patch("ofxstatement.plugin.entry_points", return_value=[ep])
with ep_patch:
p = plugin.get_plugin("sample", mock.Mock("UI"), mock.Mock("Settings"))
self.assertIsInstance(p, SamplePlugin)

def test_get_plugin_conflict(self) -> None:
ep = mock.Mock()

ep_patch = mock.patch("pkg_resources.iter_entry_points", return_value=[ep, ep])
ep_patch = mock.patch("ofxstatement.plugin.entry_points", return_value=[ep, ep])
with ep_patch:
with self.assertRaises(plugin.PluginNameConflict):
plugin.get_plugin("conflicting", mock.Mock("UI"), mock.Mock("Settings"))
Expand Down
9 changes: 6 additions & 3 deletions src/ofxstatement/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
import sys
import contextlib

import pkg_resources
if sys.version_info < (3, 10):
from importlib_metadata import version
else:
from importlib.metadata import version


from ofxstatement import ui, configuration, plugin, ofx, exceptions
from typing import Optional, TextIO, Generator
Expand Down Expand Up @@ -41,8 +45,7 @@ def smart_open(


def get_version() -> str:
dist = pkg_resources.get_distribution("ofxstatement")
return dist.version
return version("ofxstatement")


def configure_logging(args: argparse.Namespace) -> None:
Expand Down

0 comments on commit b5f7861

Please sign in to comment.