def get_escape_code(self, codetype, value):
        """ Convert user arg to escape code. """
        valuefmt = str(value).lower()
        code = codes[codetype].get(valuefmt, None)
        if code:
            # Basic code from fore, back, or style.
            return code

        named_funcs = {
            'fore': format_fore,
            'back': format_back,
            'style': format_style,
        }

        # Not a basic code, try known names.
        converter = named_funcs.get(codetype, None)
        if converter is None:
            raise ValueError(
                'Invalid code type. Expecting {}, got: {!r}'.format(
                    ', '.join(named_funcs),
                    codetype
                )
            )
        # Try as hex.
        with suppress(ValueError):
            value = int(hex2term(value, allow_short=True))
            return converter(value, extended=True)

        named_data = name_data.get(valuefmt, None)
        if named_data is not None:
            # A known named color.
            try:
                return converter(named_data['code'], extended=True)
            except TypeError:
                # Passing a known name as a style?
                if codetype == 'style':
                    raise InvalidStyle(value)
                raise
        # Not a known color name/value, try rgb.
        try:
            r, g, b = (int(x) for x in value)
            # This does not mean we have a 3 int tuple. It could '111'.
            # The converter should catch it though.
        except (TypeError, ValueError):
            # Not an rgb value.
            if codetype == 'style':
                raise InvalidStyle(value)
        try:
            escapecode = converter(value)
        except ValueError as ex:
            raise InvalidColr(value) from ex
        return escapecode
def get_escape_code ( self , codetype , value ) : valuefmt = str ( value ) . lower ( ) code = codes [ codetype ] . get ( valuefmt , None ) if code : return code named_funcs = { 'fore' : format_fore , 'back' : format_back , 'style' : format_style , } converter = named_funcs . get ( codetype , None ) if converter is None : raise ValueError ( 'Invalid code type. Expecting {}, got: {!r}' . format ( ', ' . join ( named_funcs ) , codetype ) ) with suppress ( ValueError ) : value = int ( hex2term ( value , allow_short = True ) ) return converter ( value , extended = True ) named_data = name_data . get ( valuefmt , None ) if named_data is not None : try : return converter ( named_data [ 'code' ] , extended = True ) except TypeError : if codetype == 'style' : raise InvalidStyle ( value ) raise try : r , g , b = ( int ( x ) for x in value ) except ( TypeError , ValueError ) : if codetype == 'style' : raise InvalidStyle ( value ) try : escapecode = converter ( value ) except ValueError as ex : raise InvalidColr ( value ) from ex return escapecode
Convert user arg to escape code.