-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
214 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# SPDX-FileCopyrightText: (c) 2021 Artëm IG <github.com/rtmigo> | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
import os | ||
import unittest | ||
|
||
from vien._call_funcs import relative_fn_to_module_name, NotInnerPath, \ | ||
relative_inner_path | ||
|
||
|
||
def rip(a: str, b: str) -> str: | ||
if os.name == 'posix': | ||
a = a.replace("W:", "") | ||
b = b.replace("W:", "") | ||
return relative_inner_path(a, b) | ||
|
||
|
||
class TestRelativeInnerPath(unittest.TestCase): | ||
|
||
def assertEqualAnyslash(self, a: str, b: str): | ||
self.assertEqual(a.replace('\\', '/'), | ||
b.replace('\\', '/')) | ||
|
||
def test_child(self): | ||
self.assertEqualAnyslash( | ||
rip('W:/abc/myProject/file.py', 'W:/abc/myProject'), | ||
'file.py') | ||
|
||
def test_sub_child(self): | ||
self.assertEqualAnyslash( | ||
rip('W:/abc/myProject/pkg/sub/file.py', | ||
'W:/abc/myProject'), | ||
'pkg/sub/file.py') | ||
|
||
@unittest.skipUnless(os.name == 'nt', "windows-specific") | ||
def test_sub_child_back(self): | ||
self.assertEqualAnyslash( | ||
rip('W:\\abc\\myProject\\pkg\\sub\\file.py', | ||
'W:\\abc\\myProject'), | ||
'pkg/sub/file.py') | ||
|
||
def test_both_relative(self): | ||
self.assertEqualAnyslash( | ||
rip('myProject/pkg/sub/file.py', | ||
'myProject'), | ||
'pkg/sub/file.py') | ||
|
||
def test_same(self): | ||
with self.assertRaises(NotInnerPath): | ||
rip('W:/abc/myProject/x', 'W:/abc/myProject/x'), | ||
|
||
def test_swapped(self): | ||
with self.assertRaises(NotInnerPath): | ||
rip('W:/abc', 'W:/abc/myProject/x'), | ||
|
||
@unittest.skipUnless(os.name == 'nt', "windows-specific") | ||
def test_swapped_backslash(self): | ||
with self.assertRaises(NotInnerPath): | ||
rip('W:\\abc', 'W:\\abc\\myProject\\x'), | ||
|
||
def test_neighbor(self): | ||
with self.assertRaises(NotInnerPath): | ||
rip('W:/abc', 'W:/abc/myProject/x'), | ||
|
||
|
||
class TestFnToModuleName(unittest.TestCase): | ||
|
||
def test_fwd(self): | ||
self.assertEqual( | ||
relative_fn_to_module_name('pkg/sub/module.py'), | ||
'pkg.sub.module') | ||
|
||
def test_py_uppercase(self): | ||
self.assertEqual( | ||
relative_fn_to_module_name('pkg/sub/module.PY'), | ||
'pkg.sub.module') | ||
|
||
def test_no_slashes(self): | ||
self.assertEqual( | ||
relative_fn_to_module_name('file.py'), | ||
'file') | ||
|
||
@unittest.skipUnless(os.name == 'nt', "windows-specific") | ||
def test_back(self): | ||
self.assertEqual( | ||
relative_fn_to_module_name('pkg\\sub\\module.py'), | ||
'pkg.sub.module') | ||
|
||
def test_no_py(self): | ||
with self.assertRaises(ValueError): | ||
relative_fn_to_module_name('pkg/sub/module') | ||
|
||
def test_dots_ext(self): | ||
with self.assertRaises(ValueError): | ||
relative_fn_to_module_name('pkg/sub/module.ext.py') | ||
|
||
def test_dots_parent(self): | ||
with self.assertRaises(ValueError): | ||
relative_fn_to_module_name('../sub/module.ext.py') | ||
|
||
def test_dots_name(self): | ||
with self.assertRaises(ValueError): | ||
relative_fn_to_module_name('sub/.module.py') | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
from .constants import __version__, __license__, __copyright__ | ||
# SPDX-FileCopyrightText: (c) 2021 Artëm IG <github.com/rtmigo> | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
from ._constants import __version__, __license__, __copyright__ | ||
from ._common import is_posix | ||
from .main import main_entry_point | ||
from ._main import main_entry_point | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
from vien.main import main_entry_point | ||
# SPDX-FileCopyrightText: (c) 2021 Artëm IG <github.com/rtmigo> | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
from vien._main import main_entry_point | ||
|
||
if __name__ == "__main__": | ||
main_entry_point() |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# SPDX-FileCopyrightText: (c) 2021 Artëm IG <github.com/rtmigo> | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
import os | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
|
||
def relative_fn_to_module_name(filename: str) -> str: | ||
if not filename.lower().endswith('.py'): | ||
raise ValueError("The filename does not end with '.py'.") | ||
filename = filename[:-3] | ||
if '.' in filename: | ||
raise ValueError("The filename contains dots.") | ||
if os.name == "nt": | ||
filename = filename.replace('\\', '/') | ||
assert not os.path.isabs(filename) | ||
# assert not filename.split()[0] == ".." | ||
return filename.replace('/', '.') | ||
|
||
|
||
class NotInnerPath(ValueError): | ||
pass | ||
|
||
|
||
def relative_inner_path(child: Union[str, Path], | ||
parent: Union[str, Path]) -> str: | ||
"""(/abc/parent/xyz/child, /abc/parent) -> xyz/child | ||
Not only returns the "relative" path, but also checks | ||
it is really relative. | ||
""" | ||
rel_path = os.path.relpath(child, parent) | ||
|
||
first = rel_path.split(os.path.sep)[0] | ||
print(first) | ||
if first == ".." or first == "." or os.path.isabs(rel_path): | ||
raise NotInnerPath(f"The {child} is not a child of {parent}.") | ||
return rel_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
__version__ = "8.0.1" | ||
__version__ = "8.0.2" | ||
__copyright__ = "(c) 2020-2021 Artëm IG <github.com/rtmigo>" | ||
__license__ = "BSD-3-Clause" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.