sontek/pyramid_webassets — pull request #25 by graffic (2013-08-18 09:59:16+00:00)
Title: Problem with resolve_xxxxx_to_url

# The bug report
## What did I do
I have a bundle in packageA that includes resources from packageB in the form of `packageB:static/resouce.less`

## What happened with that
When dealing with CSS, webassets will filter the url references relative to that less file.

So I got a nice
```
ValueError: Cannot determine url for /Path/to/the/packageB/static/resource.less
```

## What was I expecting
Since I have a static route for `packageB:static`, I expected the resolver to resolve correctly to that route.

# The technical discussion
## resolve_source_to_url
For the less specific case it was clear that `resolve_source_to_url` can know if the item comes from an absolute url or a package. So I changed the method to:

```python
def resolve_source_to_url(self, filepath, item):
request = get_current_request()

if request is not None:
base_path, original_file = self._split_asset_spec(item)
if base_path is not None:
package = item.split(':', 1)[0] + ':'
filepath = filepath.replace(base_path + '/', package)
try:
return request.static_url(filepath)
except ValueError:
pass
return super(PyramidResolver, self).resolve_source_to_url(
filepath,
item
)
```

Not the best patch, I know:
- Adding a '/' by hand
- It overwrites filepath so the value won't be valid for the fallback to super(...)

I know that webassets copy files that are not accessible to `webassets-external`. But that approach won't work for css assets like images, fonts, etc.

## The static_view = True
I feel that all to_url methods use the pyramid url resolver only with full paths because it expects the `static_view` option to be true.

This means that, right now, the only way to get a _pyramid url_ (a url obtained via static_url), is to enable it.

If I don't enable it, and I just put a url in `base_url`, the `request.static_url` will never be called. That is good if you want to say something like:

> Hey!, I've configured my super-duper-magic nginx to serve assets in /static, so please just set everything to /static/whatever-you-need

If I want to say:
> Hmm, I don't know my final url but it will be: wherever_my_app_is/static

Then I'm force to enable static_view. And as seen before, it won't work well with external packages.

## Setting base_url to package:something
WTF! that is a specification for a file, not for a url!

Yes, but as seen before with the `.less` example, it helped to find the url. Pyramid does not only resolve these things to files, but to urls after it has been added to a `static_view`.

If I'm able to set base_url to a `package:something` definition:
- I can have my own static_view definition without the need to tell pyramid_webassets to create one for me
- resolve_output_to_url needs to be changed in a similar way to understand that an item in the form of package:something needs to change the `filepath`

# Summary
If this makes sense, I'd like to implement the following
- If static_view is false and base_url is a path or a url, no pyramid resolver will be called. (Like now)
- If static_view is false and base_url contains a package definition, pyramid resolver will be used and you need to add a static view by yourself.
- If static_view is true, your assets need to be specified only with a path.
- If you include assets from other packages, you need to define a static_view to access them and declare them in the bundle with `package:path`

Uf! I hope I was clear enough :) And thank you guys for this awesome package :smile_cat: