Open rogfrich opened 1 year ago
Lots of discussion in this Stackoverflow thread
I've basically fudged this by moving the tests into the base directory. Bad Rich.
See #2 - this is breaking the packaging.
So what we're going to do is, we're going to ditch the txt file based template and use a function in the Output
class instead. See the docs for the Jinja loaders where I took this example from:
FunctionLoader Loads templates by calling a function which has to return a string or None if an error occoured.
from jinja import Environment, FunctionLoader
def my_load_func(template_name):
if template_name == 'foo':
return '...'
e = Environment(loader=FunctionLoader(my_load_func))
Just needs a simple class that returns the template as a string (and tests, of course).
If I run the code in the console in PyCharm it works fine:
But if we run
output.py
directly in place to test it using theif __name__ == '__main__'
code that only runs when the module is run directly, we get:This is because
output.py
specifies the location of the Jinja template thus:As currently coded,
FileSystemLoader
always looks in<cwd>/poller/template/
for the template, which is fine when the executes in the context of the top level rootpoller
directory (which it does when you import it into a REPL session as above) but not when you runoutput.py
directly, because then it executes in the context ofpoller/poller
meaning it would be looking forpoller/poller/poller/template/
which doesn't exist, leading to an error.So what I need to do is find a way to say "no matter what context the code is executing under, look in
poller/poller/template/
for the template.