Source code for ccpnmodel.ccpncore.memops.scripts.makePythonArgs

"""
Module Documentation here

Set flags from command line arguments
e.g.

    --single-input-parameter            to show these warnings (default)
    --ignore-single-input-parameter     to ignore these warnings
    --all                               show all warnings (default)
    --ignore-all                        ignore all warnings (defined in list)

current warnings supported:
    single-input-parameter
    start-with-upper-case
    start-with-lower-case
    dissimilar-to-name
    elementpairings-not-found
    elementpairings-found
    does-not-descend-from
"""
#=========================================================================================
# Licence, Reference and Credits
#=========================================================================================
__copyright__ = "Copyright (C) CCPN project (http://www.ccpn.ac.uk) 2014 - 2021"
__credits__ = ("Ed Brooksbank, Joanna Fox, Victoria A Higman, Luca Mureddu, Eliza Płoskoń",
               "Timothy J Ragan, Brian O Smith, Gary S Thompson & Geerten W Vuister")
__licence__ = ("CCPN licence. See http://www.ccpn.ac.uk/v3-software/downloads/license")
__reference__ = ("Skinner, S.P., Fogh, R.H., Boucher, W., Ragan, T.J., Mureddu, L.G., & Vuister, G.W.",
                 "CcpNmr AnalysisAssign: a flexible platform for integrated NMR analysis",
                 "J.Biomol.Nmr (2016), 66, 111-124, http://doi.org/10.1007/s10858-016-0060-y")
#=========================================================================================
# Last code modification
#=========================================================================================
__modifiedBy__ = "$modifiedBy: Ed Brooksbank $"
__dateModified__ = "$dateModified: 2021-07-29 20:02:25 +0100 (Thu, July 29, 2021) $"
__version__ = "$Revision: 3.0.4 $"
#=========================================================================================
# Created
#=========================================================================================
__author__ = "$Author: Ed Brooksbank $"
__date__ = "$Date: 2021-07-23 13:23:25 +0100 (Fri, July 23, 2021) $"
#=========================================================================================
# Start of code
#=========================================================================================

import sys


# quick method to define command line arguments
IGNORE_PREFIX = '--ignore-'
SET_PREFIX = '--'
ALL = 'all'
DEFAULT_ALL = [IGNORE_PREFIX + ALL,
               SET_PREFIX + ALL,
               ]

# available arguments
SINGLE_INPUT_PARAMETER = 'single-input-parameter'
START_WITH_UPPER_CASE = 'start-with-upper-case'
START_WITH_LOWER_CASE = 'start-with-lower-case'
DISSIMILAR_TO_NAME = 'dissimilar-to-name'
ELEMENTPAIRINGS_NOT_FOUND = 'elementpairings-not-found'
ELEMENTPAIRINGS_FOUND = 'elementpairings-found'
DOES_NOT_DESCEND_FROM = 'does-not-descend-from'
VALID_ARGS = [SINGLE_INPUT_PARAMETER,
              START_WITH_UPPER_CASE,
              START_WITH_LOWER_CASE,
              DISSIMILAR_TO_NAME,
              ELEMENTPAIRINGS_NOT_FOUND,
              ELEMENTPAIRINGS_FOUND,
              DOES_NOT_DESCEND_FROM,
              ]

argSettings = {}  # set to global below


[docs]def setMakeArguments(): """Read the command line arguments - series of flags to enable/disable warnings """ if sys.version_info[0] == 2: # override for python running from ObjectDomain - skip this bit return global argSettings # check there are not duplicated arguments _args = sys.argv[1:] if len(_args) != len(set(_args)): raise RuntimeError('multiple default command line arguments found: %s' % str(_args)) _defaultArgs = [val.lower() for val in _args if val in DEFAULT_ALL] _otherArgs = [val.lower() for val in _args if val not in DEFAULT_ALL] # set the default flags if required (defaults to True) defaultToSet = True if _defaultArgs: # contains valid arguments; either one or both (ALL) if len(_defaultArgs) > 1: raise RuntimeError('conflicting default command line arguments found: %s' % str(_args)) if _defaultArgs[0] == IGNORE_PREFIX + ALL: defaultToSet = False # defaultToSet = True if (_defaultArgs[0] == SET_PREFIX + ALL) else False print('set default arguments = %s' % defaultToSet) # store all arguments to the default value argSettings = {} for k in VALID_ARGS: argSettings[k] = defaultToSet # argSettings = {k.lower(): defaultToSet for k in VALID_ARGS} # check the other arguments for arg in _otherArgs: valueToSet = None if arg.startswith(IGNORE_PREFIX): arg = arg[len(IGNORE_PREFIX):] valueToSet = False elif arg.startswith(SET_PREFIX): arg = arg[len(SET_PREFIX):] valueToSet = True if not arg: # ignore empty arguments pass elif arg in VALID_ARGS: print('set %s = %s' % (arg, valueToSet)) argSettings[arg] = valueToSet else: raise RuntimeError('Warning: command line argument %s not recognised' % repr(arg))
[docs]def getArgument(arg): """Return the state of a given argument Raise an error if the argument does not exist """ if sys.version_info[0] == 2: # override for python running from ObjectDomain - skip and return <equivalent to True> return not 0 if arg not in VALID_ARGS: raise RuntimeError('command line argument %s not recognised' % repr(arg)) return argSettings[arg]