Skip to content

Commit

Permalink
Merge 0e556e4 into 2c26a11
Browse files Browse the repository at this point in the history
  • Loading branch information
prjemian committed Feb 25, 2022
2 parents 2c26a11 + 0e556e4 commit d0e65c1
Show file tree
Hide file tree
Showing 18 changed files with 87 additions and 167 deletions.
7 changes: 2 additions & 5 deletions conda-recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@ requirements:
- setuptools
- h5py
- lxml
- matplotlib
- matplotlib-base
- numpy
- six

run:
- h5py
- lxml
- matplotlib
- matplotlib-base
- numpy
- six

test:

Expand All @@ -50,7 +48,6 @@ test:

commands:
- spec2nexus --help
- python run_test.py

extra:
recipe-maintainers:
Expand Down
4 changes: 1 addition & 3 deletions docs/source/_static/pv_plugin.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from collections import OrderedDict
import six
from spec2nexus.plugin import AutoRegister
from spec2nexus.plugin import ControlLineHandler
from spec2nexus.utils import strip_first_word

@six.add_metaclass(AutoRegister)
class PV_ControlLine(ControlLineHandler):
class PV_ControlLine(ControlLineHandler, metaclass=AutoRegister):
'''**#PV** -- EPICS PV associates mnemonic with PV'''

key = '#PV'
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,4 @@
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# http://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#confval-autodoc_mock_imports
autodoc_mock_imports = "h5py numpy matplotlib pyRestTable".split()
autodoc_mock_imports = "h5py numpy matplotlib".split()
46 changes: 16 additions & 30 deletions docs/source/how_to_write_plugin.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,18 @@ plugin manager to automatically register your new plugin.
Write a plugin module
*********************

Give the custom plugin module a name ending with ``.py``.
As with any Python module, the name must be unique within a directory.
If the plugin is not in your working directory,
there must be a ``__init__.py`` file in the same directory (even if
that file is empty) so that your plugin module can be loaded with ``import <MODULE>``.

**Plugin module setup**

.. sidebar:: The ``six`` package

The ``six`` package is used to make our plugins run with either
Python 2.7 or Python 3.5+.

Please view the existing plugins in :mod:`~spec2nexus.plugins.spec_common`
for examples. The custom plugin module should contain, at minimum one subclass of
:class:`spec2nexus.plugin.ControlLineHandler` which is decorated
with ``@six.add_metaclass(spec2nexus.plugin.AutoRegister)``.
The ``add_metaclass`` decorator allows our custom ControlLineHandlers
to register themselves when their module is imported.
A custom plugin module can contain many such handlers, as needs dictate.
Give the custom plugin module a name ending with ``.py``. As with any Python
module, the name must be unique within a directory. If the plugin is not in your
working directory, there must be a ``__init__.py`` file in the same directory
(even if that file is empty) so that your plugin module can be loaded with
``import <MODULE>``.

Please view the existing plugins in :mod:`~spec2nexus.plugins.spec_common` for
examples. The custom plugin module should contain, at minimum one subclass of
:class:`spec2nexus.plugin.ControlLineHandler` and the keyword argument
``metaclass=AutoRegister``. The ``metaclass`` keyword argument allows our custom
ControlLineHandlers to register themselves when their module is imported. A
custom plugin module can contain many such handlers, as needs dictate.

.. sidebar:: Useful ``import``

Expand All @@ -97,7 +89,6 @@ These imports are necessary to to write plugins for *spec2nexus*:
.. code-block:: python
:linenos:
import six
from spec2nexus.plugin import AutoRegister
from spec2nexus.plugin import ControlLineHandler
from spec2nexus.utils import strip_first_word
Expand Down Expand Up @@ -253,12 +244,10 @@ we create one that ignores processing by doing nothing:
.. code-block:: python
:linenos:
import six
from spec2nexus.plugin import AutoRegister
from spec2nexus.plugin import ControlLineHandler
@six.add_metaclass(AutoRegister)
class Ignore_Y_ControlLine(ControlLineHandler):
class Ignore_Y_ControlLine(ControlLineHandler, metaclass=AutoRegister):
'''
**#Y** -- as in ``#Y 1 2 3 4 5``
Expand Down Expand Up @@ -338,13 +327,11 @@ Gathering all parts of the examples above, the custom plugin module is:
.. code-block:: python
:linenos:
import six
from spec2nexus.plugin import AutoRegister
from spec2nexus.plugin import ControlLineHandler
from spec2nexus.utils import strip_first_word
@six.add_metaclass(AutoRegister)
class User_ControlLine(ControlLineHandler):
class User_ControlLine(ControlLineHandler, metaclass=AutoRegister):
'''**#U** -- User data (#U user1 user2 user3)'''
key = '#U'
Expand All @@ -367,8 +354,7 @@ Gathering all parts of the examples above, the custom plugin module is:
scan.U_sum = sum(scan.U)
@six.add_metaclass(AutoRegister)
class Ignore_Y_ControlLine(ControlLineHandler):
class Ignore_Y_ControlLine(ControlLineHandler, metaclass=AutoRegister):
'''**#Y** -- as in ``#Y 1 2 3 4 5``'''
key = '#Y'
Expand Down Expand Up @@ -503,7 +489,7 @@ Summary Requirements for custom plugin
* for each control line:

* subclass :class:`spec2nexus.plugin.ControlLineHandler`
* add ``@six.add_metaclass(AutoRegister)`` decorator to auto-register the plugin
* add ``metaclass=AutoRegister`` keyword argument to auto-register the plugin
* import the module you defined (FIXME: check this and revise)
* identify the control line pattern
* define ``key`` with a regular expression to match [#]_
Expand Down
42 changes: 19 additions & 23 deletions docs/source/unit_testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,39 @@
Unit Testing
############

Since release 2017.0201.0, this project relies on the Python *unittest* [#]_
package to apply
unit testing [#]_ to the source code. The test code is in the `tests`
directory. Various tests have been developed starting with the *2017.0201.0*
release to provide features or resolve problems reported. The tests are not
yet exhaustive yet the reported code coverage [#]_ is well over 80%.
This project uses the Python *pytest* [#]_ package to apply unit testing [#]_ to
the source code. The test code is in the ``tests/`` directories. Various tests
have been developed starting to provide features or resolve problems reported.

The unit tests are implemented in a standard manner such that independent
review [#]_ can run the tests on this code based on the instructions provided
in a `.travis.yml` configuration file in the project directory.
review [#]_ can run the tests on this code based on the instructions provided
in ``.github/workflows`` configuration files in the project directory.

This command will run the unit tests locally::

python tests
pytest -vvv .

Additional information may be learned with a Python package to run the tests::

coverage run -a tests && coverage report -m
coverage run -m pytest --lf -vvv .

The *coverage* command ([#]_), will run the tests and then prepare a report of
the percentage of the Python source code that has been executed during the
unit tests.
The *coverage* command ([#]_), will run the tests and create a report of the
percentage of the Python source code that has been executed during the unit
tests::

.. note:: The number of lines reported by *coverage* may differ from that
reported by *travis-ci*. The primary reason is that certain tests involving
access to information from GitHub may succeed or not depending on the
"Github API rate limit". [#]_
coverage report --precision 3

.. [#] Python *unittest* package:
https://docs.python.org/2/library/unittest.html
The unit tests on GitHub automatically upload the coverage results to the
*coveralls* [#]_ site [#]_ which tracks coverage over time.

.. [#] unit testing: https://en.wikipedia.org/wiki/Unit_testing
.. [#] Python *pytest* package: https://pytest.org
.. [#] *coveralls* code coverage: https://coveralls.io/github/prjemian/spec2nexus
.. [#] unit testing: https://en.wikipedia.org/wiki/Unit_testing
.. [#] *travis-ci* continuous intregration: https://travis-ci.org/prjemian/spec2nexus
.. [#] GitHub Actions workflow: https://github.com/prjemian/spec2nexus/actions
.. [#] *coverage*: https://coverage.readthedocs.io
.. [#] Github API rate limit: https://developer.github.com/v3/rate_limit/
.. [#] *coveralls*: https://coveralls.io
.. [#] *coveralls* code coverage: https://coveralls.io/github/prjemian/spec2nexus
7 changes: 2 additions & 5 deletions environment.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
name: pyRestTable
name: spec2nexus
channels:
- defaults
- conda-forge
- prjemian
dependencies:
- h5py
- lxml
- matplotlib
- matplotlib-base
- numpy
- pyRestTable
- six
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
versioneer
coverage
coveralls
docopt
versioneer
6 changes: 1 addition & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
coverage
docopt
h5py
lxml
matplotlib
matplotlib-base
numpy
pyRestTable
six
5 changes: 1 addition & 4 deletions src/spec2nexus/eznx.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-


"""
(Easy NeXus) support reading & writing NeXus HDF5 files using h5py
Expand Down Expand Up @@ -59,10 +58,8 @@
"""


import h5py # HDF5 support
import numpy
import six


def makeFile(filename, **attr):
Expand Down Expand Up @@ -140,7 +137,7 @@ def makeDataset(parent, name, data=None, **attr):
# https://stackoverflow.com/questions/23220513/
# [n.encode("ascii", "ignore") for n in data]
def encoder(value):
if isinstance(value, six.string_types):
if isinstance(value, str):
return value.encode("ascii", "ignore")
return value

Expand Down
17 changes: 5 additions & 12 deletions src/spec2nexus/plugins/XPCS.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@
SPEC data file control lines unique to the APS XPCS instrument
"""

import six

from ..eznx import makeGroup
from ..plugin import AutoRegister, ControlLineHandler
from ..spec import SpecDataFileHeader, SpecDataFileScan
from ..utils import strip_first_word


@six.add_metaclass(AutoRegister)
class XPCS_VA(ControlLineHandler):
class XPCS_VA(ControlLineHandler, metaclass=AutoRegister):
"""**#VA**"""

key = r"#VA\d+"
Expand All @@ -43,8 +40,7 @@ def writer(self, h5parent, writer, scan, nxclass=None, *args, **kws):
writer.save_dict(group, dd)


@six.add_metaclass(AutoRegister)
class XPCS_VD(ControlLineHandler):
class XPCS_VD(ControlLineHandler, metaclass=AutoRegister):
"""**#VD** """

key = r"#VD\d+"
Expand All @@ -69,8 +65,7 @@ def writer(self, h5parent, writer, scan, nxclass=None, *args, **kws):
writer.save_dict(group, dd)


@six.add_metaclass(AutoRegister)
class XPCS_VE(ControlLineHandler):
class XPCS_VE(ControlLineHandler, metaclass=AutoRegister):
"""**#VE** """

key = r"#VE\d+"
Expand All @@ -95,8 +90,7 @@ def writer(self, h5parent, writer, scan, nxclass=None, *args, **kws):
writer.save_dict(group, dd)


@six.add_metaclass(AutoRegister)
class XPCS_XPCS(ControlLineHandler):
class XPCS_XPCS(ControlLineHandler, metaclass=AutoRegister):
"""#XPCS"""

key = r"#XPCS"
Expand All @@ -114,8 +108,7 @@ def writer(self, h5parent, writer, scan, nxclass=None, *args, **kws):
pass


@six.add_metaclass(AutoRegister)
class XPCS_CCD(ControlLineHandler):
class XPCS_CCD(ControlLineHandler, metaclass=AutoRegister):
"""#CCD"""

key = r"#CCD"
Expand Down
4 changes: 1 addition & 3 deletions src/spec2nexus/plugins/apstools_specwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@
"""

from collections import OrderedDict
import six

from .. import eznx
from ..plugin import AutoRegister, ControlLineHandler


@six.add_metaclass(AutoRegister)
class MD_apstools(ControlLineHandler):
class MD_apstools(ControlLineHandler, metaclass=AutoRegister):

"""**#MD** -- Bluesky metadata from apstools SpecWriterCallback"""

Expand Down
4 changes: 1 addition & 3 deletions src/spec2nexus/plugins/fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
"""

from collections import OrderedDict
import six

from ..eznx import makeGroup
from ..plugin import AutoRegister, ControlLineHandler
from ..spec import UNRECOGNIZED_KEY, SpecDataFileHeader, SpecDataFileScan


@six.add_metaclass(AutoRegister)
class UnrecognizedControlLine(ControlLineHandler):
class UnrecognizedControlLine(ControlLineHandler, metaclass=AutoRegister):

"""unrecognized control line"""

Expand Down

0 comments on commit d0e65c1

Please sign in to comment.