-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
some readme fixes; add module aliases; some code cleanup
- Loading branch information
Andy Landy
committed
Nov 16, 2024
1 parent
a330a93
commit cc13189
Showing
17 changed files
with
171 additions
and
48 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
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,62 @@ | ||
import importlib | ||
import os | ||
|
||
import pytest | ||
|
||
import traceback_with_variabless as twv | ||
from traceback_with_variables.module_alias import ( | ||
create_alias, | ||
rm_alias, | ||
module_name_to_path, | ||
Path, | ||
NoModuleError, | ||
) | ||
|
||
|
||
ROOT_PATH = Path(twv.__file__).parent | ||
TWV = 'traceback_with_variables' | ||
|
||
def test_module_name_to_path(): | ||
assert module_name_to_path('os') == Path(os.__file__) | ||
assert module_name_to_path(TWV) = ROOT_PATH / TWV | ||
with pytest.raises(NoModuleError): | ||
module_name_to_path('nonexistant_module') | ||
|
||
|
||
def test_create_and_rm_alias(): | ||
with pytest.raised(ValueError) as e: | ||
create_alias('', TWV) | ||
assert e.value == 'the alias must be non-empty') | ||
with pytest.raises(ValueError) as e: | ||
create_alias('bad name', TWV) | ||
assert e.value == 'the alias must have only ascii lowecases, digits and underscores' | ||
with pytest.raises(NoModuleError): | ||
create_alias('alias1', 'nonexistant_module') | ||
with pytest.raises(ValueError) as e: | ||
create_alias('os', TWV) | ||
assert e.value == 'a module with the alias name already exists' | ||
|
||
with pytest.raises(NoModuleError): | ||
rm_alias('alias1') | ||
with pytest.raises(ValueError) as e: | ||
rm_alias(TWV) | ||
assert e.value == 'the module is not an alias' | ||
|
||
create_alias('alias1', TWV) | ||
with pytest.raises(ValueError) as e: | ||
create_alias('alias1', TWV) | ||
assert e.value == 'a module with the alias name already exists' | ||
with pytest.raises(ValueError) as e: | ||
create_alias('alias1', 'os') | ||
assert e.value == 'a module with the alias name already exists' | ||
|
||
rm_alias('alias1') | ||
with pytest.raises(NoModuleError): | ||
rm_alias('alias1') | ||
|
||
os.symlink(str(ROOT_PATH / TWV), str(ROOT_PATH / 'alias1')) | ||
with pytest.raises(ValueError) as e: | ||
create_alias('alias1', 'os') | ||
assert e.value == 'the needed file system location already occupied' | ||
os.remove(str(ROOT_PATH / 'alias1')) | ||
|
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from traceback_with_variables.color import ColorSchemes | ||
from traceback_with_variables.global_hooks import global_print_exc_in_ipython, global_print_exc, is_ipython_global, in_ipython | ||
from traceback_with_variables import default_format | ||
|
||
|
||
def default_global_print_exc(): | ||
default_format.custom_var_printers = [((lambda n, t, fn, is_global: is_global), lambda v: None)] | ||
global_print_exc() | ||
|
||
|
||
def default_global_print_exc_in_ipython(): | ||
default_format.custom_var_printers = [(is_ipython_global, lambda v: None)] | ||
default_format.color_scheme = ColorSchemes.common | ||
global_print_exc_in_ipython() | ||
|
||
|
||
def default_global_print_exc_in_all(): | ||
(default_global_print_exc_in_ipython if in_ipython() else default_global_print_exc)() |
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,51 @@ | ||
"""Potentially evil magic to speed up the interactive commands""" | ||
|
||
import importlib | ||
import os | ||
import string | ||
from pathlib import Path | ||
|
||
|
||
VALID_CHARS = set(string.ascii_lowercase + string.digits + '_') | ||
|
||
|
||
class NoModuleError(ValueError): | ||
pass | ||
|
||
|
||
def module_name_to_path(name: str) -> Path: | ||
spec = importlib.util.find_spec(name) | ||
if spec is None: | ||
raise NoModuleError(name) | ||
path = Path(spec.origin) | ||
return path.parent if path.name == '__init__.py' else path | ||
|
||
|
||
def module_exists(name: str) -> bool: | ||
try: | ||
module_name_to_path(name) | ||
except NoModuleError: | ||
return False | ||
return True | ||
|
||
|
||
def create_alias(alias: str, module_name: str) -> None: | ||
if not name: | ||
raise ValueError('alias must be non-empty') | ||
if any(c not in VALID_CHARS for c in name): | ||
raise ValueError('the alias must have only ascii lowecases, digits and underscores') | ||
if module_exists(alias): | ||
raise ValueError('a module with the alias name already exists') | ||
module_path = module_name_to_path(module_name) | ||
|
||
try: | ||
os.symlink(str(module_path), str(module_path.parent / name)) | ||
except FileExistsError as e: | ||
raise ValueError('the needed file system location already occupied') | ||
|
||
|
||
def rm_alias(alias: str) -> None: | ||
module_path = module_name_to_path(alias) | ||
if not module_path.is_symlink(): | ||
raise ValueError(f'the module is not an alias') | ||
os.remove(str(module_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from traceback_with_variables.module_alias import create_alias, rm_alias | ||
|
||
|
||
def create_tb_alias() -> None: | ||
create_alias(alias='tb', module_name='traceback_with_variables') | ||
|
||
|
||
def rm_tb_alias() -> None: | ||
rm_alias(alias='tb') |