Mercurial > repos > davidmurphy > codonlogo
comparison corebio/_future/__init__.py @ 7:8d676bbd1f2d
Uploaded
author | davidmurphy |
---|---|
date | Mon, 16 Jan 2012 07:03:36 -0500 |
parents | c55bdc2fb9fa |
children |
comparison
equal
deleted
inserted
replaced
6:4a4aca3d57c9 | 7:8d676bbd1f2d |
---|---|
1 | |
2 """ | |
3 Private compatability module for running under python version 2.3. | |
4 | |
5 Replacement for | |
6 o string.Template -- introduced in python 2.4 | |
7 o subprocess -- introduced in python 2.4 | |
8 | |
9 o resource_string -- introduced in pkg_resource of setuptools | |
10 o resource_stream | |
11 o resource_filename | |
12 | |
13 from string import Template -> from corebio._future import Template | |
14 | |
15 """ | |
16 | |
17 | |
18 try : | |
19 import pkg_resources | |
20 except ImportError : | |
21 pkg_resources = None | |
22 | |
23 | |
24 try : | |
25 from string import Template | |
26 except ImportError : | |
27 from _string import Template | |
28 | |
29 | |
30 | |
31 | |
32 def resource_string( modulename, resource, basefilename = None): | |
33 """Locate and return a resource as a string. | |
34 >>> f = resource_string( __name, 'somedatafile', __file__) | |
35 """ | |
36 if pkg_resources : | |
37 return pkg_resources.resource_string(modulename, resource) | |
38 | |
39 f = resource_stream( modulename, resource, basefilename) | |
40 return f.read() | |
41 | |
42 def resource_stream( modulename, resource, basefilename = None): | |
43 """Locate and return a resource as a stream. | |
44 >>> f = resource_stream( __name__, 'somedatafile', __file__) | |
45 """ | |
46 if pkg_resources : | |
47 return pkg_resources.resource_stream(modulename, resource) | |
48 | |
49 return open( resource_filename( modulename, resource, basefilename) ) | |
50 | |
51 def resource_filename( modulename, resource, basefilename = None): | |
52 """Locate and return a resource filename. | |
53 >>> f = resource_stream( __name__, 'somedatafile', __file__) | |
54 | |
55 A resource is a data file stored with the python code in a package. | |
56 All three resource methods (resource_string, resource_stream, | |
57 resource_filename) call the corresponding methods in the 'pkg_resources' | |
58 module, if installed. Otherwise, we resort to locating the resource | |
59 in the local filesystem. However, this does not work if the package | |
60 is located inside a zip file. | |
61 """ | |
62 if pkg_resources : | |
63 return pkg_resources.resource_filename(modulename, resource) | |
64 | |
65 if basefilename is None : | |
66 raise NotImplementedError( | |
67 "Require either basefilename or pkg_resources") | |
68 | |
69 import os | |
70 return os.path.join(os.path.dirname(basefilename), resource) | |
71 | |
72 | |
73 | |
74 | |
75 | |
76 | |
77 | |
78 |