#!/usr/bin/env python # # file: $NEDC_NFC/util/python/nedc_edf_py*_header/nedc_edf_pyprint_header.py # # revision history: # # 20240717 (DB): refactored the code for the new environment # 20220514 (DH): renamed the utility and restricted it to edf files # 20200607 (JP): first version # # This is a Python version of the C++ utility nedc_edf_print_header. #------------------------------------------------------------------------------ # import system modules # import os import sys # import nedc_modules # import nedc_cmdl_parser as ncp import nedc_debug_tools as ndt import nedc_edf_tools as net import nedc_file_tools as nft #------------------------------------------------------------------------------ # # global variables are listed here # #------------------------------------------------------------------------------ # set the filename using basename # __FILE__ = os.path.basename(__file__) # define the location of the help files # HELP_FILE = \ "$NEDC_NFC/util/python/nedc_edf_pyprint_header/nedc_edf_pyprint_header.help" USAGE_FILE = \ "$NEDC_NFC/util/python/nedc_edf_pyprint_header/nedc_edf_pyprint_header.usage" #------------------------------------------------------------------------------ # # functions are listed here # #------------------------------------------------------------------------------ # declare a global debug object so we can use it in functions # dbgl = ndt.Dbgl() # function: main # def main(argv): # declare local variables # edf = net.Edf() # create a command line parser # cmdl = ncp.Cmdl(USAGE_FILE, HELP_FILE) cmdl.add_argument("files", type = str, nargs = '*') # parse the command line # args = cmdl.parse_args() # check the number of arguments # if len(args.files) == int(0): cmdl.print_usage('stdout') # display an informational message # print("beginning argument processing...") # main processing loop: loop over all input filenames # num_files_att = int(0) num_files_proc = int(0) for fname in args.files: # expand the filename (checking for environment variables) # ffile = nft.get_fullpath(fname) # check if the file exists # if os.path.exists(ffile) is False: print("Error: %s (line: %s) %s: file does not exist (%s)" % (__FILE__, ndt.__LINE__, ndt.__NAME__, fname)) sys.exit(os.EX_SOFTWARE) # case (1): an edf file # if (nft.is_edf(fname)): num_files_att += int(1) print(" %6ld: %s" % (num_files_att, fname)) if edf.print_header_from_file(fname, sys.stdout): num_files_proc += int(1) edf.cleanup() # case (2): a list # else: # display debug information # if dbgl > ndt.NONE: print("%s (line: %s) %s: opening list (%s)" % (__FILE__, ndt.__LINE__, ndt.__NAME__, fname)) # fetch the list # files = nft.get_flist(ffile) if files is None: print("Error: %s (line: %s) %s: %s (%s)" % (__FILE__, ndt.__LINE__, ndt.__NAME__, " error opening file list", fname)) sys.exit(os.EX_SOFTWARE) else: # loop over all files in the list # for edf_fname in files: # expand the filename (checking for environment variables) # ffile = nft.get_fullpath(edf_fname) # check if the file exists # if os.path.exists(ffile) is False: print("Error: %s (line: %s) %s: %s (%s)" % (__FILE__, ndt.__LINE__, ndt.__NAME__, "file does not exist",edf_fname)) sys.exit(os.EX_SOFTWARE) # if an edf file process it # if nft.is_edf(ffile): num_files_att += int(1) print(" %6ld: %s" % (num_files_att, edf_fname)) if edf.print_header_from_file(ffile, sys.stdout): num_files_proc += int(1) edf.cleanup() else: print("Error: %s (line: %s) %s: %s (%s)" % (__FILE__, ndt.__LINE__, ndt.__NAME__, "error opening file list", edf_fname)) sys.exit(os.EX_SOFTWARE) # display the results # print("processed %ld out of %ld files successfully" % (num_files_proc, num_files_att)) # exit gracefully # return True # begin gracefully # if __name__ == '__main__': main(sys.argv[0:]) # # end of file