Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for pathlib-based paths #342

Open
markkuleinio opened this issue Mar 14, 2021 · 3 comments
Open

Add support for pathlib-based paths #342

markkuleinio opened this issue Mar 14, 2021 · 3 comments
Labels

Comments

@markkuleinio
Copy link

Pathlib (https://docs.python.org/3/library/pathlib.html) was introduced in Python 3.4, I'm suggesting adding support for it when handling file paths in chameleon.

The benefit of using pathlib.Path is user-friendly interface for OS-independent path manipulations because you can use "/" operator to concatenate paths instead of using separate os.path.join() function calls (see templates_dir below).

Pathlib paths are also supported in Python standard library modules and functions (open() and other similar cases), so using pathlib.Path objects (instead of strings) is transparent for the user in the usual cases where file paths are used in Python.

Example chamtest.py (with testtemplate.pt in templates directory):

from pathlib import Path

from chameleon import PageTemplateLoader

current_dir = Path(__file__).resolve().parent
print("=== Current absolute directory:", current_dir)

templates_dir = current_dir / "templates"
print("=== Templates directory:", templates_dir)

print("=== Type:", type(str(templates_dir)))
templates = PageTemplateLoader(str(templates_dir))
template = templates["testtemplate.pt"]
print("=== stringified template path loaded ok")

print("=== Type:", type(templates_dir))
templates = PageTemplateLoader(templates_dir)
template = templates["testtemplate.pt"]
print("=== template path never loaded")

Results:

(venv)$ python3 chamtest.py
=== Current absolute directory: /home/markku/devel/cham-test
=== Templates directory: /home/markku/devel/cham-test/templates
=== Type: <class 'str'>
=== stringified template path loaded ok
=== Type: <class 'pathlib.PosixPath'>
Traceback (most recent call last):
  File "chamtest.py", line 18, in <module>
    template = templates["testtemplate.pt"]
  File "/home/markku/devel/cham-test/venv/lib/python3.7/site-packages/chameleon/zpt/loader.py", line 28, in load
    return super(TemplateLoader, self).load(filename, cls)
  File "/home/markku/devel/cham-test/venv/lib/python3.7/site-packages/chameleon/loader.py", line 45, in load
    self.registry[args] = template = func(self, *args, **kwargs)
  File "/home/markku/devel/cham-test/venv/lib/python3.7/site-packages/chameleon/loader.py", line 102, in load
    for path in self.search_path:
TypeError: 'PosixPath' object is not iterable
(venv)$
@malthe
Copy link
Owner

malthe commented Mar 14, 2021

This is a good idea – I suppose it might be nice to use this as an excuse to add type specs to the main interfaces.

@markkuleinio
Copy link
Author

Yeah and to be extra clear for other readers, I'm suggesting accepting both str and pathlib.Path (in type hint something like Union[str, pathlib.Path]).

@malthe
Copy link
Owner

malthe commented Dec 12, 2023

@markkuleinio note that search_path must be an iterable, e.g. search_path=[my_path1, my_path2].

Ideally there would be some type-checking, but this is old-school Python and we haven't gotten around to adding any typings and there's not a lot of arguments checking either.

@malthe malthe added the feature label Dec 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants