from cmdint import CmdInterface from pathlib import Path '''By default, CmdInterface logs the git repository hashes of the respective last commit of two repositories: 1. the git repository of the main file that is being executed (if located in a repository) 2. the git repository of the file containing the CmdInterface class (if located in a repository) Additonally, any other repository can be added for logging and if desired, also automatically committed if dirty. The logged git information can be found under ['cmd_interface']['repositories'] in the json logfile ''' # If autocommit=True, pending changes in a dirty repo are commited with an automatic commit message. # This can be sensible since the logged commit hash otherwise does not capture the full state of the repository. CmdInterface.add_repo_path(str(Path.home()) + '/coding/mitk/mitk/', autocommit=False) CmdInterface.set_static_logfile('git_repository_logging.json', delete_existing=True) test = CmdInterface('ls') test.add_arg(key='-l', arg='/') test.run() CmdInterface.anonymize_log(out_log_name='git_repository_logging.json')
# somy dummy function to be executed using CmdInterface def my_python_function(my_arg: str): for i in range(10): print(my_arg + ' ' + str(i)) # another dummy with progress bar def my_python_function_with_progress(): bar = ProgressBar(10) for i in range(10): time.sleep(1) bar.next() # set output logfile and tell CmdInterface to delete a potentially present old logfile CmdInterface.set_static_logfile('python_function_logging.json', delete_existing=True) # create instance of CmdInterface that calls the previously defined python function "my_python_function" test_my_python_function = CmdInterface(my_python_function) test_my_python_function.add_arg('my_arg', 'BLABLA') test_my_python_function.run() # another call with our progress bar function # the progress bar in the output logfile is updated continuously test_my_python_function_with_progress = CmdInterface( my_python_function_with_progress) test_my_python_function_with_progress.run() # anonymize logfile CmdInterface.anonymize_log(out_log_name='python_function_logging.json')
from cmdint import CmdInterface # set output logfile and tell CmdInterface to delete a potentially present old logfile CmdInterface.set_static_logfile('command_line_logging.json', delete_existing=True) # create instance of CmdInterface with the name of the command to be called (here "ls") test = CmdInterface('ls') # add keyword based argument test.add_arg(key='-l', arg='/') # run command test.run() # anonymize logfile CmdInterface.anonymize_log(out_log_name='command_line_logging.json') # run the same command again but also print version information # note that we don't have to set the argument again CmdInterface.set_static_logfile('command_line_logging_with_version.json', delete_existing=True) test.run(version_arg='--version') # anonymize logfile CmdInterface.anonymize_log(out_log_name='command_line_logging_with_version.json')
from cmdint import CmdInterface import os # remove some files that are created in this script if os.path.isfile('io_file_logging.txt'): os.remove('io_file_logging.txt') if os.path.isfile('io_file_logging_copy.txt'): os.remove('io_file_logging_copy.txt') # set output logfile and tell CmdInterface to delete a potentially present old logfile CmdInterface.set_static_logfile('io_file_logging.json', delete_existing=True) CmdInterface.set_throw_on_error(do_throw=False) # we want to log if a run is not necessary. by default this is disabled since we don't want to swamp our # logfile if we call the script multiple times CmdInterface.set_immediate_return_on_run_not_necessary(do_return=False) # for this example we dont't want to exit python if a run fails. default is True. CmdInterface.set_exit_on_error(do_exit=False) #################################################################################################### # create a new text file using "echo" step1 = CmdInterface('echo') step1.add_arg(arg='unus ignis quis vir multum ab audere') # by setting "check_output" to True, the argument is regarded as an expected output file # (logged under ['command']['output']['expected']). # If the file is found BEFORE execution, the execution is skipped, since a run is regarded as unnecessary when all # outputf iles are already present. # The run fails if this file is not found AFTER execution # (logged under: ['command']['output']['missing']).
from cmdint import CmdInterface from pathlib import Path """ Example how to call one of MITKs command line tools """ # Optional. We set this to change the default logfile name to mitk_example for this example script and to overwrite a # potentially existing old logfile instead of appending the new command line call. CmdInterface.set_static_logfile(file='mitk_example.json', delete_existing=True) CmdInterface.set_throw_on_error(do_throw=False) # Convert a nrrd image file to a nifti file. Also log input and output file hashes and check if the corresponding # files are present before or after the command execution respectively by setting check_input and check_output. converter = CmdInterface('MitkFileFormatConverter') converter.add_arg('-i', str(Path.home()) + '/mitk/mitkdata/brain.nrrd', check_input=True) converter.add_arg('-o', str(Path.home()) + '/mitk/mitkdata/brain.nii.gz', check_output=True) converter.run() converter.anonymize_log(CmdInterface.get_static_logfile()) # If you are using a downloaded prebuilt version of MITK, call CmdInterface.set_use_installer(True) or # directly call MitkFileFormatConverter.sh instead of MitkFileFormatConverter. set_use_installer(True) simply appends # '.sh' to your command, which is just convenience if you frequently want to switch between installer and non installer # versions of MITK and don't want to change all your command line calls every time. On Windows you can adjust this to # append '.bat': CmdInterface.set_use_installer(True, command_suffix='.bat')