2

I want to be able to dump into an object.

According to the documentation, I can dump into a file or a file like object.

What can I use as a file like object?

Obs.: I don't want to save it nor display it. Its a downloadable config template for a router thats created on the fly from data stored in the database.

env = Environment(autoescape=False, optimized=False)    

config_file = None
device_config = None
device_config = env.from_string(config_template.config)
device_config.stream(
  STR         = site.location.upper()[:4],
).dump(config_file)

Reason being is I want to use a content disposition to output the file to a users browser with django.

EDIT: tested with stringIO

import StringIO
>>> config_file = StringIO
>>> device_config = None
>>> device_config = env.from_string(config_template.config)
>>> device_config.stream(
...   STR         = site.location.upper()[:4],
... ).dump(config_file)
Traceback (most recent call last):
  File "<console>", line 3, in <module>
  File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 1167, in dump
    fp.write(item)
AttributeError: 'module' object has no attribute 'write'
>>>

Thanks

AlexW
  • 2,843
  • 12
  • 74
  • 156
  • you wanna render a dynamic report and show it in your django template? why don't you render it to a HTML + CSS file, save it into your server and display it in your django environment? – dot.Py Jun 29 '16 at 11:23
  • 1
    i dont want to save it nor display it, its a downloadable config template for a router thats created on the fly from data stored in the database. – AlexW Jun 29 '16 at 11:24
  • 1
    @AlexW : You should use `StringIO.StringIO()` to make a new object. Right now `config_file` is pointing to the module, not an object of type `StringIO`. – wildwilhelm Jun 29 '16 at 11:47

2 Answers2

1

In general, file-like objects are just things that act like files in some way (typically by implementing read and/or write). I like the answer given to Check if object is file-like in Python.

So, in your case, it seems it's enough if your config_file object is of a type that is not derived from a string type, and that implements the write method. You can verify this with a quick peek at the source of jinja2.TemplateStream.dump.

Community
  • 1
  • 1
wildwilhelm
  • 4,809
  • 1
  • 19
  • 24
  • not quite sure i understand, what type should i set config_file to? – AlexW Jun 29 '16 at 11:41
  • 1
    You can use any type that implements `write` (or define your own class with a `write` method). The right choice will depend on what you're trying to do. For getting started, you might start testing with [StringIO](https://docs.python.org/2/library/stringio.html). – wildwilhelm Jun 29 '16 at 11:43
1

I've never used jinja2, but I suspect you can use a StringIO to get the desired content as a string.

The following ought to work, but I can't (easily) test it...

from StringIO import StringIO

env = Environment(autoescape=False, optimized=False)

sio = StringIO()

device_config = env.from_string(config_template.config)
device_config.stream(
  STR         = site.location.upper()[:4],
).dump(sio)

content = sio.getvalue()

However, if you just want a string, it looks like you should use the render() method instead...

env = Environment(autoescape=False, optimized=False)

device_config = env.from_string(config_template.config)
content = device_config.render(
  STR         = site.location.upper()[:4],
)

...but, again, I can't easily test it.

Aya
  • 39,884
  • 6
  • 55
  • 55