Skip to content

Commit

Permalink
Update to chemfiles 0.10.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Luthaf committed Jul 23, 2021
1 parent 377e85c commit 2b41c92
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
5 changes: 3 additions & 2 deletions chemfiles/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# -*- coding=utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

from .misc import ChemfilesError, set_warnings_callback, add_configuration, formats_list
from .misc import ChemfilesError, set_warnings_callback, add_configuration
from .misc import formats_list, guess_format
from .atom import Atom
from .residue import Residue
from .topology import Topology, BondOrder
Expand All @@ -11,4 +12,4 @@
from .selection import Selection
from .property import Property

__version__ = "0.10.0"
__version__ = "0.10.1"
5 changes: 5 additions & 0 deletions chemfiles/ffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ def set_interface(c_lib):
c_lib.chfl_formats_list.restype = chfl_status
c_lib.chfl_formats_list.errcheck = _check_return_code

# Function "chfl_guess_format", at misc.h:93
c_lib.chfl_guess_format.argtypes = [c_char_p, c_char_p, c_uint64]
c_lib.chfl_guess_format.restype = chfl_status
c_lib.chfl_guess_format.errcheck = _check_return_code

# Function "chfl_property_bool", at property.h:37
c_lib.chfl_property_bool.argtypes = [c_bool]
c_lib.chfl_property_bool.restype = POINTER(CHFL_PROPERTY)
Expand Down
38 changes: 29 additions & 9 deletions chemfiles/misc.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# -*- coding=utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import warnings
import sys

import ctypes
from ctypes import util
from ctypes import c_uint64, POINTER
from ctypes import c_uint64, POINTER, create_string_buffer

from .clib import _get_c_library

Expand Down Expand Up @@ -154,10 +151,10 @@ def formats_list():

def set_warnings_callback(function):
"""
Call `function` on every warning event. The callback should take a string
Call ``function`` on every warning event. The callback should take a string
message and return nothing.
By default, warnings are send to python `warnings` module.
By default, warnings are send to python ``warnings`` module.
"""
from .ffi import chfl_warning_callback

Expand All @@ -178,9 +175,9 @@ def add_configuration(path):
"""
Read configuration data from the file at ``path``.
By default, chemfiles reads configuration from any file name `.chemfilesrc`
in the current directory or any parent directory. This function can be used
to add data from another configuration file.
By default, chemfiles reads configuration from any file named
``.chemfilesrc`` in the current directory or any parent directory. This
function can be used to add data from another configuration file.
This function will fail if there is no file at ``path``, or if the file is
incorrectly formatted. Data from the new configuration file will overwrite
Expand All @@ -205,3 +202,26 @@ def _set_default_warning_callback():
# adaptor => C++ code => Python binding => user code
lambda message: warnings.warn(message, ChemfilesWarning, stacklevel=4)
)


def guess_format(path):
"""
Get the format that chemfiles would use to read a file at the given
``path``.
The format is mostly guessed from the path extension, chemfiles only tries
to read the file to distinguish between CIF and mmCIF files. Opening the
file using the returned format string might still fail. For example, it will
fail if the file is not actually formatted according to the guessed format;
or the format/compression combination is not supported (e.g. ``XTC / GZ``
will not work since the XTC reader does not support compressed files).
The returned format is represented in a way compatible with the various
``Trajectory`` constructors, i.e. ``"<format name> [/ <compression>]"``,
where compression is optional.
"""
lib = _get_c_library()

buffer = create_string_buffer(b"\0", 128)
lib.chfl_guess_format(path.encode("utf8"), buffer, 128)
return buffer.value.decode("utf8")
2 changes: 1 addition & 1 deletion lib
Submodule lib updated 61 files
+2 −1 .codecov.yml
+211 −0 .github/workflows/tests.yml
+0 −153 .travis.yml
+28 −7 CHANGELOG.md
+7 −4 CMakeLists.txt
+1 −1 VERSION
+8 −15 cmake/CompilerFlags.cmake
+3 −2 doc/requirements.txt
+2 −0 doc/src/capi/misc.rst
+2 −0 doc/src/classes/misc.rst
+4 −2 doc/src/devdoc/format.rst
+0 −1 doc/src/formats.rst
+19 −8 doc/src/installation.rst
+8 −0 doc/src/properties/frame.toml
+4 −11 include/chemfiles/FormatFactory.hpp
+15 −16 include/chemfiles/Trajectory.hpp
+22 −0 include/chemfiles/capi/misc.h
+14 −2 include/chemfiles/files/NcFile.hpp
+14 −3 include/chemfiles/formats/AmberNetCDF.hpp
+48 −0 include/chemfiles/formats/LAMMPSTrajectory.hpp
+0 −2 include/chemfiles/formats/Molfile.hpp
+1 −3 include/chemfiles/formats/mmCIF.hpp
+21 −0 include/chemfiles/misc.hpp
+0 −116 scripts/ci/setup-travis.sh
+10 −20 src/FormatFactory.cpp
+17 −48 src/Trajectory.cpp
+19 −0 src/capi/misc.cpp
+29 −1 src/files/NcFile.cpp
+214 −86 src/formats/AmberNetCDF.cpp
+6 −6 src/formats/CIF.cpp
+30 −23 src/formats/GRO.cpp
+1 −1 src/formats/LAMMPSData.cpp
+730 −0 src/formats/LAMMPSTrajectory.cpp
+56 −40 src/formats/MOL2.cpp
+0 −22 src/formats/Molfile.cpp
+83 −108 src/formats/SDF.cpp
+61 −71 src/formats/Tinker.cpp
+18 −14 src/formats/XYZ.cpp
+20 −43 src/formats/mmCIF.cpp
+120 −0 src/guess_format.cpp
+2 −2 tests/CMakeLists.txt
+17 −0 tests/capi/misc.cpp
+18 −0 tests/doc/capi/chfl_guess_format.c
+17 −0 tests/doc/guess_format.cpp
+3 −3 tests/external/helpers.cpp
+1 −1 tests/external/helpers.hpp
+21 −7 tests/factory.cpp
+85 −29 tests/files/netcdf-file.cpp
+6 −0 tests/formats/amber-netcdf.cpp
+97 −0 tests/formats/amber-restart.cpp
+16 −4 tests/formats/cif.cpp
+14 −2 tests/formats/gro.cpp
+4 −1 tests/formats/lammps-data.cpp
+0 −72 tests/formats/lammps-molfile.cpp
+632 −0 tests/formats/lammps-trajectory.cpp
+8 −15 tests/formats/mmcif.cpp
+1 −1 tests/formats/mmtf.cpp
+5 −10 tests/formats/sdf.cpp
+1 −0 tests/lints/check-capi-docs.py
+3 −1 tests/lints/check-error-messages.py
+18 −4 tests/trajectory.cpp
12 changes: 12 additions & 0 deletions tests/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ def __exit__(self, *args):
remove_warnings = RemoveChemfilesWarnings()


class TestVersion(unittest.TestCase):
def test_version(self):
version = chemfiles.clib._get_c_library().chfl_version()
self.assertEqual(chemfiles.__version__, version.decode("utf8"))


class TestErrors(unittest.TestCase):
def test_last_error(self):
chemfiles.misc._clear_errors()
Expand Down Expand Up @@ -101,5 +107,11 @@ def test_format_list(self):
self.assertEqual(xyz.bonds, False)


class TestGuessFormat(unittest.TestCase):
def test_guess_format(self):
self.assertEqual(chemfiles.guess_format("test.xtc.gz"), "XTC / GZ")
self.assertEqual(chemfiles.guess_format("test.nc"), "Amber NetCDF")


if __name__ == "__main__":
unittest.main()

0 comments on commit 2b41c92

Please sign in to comment.