Adding a File Importer

Caution

This section is under development

These are the basic steps to add a new file format importer to NeXpy.

  1. Create a Python source code module named readabcde.py where abcde is the name of the new support and does not conflict with existing names.

  2. Place that file in the source code tree, in path <nexpy>/src/nexpy/readers/

  3. Inside that file, create several required structures as described below.

  4. Create other structure as necessary to support the reader.

  5. Provide an example data file (or files) in the <nexpy>/src/nexpy/examples/ directory and update the README.rst file there, describing the new example(s).

Note

All new file format importers must be placed in the NeXpy source code tree in the readers subdirectory.

Required Content

Start with this basic template:

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 
 4 """describe this importer"""
 5 
 6 from nexpy.api.nexus import *
 7 from nexpy.gui.importdialog import NXImportDialog
 8 
 9 filetype = 'my file format'   # these words go in the import menu
10 
11 class ImportDialog(NXImportDialog):
12     """Dialog to import my file format"""
13 
14     def __init__(self, parent=None):
15         super(ImportDialog, self).__init__(parent)
16 
17         self.accepted = False
18         self.import_file = None     # must set in self.get_data()
19 
20         self.set_layout(self.filebox(), self.progress_layout(save=True))
21 
22         self.set_title(f"Import {filetype}")
23 
24     def get_data(self):
25        """Read the data and return either a NXroot or NXentry group"""
26        # MUST define self.import_file as chosen file name
27        # use convenience method to get from dialog widget
28        self.import_file = self.get_filename()
29 
30        x = range(0,10)     # example data
31        y = range(1,11)
32        return NXroot(NXentry(NXdata(y,x)))

About the GUI layout

Each importer needs to layout the GUI buttons in class ImportDialog(NXImportDialog) necessary for defining the imported file and its attributes and the single module, get_data(), which returns either an NXroot or NXentry object. This will be added to the NeXpy tree.

Features from the Superclass

Three GUI convenience elements are provided from the superclass nexpy.gui.importdialog.NXImportDialog:

ImportDialog.filebox: Contains a “Choose File” button and a text box. Both can be used to set the path to the imported file. This can be retrieved as a string using self.get_filename().

ImportDialog.close_buttons: Contains a “Cancel” and “OK” button to close the dialog. This should be placed at the bottom of all import dialogs.