Remove context.py
Our context.py is currently our approach to locate installed data files (test data files, basis sets, ...), which we install nicely in the FHS-designated $PREFIX/share directory. (See http://www.pathname.com/fhs/) This has a few problems though:
- The solution is non-modular. When we start to split of packages, every single one will have a
context.py(if the package has data files). - It duplicates some functionality already present in Python (and setuptools) to access installed data files.
- It does not work on
.egginstallations.
There are a few alternatives:
- Bad solution, similar to
context.py: use__file__to locate data files. - Very basic: https://docs.python.org/3/library/pkgutil.html#pkgutil.get_data This just loads some file and returns a binary string with the content of the file.
- Very elaborate but robust, and adds a dependency to setuptools: http://setuptools.readthedocs.io/en/latest/setuptools.html#accessing-data-files-at-runtime
I've gained some more experience with this. The setuptools option works very well. These are the required steps:
- Put all files that HORTON or its tests need inside the package directory, e.g. in a
datasubdirectory. - Include all these files in
MANIFEST.in - Replace
distutilstosetuptoolsinsetup.py - Replace
Cython.DistutilsbyCython.Buildinsetup.py - Set
include_package_data=Truewhen calling thesetupfunction. Remove all other references to data files insetup.py. - Remove
my_install_datafromsetup.py - Remove
horton.context - Whenever you need an installed data file in HORTON, use the
pkg_resourcesmodule, instead ofhorton.context. The modulepkg_resourcescomes withsetuptools. There are two ways of using it:- Efficient: http://setuptools.readthedocs.io/en/latest/pkg_resources.html#basic-resource-access This is to be preferred inside the HORTON library itself, e.g. in horton.periodic.
- Easier: http://setuptools.readthedocs.io/en/latest/pkg_resources.html#resource-extraction This could be useful when a real file is needed somewhere, e.g. when testing if IOData can recognize the type of file from its name. In general this is OK for tests.
P.S. Depending on setuptools is not a big deal. It is omnipresent these days, e.g installed when you have pip.
A potential drawback is that nosetests does not find the tests when the package is installed as an Egg. The egg can be disabled in setup.py by adding an argument zip_safe=False to the setup() call.