Skip to content

Commit

Permalink
Merge branch 'release/1.1.9'
Browse files Browse the repository at this point in the history
  • Loading branch information
cnheider committed Aug 8, 2023
2 parents ba2ac24 + c25789a commit 4795659
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 14 deletions.
3 changes: 2 additions & 1 deletion warg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
__project__ = "Warg"

__author__ = "Christian Heider Nielsen"
__version__ = "1.1.8"
__version__ = "1.1.9"
__doc__ = r"""
Created on 27/04/2019
Expand Down Expand Up @@ -61,6 +61,7 @@
from .config_shell import *
from .colors import *
from .packages import *
from .iteration import *
except ImportError as ix:
this_package_name = Path(__file__).parent.name
this_package_reqs = Path(__file__).parent.parent / f"requirements.txt"
Expand Down
41 changes: 41 additions & 0 deletions warg/iteration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from typing import Sequence, Any, Tuple

__all__ = ["pairs", "chunks"]


def pairs(s: Sequence) -> Tuple[Any, Any]:
"""
Iterate over a list in overlapping pairs.
Usage:
lst = [4, 7, 11, 2]
pairs(lst) yields (4, 7), (7, 11), (11, 2)
http://stackoverflow.com/questions/1257413/1257446#1257446
:param s: An iterable/list
:return: Yields a pair of consecutive elements (lst[k], lst[k+1]) of lst. Last call yields (lst[-2], lst[-1]).
"""
i = iter(s)
prev = next(i)

for item in i:
yield prev, item
prev = item


def chunks(lst: Sequence, n: int) -> Any:
"""
Yield successive n-sized chunks from lst.
:param lst:
:param n:
:return:
"""
for i in range(0, len(lst), n):
yield lst[i : i + n]


if __name__ == "__main__":
print(list(chunks(list(range(10)), 3)))
24 changes: 18 additions & 6 deletions warg/packages/editable.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from importlib.metadata import Distribution
from importlib.metadata import Distribution, PackageNotFoundError

__all__ = [
"dist_is_editable",
Expand All @@ -15,8 +15,12 @@ def dist_is_editable(dist: Distribution) -> bool:
"""
Return True if given Distribution is an editable installation.
"""
top_level = dist.read_text("top_level.txt")

top_level_name = dist.read_text("top_level.txt").split("\n")[0].strip()
if top_level:
top_level_name = top_level.split("\n")[0].strip()
else: # assume top level namespace is the same as dist
top_level_name = dist.name

if dist._read_files_egginfo() is not None:
if top_level_name == dist._path.parent.stem:
Expand All @@ -37,13 +41,21 @@ def package_is_editable(package_name: str) -> bool:
"""
Return True if given Package is an editable installation.
"""
return dist_is_editable(Distribution.from_name(package_name))
try:
dist = Distribution.from_name(package_name)

return dist_is_editable(dist)
except PackageNotFoundError as p:
print(p)


def get_package_location(package_name: str) -> Path:
dist = Distribution.from_name(package_name)
if dist:
return get_dist_package_location(dist)
try:
dist = Distribution.from_name(package_name)
if dist:
return get_dist_package_location(dist)
except PackageNotFoundError as p:
print(p)


def get_dist_package_location(dist: Distribution) -> Path:
Expand Down
9 changes: 9 additions & 0 deletions warg/packages/pip_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@


def get_reqed(req: ParsedRequirement) -> Requirement:
"""
https://packaging.python.org/en/latest/specifications/direct-url/#example-pip-commands-and-their-effect-on-direct-url-json
:param req:
:type req: ParsedRequirement
:return:
:rtype: Requirement
"""

req_ = req.requirement
if req.is_editable: # parse out egg=... fragment from VCS URL
parsed = urlparse(req_)
Expand Down
22 changes: 15 additions & 7 deletions warg/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
Created on 13/06/2020
"""

from importlib.metadata import entry_points, EntryPoint
from typing import Tuple, Any, Generator, Union
import sys

if sys.version_info[:2] >= (3, 10):
# pylint: disable=no-name-in-module
from importlib.metadata import entry_points, EntryPoint
else:
from importlib_metadata import entry_points, EntryPoint

from typing import Tuple, Generator, Any

__all__ = ["get_plugins", "get_static_plugins", "get_dynamic_plugins"]

Expand Down Expand Up @@ -42,7 +49,7 @@ def get_static_plugins(package_name: str) -> Tuple:

def get_dynamic_plugins(
package_name: str,
) -> Generator[Union[str, EntryPoint], Any, None]:
) -> Generator[EntryPoint, Any, None]:
"""Returns a list specifying dynamically loaded plugins.
Returns:
Expand All @@ -51,11 +58,12 @@ def get_dynamic_plugins(
[1]: https://packaging.python.org/specifications/entry-points/
"""

# .load() method to import and load that entry point (module or object).
# from importlib import metadata # new method!
# return [ entry_point.load() for entry_point in metadata.entry_points()[f'{package_name}_plugins'] ]
return (entry_point for entry_point in entry_points(group="console_scripts", name=package_name))
return (
entry_point.load() for entry_point in entry_points(group=f"{package_name}_plugins", name=package_name)
)


if __name__ == "__main__":
print(get_plugins("warg"))

print(entry_points())

0 comments on commit 4795659

Please sign in to comment.