Skip to content

Commit

Permalink
Merge pull request #57 from UCSD-E4E/44-add-exception-and-traceback-t…
Browse files Browse the repository at this point in the history
…o-logs

fix: Adds exception logging to main invocation
  • Loading branch information
ntlhui authored Jan 11, 2025
2 parents 8a811be + de9e0d5 commit cb9c732
Showing 1 changed file with 74 additions and 66 deletions.
140 changes: 74 additions & 66 deletions e4e_data_management/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,64 +35,68 @@ def __init__(self):
self.__configure_logging()
self._log = logging.getLogger('e4edm.cli')
self._log.debug('Invoking version %s from %s', __version__, __file__)
self.app = DataManager.load()
commands = [
'init_dataset',
'init_mission',
'status',
'list',
'config',
'activate',
'add',
'commit',
'duplicate',
'validate',
'push',
'zip',
'unzip',
'prune',
'ls',
]
self.parameters = [
Parameter(
name='dataset_dir',
getter=lambda: getattr(self.app, 'dataset_dir'),
setter=lambda x: setattr(self.app, 'dataset_dir', x),
parser=Path,
formatter=Path.as_posix,
validator=Path.is_dir
),
Parameter(
name='version',
getter=lambda: getattr(self.app, 'version'),
setter=None,
parser=int,
formatter=str,
validator=None
)
]
self.parser = argparse.ArgumentParser()
subparsers = self.parser.add_subparsers()
parsers = {cmd:subparsers.add_parser(cmd) for cmd in commands}

self.__configure_init_dataset_parser(parsers['init_dataset'])
self.__configure_init_mission_parser(parsers['init_mission'])
self.__configure_status_parser(parsers['status'])
self.__configure_list_parser(parsers['list'])
self.__configure_add_parser(parsers['add'])
self.__configure_commit_parser(parsers['commit'])
self.__configure_duplicate_parser(parsers['duplicate'])
self.__configure_push_parser(parsers['push'])
self.__configure_prune_parser(parsers['prune'])
self.__configure_config_parser(parsers['config'])
self.__configure_activate_parser(parsers['activate'])
self.__configure_ls_parser(parsers['ls'])
# self.__configure_validate_parser(parsers['validate'])
# self.__configure_zip_parser(parsers['zip'])
# self.__configure_unzip_parser(parsers['unzip'])

self.parser.add_argument('--version', action='version', version=f'e4edm {__version__}')
self.parser.set_defaults(func=self.parser.print_help)
try:
self.app = DataManager.load()
commands = [
'init_dataset',
'init_mission',
'status',
'list',
'config',
'activate',
'add',
'commit',
'duplicate',
'validate',
'push',
'zip',
'unzip',
'prune',
'ls',
]
self.parameters = [
Parameter(
name='dataset_dir',
getter=lambda: getattr(self.app, 'dataset_dir'),
setter=lambda x: setattr(self.app, 'dataset_dir', x),
parser=Path,
formatter=Path.as_posix,
validator=Path.is_dir
),
Parameter(
name='version',
getter=lambda: getattr(self.app, 'version'),
setter=None,
parser=int,
formatter=str,
validator=None
)
]
self.parser = argparse.ArgumentParser()
subparsers = self.parser.add_subparsers()
parsers = {cmd:subparsers.add_parser(cmd) for cmd in commands}

self.__configure_init_dataset_parser(parsers['init_dataset'])
self.__configure_init_mission_parser(parsers['init_mission'])
self.__configure_status_parser(parsers['status'])
self.__configure_list_parser(parsers['list'])
self.__configure_add_parser(parsers['add'])
self.__configure_commit_parser(parsers['commit'])
self.__configure_duplicate_parser(parsers['duplicate'])
self.__configure_push_parser(parsers['push'])
self.__configure_prune_parser(parsers['prune'])
self.__configure_config_parser(parsers['config'])
self.__configure_activate_parser(parsers['activate'])
self.__configure_ls_parser(parsers['ls'])
# self.__configure_validate_parser(parsers['validate'])
# self.__configure_zip_parser(parsers['zip'])
# self.__configure_unzip_parser(parsers['unzip'])

self.parser.add_argument('--version', action='version', version=f'e4edm {__version__}')
self.parser.set_defaults(func=self.parser.print_help)
except Exception as exc:
self._log.exception('Exception during application load/configuration')
raise exc

def __configure_logging(self) -> None:
log_dir = Path(DataManager.dirs.user_log_dir).resolve()
Expand Down Expand Up @@ -253,14 +257,18 @@ def prune_cmd(self):
def main(self):
"""Main function
"""
self._log.info("Invoked with %s", ' '.join(sys.argv))
args = self.parser.parse_args()
arg_dict = vars(args)

arg_fn = args.func
arg_dict.pop('func')

arg_fn(**arg_dict)
try:
self._log.info("Invoked with %s", ' '.join(sys.argv))
args = self.parser.parse_args()
arg_dict = vars(args)

arg_fn = args.func
arg_dict.pop('func')

arg_fn(**arg_dict)
except Exception as exc:
self._log.exception('Exception during main execution')
raise exc

def __configure_ls_parser(self, parser: argparse.ArgumentParser):
parser.add_argument('path', type=Path, default=Path('.'))
Expand Down

0 comments on commit cb9c732

Please sign in to comment.