use pkg_resources to access bundled files

* egg file can remain zipped
* see http://peak.telecommunity.com/DevCenter/PythonEggs#accessing-package-resources
  for details
This commit is contained in:
Oliver Beckstein
2013-07-25 11:28:56 -07:00
parent b928c18bab
commit 7008ed9f24
4 changed files with 31 additions and 30 deletions

View File

@@ -4,6 +4,7 @@ from __future__ import print_function
import pickle,sys,os,math,propka.calculations
import pkg_resources
class bondmaker:
def __init__(self):
@@ -26,8 +27,7 @@ class bondmaker:
self.max_sq_distance = max(list(self.distances_squared.values())+[self.default_dist_squared])
# protein bonding data
path = os.path.split(__file__)[0]
self.data_file_name = os.path.join(path,'protein_bonds.dat')
self.data_file_name = pkg_resources.resource_filename(__name__, 'protein_bonds.dat')
data = open(self.data_file_name,'rb')
self.protein_bonds = pickle.load(data)

View File

@@ -1,23 +1,24 @@
#!/usr/bin/python
from __future__ import division
from __future__ import print_function
import string, sys, copy, math, os
import pkg_resources
#
# file I/O
#
def open_file_for_reading(filename):
if not os.path.isfile(filename):
try:
f = open(filename,'r')
except:
raise Exception('Cannot find file %s' %filename)
return open(filename,'r')
return f
def open_file_for_writing(filename):
res = open(filename,'w')
if not res:
try:
res = open(filename,'w')
except:
raise Exception('Could not open %s'%filename)
return res
@@ -102,8 +103,8 @@ def loadOptions():
help="specifying mutation labels which is used to modify <filename> according to, e.g. N25R/N181D")
parser.add_option("-v", "--version", dest="version_label", default="Jan15",
help="specifying the sub-version of propka [Jan15/Dec19]")
parser.add_option("-p", "--parameters",dest="parameters", default="propka.cfg",
help="set the parameter file")
parser.add_option("-p", "--parameters",dest="parameters", default=pkg_resources.resource_filename(__name__, "propka.cfg"),
help="set the parameter file [%default]")
parser.add_option("-z", "--verbose", dest="verbose", action="store_true", default=True,
help="sleep during calculations")
parser.add_option("-q", "--quiet", dest="verbose", action="store_false",

View File

@@ -6,6 +6,7 @@ import math
import propka.lib as lib
import sys, os
import pkg_resources
# names and types of all key words in configuration file
matrices = ['interaction_matrix']
@@ -57,8 +58,7 @@ class Parameters:
def read_parameters(self, file):
# try to locate the parameters file
try:
path = os.path.dirname(__file__)
ifile = os.path.join(path,'../'+file)
ifile = pkg_resources.resource_filename(__name__, file)
input = lib.open_file_for_reading(ifile)
except:
input = lib.open_file_for_reading(file)

View File

@@ -47,5 +47,5 @@ using the tutorial http://propka.ki.ku.dk/~luca/wiki/index.php/PROPKA_3.1_Tutori
'propka31 = propka.run:main',
],
},
zip_safe=False,
zip_safe=True,
)