-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_handler.py
More file actions
205 lines (164 loc) · 7.14 KB
/
command_handler.py
File metadata and controls
205 lines (164 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from rich.console import Console
from rich.prompt import Prompt
from filterer import Filter
from operations.basic import about, redirect
from operations.referral import refer, referwith_template
from operations.referral import (
referwith_message
)
from startup import OnStartup
from operations.basic import (
usage,
)
import sys
console = Console()
class Handler:
def refer_with_local_path_command(self, command: str):
"""Handle refer through system's local path command
"""
user_command = command.lower().split()
if len(user_command) != 3:
console.print(
"[red]Wrong use of[/red] refer [red]command![/red]\n[bold]Use like this:[/bold] refer -path \'<path>\'\n")
return False
exact_command = ["refer", "-path"]
for parameter in range(2):
if user_command[parameter] not in exact_command:
console.print(
"[red]Wrong use of[/red] refer [red]command![/red]\n[bold]Use like this:[/bold] refer -path \'<path>\'\n")
return False
refer.refer_path(path=user_command[2])
def referwith_message_through_paths(self, command: str):
"""Handle referwith message command to share multiple packages through their system paths
"""
import shlex
try:
user_command = shlex.split(command)
except ValueError:
console.print("[red]Invalid command format[/red]")
return False
if len(user_command) < 5:
console.print(
"[red]Wrong use of[/red] referwith [red]command![/red]\n"
"[bold]Use like this:[/bold]\n"
'referwith -message "your message" -paths "path 1" "path 2" "path 3"\n'
)
return False
if (
user_command[0] != "referwith"
or user_command[1] != "-message"
or "-paths" not in user_command
):
console.print("[red]Invalid referwith command[/red]")
return False
paths_index = user_command.index("-paths")
message = user_command[2]
# collect paths after -paths until another flag appears
paths = []
for token in user_command[paths_index + 1:]:
if token.startswith("-"):
break
paths.append(token)
if not paths:
console.print("[red]No package paths provided[/red]")
return False
pkgs_paths = [Filter.word(p) for p in paths]
referwith_message.referwith_message_th_path(
paths=pkgs_paths,
message=Filter.word(message)
)
return True
def referwith_message_with_template_through_paths(self, command: str):
"""Handle referwith template command to share multiple packages through their local paths in cool template with message
"""
import shlex
try:
user_command = shlex.split(command)
except ValueError:
console.print("[red]Invalid command format[/red]")
return False
# Expected pattern:
# referwith -template -message <msg> -paths <path1> <path2> ...
if len(user_command) < 5:
console.print(
"[red]Wrong use of[/red] referwith [red]command! To refer multiple packages through their local paths in cool template with custom message[/red]\n"
"[bold]Use like this:[/bold] referwith -template -message \"<your_message>\" -paths \"path 1\" \"path 2\" \"path 3\" \"path 3\"\n"
)
return False
if (
user_command[0] != "referwith"
or user_command[1] != "-template"
or user_command[2] != "-message"
or "-paths" not in user_command
):
console.print("[red]Invalid referwith -template command[/red]")
return False
paths_index = user_command.index("-paths")
# message is right after -message
message = user_command[3]
# everything after -paths is a package name
paths = user_command[paths_index + 1:]
if not paths:
console.print("[red]No package paths provided[/red]")
return False
local_paths = [Filter.word(path) for path in paths]
referwith_template.referwith_template_th_path(
paths=local_paths,
message=Filter.word(message)
)
return True
def main_handler(self):
"""Main handler
"""
while True:
try:
user = Prompt.ask("\n[bold]>_[/bold] ")
user_lower = user.lower()
# if user enter without typing anything then ignore
if not user:
pass
# --------------------------------------------------------
# BASIC COMMMANDS
# --------------------------------------------------------
# display about refer in proper markdown format
elif user_lower == "about":
about.show_about()
# display usage documentation
elif user_lower == "usage":
usage.show_usage()
usage.show_command_quick_reference()
# redirect to official github repository
elif user_lower == "contribute":
redirect.to_official_repository()
# --------------------------------------------------------
# REFERRAL COMMMANDS
# --------------------------------------------------------
# to refer by path and create downloadable link
elif user_lower.startswith("refer -path"):
handle = self.refer_with_local_path_command(user)
if not handle:
continue
# to refer multiple packages with message through paths
elif user_lower.startswith("referwith -message") and "-paths" in user_lower:
handle = self.referwith_message_through_paths(
user)
if not handle:
continue
# to refer multiple packages through paths with message and with cool available template options
elif user_lower.startswith("referwith -template") and "-paths" in user_lower:
handle = self.referwith_message_with_template_through_paths(
user)
if not handle:
continue
# display all available options
elif user_lower == "help" or user_lower == "h":
commands = OnStartup()
commands.commands_display()
# to quit application
elif user_lower == "q" or user_lower == "quit" or user_lower == "exit":
sys.exit(0)
else:
console.print(f"[red]Wrong command![/red]")
except EOFError:
console.print(
"[bold]use ctrl + c / cmd + c again to exit[/bold]")