def check_MAC_address(option, opt_str, value, parser): """ Check MAC address as an OptionParser callback Postcondition: sets value to proper option if check passes Raises: OptionValueError if MAC address is malformed """ try: value = str(com.MACAddress(value)) except com.MACAddress.MACAddressError, e: raise OptionValueError(str(e))
def check_MAC_address(option, opt_str, value, parser): ''' Check MAC address as an OptionParser callback Postcondition: sets value to proper option if check passes Raises: OptionValueError if MAC address is malformed ''' try: value = str(com.MACAddress(value)) except com.MACAddress.MACAddressError as err: raise OptionValueError(str(err)) setattr(parser.values, option.dest, value)
def parse_options(cmd_options=None): ''' Parse and validate options ''' usage = '\n' + get_usage() parser = OptionParser(usage=usage) (options, args) = parser.parse_args(cmd_options) # check that we got the client's name passed in if not args: parser.error(_("Missing required argument, <macaddr>")) elif len(args) > 1: parser.error(_("Too many arguments: %s") % args) # Create a macAddress object and exit if MAC is not valid try: options.mac = com.MACAddress(args[0]) except com.MACAddress.MACAddressError, err: raise SystemExit("Error:\t" + str(err))
def parse_options(): ''' Parse and validate options Args: None Returns: A tuple of a class object representing client to delete and an options object ''' parser = OptionParser(usage=_("usage: %prog [options] MAC_address")) (options, args) = parser.parse_args() # check that we got the client's name passed in if len(args) != 1: parser.print_help() sys.exit(1) # Create a macAddress object and exit if MAC is not valid try: mac = com.MACAddress(args[0]) except com.MACAddress.MACAddressError, e: raise SystemExit("Error:\t" + str(e))
def __checkMAC(value): # ============================================================================== # Private function that checks an MAC address string, that it has six hex # values 0 <= value <= FF, separated by colons. Adds zero padding to two digits # per value. # # Args: # value: the string being checked and processed # # Returns: # checked and massaged value. # # Raises: # ValueError: Malformed MAC address in criteria # ============================================================================== mac_err_msg = "Malformed MAC address in criteria" try: macAddress = com.MACAddress(value) except com.MACAddress.MACAddressError: raise ValueError, mac_err_msg return str(macAddress).lower()