annotate corebio/_future/__init__.py @ 0:c55bdc2fb9fa

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