diff --git a/dynamic/__main__.py b/dynamic/__main__.py index 35a0d3e..dad5229 100644 --- a/dynamic/__main__.py +++ b/dynamic/__main__.py @@ -1,25 +1,31 @@ #!/usr/bin/env python import argparse -from dynamic.search import Search +from dynamic.namespace import map_arguments +from dynamic.autocomplete import take_input + version = "1.1.0" parser = argparse.ArgumentParser() + + parser.add_argument( - "-st", "--start", help="introduce you to dynamic", action="store_true" + "-s", "--search", help="search a question on StackOverflow", action="store_true" ) parser.add_argument( - "-s", "--search", help="search a question on StackOverflow", action="store_true" + "-v", "--version", help="prints version", action="store_true" ) parser.add_argument( - "-v", "--version", version=f"Dynamic-CLI version {version}", action="version" + "-st", "--start", help="prints starting info", action="store_true" ) + + parser.add_argument( "-n", "--new", @@ -61,27 +67,24 @@ action="store_true", ) -ARGV = parser.parse_args() - -search_flag = Search(ARGV) - def main(): - if ARGV.start: - print( + print( """\U0001F604 Hello and Welcome to Dynamic CLI \U0001F917 Use the following commands to get started - \U0001F50E Search on StackOverflow with '-s' + \U0001F50E Search on StackOverflow with 'dynamic -s' \U0001F4C4 Open browser to create new Stack Overflow question with '-n [title(optional)]' - \U0001F4C2 Save answer to a file with '-file' - \U00002728 Know the version of Dynamic CLI with '-v' - \U0001F609 See this message again with '-st' - \U00002755 Get help with '-h' - """ - ) - else: - search_flag.search_args() - + \U0001F4C2 Save answer to a file with 'dynamic -file' + \U00002728 Know the version of Dynamic CLI with 'dynamic -v' + \U0001F609 See this message again with 'dynamic -st' + \U00002755 Get help with 'dynamic -h' + """) + while 1: + x = take_input() + if(x == 'exit'): break + map_arguments(x) + if __name__ == "__main__": - main() \ No newline at end of file + main() + diff --git a/dynamic/autocomplete.py b/dynamic/autocomplete.py new file mode 100644 index 0000000..1c9738c --- /dev/null +++ b/dynamic/autocomplete.py @@ -0,0 +1,45 @@ +import imp +from prompt_toolkit import prompt +from prompt_toolkit.completion import Completer, Completion +from dynamic.validation import Arg0_Validator + +command_dict = { + 'dynamic':{ + '-s': '--search for a question', + '-v': '--current version', + '-no': '--notion-login', + '-c': '--custom API Key', + '-p': '--access playbook', + '-st': '--start info', + '-h': '--help', + '-GET': '--API GET', + '-POST': '--API POST', + '-DELETE': 'API DELETE' + } +} + + +class CustomCompleter(Completer): + def __init__(self, command_dict): + self.command_dict = command_dict + + def get_completions(self, document, complete_event): + matches = [name for name in self.command_dict.keys() if document.text in name] + for m in matches: + yield Completion(m, start_position=-len(document.text_before_cursor), + display = m) + if(document.text[0:7] == 'dynamic'): + matches = [name for name in self.command_dict['dynamic'].keys() if document.get_word_before_cursor() in name] + for m in matches: + yield Completion(m, start_position=-len(document.get_word_under_cursor()), + display = m, display_meta = self.command_dict['dynamic'][m]) + + + +completer = CustomCompleter(command_dict) + + +def take_input(): + answer = prompt('>>> ', completer = completer, validator=Arg0_Validator()) + return answer + \ No newline at end of file diff --git a/dynamic/namespace.py b/dynamic/namespace.py new file mode 100644 index 0000000..e78fb0d --- /dev/null +++ b/dynamic/namespace.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python + +from dynamic.search import Search +class Namespace: + def __init__(self, **kwargs): + self.__dict__.update(kwargs) + +nameSpace_Mappings = { + "dynamic -v" : Namespace(DELETE=False, GET=False, POST=False, custom=False, + file=False, new=None, notion=False, playbook=False, + search=False, update=False, version=True, start=False ), + + "dynamic -s" : Namespace(DELETE=False, GET=False, POST=False, custom=False, + file=False, new=None, notion=False, playbook=False, + search=True, update=False, version=False, start=False ), + + "dynamic -no" : Namespace(DELETE=False, GET=False, POST=False, custom=False, + file=False, new=None, notion=True, playbook=False, + search=False, update=False, version=False, start=False ), + + "dynamic -c" : Namespace(DELETE=False, GET=False, POST=False, custom=True, + file=False, new=None, notion=False, playbook=False, + search=False, update=False, version=False, start=False ), + + "dynamic -p" : Namespace(DELETE=False, GET=False, POST=False, custom=False, + file=False, new=None, notion=False, playbook=True, + search=False, update=False, version=False, start=False ), + + "dynamic -GET" : Namespace(DELETE=False, GET=True, POST=False, custom=False, + file=False, new=None, notion=False, playbook=False, + search=False, update=False, version=False, start=False ), + + "dynamic -POST" : Namespace(DELETE=False, GET=False, POST=True, custom=False, + file=False, new=None, notion=False, playbook=False, + search=False, update=False, version=False, start=False ), + + "dynamic -DELETE" : Namespace(DELETE=True, GET=False, POST=False, custom=False, + file=False, new=None, notion=False, playbook=False, + search=False, update=False, version=False, start=False ), + "dynamic -st" : Namespace(DELETE=True, GET=False, POST=False, custom=False, + file=False, new=None, notion=False, playbook=False, + search=False, update=False, version=False, start=True ), + + +} + +def map_arguments(input): + try: + ARGV = nameSpace_Mappings[input] + except KeyError: + print("%s is an Invalid option" % input[8:len(input)]) + return + search_flag = Search(ARGV) + search_flag.search_args() diff --git a/dynamic/new.py b/dynamic/new.py new file mode 100644 index 0000000..f962f7d --- /dev/null +++ b/dynamic/new.py @@ -0,0 +1,42 @@ +from prompt_toolkit import prompt +from prompt_toolkit.completion import Completer, Completion, NestedCompleter + +command_dict = { + 'dynamic':{ + '-s': '--search for a question', + '-v': '--current version', + '-no': '--notion-login', + '-c': '--custom API Key', + '-p': '--access playbook', + '-h': '--help', + '-GET': '--API GET', + '-POST': '--API POST', + '-DELETE': 'API DELETE' + } +} + + +class CustomCompleter(Completer): + def __init__(self, command_dict): + self.command_dict = command_dict + + def get_completions(self, document, complete_event): + matches = [name for name in self.command_dict.keys() if document.text in name] + for m in matches: + yield Completion(m, start_position=-len(document.text_before_cursor), + display = m) + if(document.text[0:7] == 'dynamic'): + matches = [name for name in self.command_dict['dynamic'].keys() if document.get_word_before_cursor() in name] + for m in matches: + yield Completion(m, start_position=-len(document.get_word_under_cursor()), + display = m, display_meta = self.command_dict['dynamic'][m]) + + + +completer = CustomCompleter(command_dict) + + +if __name__ == '__main__': + answer = prompt('>>> ', completer = completer) + print('ID: %s' % answer) + diff --git a/dynamic/search.py b/dynamic/search.py index d889540..2f62ad1 100755 --- a/dynamic/search.py +++ b/dynamic/search.py @@ -24,6 +24,7 @@ def prompt(self): return data + class Search: def __init__(self, arguments): self.arguments = arguments @@ -34,6 +35,19 @@ def __init__(self, arguments): def search_args(self): if self.arguments.search: self.search_for_results() + elif self.arguments.start: + print( + """\U0001F604 Hello and Welcome to Dynamic CLI + \U0001F917 Use the following commands to get started + \U0001F50E Search on StackOverflow with 'dynamic -s' + \U0001F4C4 Open browser to create new Stack Overflow question with '-n [title(optional)]' + \U0001F4C2 Save answer to a file with 'dynamic -file' + \U00002728 Know the version of Dynamic CLI with 'dynamic -v' + \U0001F609 See this message again with 'dynamic -st' + \U00002755 Get help with 'dynamic -h' + """) + elif self.arguments.version: + print(f"Dynamic-CLI version {version}") elif self.arguments.file: self.search_for_results(True) elif self.arguments.playbook: @@ -57,6 +71,7 @@ def search_args(self): self.api_test_object.delete_request() elif self.arguments.notion: NotionClient().get_tokenv2_cookie() + def search_for_results(self, save=False): queries = ["What do you want to search", "Tags (optional)"] diff --git a/dynamic/utility.py b/dynamic/utility.py index 4c4a631..a44bd2b 100644 --- a/dynamic/utility.py +++ b/dynamic/utility.py @@ -157,7 +157,7 @@ def display_panel(self): "You have no entries in the playbook", "Browse and save entries in playbook with 'p' key", ) - sys.exit() + return # Creates QuestionPanelStackoverflow object # populates its question_data and answer_data and displays it question_panel = QuestionsPanelStackoverflow() @@ -302,7 +302,7 @@ def navigate_questions_panel(self, playbook=False): try: question_link = self.questions_data[options_index][2] except Exception: - return sys.exit() if playbook else None + return else: if question_menu.chosen_accept_key == "enter": webbrowser.open(question_link) diff --git a/dynamic/validation.py b/dynamic/validation.py new file mode 100644 index 0000000..c4bda49 --- /dev/null +++ b/dynamic/validation.py @@ -0,0 +1,16 @@ +from prompt_toolkit.validation import Validator, ValidationError + +class Arg0_Validator(Validator): + def validate(self, document): + text = document.text + + if(len(text) > 6): + if(text[0:7] != 'dynamic'): + raise ValidationError(message='Usage: dynamic -[OPTIONS]') + if(len(text) >= 8 and text[7] != ' '): + raise ValidationError(message='Usage: dynamic -[OPTIONS]') + if(len(text) >= 9 and text[8] != '-'): + raise ValidationError(message='Usage: dynamic -[OPTIONS]') + + else: + raise ValidationError(message='Usage: dynamic -[OPTIONS]') \ No newline at end of file