Commit 361db168 authored by peastman's avatar peastman
Browse files

Defined entry point so other packages can add force field directories

parent 58c63b05
...@@ -46,6 +46,13 @@ import simtk.unit as unit ...@@ -46,6 +46,13 @@ import simtk.unit as unit
from . import element as elem from . import element as elem
from simtk.openmm.app import Topology from simtk.openmm.app import Topology
from simtk.openmm.app.internal.singleton import Singleton from simtk.openmm.app.internal.singleton import Singleton
from pkg_resources import iter_entry_points
# Directories from which to load built in force fields.
_dataDirectories = [os.path.join(os.path.dirname(__file__), 'data')]
for entry in iter_entry_points(group='openmm.forcefielddir'):
_dataDirectories.append(entry.load()())
def _convertParameterToNumber(param): def _convertParameterToNumber(param):
if unit.is_quantity(param): if unit.is_quantity(param):
...@@ -186,11 +193,16 @@ class ForceField(object): ...@@ -186,11 +193,16 @@ class ForceField(object):
trees = [] trees = []
for file in files: for file in files:
tree = None
try: try:
# this handles either filenames or open file-like objects # this handles either filenames or open file-like objects
tree = etree.parse(file) tree = etree.parse(file)
except IOError: except IOError:
tree = etree.parse(os.path.join(os.path.dirname(__file__), 'data', file)) for dataDir in _dataDirectories:
f = os.path.join(dataDir, file)
if os.path.isfile(f):
tree = etree.parse(f)
break
except Exception as e: except Exception as e:
# Fail with an error message about which file could not be read. # Fail with an error message about which file could not be read.
# TODO: Also handle case where fallback to 'data' directory encounters problems, # TODO: Also handle case where fallback to 'data' directory encounters problems,
...@@ -202,6 +214,8 @@ class ForceField(object): ...@@ -202,6 +214,8 @@ class ForceField(object):
filename = str(file) filename = str(file)
msg += "ForceField.loadFile() encountered an error reading file '%s'\n" % filename msg += "ForceField.loadFile() encountered an error reading file '%s'\n" % filename
raise Exception(msg) raise Exception(msg)
if tree is None:
raise ValueError('Could not locate file "%s"' % file)
trees.append(tree) trees.append(tree)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment