-
Notifications
You must be signed in to change notification settings - Fork 0
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
9 changed files
with
129 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
import tagstr | ||
import os; import importlib; os.getenv("TAGSTR_DISABLE_IMPORT_HOOK", "false").lower() != "true" and importlib.import_module("tagstr"); |
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,6 +1,18 @@ | ||
import os | ||
|
||
from tagstr.decorator import tagfunc | ||
from tagstr.utils import Thunk, decode_raw, format_value | ||
from tagstr.types import TagFunc, Thunk | ||
from tagstr.utils import decode_raw, format_value | ||
|
||
if os.getenv("TAGSTR_DISABLE_IMPORT_HOOK", "false").lower() != "true": | ||
import tagstr.importer # pragma: no cover # noqa | ||
|
||
__version__ = "0.0.1" | ||
__version__ = "0.1.0" | ||
|
||
__all__ = ["tagfunc", "Thunk", "decode_raw", "format_value"] | ||
__all__ = [ | ||
"decode_raw", | ||
"format_value", | ||
"tagfunc", | ||
"TagFunc", | ||
"Thunk", | ||
] |
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 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Iterator | ||
|
||
from tagstr.types import Thunk | ||
|
||
|
||
def decode_raw(*args: str | Thunk) -> Iterator[str | Thunk]: | ||
"""Decode raw strings and thunks.""" | ||
for arg in args: | ||
match arg: | ||
case str(): | ||
yield arg.encode("utf-8").decode("unicode-escape") | ||
case _: | ||
yield arg | ||
|
||
|
||
def format_value(arg: str | Thunk) -> str: | ||
"""Format a value from a thunk or a string.""" | ||
match arg: | ||
case str(): | ||
return arg | ||
case getvalue, _, conv, spec: | ||
value = getvalue() | ||
match conv: | ||
case "r": | ||
value = repr(value) | ||
case "s": | ||
value = str(value) | ||
case "a": | ||
value = ascii(value) | ||
case None: | ||
pass | ||
case _: | ||
raise ValueError(f"Bad conversion: {conv!r}") | ||
return format(value, spec if spec is not None else "") | ||
case _: | ||
raise ValueError(f"Cannot format {arg!r} - expected a thunk or a string") |
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,36 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Iterator | ||
|
||
from tagstr.types import Thunk | ||
|
||
|
||
def decode_raw(*args: str | Thunk) -> Iterator[str | Thunk]: | ||
"""Decode raw strings and thunks.""" | ||
for arg in args: | ||
if isinstance(arg, str): | ||
yield arg.encode("utf-8").decode("unicode-escape") | ||
else: | ||
yield arg | ||
|
||
|
||
def format_value(arg: str | Thunk) -> str: | ||
"""Format a value from a thunk or a string.""" | ||
if isinstance(arg, str): | ||
return arg | ||
elif isinstance(arg, tuple) and len(arg) == 4: | ||
getvalue, _, conv, spec = arg | ||
value = getvalue() | ||
if conv == "r": | ||
value = repr(value) | ||
elif conv == "s": | ||
value = str(value) | ||
elif conv == "a": | ||
value = ascii(value) | ||
elif conv is None: | ||
pass | ||
else: | ||
raise ValueError(f"Bad conversion: {conv!r}") | ||
return format(value, spec if spec is not None else "") | ||
else: | ||
raise ValueError(f"Cannot format {arg!r} - expected a thunk or a string") |
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,23 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any, Callable, Protocol, TypeAlias, TypeVar | ||
|
||
Thunk: TypeAlias = "tuple[Callable[[], Any], str, str | None, str | None]" | ||
""" | ||
A "thunk" is a tuple contianing the following: | ||
1. getvalue: a callable that returns the value to be formatted | ||
2. raw: the raw string that was used to create the thunk | ||
3. conv: the conversion character (e.g. "r", "s", "a") | ||
4. formatspec: the format specification (e.g. ".2f") | ||
TODO: thunks likely should like be a namedtuple in the future. | ||
""" | ||
|
||
|
||
T_co = TypeVar("T_co", covariant=True) | ||
|
||
|
||
class TagFunc(Protocol[T_co]): | ||
def __call__(self, *args: str | Thunk) -> T_co: | ||
... |
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,43 +1,8 @@ | ||
from typing import Any, Callable, Iterator | ||
import sys | ||
|
||
# TODO: thunks likely should have a "tuple-like object with named attributes" | ||
# (so namedtuples) as seen in the os module. This will enable future expansion. | ||
# Of course such named attribute support can also be done in the future! | ||
Thunk = tuple[ | ||
Callable[[], Any], # getvalue | ||
str, # raw | ||
str | None, # conv | ||
str | None, # formatspec | ||
] | ||
if sys.version_info < (3, 10): | ||
from tagstr._py_lt_310 import decode_raw, format_value | ||
else: | ||
from tagstr._py_gte_310 import decode_raw, format_value | ||
|
||
|
||
def decode_raw(*args: str | Thunk) -> Iterator[str | Thunk]: | ||
"""Decode raw strings and thunks.""" | ||
for arg in args: | ||
if isinstance(arg, str): | ||
yield arg.encode("utf-8").decode("unicode-escape") | ||
else: | ||
yield arg | ||
|
||
|
||
def format_value(arg: str | Thunk) -> str: | ||
"""Format a value from a thunk or a string.""" | ||
match arg: | ||
case str(): | ||
return arg | ||
case getvalue, _, conv, spec: | ||
value = getvalue() | ||
match conv: | ||
case "r": | ||
value = repr(value) | ||
case "s": | ||
value = str(value) | ||
case "a": | ||
value = ascii(value) | ||
case None: | ||
pass | ||
case _: | ||
raise ValueError(f"Bad conversion: {conv!r}") | ||
return format(value, spec if spec is not None else "") | ||
case _: | ||
raise ValueError(f"Cannot format {arg!r} - expected a thunk or a string") | ||
__all__ = ["format_value", "decode_raw"] |
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,3 @@ | ||
import tagstr # noqa | ||
|
||
print @ f"asd {1}" |