Skip to content
Snippets Groups Projects
editexp 1.94 KiB
#! /usr/bin/env python3
#
# Edit experiment config for given update file
#
# $Id$
#

import argparse
import os

import update
import feedback
import package_info


#
# Main routine
#

# Check command line

command_line = argparse.ArgumentParser(description=
    'Edit experiment config for given update file')
command_line.add_argument('-u', '--update', default='update', 
                          help='update script file name [%(default)s]')
command_line.add_argument('-v', '--verbose', action='count', default=0,
                          help='enable informational output'
                               ', repeat for debug output')
command_line.add_argument('-V', '--version', action='version',
                          version=package_info.version)
command_line.add_argument('files', nargs='*', default=[],
                          help='files to edit instead of experiment config')

args = command_line.parse_args()

if args.verbose:
    feedback.setLevel(feedback.INFO if args.verbose == 1 else feedback.DEBUG)
feedback.debug('no debugging info available')

try:
    update_data = update.Update(args.update)
    if not args.files:
        args.files.append(update_data.get_config_file())
    files = []
    for name in args.files:
        if os.path.isabs(name):
            files.append(name)
        else:
            for dirname in update_data.mkexp_path:
                if not os.path.isabs(dirname):
                    dirname = os.path.join(update_data.get_config_dir(), dirname)
                fullname = os.path.join(dirname, name)
                if os.path.exists(fullname):
                    files.append(fullname)
                    break
                else:
                    feedback.info(f"skipping non-existent '{fullname}'")
    if files:
        editor = os.environ.get('VISUAL', os.environ.get('EDITOR', 'vim'))
        os.execlp(editor, editor, *files)
except IOError as error:
    feedback.die("'{0}': {1}".format(error.filename, error.strerror))