Newbie question: physical file path from asset specification ?

17 views
Skip to first unread message

Laurent DAVERIO

unread,
May 1, 2013, 12:41:56 PM5/1/13
to pylons-...@googlegroups.com
Hello list,

sorry for asking such a newbie question (and making you work on May Day
;-), but I can't seem to find the answer by myself... :-(


I have a Pyramid app defining a few static routes, e.g. :

> for d in ('css', 'img', 'js'):
> config.add_static_view('%s' % d, 'mypkg:static/%s' % d, cache_max_age=3600)

This allows me to use static assets in my templates, e.g.:

> <img src="${request.static_url('mypkg:static/img/%s' % img_filename}" />


Now, I would like to test in my view if "img_filename" corresponds to an
existing file.

Is there an easy way to get the physical file path from the asset
specification ? Of course, I don't want to hardcode the image directory
path in my code, and I'd also like to avoid having to add a new setting
("image_dir=...") to my development.ini.

Many thanks in advance,


Laurent.

Michael Merickel

unread,
May 1, 2013, 1:28:53 PM5/1/13
to Pylons
This is the purpose of the AssetResolver.

resolver = AssetResolver()
asset = resolver.resolve('mypkg:static/img/%s' % img_filename)
asset.abspath() # -> /foo/bar/static/img/...



--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.
Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.



Laurent DAVERIO

unread,
May 1, 2013, 2:08:32 PM5/1/13
to pylons-...@googlegroups.com
Le 01/05/13 19:28, Michael Merickel a �crit :
> This is the purpose of the AssetResolver.
>
> resolver = AssetResolver()
> asset = resolver.resolve('mypkg:static/img/%s' % img_filename)
> asset.abspath() # -> /foo/bar/static/img/...
>
> http://docs.pylonsproject.org/projects/pyramid/en/1.4-branch/api/path.html#pyramid.path.AssetResolver

Perfect! Thank you very much :-)

Laurent.

Reply all
Reply to author
Forward
0 new messages