import-parent 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- import_parent-0.0.1/PKG-INFO +29 -0
- import_parent-0.0.1/import_parent.egg-info/PKG-INFO +29 -0
- import_parent-0.0.1/import_parent.egg-info/SOURCES.txt +7 -0
- import_parent-0.0.1/import_parent.egg-info/dependency_links.txt +1 -0
- import_parent-0.0.1/import_parent.egg-info/top_level.txt +1 -0
- import_parent-0.0.1/pyproject.toml +22 -0
- import_parent-0.0.1/readme.md +16 -0
- import_parent-0.0.1/setup.cfg +4 -0
- import_parent-0.0.1/src.py +58 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: import_parent
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Python package that allows a user to easily import local functions from parent folders.
|
|
5
|
+
Author-email: Michael Boerman <michaelboerman@hey.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/michaelboerman/import_parent
|
|
7
|
+
Project-URL: Issues, https://github.com/michaelboerman/import_parent/issues
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
|
|
14
|
+
# import-parent
|
|
15
|
+
|
|
16
|
+
A small utility for importing Python modules using paths relative to the
|
|
17
|
+
calling script — without modifying your project structure.
|
|
18
|
+
|
|
19
|
+
Inspired by R's `here()`.
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
`pip install import-parent`
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
`from import_parent import import_parent`
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: import_parent
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Python package that allows a user to easily import local functions from parent folders.
|
|
5
|
+
Author-email: Michael Boerman <michaelboerman@hey.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/michaelboerman/import_parent
|
|
7
|
+
Project-URL: Issues, https://github.com/michaelboerman/import_parent/issues
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
|
|
14
|
+
# import-parent
|
|
15
|
+
|
|
16
|
+
A small utility for importing Python modules using paths relative to the
|
|
17
|
+
calling script — without modifying your project structure.
|
|
18
|
+
|
|
19
|
+
Inspired by R's `here()`.
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
`pip install import-parent`
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
`from import_parent import import_parent`
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
src
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "import_parent"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name="Michael Boerman", email="michaelboerman@hey.com" },
|
|
10
|
+
]
|
|
11
|
+
description = "Python package that allows a user to easily import local functions from parent folders."
|
|
12
|
+
readme = "readme.md"
|
|
13
|
+
requires-python = ">=3.8"
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[project.urls]
|
|
21
|
+
Homepage = "https://github.com/michaelboerman/import_parent"
|
|
22
|
+
Issues = "https://github.com/michaelboerman/import_parent/issues"
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# import-parent
|
|
2
|
+
|
|
3
|
+
A small utility for importing Python modules using paths relative to the
|
|
4
|
+
calling script — without modifying your project structure.
|
|
5
|
+
|
|
6
|
+
Inspired by R's `here()`.
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
`pip install import-parent`
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
`from import_parent import import_parent`
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import sys
|
|
3
|
+
import inspect
|
|
4
|
+
from typing import Union
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def _get_caller_dir(stack_level: int = 2) -> str:
|
|
8
|
+
frame = inspect.stack()[stack_level]
|
|
9
|
+
caller_file = frame.filename
|
|
10
|
+
return os.path.dirname(os.path.abspath(caller_file))
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _count_levels(path: str) -> int:
|
|
14
|
+
parts = [p for p in path.replace("\\", "/").split("/") if p]
|
|
15
|
+
levels = sum(1 for p in parts if p == "..")
|
|
16
|
+
|
|
17
|
+
if levels == 0:
|
|
18
|
+
raise ValueError("Path must contain at least one '..'")
|
|
19
|
+
|
|
20
|
+
return levels
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def import_parent(path: Union[str, int] = "..") -> str:
|
|
24
|
+
"""
|
|
25
|
+
Add a parent directory (relative to the caller) to sys.path.
|
|
26
|
+
|
|
27
|
+
Parameters
|
|
28
|
+
----------
|
|
29
|
+
path : str or int
|
|
30
|
+
'../..' style path or an integer number of levels.
|
|
31
|
+
|
|
32
|
+
Returns
|
|
33
|
+
-------
|
|
34
|
+
str
|
|
35
|
+
The directory added to sys.path
|
|
36
|
+
"""
|
|
37
|
+
if isinstance(path, int):
|
|
38
|
+
if path <= 0:
|
|
39
|
+
raise ValueError("Integer path must be > 0")
|
|
40
|
+
levels_up = path
|
|
41
|
+
elif isinstance(path, str):
|
|
42
|
+
levels_up = _count_levels(path)
|
|
43
|
+
else:
|
|
44
|
+
raise TypeError("path must be a string or int")
|
|
45
|
+
|
|
46
|
+
caller_dir = _get_caller_dir()
|
|
47
|
+
|
|
48
|
+
target_dir = caller_dir
|
|
49
|
+
for _ in range(levels_up):
|
|
50
|
+
target_dir = os.path.dirname(target_dir)
|
|
51
|
+
|
|
52
|
+
if not os.path.isdir(target_dir):
|
|
53
|
+
raise FileNotFoundError(f"Resolved path does not exist: {target_dir}")
|
|
54
|
+
|
|
55
|
+
if target_dir not in sys.path:
|
|
56
|
+
sys.path.insert(0, target_dir)
|
|
57
|
+
|
|
58
|
+
return target_dir
|