From f865945de8cf2b377ecd966a36103687c18149d9 Mon Sep 17 00:00:00 2001
From: jacques Grelet <Jacques.Grelet@ird.fr>
Date: Fri, 29 Mar 2019 23:41:34 +0000
Subject: [PATCH] to many change

---
 file_extractor.py |   4 +-
 oceano.py         | 104 ++++++++++++++++++++++++++--------------------
 2 files changed, 60 insertions(+), 48 deletions(-)

diff --git a/file_extractor.py b/file_extractor.py
index a27ab70..01e6f00 100644
--- a/file_extractor.py
+++ b/file_extractor.py
@@ -69,7 +69,7 @@ class FileExtractor:
 
         for file in self.fname:
             with fileinput.input(
-                    file.name, openhook=fileinput.hook_encoded("ISO-8859-1")) as f:
+                    file, openhook=fileinput.hook_encoded("ISO-8859-1")) as f:
                 filesRead += 1
                 for line in f:
                     if line[0] == '#' or line[0] == '*':
@@ -123,7 +123,7 @@ class FileExtractor:
 
         for file in self.fname:
             with fileinput.input(
-                    file.name, openhook=fileinput.hook_encoded("ISO-8859-1")) as f:
+                    file, openhook=fileinput.hook_encoded("ISO-8859-1")) as f:
                 for line in f:
                     if f.filelineno() < self.__skip_header + 1:
                         continue
diff --git a/oceano.py b/oceano.py
index 682ea66..faef6a6 100644
--- a/oceano.py
+++ b/oceano.py
@@ -14,6 +14,8 @@ import distutils.util as du
 typeInstrument = {'CTD': ('cnv', 'CNV'), 'XBT': (
     'EDF', 'edf'), 'LADCP': ('lad', 'LAD'), 'TSG': 'COLCOR'}
 ti = typeInstrument  # an alias
+filesBrowsePosition_row = 2
+filesBrowsePosition_column = 1
 
 
 def processArgs():
@@ -22,18 +24,18 @@ def processArgs():
                     following ROSCOP codification at the given column, fill arrays, write header file ',
         usage='\npython oceano.py data/CTD/cnv/dfr2900[1-3].cnv -i CTD -d\n'
         'python oceano.py data/CTD/cnv/dfr2900[1-3].cnv -i CTD -k PRES TEMP PSAL DOX2 DENS\n'
-        'python oceano.py data/CTDcnv/dfr29*.cnv -d\n'
+        'python oceano.py data/CTDcnv/dfr29*.cnv -i CTD -d\n'
         'python oceano.py data/XBT/T7_0000*.EDF -i XBT -k DEPTH TEMP SVEL\n'
         'python oceano.py data/LADCP/*.lad - i LADCP - k DEPTH EWCT NSCT\n'
         ' \n',
         epilog='J. Grelet IRD US191 - March 2019')
     parser.add_argument('-d', '--debug', help='display debug informations',
                         action='store_true')
-    parser.add_argument('--demo', nargs='?',
+    parser.add_argument('--demo', nargs='?', choices=ti.keys(),
                         help='specify the commande line for instrument, eg CTD, XBT, TSG, LADCP')
     parser.add_argument('-c', '--config', help="toml configuration file, (default: %(default)s)",
                         default='tests/test.toml')
-    parser.add_argument('-i', '--instrument', nargs='?',
+    parser.add_argument('-i', '--instrument', nargs='?', choices=ti.keys(),
                         help='specify the instrument that produce files, eg CTD, XBT, TSG, LADCP')
     parser.add_argument('-k', '--key', nargs='+', default=['PRES', 'TEMP', 'PSAL'],
                         help='display dictionary for key(s), (default: %(default)s)')
@@ -61,32 +63,23 @@ def defineGUI():
     # define a frame layout for each instrument (device)
     for d in devices:
         keys = cfg['split'][d.lower()].keys()
-        frameLayout[d] = [* [[sg.Checkbox(k, key=k,
-                                          tooltip='Select the extract the physical parameter {}'.format(k))] for k in keys]]
-    # for d in devices:
-    #    deviceLayout[d] = [sg.Frame(d, frameLayout[d])]
+        # List comprehensions
+        frameLayout[d] = [[sg.Checkbox(k, key=k,
+                                          tooltip='Select the extract the physical parameter {}'.format(k))] for k in keys]
 
     # define GUI layout
-    layout = ([[sg.Text('File(s) to read and convert')],
-               [sg.Multiline(size=(40, 5), key='_IN_'),
-                sg.Input(key='_HIDDEN_', visible=False,
+    layout = ([[sg.Text('File(s) to read and convert')],                              # row 0
+               [sg.Multiline(size=(40, 5), key='_IN_'),                               # row 1, col 0
+                sg.Input(key='_HIDDEN_', visible=False,                               # row 1, col 1
                          enable_events=True),
-                sg.FilesBrowse(key='_HIDDEN_',
-                               tooltip='Choose one or more files',
-                               initial_folder='data/{}'.format(ti[device][0]))],
-               [sg.Combo(list(ti.keys()), enable_events=True, size=(8, 1),
+                sg.FilesBrowse(key='_HIDDEN_', initial_folder=None,                   # row 1, col 2
+                               tooltip='Choose one or more files')],    
+               [sg.Combo(list(ti.keys()), enable_events=True, size=(8, 1),            # row 2
                          key='_COMBO_', tooltip='Select the instrument')],
-               # [[sg.Frame(d, frameLayout[d])] for d in devices],
-               # for d in devices],
-               [sg.Frame('CTD', frameLayout['CTD'], key='_FRAME_CTD', visible=False),
-                sg.Frame('XBT', frameLayout['XBT'],
-                         key='_FRAME_XBT', visible=False),
-                sg.Frame('LADCP', frameLayout['LADCP'],
-                         key='_FRAME_LADCP', visible=False),
-                sg.Frame('TSG', frameLayout['TSG'], key='_FRAME_TSG', visible=False)],
-
-               [sg.OK(), sg.CloseButton('Cancel')]])
-    # [sg.CloseButton('Run'), sg.CloseButton('Cancel')]])
+               [sg.Frame(d, frameLayout[d], key='_FRAME_{:s}'.format(d) , visible=True)          # row 3
+                for d in devices],                                                    
+               [sg.OK(), sg.CloseButton('Cancel')]])                                  # row 4
+               # [sg.CloseButton('Run'), sg.CloseButton('Cancel')]])
 
     # create a local instance windows used to reload the saved config from file
     window = sg.Window('Oceano converter').Layout(layout)
@@ -95,11 +88,16 @@ def defineGUI():
     return window
 
 
-def updateFilesBrowseCombo(extentions):
-    e = window.Rows[1][2]   # hardcoded
-    e.FileTypes = []      # init to empty list
+def updateFilesBrowseCombo(extentions, x, y):
+    '''# special function used to update the FilesBrowseCombo with canvas poisition
+    # instead of key because the same key is assign to shadow input object
+    '''
+    e = window.Rows[x][y]    # hardcoded
+    e.FileTypes = []         # init to empty list
     for ext in extentions:
         e.FileTypes.append(("{} files".format(ext), "*.{}".format(ext)))
+
+    e.initial_folder = 'data/{}'.format(extentions[0])
     window.Finalize
 
 
@@ -159,12 +157,6 @@ if __name__ == "__main__":
     cfg = toml.load(args.config)
     # this the select device from command line !
     device = str(args.instrument)  # convert one element list to str
-    if device == 'None':
-        print('Missing option --key or -k, key = {}'.format(device))
-        print('usage:')
-        print(parser.usage)
-        sys.exit(0)
-    keys = cfg['split'][device.lower()].keys()
 
     # test arguements from sys.argv, args is never to None with default option set
     if args.gui or len(sys.argv) == 1:
@@ -172,15 +164,21 @@ if __name__ == "__main__":
         # setup the GUI windows Layout
         window = defineGUI()
         device = window.FindElement('_COMBO_').DefaultValue
-        updateFilesBrowseCombo(ti[device])
-        # get all devices
-
-        for d in list(ti.keys()):
-            frame = '_FRAME_' + d
-            print(frame)
-            if d == device:
-                print(d)
-                window.FindElement(frame).Update(visible=True)
+        keys = cfg['split'][device.lower()].keys()
+        # can't update combo with FindElement('_HIDDEN_').Update(), we use this function
+        # with hardcoded FilesBrowseCombo position
+        updateFilesBrowseCombo(
+            ti[device], filesBrowsePosition_column, filesBrowsePosition_row)
+
+        # set the rigth frame for device visible, dosn't work
+        #  File "C:\git\python\PySimpleGUI\PySimpleGUI.py", line 2362, in Update
+        #  self.TKFrame.pack()
+        #  AttributeError: 'NoneType' object has no attribute 'pack'
+        # for d in list(ti.keys()):
+        #     print(d)
+        #     if d == device:
+        #         window.FindElement(
+        #             '_FRAME_{:s}'.format(d)).Update(visible=True)
 
         # main GUI loop
         while True:
@@ -199,14 +197,15 @@ if __name__ == "__main__":
                 # test if a or more file are selected
                 if not all(args.files):
                     sg.Popup("Cancel", "No filename supplied")
-                    #raise SystemExit("Cancelling: no filename supplied")
+                    # raise SystemExit("Cancelling: no filename supplied")
                     continue
                 break
 
             if event is '_COMBO_':
                 # you have to go into the bowels of the pygi code, to get the instance of the Combo
                 # by the line and column number of the window to update its "fileType" property.
-                updateFilesBrowseCombo(ti[values['_COMBO_']])
+                updateFilesBrowseCombo(
+                    ti[values['_COMBO_']], filesBrowsePosition_column, filesBrowsePosition_row)
 
             # update the Multilines instance from FilesBrowse return
             if event is '_HIDDEN_':
@@ -247,9 +246,22 @@ if __name__ == "__main__":
         # test if a or more file are selected
         else:
             if args.files == []:
-                print('You need to specify one or more files to process !!!', end='\n\n')
+                print(
+                    'Error, you need to specify one or more files to process !!!', end='\n\n')
                 parser.print_help(sys.stderr)
                 sys.exit(1)
+            else:
+                files = []
+                for f in args.files:
+                    files.append(f.name)
+                args.files = files
+        if device == 'None':
+            print(
+                'Error: missing option -i or --instrument, instrument = {}\n'.format(device))
+            parser.print_help(sys.stderr)
+            sys.exit(1)
+
+        keys = cfg['split'][device.lower()].keys()
         # in command line mode (console)
         fe, n, m = process(args, cfg, device)
         print("Dimensions: {} x {}".format(m, n))
-- 
GitLab