<commit_before>#!/usr/bin/env python3

from .jsonubus import JsonUbus

class CliApp(object):
    def __init__(self):
        self.__prompt = None
        self.__command = {}
        self.__commands = []
        self.__completer = []

    @property
    def prompt(self):
        return self.__prompt

    @prompt.setter
    def set_prompt(self, prompt):
        self.__prompt = prompt

    def input_loop(self):
        # ctrl D + exit
        line = ''
        while line != 'exit':
            line = input(self.prompt)
            self.dispatch(line)

    def completer(self, text, state):
        # first complete commands
        # second delegate completer
        if state == 0:
            if text:
                split = text.split(' ')
                if len(split) <= 1:
                    self.__completer = [s for s in self.__commands if text.startswith(s)]
                else:
                    self.__completer = self.__command[split[0]].complete(text)
            else:
                self.__completer = self.__commands

        try:
            return self.__completer[state]
        except IndexError:
            return None

    def dispatch(self, line):
        split = line.split(' ')
        cmd = split[0]
        argument = split[1:]
        if cmd in self.__command:
            self.__command[cmd].dispatch(cmd, argument)

    def register_command(self, name, cmdclass):
        self.__command[name] = cmdclass
        self.__commands.append(name)

class Cli(CliApp):
    def __init__(self, url, user, password):
        super().__init__()
        self.__url = url
        self.__user = user
        self.__password = password
        self.__ubus = JsonUbus(url, user, password)

class SubCommand(object):
    def complete(self, text):
        """ returns an array of possible extensions
            text is "cmd f"
            return ["cmd foo", "cmd fun", "cmd far"]
        """
        pass

    def dispatch(self, arguments):
        pass

class Ubus(SubCommand):
    pass

class Uci(SubCommand):
    pass
<commit_msg>init #2<commit_after>#!/usr/bin/env python3

from jsonubus import JsonUbus
import readline
import logging

LOG = logging.getLogger('netcli')

class CliApp(object):
    def __init__(self):
        self.__prompt = "netcli#>"
        self.__command = {}
        self.__commands = []
        self.__completer = []
        self.register_command('help', self)
        self.register_command('?', self)
        self.register_command('verbose', self)
        readline.parse_and_bind("tab: complete")
        readline.set_completer(self.completer)

    def error(self, message):
        print(message)

    @property
    def prompt(self):
        return self.__prompt

    @prompt.setter
    def set_prompt(self, prompt):
        self.__prompt = prompt

    def input_loop(self):
        # ctrl D + exit
        line = ''
        while line != 'exit':
            line = input(self.prompt)
            self.dispatcher(line)

    def completer(self, text, state):
        # first complete commands
        # second delegate completer
        if state == 0:
            if text:
                split = text.split(' ')
                if len(split) <= 1:
                    self.__completer = [s for s in self.__commands if s.startswith(split[0])]
                else:
                    if split[0] in self.__command:
                        self.__completer = self.__command[split[0]].complete(text)
                    else:
                        return None
            else:
                self.__completer = self.__commands
        try:
            return self.__completer[state]
        except IndexError:
            return None

    def dispatcher(self, line):
        split = line.split(' ')
        cmd = split[0]
        argument = split[1:]
        if cmd in self.__command:
            self.__command[cmd].dispatch(cmd, argument)

        else:
            self.error("No such command. See help or ?")

    def register_command(self, name, cmdclass):
        # TODO: move this into class var of SubCommand
        self.__command[name] = cmdclass
        self.__commands.append(name)

    def dispatch(self, cmd, arg):
        """ self implemented commands """
        if cmd == "help" or cmd == "?":
            self.help()
        elif cmd == "verbose":
            self.verbose()

    def help(self):
        print("available commands : %s" % self.__commands)

    def verbose(self):
        logging.basicConfig()

class Cli(CliApp):
    def __init__(self, url, user, password):
        super().__init__()
        self.__url = url
        self.__user = user
        self.__password = password
        self.__ubus = JsonUbus(url, user, password)

class SubCommand(object):
    # todo class variables
    def complete(self, text):
        """ returns an array of possible extensions
            text is "cmd f"
            return ["cmd foo", "cmd fun", "cmd far"]
        """
        pass

    def dispatch(self, arguments):
        pass

class Ubus(SubCommand):
    pass

class Uci(SubCommand):
    pass

if __name__ == '__main__':
    Cli(url='http://127.0.0.1:8080/ubus', user='root', password='<PASSWORD>').input_loop()
{'fraction_non_alphanumeric': 0.10933281912842267, 'fraction_numerical': 0.004820671037408407, 'mean_word_length': 1.853135313531353, 'pattern_counts': {'":': 2, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '438656', 'n_tokens_mistral': 1501, 'n_tokens_neox': 1419, 'n_words': 381}