File: readers.rst

package info (click to toggle)
python-nexpy 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,604 kB
  • sloc: python: 14,518; makefile: 12; sh: 1
file content (86 lines) | stat: -rw-r--r-- 2,845 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
Adding a File Importer
======================

.. caution::  This section is under development

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

#. 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.
#. Place that file in the source code tree, in path
   *<nexpy>/src/nexpy/readers/*
#. Inside that file, create several required structures
   as described below.
#. Create other structure as necessary to support the reader.
#. 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:

.. code-block:: python
   :linenos:

   #!/usr/bin/env python 
   # -*- coding: utf-8 -*-
   
   """describe this importer"""
   
   from nexpy.api.nexus import *
   from nexpy.gui.importdialog import NXImportDialog
   
   filetype = 'my file format'   # these words go in the import menu

   class ImportDialog(NXImportDialog):
       """Dialog to import my file format"""
    
       def __init__(self, parent=None):
           super(ImportDialog, self).__init__(parent)

           self.accepted = False
           self.import_file = None     # must set in self.get_data()

           self.set_layout(self.filebox(), self.progress_layout(save=True))
  
           self.set_title(f"Import {filetype}")
 
       def get_data(self):
          """Read the data and return either a NXroot or NXentry group"""
          # MUST define self.import_file as chosen file name
          # use convenience method to get from dialog widget
          self.import_file = self.get_filename()
          
          x = range(0,10)     # example data
          y = range(1,11)
          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 
:mod:`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.