Skip to content

Commit

Permalink
Merge branch 'release/1.2.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
cnheider committed Aug 25, 2023
2 parents 7ce6f42 + 359c1d0 commit f8702f3
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 49 deletions.
2 changes: 1 addition & 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.2.5"
__version__ = "1.2.6"
__doc__ = r"""
Created on 27/04/2019
Expand Down
2 changes: 1 addition & 1 deletion warg/packages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
with open(Path(__file__).parent / "README.md", "r") as this_init_file:
__doc__ += this_init_file.read()

from .pip_parsing import *
# from .pip_parsing import *
from .reloading import *
from .editable import *
5 changes: 4 additions & 1 deletion warg/packages/editable.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import json

# noinspection PyProtectedMember
from importlib.metadata import Distribution, PackageNotFoundError, PathDistribution
try:
from importlib.metadata import Distribution, PackageNotFoundError, PathDistribution
except:
from importlib_metadata import Distribution, PackageNotFoundError, PathDistribution

__all__ = [
"dist_is_editable",
Expand Down
96 changes: 51 additions & 45 deletions warg/packages/pip_parsing.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,61 @@
from pathlib import Path
from typing import List, Union

from packaging.requirements import Requirement

# noinspection PyProtectedMember
from pip._internal.network.session import PipSession

# noinspection PyProtectedMember
from pip._internal.req import parse_requirements

# noinspection PyProtectedMember
from pip._internal.req.req_file import ParsedRequirement

# noinspection PyProtectedMember
from pip._internal.utils.packaging import get_requirement


from urllib.parse import urlparse
from warg.functions import identity, sink

__all__ = ["get_requirements_from_file"]


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_)
egg_name = parsed.fragment.partition("egg=")[-1]
without_fragment = parsed._replace(fragment="").geturl()
req_parsed = f"{egg_name} @ {without_fragment}"
else:
req_parsed = req_
return get_requirement(req_parsed)


def get_requirements_from_file(
file_path: Union[str, Path], session: Union[str, PipSession] = "test"
) -> List[Requirement]:
"""Turn requirements.txt into a list"""
if isinstance(file_path, Path):
file_path = str(file_path)
return [get_reqed(ir) for ir in parse_requirements(file_path, session=session)]

try:
# from packaging.requirements import Requirement

# noinspection PyProtectedMember
from pip._internal.network.session import PipSession

# noinspection PyProtectedMember
from pip._internal.req import parse_requirements

# noinspection PyProtectedMember
from pip._internal.req.req_file import ParsedRequirement

# noinspection PyProtectedMember
from pip._internal.utils.packaging import get_requirement

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_)
egg_name = parsed.fragment.partition("egg=")[-1]
without_fragment = parsed._replace(fragment="").geturl()
req_parsed = f"{egg_name} @ {without_fragment}"
else:
req_parsed = req_
return get_requirement(req_parsed)

def get_requirements_from_file(
file_path: Union[str, Path], session: Union[str, PipSession] = "test"
): # -> List[Requirement]
"""Turn requirements.txt into a list"""
if isinstance(file_path, Path):
file_path = str(file_path)
return [get_reqed(ir) for ir in parse_requirements(file_path, session=session)]

except:
get_requirements_from_file = sink
...
# print('You version of python is to old!')

if __name__ == "__main__":
print(get_requirements_from_file(Path(__file__).parent.parent.parent / "requirements.txt"))
4 changes: 3 additions & 1 deletion warg/packages/reloading.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
import sys
from importlib import reload
from importlib.util import find_spec

from pathlib import Path
from typing import Optional, Any, Union, List, Iterable, Callable
from warnings import warn


from warg.packages import get_requirements_from_file
from warg.decorators import passes_kws_to

"""
Expand Down Expand Up @@ -94,6 +94,8 @@ def reload_requirements(requirements_path: Path, containment_test: Callable = co
:param containment_test:
:return:
"""
from warg.packages.pip_parsing import get_requirements_from_file

for r in get_requirements_from_file(requirements_path):
reload_module(r.name, containment_test=containment_test)

Expand Down

0 comments on commit f8702f3

Please sign in to comment.