Skip to content

Commit

Permalink
Merge branch 'release/1.1.8'
Browse files Browse the repository at this point in the history
  • Loading branch information
cnheider committed Aug 1, 2023
2 parents 290b7c3 + e0b455a commit ba2ac24
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 89 deletions.
34 changes: 0 additions & 34 deletions tests/req_unit_test.py

This file was deleted.

35 changes: 21 additions & 14 deletions warg/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from importlib import resources
from importlib.metadata import Distribution, PackageNotFoundError
from warnings import warn

import pkg_resources

__project__ = "Warg"

__author__ = "Christian Heider Nielsen"
__version__ = "1.1.7"
__version__ = "1.1.8"
__doc__ = r"""
Created on 27/04/2019
Expand All @@ -18,6 +17,7 @@

from pathlib import Path


with open(Path(__file__).parent / "README.md", "r") as this_init_file:
__doc__ += this_init_file.read()
# del Path
Expand All @@ -44,7 +44,6 @@
from .boolean_tests import *
from .map_itertools import *
from .ast_ops import *
from .importing import *
from .functions import *
from .os_utilities import *
from .generators import *
Expand Down Expand Up @@ -82,16 +81,20 @@
# from apppath import AppPath # CAREFUL CIRCULAR DEPENDENCY WARNING!
# PROJECT_APP_PATH = AppPath(app_name=PROJECT_NAME, app_author=PROJECT_AUTHOR) # NOT USED!

distributions = {v.key: v for v in pkg_resources.working_set}
if PROJECT_NAME in distributions:
distribution = distributions[PROJECT_NAME]
DEVELOP = dist_is_editable(distribution)
else:
PACKAGE_DATA_PATH = resources.files(PROJECT_NAME) / "data"

try:
DEVELOP = package_is_editable(PROJECT_NAME)
except PackageNotFoundError as e:
DEVELOP = True


def get_version(append_time: Any = DEVELOP) -> str:
"""description"""
"""
:param append_time:
:return:
"""
import datetime
import os

Expand All @@ -106,17 +109,17 @@ def get_version(append_time: Any = DEVELOP) -> str:

if version:
# Most git tags are prefixed with 'v' (example: v1.2.3) this is
# never desirable for artifact repositories, so we strip the
# never desirable for artefact repositories, so we strip the
# leading 'v' if it's present.
version = version[1:] if isinstance(version, str) and version.startswith("v") else version
else:
# Default version is an ISO8601 compliant datetime. PyPI doesn't allow
# The Default version is an ISO8601 compliant datetime. PyPI doesn't allow
# the colon ':' character in its versions, and time is required to allow
# for multiple publications to master in one day. This datetime string
# uses the 'basic' ISO8601 format for both its date and time components
# to avoid issues with the colon character (ISO requires that date and
# time components of a date-time string must be uniformly basic or
# extended, which is why the date component does not have dashes.
# extended, which is why the date component does not have dashes.)
#
# Publications using datetime versions should only be made from master
# to represent the HEAD moving forward.
Expand All @@ -133,3 +136,7 @@ def get_version(append_time: Any = DEVELOP) -> str:
__version__ = get_version(append_time=True)

__version_info__ = tuple(int(segment) for segment in __version__.split("."))


if __name__ == "__main__":
print(__version__)
11 changes: 10 additions & 1 deletion warg/decorators/caching/look_up_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@
"""

from time import sleep, time
from typing import Iterable, Mapping, Set, Tuple, Sequence, MutableMapping, Any, Callable
from typing import (
Iterable,
Mapping,
Set,
Tuple,
Sequence,
MutableMapping,
Any,
Callable,
)

from warg.decorators.hashing import make_hash

Expand Down
14 changes: 12 additions & 2 deletions warg/os_utilities/path_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
Created on 08/03/2020
"""

__all__ = ["ensure_existence", "path_rmtree", "sanitise_path", "path_join", "keep_last_n_modified"]
__all__ = [
"ensure_existence",
"path_rmtree",
"sanitise_path",
"path_join",
"keep_last_n_modified",
]

import collections
import os
Expand Down Expand Up @@ -161,7 +167,11 @@ def ensure_existence(

# @passes_kws_to(rmtree)
def keep_last_n_modified(
directory: Union[Path, str], n: int, only_directories: bool = False, only_files: bool = False, **kwargs
directory: Union[Path, str],
n: int,
only_directories: bool = False,
only_files: bool = False,
**kwargs,
):
directory = Path(directory)
from shutil import rmtree
Expand Down
25 changes: 0 additions & 25 deletions warg/packages.py

This file was deleted.

1 change: 1 addition & 0 deletions warg/packages/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Packages
18 changes: 18 additions & 0 deletions warg/packages/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__doc__ = r"""
Created on 31/07/2023
"""

__author__ = "Christian Heider Nielsen"

from pathlib import Path

with open(Path(__file__).parent / "README.md", "r") as this_init_file:
__doc__ += this_init_file.read()

from .pip_parsing import *
from .reloading import *
from .editable import *
93 changes: 93 additions & 0 deletions warg/packages/editable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import json
from importlib.metadata import Distribution

__all__ = [
"dist_is_editable",
"package_is_editable",
"get_dist_package_location",
"get_package_location",
]

from pathlib import Path


def dist_is_editable(dist: Distribution) -> bool:
"""
Return True if given Distribution is an editable installation.
"""

top_level_name = dist.read_text("top_level.txt").split("\n")[0].strip()

if dist._read_files_egginfo() is not None:
if top_level_name == dist._path.parent.stem:
return True

if dist._read_files_distinfo() is not None:
direct_url_str = dist.read_text("direct_url.json")
if direct_url_str is not None:
direct_url_json = json.loads(direct_url_str)
if "dir_info" in direct_url_json:
if "editable" in direct_url_json["dir_info"]:
return direct_url_json["dir_info"]["editable"]

return False


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))


def get_package_location(package_name: str) -> Path:
dist = Distribution.from_name(package_name)
if dist:
return get_dist_package_location(dist)


def get_dist_package_location(dist: Distribution) -> Path:
"""
FULL OF ASSUMPTIONS!
:param dist:
:return:
"""

top_level_name = dist.read_text("top_level.txt").split("\n")[0].strip()

if dist._read_files_egginfo() is not None:
if top_level_name == dist._path.parent.stem:
return dist._path.parent

if dist._read_files_distinfo() is not None:
direct_url_str = dist.read_text("direct_url.json")
if direct_url_str is not None:
direct_url_json = json.loads(direct_url_str)
if "dir_info" in direct_url_json:
if "editable" in direct_url_json["dir_info"]:
return Path(direct_url_json["url"])

if top_level_name:
package_location = dist._path.parent / top_level_name
if package_location.exists() and package_location.is_dir():
return package_location

return None


if __name__ == "__main__":
print(package_is_editable(package_name="draugr"))
print(get_package_location(package_name="draugr"))

print(package_is_editable(package_name="warg"))
print(get_package_location(package_name="warg"))

print(package_is_editable(package_name="apppath"))
print(get_package_location(package_name="apppath"))

print(get_package_location(package_name="numpy"))

print(package_is_editable(package_name="Pillow"))
print(get_package_location(package_name="Pillow"))
print(get_package_location(package_name="pillow"))
38 changes: 38 additions & 0 deletions warg/packages/pip_parsing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from pathlib import Path
from typing import List, Union

from packaging.requirements import Requirement
from pip._internal.network.session import PipSession
from pip._internal.req import parse_requirements
from pip._internal.req.req_file import ParsedRequirement
from pip._internal.utils.packaging import get_requirement


from urllib.parse import urlparse

__all__ = ["get_requirements_from_file"]


def get_reqed(req: ParsedRequirement) -> 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)]


if __name__ == "__main__":
print(get_requirements_from_file(Path(__file__).parent.parent.parent / "requirements.txt"))
13 changes: 6 additions & 7 deletions warg/importing.py → warg/packages/reloading.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
from typing import Optional, Any, Union, List, Iterable, Callable
from warnings import warn

import pkg_resources

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

"""
PRELOADED_MODULES = set()
Expand Down Expand Up @@ -94,9 +94,8 @@ def reload_requirements(requirements_path: Path, containment_test: Callable = co
:param containment_test:
:return:
"""
with open(requirements_path) as f:
for r in pkg_resources.parse_requirements(f.readlines()):
reload_module(r.project_name, containment_test=containment_test)
for r in get_requirements_from_file(requirements_path):
reload_module(r.name, containment_test=containment_test)


def reload_all_modules(catch_exceptions: bool = True, verbose: bool = True) -> None:
Expand Down Expand Up @@ -187,7 +186,7 @@ def walk_down(path: Path, max_descent: int = None):

def find_ancestral_relatives(
target: Union[str, Path],
context: Path, # = Path.cwd(),
context: Path = Path.cwd(),
*,
from_parent_of_context: bool = True,
ancestral_levels: int = 2,
Expand Down Expand Up @@ -416,7 +415,7 @@ def iajsd():
print(s == s2, set(s2) - set(s), set(s) - set(s2), s2)

def asuhdsaud():
print(find_ancestral_relatives("queues"))
print(find_ancestral_relatives("queues", context=__file__))

# _main()
# aisjdi()
Expand Down
Loading

0 comments on commit ba2ac24

Please sign in to comment.