rogfrich / poller

A CLI app to construct a Discourse poll from the posters in a thread
1 stars 0 forks source link

Update package to include the template.txt file for Jinja #2

Open rogfrich opened 1 year ago

rogfrich commented 1 year ago

Made a package from the code. Forgot to include a MANIFEST.in file so that template.txt gets packaged. Oops.

rogfrich commented 1 year ago

I've added a manifest.in file, but Jinja2 can't find the template when the package is imported into a Python venv:

(.venv) rich@Richs-Mac-mini ttt % pip install ../poller/dist/poller-0.2.0-py3-none-any.whl
Processing /Users/rich/code/poller/dist/poller-0.2.0-py3-none-any.whl
Collecting requests (from poller==0.2.0)
  Using cached requests-2.31.0-py3-none-any.whl.metadata (4.6 kB)
Collecting jinja2 (from poller==0.2.0)
  Using cached Jinja2-3.1.2-py3-none-any.whl (133 kB)
Collecting MarkupSafe>=2.0 (from jinja2->poller==0.2.0)
  Using cached MarkupSafe-2.1.3-cp39-cp39-macosx_10_9_x86_64.whl.metadata (3.0 kB)
Collecting charset-normalizer<4,>=2 (from requests->poller==0.2.0)
  Using cached charset_normalizer-3.3.0-cp39-cp39-macosx_10_9_x86_64.whl.metadata (32 kB)
Collecting idna<4,>=2.5 (from requests->poller==0.2.0)
  Using cached idna-3.4-py3-none-any.whl (61 kB)
Collecting urllib3<3,>=1.21.1 (from requests->poller==0.2.0)
  Using cached urllib3-2.0.7-py3-none-any.whl.metadata (6.6 kB)
Collecting certifi>=2017.4.17 (from requests->poller==0.2.0)
  Using cached certifi-2023.7.22-py3-none-any.whl.metadata (2.2 kB)
Using cached requests-2.31.0-py3-none-any.whl (62 kB)
Using cached certifi-2023.7.22-py3-none-any.whl (158 kB)
Using cached charset_normalizer-3.3.0-cp39-cp39-macosx_10_9_x86_64.whl (119 kB)
Using cached MarkupSafe-2.1.3-cp39-cp39-macosx_10_9_x86_64.whl (13 kB)
Using cached urllib3-2.0.7-py3-none-any.whl (124 kB)
Installing collected packages: urllib3, MarkupSafe, idna, charset-normalizer, certifi, requests, jinja2, poller
Successfully installed MarkupSafe-2.1.3 certifi-2023.7.22 charset-normalizer-3.3.0 idna-3.4 jinja2-3.1.2 poller-0.2.0 requests-2.31.0 urllib3-2.0.7
(.venv) rich@Richs-Mac-mini ttt % python
Python 3.9.6 (default, Jun 29 2021, 05:25:02) 
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import poller
>>> from poller.manager import Manager
>>> x = Manager("984627", "TEST")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/rich/code/ttt/.venv/lib/python3.9/site-packages/poller/manager.py", line 10, in __init__
    self.output = self._get_rendered_output()
  File "/Users/rich/code/ttt/.venv/lib/python3.9/site-packages/poller/manager.py", line 17, in _get_rendered_output
    output = Output(self.entrants, self.voting_deadline)
  File "/Users/rich/code/ttt/.venv/lib/python3.9/site-packages/poller/output.py", line 16, in __init__
    self.template = self.environment.get_template("template.txt")
  File "/Users/rich/code/ttt/.venv/lib/python3.9/site-packages/jinja2/environment.py", line 1010, in get_template
    return self._load_template(name, globals)
  File "/Users/rich/code/ttt/.venv/lib/python3.9/site-packages/jinja2/environment.py", line 969, in _load_template
    template = self.loader.load(self, name, self.make_globals(globals))
  File "/Users/rich/code/ttt/.venv/lib/python3.9/site-packages/jinja2/loaders.py", line 126, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "/Users/rich/code/ttt/.venv/lib/python3.9/site-packages/jinja2/loaders.py", line 218, in get_source
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: template.txt
>>> 
rogfrich commented 1 year ago

This is because the code is looking in a (hard-coded) directory, and when the package is created, it basically loses the top level base dir (the one with pyproject.toml etc in it) so the hard coded poller/poller/template/template.txt doesn't work in the imported code, since it should be poller/template.txt.

I think I'm going to have to derive the location of the template in code and supply it to FileSystemLoader as a Path object.