Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autocomplete REPL #172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 24 additions & 21 deletions dynamic/__main__.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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()
main()

45 changes: 45 additions & 0 deletions dynamic/autocomplete.py
Original file line number Diff line number Diff line change
@@ -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

54 changes: 54 additions & 0 deletions dynamic/namespace.py
Original file line number Diff line number Diff line change
@@ -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()
42 changes: 42 additions & 0 deletions dynamic/new.py
Original file line number Diff line number Diff line change
@@ -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)

15 changes: 15 additions & 0 deletions dynamic/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def prompt(self):
return data



class Search:
def __init__(self, arguments):
self.arguments = arguments
Expand All @@ -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:
Expand All @@ -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)"]
Expand Down
4 changes: 2 additions & 2 deletions dynamic/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions dynamic/validation.py
Original file line number Diff line number Diff line change
@@ -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 <space> -[OPTIONS]')
if(len(text) >= 9 and text[8] != '-'):
raise ValidationError(message='Usage: dynamic -[OPTIONS]')

else:
raise ValidationError(message='Usage: dynamic -[OPTIONS]')