spec2nexus.plugin

An extensible plug-in architecture is used to handle the different possible control line control lines (such as #F, #E, #S, …) in a SPEC data file.

A SPEC control line provides metadata about the SPEC scan or SPEC data file.

Plugins can be used to parse or ignore certain control lines in SPEC data files. Through this architecture, it is possible to support custom control lines, such as #U (SPEC standard control line for any user data). One example is support for the UNICAT-style of metadata provided in the scan header.

Plugins are now used to handle all control lines in spec2nexus.spec. Any control line encountered but not recognized will be placed as text in a NeXus NXnote group named unrecognized_NNN (where NNN is from 1 to the maximum number of unrecognized control lines).

Supplied spec plugin modules

These plugin modules are supplied:

spec2nexus.plugins.spec_common
spec2nexus.plugins.fallback
spec2nexus.plugins.apstools_specwriter
spec2nexus.plugins.unicat
spec2nexus.plugins.uim
spec2nexus.plugins.uxml
spec2nexus.plugins.XPCS

Writing a custom plugin

While spec2nexus provides a comprehensive set of plugins to handle the common SPEC control line control lines, custom control lines are used at many facilities to write additional scan data and scan metadata into the SPEC data file. Custom plugins are written to process these additions.

Overview of the supplied spec plugins

Plugins for these control lines [1] are provided in spec2nexus:

spec2nexus.plugins.spec_common.SPEC_File
spec2nexus.plugins.spec_common.SPEC_Epoch
spec2nexus.plugins.spec_common.SPEC_Date
spec2nexus.plugins.spec_common.SPEC_Comment
spec2nexus.plugins.spec_common.SPEC_Geometry
spec2nexus.plugins.spec_common.SPEC_NormalizingFactor
spec2nexus.plugins.spec_common.SPEC_CounterNames
spec2nexus.plugins.spec_common.SPEC_CounterMnemonics
spec2nexus.plugins.spec_common.SPEC_Labels
spec2nexus.plugins.spec_common.SPEC_Monitor
spec2nexus.plugins.spec_common.SPEC_NumColumns
spec2nexus.plugins.spec_common.SPEC_PositionerNames
spec2nexus.plugins.spec_common.SPEC_PositionerMnemonics
spec2nexus.plugins.spec_common.SPEC_Positioners
spec2nexus.plugins.spec_common.SPEC_HKL
spec2nexus.plugins.spec_common.SPEC_Scan
spec2nexus.plugins.spec_common.SPEC_CountTime
spec2nexus.plugins.spec_common.SPEC_UserReserved
spec2nexus.plugins.spec_common.SPEC_TemperatureSetPoint
spec2nexus.plugins.spec_common.SPEC_DataLine
spec2nexus.plugins.spec_common.SPEC_MCA
spec2nexus.plugins.spec_common.SPEC_MCA_Array
spec2nexus.plugins.spec_common.SPEC_MCA_Calibration
spec2nexus.plugins.spec_common.SPEC_MCA_ChannelInformation
spec2nexus.plugins.spec_common.SPEC_MCA_CountTime
spec2nexus.plugins.spec_common.SPEC_MCA_RegionOfInterest
spec2nexus.plugins.fallback.UnrecognizedControlLine
spec2nexus.plugins.unicat.UNICAT_MetadataMnemonics
spec2nexus.plugins.unicat.UNICAT_MetadataValues
spec2nexus.plugins.uim.UIM_generic
spec2nexus.plugins.XPCS.XPCS_VA
spec2nexus.plugins.XPCS.XPCS_VD
spec2nexus.plugins.XPCS.XPCS_VE
[1]Compare this list with Control lines (keys) defined by SPEC

source code documentation

define the plug-in architecture

Use spec2nexus.plugin.ControlLineHandler as a metaclass to create a plugin handler class for each SPEC control line. In each such class, it is necessary to:

  • define a string value for the key (class attribute)
  • override the definition of process()

It is optional to:

  • define postprocess()
  • define writer()
  • define match_key()

Classes

ControlLineHandler base class for SPEC data file control line handler plugins
PluginManager() Manage the set of SPEC data file control line plugins

Exceptions

DuplicateControlLineKey This control line key regular expression has been used more than once.
DuplicateControlLinePlugin This control line handler has been used more than once.
DuplicatePlugin This plugin file name has been used more than once.
PluginBadKeyError The plugin ‘key’ value is not acceptable.
PluginDuplicateKeyError This plugin key has been used before.
PluginKeyNotDefined Must define ‘key’ in class declaration.
PluginProcessMethodNotDefined Must define ‘process()’ method in class declaration.
class spec2nexus.plugin.AutoRegister(*args)[source]

plugin to handle a single control line in a SPEC data file

This class is a metaclass to auto-register plugins to handle various parts of a SPEC data file. See spec_common for many examples.

Parameters:key (str) – regular expression to match a control line key, up to the first space
Returns:None
class spec2nexus.plugin.ControlLineHandler[source]

base class for SPEC data file control line handler plugins

define one ControlLineHandler class for each different type of control line

Parameters:
  • key (str) – regular expression to match a control line key, up to the first space
  • scan_attributes_defined ([str]) – list of scan attributes defined in this class
Returns:

None

EXAMPLE of match_key method:

Declaration of the match_key method is optional in a subclass. This is used to test a given line from a SPEC data file against the key of each ControlLineHandler.

If this method is defined in the subclass, it will be called instead of match_key(). This is the example used by SPEC_DataLine:

def match_key(self, text):
    try:
        float( text.strip().split()[0] )
        return True
    except ValueError:
        return False
postprocess(header, *args, **kws)[source]

optional: additional processing deferred until after data file has been read

process(text, spec_file_obj, *args, **kws)[source]

required: handle this line from a SPEC data file

writer(h5parent, writer, scan, nxclass=None, *args, **kws)[source]

optional: Describe how to store this data in an HDF5 NeXus file

exception spec2nexus.plugin.DuplicateControlLineKey[source]

This control line key regular expression has been used more than once.

exception spec2nexus.plugin.DuplicateControlLinePlugin[source]

This control line handler has been used more than once.

exception spec2nexus.plugin.DuplicatePlugin[source]

This plugin file name has been used more than once.

exception spec2nexus.plugin.PluginBadKeyError[source]

The plugin ‘key’ value is not acceptable.

exception spec2nexus.plugin.PluginDuplicateKeyError[source]

This plugin key has been used before.

exception spec2nexus.plugin.PluginException[source]

parent exception for this module

exception spec2nexus.plugin.PluginKeyNotDefined[source]

Must define ‘key’ in class declaration.

class spec2nexus.plugin.PluginManager[source]

Manage the set of SPEC data file control line plugins

Class Methods

get(key) return the handler identified by key or None
getKey(spec_data_file_line) Find the key that matches this line in a SPEC data file.
load_plugins() load all spec2nexus plugin modules
match_key(text) test if any handler’s key matches text
process(key, *args, **kw) pick the control line handler by key and call its process() method
register_control_line_handler(handler) auto-registry of all AutoRegister plugins
get(key)[source]

return the handler identified by key or None

getKey(spec_data_file_line)[source]

Find the key that matches this line in a SPEC data file. Return None if not found.

Parameters:spec_data_file_line (str) – one line from a SPEC data file
load_plugins()[source]

load all spec2nexus plugin modules

called from spec2nexus.plugin.get_plugin_manager()

match_key(text)[source]

test if any handler’s key matches text

Parameters:text (str) – first word on the line, up to but not including the first whitespace
Returns:key or None

Applies a regular expression match using each handler’s key as the regular expression to match with text.

process(key, *args, **kw)[source]

pick the control line handler by key and call its process() method

register_control_line_handler(handler)[source]

auto-registry of all AutoRegister plugins

Called from AutoRegister.__init__

exception spec2nexus.plugin.PluginProcessMethodNotDefined[source]

Must define ‘process()’ method in class declaration.

spec2nexus.plugin.get_plugin_manager()[source]

get the instance of the plugin_manager (a singleton)

Create instance of PluginManager() if necessary. Also,