'Implement a simple menu / settings'

def handle_menu_key(self, c):

    if ((c == self.menu_character) or (c == self.exit_character)):
        self.serial.write(self.tx_encoder.encode(c))
        if self.echo:
            self.console.write(c)
    elif (c == '\x15'):
        sys.stderr.write('\n---    File    to    upload:    ')
        sys.stderr.flush()
        with self.console:
            filename = sys.stdin.readline().rstrip('\r\n')
            if filename:
                try:
                    with open(filename, 'rb') as f:
                        sys.stderr.write('---    Sending    file    {}    ---\n'.format(filename))
                        while True:
                            block = f.read(1024)
                            if (not block):
                                break
                            self.serial.write(block)
                            self.serial.flush()
                            sys.stderr.write('.')
                    sys.stderr.write('\n---    File    {}    sent    ---\n'.format(filename))
                except IOError as e:
                    sys.stderr.write('---    ERROR    opening    file    {}:    {}    ---\n'.format(filename, e))
    elif (c in '\x08hH?'):
        sys.stderr.write(self.get_help_text())
    elif (c == '\x12'):
        self.serial.rts = (not self.serial.rts)
        sys.stderr.write('---    RTS    {}    ---\n'.format(('active' if self.serial.rts else 'inactive')))
    elif (c == '\x04'):
        self.serial.dtr = (not self.serial.dtr)
        sys.stderr.write('---    DTR    {}    ---\n'.format(('active' if self.serial.dtr else 'inactive')))
    elif (c == '\x02'):
        self.serial.break_condition = (not self.serial.break_condition)
        sys.stderr.write('---    BREAK    {}    ---\n'.format(('active' if self.serial.break_condition else 'inactive')))
    elif (c == '\x05'):
        self.echo = (not self.echo)
        sys.stderr.write('---    local    echo    {}    ---\n'.format(('active' if self.echo else 'inactive')))
    elif (c == '\x06'):
        sys.stderr.write('\n---    Available    Filters:\n')
        sys.stderr.write('\n'.join(('---            {:<10}    =    {.__doc__}'.format(k, v) for (k, v) in sorted(TRANSFORMATIONS.items()))))
        sys.stderr.write('\n---    Enter    new    filter    name(s)    [{}]:    '.format('    '.join(self.filters)))
        with self.console:
            new_filters = sys.stdin.readline().lower().split()
        if new_filters:
            for f in new_filters:
                if (f not in TRANSFORMATIONS):
                    sys.stderr.write('---    unknown    filter:    {}\n'.format(repr(f)))
                    break
            else:
                self.filters = new_filters
                self.update_transformations()
        sys.stderr.write('---    filters:    {}\n'.format('    '.join(self.filters)))
    elif (c == '\x0c'):
        modes = list(EOL_TRANSFORMATIONS)
        eol = (modes.index(self.eol) + 1)
        if (eol >= len(modes)):
            eol = 0
        self.eol = modes[eol]
        sys.stderr.write('---    EOL:    {}    ---\n'.format(self.eol.upper()))
        self.update_transformations()
    elif (c == '\x01'):
        sys.stderr.write('\n---    Enter    new    encoding    name    [{}]:    '.format(self.input_encoding))
        with self.console:
            new_encoding = sys.stdin.readline().strip()
        if new_encoding:
            try:
                codecs.lookup(new_encoding)
            except LookupError:
                sys.stderr.write('---    invalid    encoding    name:    {}\n'.format(new_encoding))
            else:
                self.set_rx_encoding(new_encoding)
                self.set_tx_encoding(new_encoding)
        sys.stderr.write('---    serial    input    encoding:    {}\n'.format(self.input_encoding))
        sys.stderr.write('---    serial    output    encoding:    {}\n'.format(self.output_encoding))
    elif (c == ' DCTB '):
        self.dump_port_settings()
    elif (c in 'pP'):
        with self.console:
            try:
                port = ask_for_port()
            except KeyboardInterrupt:
                port = None
        if (port and (port != self.serial.port)):
            self._stop_reader()
            settings = self.serial.getSettingsDict()
            try:
                new_serial = serial.serial_for_url(port, do_not_open=True)
                new_serial.applySettingsDict(settings)
                new_serial.rts = self.serial.rts
                new_serial.dtr = self.serial.dtr
                new_serial.open()
                new_serial.break_condition = self.serial.break_condition
            except Exception as e:
                sys.stderr.write('---    ERROR    opening    new    port:    {}    ---\n'.format(e))
                new_serial.close()
            else:
                self.serial.close()
                self.serial = new_serial
                sys.stderr.write('---    Port    changed    to:    {}    ---\n'.format(self.serial.port))
            self._start_reader()
    elif (c in 'bB'):
        sys.stderr.write('\n---    Baudrate:    ')
        sys.stderr.flush()
        with self.console:
            backup = self.serial.baudrate
            try:
                self.serial.baudrate = int(sys.stdin.readline().strip())
            except ValueError as e:
                sys.stderr.write('---    ERROR    setting    baudrate:    {}    ---\n'.format(e))
                self.serial.baudrate = backup
            else:
                self.dump_port_settings()
    elif (c == '8'):
        self.serial.bytesize = serial.EIGHTBITS
        self.dump_port_settings()
    elif (c == '7'):
        self.serial.bytesize = serial.SEVENBITS
        self.dump_port_settings()
    elif (c in 'eE'):
        self.serial.parity = serial.PARITY_EVEN
        self.dump_port_settings()
    elif (c in 'oO'):
        self.serial.parity = serial.PARITY_ODD
        self.dump_port_settings()
    elif (c in 'mM'):
        self.serial.parity = serial.PARITY_MARK
        self.dump_port_settings()
    elif (c in 'sS'):
        self.serial.parity = serial.PARITY_SPACE
        self.dump_port_settings()
    elif (c in 'nN'):
        self.serial.parity = serial.PARITY_NONE
        self.dump_port_settings()
    elif (c == '1'):
        self.serial.stopbits = serial.STOPBITS_ONE
        self.dump_port_settings()
    elif (c == '2'):
        self.serial.stopbits = serial.STOPBITS_TWO
        self.dump_port_settings()
    elif (c == '3'):
        self.serial.stopbits = serial.STOPBITS_ONE_POINT_FIVE
        self.dump_port_settings()
    elif (c in 'xX'):
        self.serial.xonxoff = (c == 'X')
        self.dump_port_settings()
    elif (c in 'rR'):
        self.serial.rtscts = (c == 'R')
        self.dump_port_settings()
    else:
        sys.stderr.write('---    unknown    menu    character    {}    --\n'.format(key_description(c)))
