commit | author | age
|
c0c7b5
|
1 |
import os |
CM |
2 |
import pkg_resources |
|
3 |
|
e6c2d2
|
4 |
from pyramid.compat import string_types |
CM |
5 |
|
0c1c39
|
6 |
from pyramid.path import ( |
CM |
7 |
package_path, |
|
8 |
package_name, |
|
9 |
) |
c0c7b5
|
10 |
|
CM |
11 |
def resolve_asset_spec(spec, pname='__main__'): |
e6c2d2
|
12 |
if pname and not isinstance(pname, string_types): |
c0c7b5
|
13 |
pname = pname.__name__ # as package |
CM |
14 |
if os.path.isabs(spec): |
|
15 |
return None, spec |
|
16 |
filename = spec |
|
17 |
if ':' in spec: |
|
18 |
pname, filename = spec.split(':', 1) |
|
19 |
elif pname is None: |
|
20 |
pname, filename = None, spec |
|
21 |
return pname, filename |
|
22 |
|
|
23 |
def asset_spec_from_abspath(abspath, package): |
|
24 |
""" Try to convert an absolute path to a resource in a package to |
|
25 |
a resource specification if possible; otherwise return the |
|
26 |
absolute path. """ |
|
27 |
if getattr(package, '__name__', None) == '__main__': |
|
28 |
return abspath |
|
29 |
pp = package_path(package) + os.path.sep |
|
30 |
if abspath.startswith(pp): |
|
31 |
relpath = abspath[len(pp):] |
|
32 |
return '%s:%s' % (package_name(package), |
|
33 |
relpath.replace(os.path.sep, '/')) |
|
34 |
return abspath |
cdc6d4
|
35 |
|
732c74
|
36 |
# bw compat only; use pyramid.path.AssetResolver().resolve(spec).abspath() |
c0c7b5
|
37 |
def abspath_from_asset_spec(spec, pname='__main__'): |
CM |
38 |
if pname is None: |
|
39 |
return spec |
|
40 |
pname, filename = resolve_asset_spec(spec, pname) |
|
41 |
if pname is None: |
|
42 |
return filename |
|
43 |
return pkg_resources.resource_filename(pname, filename) |
56df90
|
44 |
|