Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ Parsing output:
`pathbase` also includes a lightweight CLI:

```bash
pathbase format --template '${ROOT}/{project}/{name}_v{version:03d}.txt' \
ROOT=/mnt/projects project=demo name=report version=1
ROOT=/mnt/projects pathbase format --template '${ROOT}/{project}/{name}_v{version:03d}.txt' \
project=demo name=report version=1

ENVPATH=./examples/vfx/ pathbase parse \
'/mnt/projects/bigbuckbunny/seq001/shot010/lighting/render_beauty_v001.1001.exr'
Expand Down
4 changes: 2 additions & 2 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@ The `pathbase` CLI provides a thin wrapper around the same template operations.
Format a path from fields:

```bash
pathbase format --template '${ROOT}/{project}/{name}_v{version:03d}.txt' \
ROOT=/mnt/projects project=demo name=report version=1
ROOT=/mnt/projects pathbase format --template '${ROOT}/{project}/{name}_v{version:03d}.txt' \
project=demo name=report version=1
```

Expected output:
Expand Down
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ Parsing output:
`pathbase` also includes a lightweight CLI:

```bash
pathbase format --template '${ROOT}/{project}/{name}_v{version:03d}.txt' \
ROOT=/mnt/projects project=demo name=report version=1
ROOT=/mnt/projects pathbase format --template '${ROOT}/{project}/{name}_v{version:03d}.txt' \
project=demo name=report version=1

ENVPATH=./examples/vfx/ pathbase parse \
'/mnt/projects/bigbuckbunny/seq001/shot010/lighting/render_beauty_v001.1001.exr'
Expand Down
2 changes: 1 addition & 1 deletion lib/pathbase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@
"__version__",
]

__version__ = "0.1.1"
__version__ = "0.1.2"
12 changes: 11 additions & 1 deletion lib/pathbase/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def _default_scope(path: PathInput) -> str:

def _iter_template_items(env: Mapping[str, Any]) -> List[Tuple[str, str]]:
"""Return environment entries that look like path templates."""
formatter = string.Formatter()
items = []
for key, value in env.items():
if not isinstance(value, str) or not value:
Expand All @@ -96,6 +97,12 @@ def _iter_template_items(env: Mapping[str, Any]) -> List[Tuple[str, str]]:
continue
if "/" not in value and "\\" not in value:
continue
try:
parts = tuple(formatter.parse(_escape_env_vars(value)))
except ValueError:
continue
if not any(field_name is not None for _, field_name, _, _ in parts):
continue
items.append((key, value))
return items

Expand Down Expand Up @@ -186,7 +193,10 @@ def __init__(
raise InvalidTemplateError(str(err)) from err
self._fields: List[str] = []
self._formats: Dict[str, FieldType] = {}
self._pattern: Pattern[str] = self._compile_pattern()
try:
self._pattern = self._compile_pattern()
except re.error as err:
raise InvalidTemplateError(str(err)) from err

def __repr__(self) -> str:
return f"Template({self._template!r})"
Expand Down
31 changes: 31 additions & 0 deletions tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,37 @@ def test_find_matching_templates_from_environment():
assert [name for name, _ in matches] == ["FILEPATH"]


def test_find_matching_templates_ignores_shell_env_values():
env = {
"FILEPATH": "/mnt/projects/{project}/shots/{sequence}/{shot}/{name}.mp4",
"BASH_FUNC_module%%": (
" () { typeset swfound=1;\n"
" if [ \"${MODULES_USE_COMPAT_VERSION:-0}\" = '1' ]; then\n"
" typeset swname='main';\n"
" if [ -e /usr/share/Modules/libexec/modulecmd.tcl ]; then\n"
" typeset swfound=0;\n"
" unset MODULES_USE_COMPAT_VERSION;\n"
" fi;\n"
" else\n"
" typeset swname='compatibility';\n"
" if [ -e /usr/share/Modules/libexec/modulecmd-compat ]; then\n"
" typeset swfound=0;\n"
" MODULES_USE_COMPAT_VERSION=1;\n"
" export MODULES_USE_COMPAT_VERSION;\n"
" fi;\n"
" fi\n"
"}"
),
}

matches = find_matching_templates(
"/mnt/projects/demo/shots/seq001/shot010/shot010_plate.mp4",
env=env,
)

assert [name for name, _ in matches] == ["FILEPATH"]


def test_match_template_returns_unique_match():
env = {
"FILEPATH": "{project}/{name}_v{version:03d}.txt",
Expand Down
Loading