midir 1.0.0__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.
- midir-1.0.0/LICENSE +0 -0
- midir-1.0.0/PKG-INFO +47 -0
- midir-1.0.0/README.md +32 -0
- midir-1.0.0/pyproject.toml +25 -0
- midir-1.0.0/setup.cfg +4 -0
- midir-1.0.0/src/midir/__init__.py +131 -0
- midir-1.0.0/src/midir.egg-info/PKG-INFO +47 -0
- midir-1.0.0/src/midir.egg-info/SOURCES.txt +10 -0
- midir-1.0.0/src/midir.egg-info/dependency_links.txt +1 -0
- midir-1.0.0/src/midir.egg-info/requires.txt +3 -0
- midir-1.0.0/src/midir.egg-info/top_level.txt +1 -0
- midir-1.0.0/tests/test_unit.py +41 -0
midir-1.0.0/LICENSE
ADDED
|
File without changes
|
midir-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: midir
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Author-email: Jordi Carrera Ventura <jordi.carrera.ventura@gmail.com>
|
|
5
|
+
Project-URL: Google page, https://www.google.com
|
|
6
|
+
Classifier: Programming Language :: Python :: 3
|
|
7
|
+
Classifier: Operating System :: OS Independent
|
|
8
|
+
Requires-Python: >=3.14
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Requires-Dist: build
|
|
12
|
+
Requires-Dist: pytest
|
|
13
|
+
Requires-Dist: twine
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# midir
|
|
17
|
+
|
|
18
|
+
A library for resolving relative module dependencies.
|
|
19
|
+
|
|
20
|
+
# Description
|
|
21
|
+
|
|
22
|
+
`midir` is a Python library designed to handle relative module dependencies by resolving directory paths in an intelligent and system-agnostic way. This library can be used to introspect the file structure of your project and manipulate Python's `sys.path`, allowing for dynamic module imports based on directory hierarchy. It is especially helpful for larger projects where file structure can become complex and static imports cumbersome to manage.
|
|
23
|
+
|
|
24
|
+
## Features
|
|
25
|
+
|
|
26
|
+
1. `midir(path: str) -> str:` Returns the directory name of the given file or directory path.
|
|
27
|
+
|
|
28
|
+
2. `get_caller() -> str:` Returns the name of the file or the location (directory) where the current execution point is present.
|
|
29
|
+
|
|
30
|
+
3. `mipath(path: str = None) -> str:` Returns the canonical (absolute) path of the current execution point or a provided path.
|
|
31
|
+
|
|
32
|
+
4. `midir(path: str = None) -> str:` Returns the directory name where the current execution point is or from a provided path.
|
|
33
|
+
|
|
34
|
+
5. `root_levels(levels: int = 1) -> None:` Makes directories available for import by adding them to sys.path. It starts from directory of the caller file and move up the directory hierarchy. The number of levels up to move is determined by input argument.
|
|
35
|
+
|
|
36
|
+
6. `root_suffix(suffix: str) -> None:` Similar to `root_levels`, but instead of moving up a certain number of directories, it continues to move up until it finds a directory whose name ends with the provided suffix and adds that directory to sys.path.
|
|
37
|
+
|
|
38
|
+
## Exceptions
|
|
39
|
+
|
|
40
|
+
1. `FolderNotFoundError:` Custom exception raised when no folder matching the required conditions (in `root_suffix`) is found in the directory hierarchy.
|
|
41
|
+
|
|
42
|
+
## Build instructions
|
|
43
|
+
|
|
44
|
+
1. Building the package before uploading: 'python -m build' (from "midir").
|
|
45
|
+
2. Upload the package to pypi: 'python -m twine upload --repository {pypi|testpypi} dist/*'
|
|
46
|
+
3. Install the package from pypi: 'python -m pip install --index-url {https://test.pypi.org/simple|https://pypi.org/simple} --no-deps midir'
|
|
47
|
+
4. If any dependencies are required, edit the `pyproject.toml` file, "\[project\]" field, and add a `dependencies` key with a `List\[str\]` value, where each string is a `pip`-readable dependency.
|
midir-1.0.0/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# midir
|
|
2
|
+
|
|
3
|
+
A library for resolving relative module dependencies.
|
|
4
|
+
|
|
5
|
+
# Description
|
|
6
|
+
|
|
7
|
+
`midir` is a Python library designed to handle relative module dependencies by resolving directory paths in an intelligent and system-agnostic way. This library can be used to introspect the file structure of your project and manipulate Python's `sys.path`, allowing for dynamic module imports based on directory hierarchy. It is especially helpful for larger projects where file structure can become complex and static imports cumbersome to manage.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
1. `midir(path: str) -> str:` Returns the directory name of the given file or directory path.
|
|
12
|
+
|
|
13
|
+
2. `get_caller() -> str:` Returns the name of the file or the location (directory) where the current execution point is present.
|
|
14
|
+
|
|
15
|
+
3. `mipath(path: str = None) -> str:` Returns the canonical (absolute) path of the current execution point or a provided path.
|
|
16
|
+
|
|
17
|
+
4. `midir(path: str = None) -> str:` Returns the directory name where the current execution point is or from a provided path.
|
|
18
|
+
|
|
19
|
+
5. `root_levels(levels: int = 1) -> None:` Makes directories available for import by adding them to sys.path. It starts from directory of the caller file and move up the directory hierarchy. The number of levels up to move is determined by input argument.
|
|
20
|
+
|
|
21
|
+
6. `root_suffix(suffix: str) -> None:` Similar to `root_levels`, but instead of moving up a certain number of directories, it continues to move up until it finds a directory whose name ends with the provided suffix and adds that directory to sys.path.
|
|
22
|
+
|
|
23
|
+
## Exceptions
|
|
24
|
+
|
|
25
|
+
1. `FolderNotFoundError:` Custom exception raised when no folder matching the required conditions (in `root_suffix`) is found in the directory hierarchy.
|
|
26
|
+
|
|
27
|
+
## Build instructions
|
|
28
|
+
|
|
29
|
+
1. Building the package before uploading: 'python -m build' (from "midir").
|
|
30
|
+
2. Upload the package to pypi: 'python -m twine upload --repository {pypi|testpypi} dist/*'
|
|
31
|
+
3. Install the package from pypi: 'python -m pip install --index-url {https://test.pypi.org/simple|https://pypi.org/simple} --no-deps midir'
|
|
32
|
+
4. If any dependencies are required, edit the `pyproject.toml` file, "\[project\]" field, and add a `dependencies` key with a `List\[str\]` value, where each string is a `pip`-readable dependency.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=75.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "midir"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name="Jordi Carrera Ventura", email="jordi.carrera.ventura@gmail.com" },
|
|
10
|
+
]
|
|
11
|
+
description = ""
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
requires-python = ">=3.14"
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
]
|
|
18
|
+
dependencies = [
|
|
19
|
+
"build",
|
|
20
|
+
"pytest",
|
|
21
|
+
"twine",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
"Google page" = "https://www.google.com"
|
midir-1.0.0/setup.cfg
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
import os
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
def midir(path: str) -> str:
|
|
6
|
+
"""
|
|
7
|
+
Returns the directory name of the given path
|
|
8
|
+
|
|
9
|
+
Parameters
|
|
10
|
+
----------
|
|
11
|
+
path: str
|
|
12
|
+
Path to a file or a directory
|
|
13
|
+
|
|
14
|
+
Returns
|
|
15
|
+
-------
|
|
16
|
+
str: Directory name of the given path
|
|
17
|
+
"""
|
|
18
|
+
return os.path.dirname(path)
|
|
19
|
+
|
|
20
|
+
class FolderNotFoundError(OSError):
|
|
21
|
+
"""
|
|
22
|
+
Custom exception class to handle the situations when folder is not found
|
|
23
|
+
"""
|
|
24
|
+
pass
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def get_caller() -> str:
|
|
28
|
+
"""
|
|
29
|
+
Returns the filename of the caller
|
|
30
|
+
|
|
31
|
+
Returns
|
|
32
|
+
-------
|
|
33
|
+
str: The name of the file that contains the current execution point
|
|
34
|
+
"""
|
|
35
|
+
return inspect.stack()[2].filename
|
|
36
|
+
|
|
37
|
+
def mipath(path: str = None) -> str:
|
|
38
|
+
"""
|
|
39
|
+
Returns the canonical path
|
|
40
|
+
|
|
41
|
+
Parameters
|
|
42
|
+
----------
|
|
43
|
+
path: (str, optional)
|
|
44
|
+
File or directory path. Defaults to None.
|
|
45
|
+
|
|
46
|
+
Returns
|
|
47
|
+
-------
|
|
48
|
+
str: Canonical path of the current execution point or given path
|
|
49
|
+
"""
|
|
50
|
+
if path is None:
|
|
51
|
+
return os.path.realpath(get_caller())
|
|
52
|
+
else:
|
|
53
|
+
return os.path.realpath(path)
|
|
54
|
+
|
|
55
|
+
def midir(path: str = None) -> str:
|
|
56
|
+
"""
|
|
57
|
+
Returns the directory name from the given path
|
|
58
|
+
|
|
59
|
+
Parameters
|
|
60
|
+
----------
|
|
61
|
+
path: str, optional
|
|
62
|
+
File or directory path. Defaults to None.
|
|
63
|
+
|
|
64
|
+
Returns
|
|
65
|
+
-------
|
|
66
|
+
str: Directory name of the current execution point or given path
|
|
67
|
+
"""
|
|
68
|
+
if path is None:
|
|
69
|
+
return os.path.dirname(get_caller())
|
|
70
|
+
else:
|
|
71
|
+
return os.path.dirname(path)
|
|
72
|
+
|
|
73
|
+
def root_levels(levels: int = 1) -> None:
|
|
74
|
+
"""
|
|
75
|
+
Adds directories to sys.path
|
|
76
|
+
Starts from the directory of the caller file and move up the directory hierarchy.
|
|
77
|
+
Number of levels to move up is determined by input argument.
|
|
78
|
+
|
|
79
|
+
Parameters
|
|
80
|
+
----------
|
|
81
|
+
levels: int
|
|
82
|
+
Levels to move up the directory hierarchy. Defaults to 1.
|
|
83
|
+
|
|
84
|
+
Raises
|
|
85
|
+
------
|
|
86
|
+
TypeError
|
|
87
|
+
ValueError
|
|
88
|
+
"""
|
|
89
|
+
folder = midir(get_caller())
|
|
90
|
+
if not isinstance(levels, int):
|
|
91
|
+
raise TypeError(levels)
|
|
92
|
+
elif levels < 0:
|
|
93
|
+
raise ValueError(f'Expects a positive integer: {levels}')
|
|
94
|
+
while levels:
|
|
95
|
+
if folder not in sys.path:
|
|
96
|
+
sys.path.append(folder)
|
|
97
|
+
folder = os.path.dirname(folder)
|
|
98
|
+
levels -= 1
|
|
99
|
+
|
|
100
|
+
def root_suffix(suffix: str) -> None:
|
|
101
|
+
"""
|
|
102
|
+
Adds directories to sys.path
|
|
103
|
+
Starts from the directory of the caller, moves up the directory hierarchy
|
|
104
|
+
and adds the first folder with the given suffix to sys.path.
|
|
105
|
+
|
|
106
|
+
Parameters
|
|
107
|
+
----------
|
|
108
|
+
suffix: str
|
|
109
|
+
Suffix to match
|
|
110
|
+
|
|
111
|
+
Raises
|
|
112
|
+
------
|
|
113
|
+
TypeError
|
|
114
|
+
ValueError
|
|
115
|
+
FolderNotFoundError: if no folder with the given suffix is found
|
|
116
|
+
"""
|
|
117
|
+
if not isinstance(suffix, str):
|
|
118
|
+
raise TypeError(suffix)
|
|
119
|
+
elif not suffix.strip():
|
|
120
|
+
raise ValueError(f'Expects a non-null string: {suffix}')
|
|
121
|
+
folder = midir(get_caller())
|
|
122
|
+
while True:
|
|
123
|
+
if (
|
|
124
|
+
os.path.basename(folder).endswith(suffix)
|
|
125
|
+
and folder not in sys.path
|
|
126
|
+
):
|
|
127
|
+
sys.path.append(folder)
|
|
128
|
+
return
|
|
129
|
+
folder = os.path.dirname(folder)
|
|
130
|
+
if folder == '/':
|
|
131
|
+
raise FolderNotFoundError(suffix)
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: midir
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Author-email: Jordi Carrera Ventura <jordi.carrera.ventura@gmail.com>
|
|
5
|
+
Project-URL: Google page, https://www.google.com
|
|
6
|
+
Classifier: Programming Language :: Python :: 3
|
|
7
|
+
Classifier: Operating System :: OS Independent
|
|
8
|
+
Requires-Python: >=3.14
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Requires-Dist: build
|
|
12
|
+
Requires-Dist: pytest
|
|
13
|
+
Requires-Dist: twine
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# midir
|
|
17
|
+
|
|
18
|
+
A library for resolving relative module dependencies.
|
|
19
|
+
|
|
20
|
+
# Description
|
|
21
|
+
|
|
22
|
+
`midir` is a Python library designed to handle relative module dependencies by resolving directory paths in an intelligent and system-agnostic way. This library can be used to introspect the file structure of your project and manipulate Python's `sys.path`, allowing for dynamic module imports based on directory hierarchy. It is especially helpful for larger projects where file structure can become complex and static imports cumbersome to manage.
|
|
23
|
+
|
|
24
|
+
## Features
|
|
25
|
+
|
|
26
|
+
1. `midir(path: str) -> str:` Returns the directory name of the given file or directory path.
|
|
27
|
+
|
|
28
|
+
2. `get_caller() -> str:` Returns the name of the file or the location (directory) where the current execution point is present.
|
|
29
|
+
|
|
30
|
+
3. `mipath(path: str = None) -> str:` Returns the canonical (absolute) path of the current execution point or a provided path.
|
|
31
|
+
|
|
32
|
+
4. `midir(path: str = None) -> str:` Returns the directory name where the current execution point is or from a provided path.
|
|
33
|
+
|
|
34
|
+
5. `root_levels(levels: int = 1) -> None:` Makes directories available for import by adding them to sys.path. It starts from directory of the caller file and move up the directory hierarchy. The number of levels up to move is determined by input argument.
|
|
35
|
+
|
|
36
|
+
6. `root_suffix(suffix: str) -> None:` Similar to `root_levels`, but instead of moving up a certain number of directories, it continues to move up until it finds a directory whose name ends with the provided suffix and adds that directory to sys.path.
|
|
37
|
+
|
|
38
|
+
## Exceptions
|
|
39
|
+
|
|
40
|
+
1. `FolderNotFoundError:` Custom exception raised when no folder matching the required conditions (in `root_suffix`) is found in the directory hierarchy.
|
|
41
|
+
|
|
42
|
+
## Build instructions
|
|
43
|
+
|
|
44
|
+
1. Building the package before uploading: 'python -m build' (from "midir").
|
|
45
|
+
2. Upload the package to pypi: 'python -m twine upload --repository {pypi|testpypi} dist/*'
|
|
46
|
+
3. Install the package from pypi: 'python -m pip install --index-url {https://test.pypi.org/simple|https://pypi.org/simple} --no-deps midir'
|
|
47
|
+
4. If any dependencies are required, edit the `pyproject.toml` file, "\[project\]" field, and add a `dependencies` key with a `List\[str\]` value, where each string is a `pip`-readable dependency.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
midir
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
|
|
3
|
+
from src.midir import (
|
|
4
|
+
root_levels,
|
|
5
|
+
root_suffix,
|
|
6
|
+
midir,
|
|
7
|
+
mipath
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
def test_midir():
|
|
11
|
+
#print(midir(__file__))
|
|
12
|
+
assert midir().endswith("midir/tests")
|
|
13
|
+
|
|
14
|
+
def test_mipath():
|
|
15
|
+
assert mipath().endswith("midir/tests/test_unit.py")
|
|
16
|
+
|
|
17
|
+
def test_root_levels():
|
|
18
|
+
assert not [path for path in sys.path if path.endswith("/tests")]
|
|
19
|
+
with_midir = [path for path in sys.path if path.endswith("/midir")]
|
|
20
|
+
root_levels(levels=1)
|
|
21
|
+
assert [path for path in sys.path if path.endswith("/tests")]
|
|
22
|
+
root_levels(levels=2)
|
|
23
|
+
assert with_midir == [path for path in sys.path if path.endswith("/midir")]
|
|
24
|
+
|
|
25
|
+
def test_root_suffix():
|
|
26
|
+
assert '/Users/jordi' not in sys.path
|
|
27
|
+
root_suffix("jordi")
|
|
28
|
+
assert '/Users/jordi' in sys.path
|
|
29
|
+
|
|
30
|
+
def test_root_suffix_oserror():
|
|
31
|
+
try:
|
|
32
|
+
root_suffix("jona")
|
|
33
|
+
except Exception:
|
|
34
|
+
assert True
|
|
35
|
+
|
|
36
|
+
if __name__ == '__main__':
|
|
37
|
+
test_midir()
|
|
38
|
+
test_mipath()
|
|
39
|
+
test_root_levels()
|
|
40
|
+
test_root_suffix()
|
|
41
|
+
test_root_suffix_oserror()
|