-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmcli.py
191 lines (163 loc) · 4.97 KB
/
mcli.py
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
"""
Simple command-line interface to music service
"""
# Standard library modules
import argparse
import cmd
import re
# Installed packages
import requests
# The services check only that we pass an authorization,
# not whether it's valid
DEFAULT_AUTH = 'Bearer A'
def parse_args():
argp = argparse.ArgumentParser(
'mcli',
description='Command-line query interface to music service'
)
argp.add_argument(
'name',
help="DNS name or IP address of music server"
)
argp.add_argument(
'port',
type=int,
help="Port number of music server"
)
return argp.parse_args()
def get_url(name, port):
return "http://{}:{}/api/v1/music/".format(name, port)
def parse_quoted_strings(arg):
"""
Parse a line that includes words and '-, and "-quoted strings.
This is a simple parser that can be easily thrown off by odd
arguments, such as entries with mismatched quotes. It's good
enough for simple use, parsing "-quoted names with apostrophes.
"""
mre = re.compile(r'''(\w+)|'([^']*)'|"([^"]*)"''')
args = mre.findall(arg)
return [''.join(a) for a in args]
class Mcli(cmd.Cmd):
def __init__(self, args):
self.name = args.name
self.port = args.port
cmd.Cmd.__init__(self)
self.prompt = 'mql: '
self.intro = """
Command-line interface to music service.
Enter 'help' for command list.
'Tab' character autocompletes commands.
"""
def do_read(self, arg):
"""
Read a single song or list all songs.
Parameters
----------
song: music_id (optional)
The music_id of the song to read. If not specified,
all songs are listed.
Examples
--------
read 6ecfafd0-8a35-4af6-a9e2-cbd79b3abeea
Return "The Last Great American Dynasty".
read
Return all songs (if the server supports this).
Notes
-----
Some versions of the server do not support listing
all songs and will instead return an empty list if
no parameter is provided.
"""
url = get_url(self.name, self.port)
r = requests.get(
url+arg.strip(),
headers={'Authorization': DEFAULT_AUTH}
)
if r.status_code != 200:
print("Non-successful status code:", r.status_code)
items = r.json()
if 'Count' not in items:
print("0 items returned")
return
print("{} items returned".format(items['Count']))
for i in items['Items']:
print("{} {:20.20s} {}".format(
i['music_id'],
i['Artist'],
i['SongTitle']))
def do_create(self, arg):
"""
Add a song to the database.
Parameters
----------
artist: string
title: string
Both parameters can be quoted by either single or double quotes.
Examples
--------
create 'Steely Dan' "Everyone's Gone to the Movies"
Quote the apostrophe with double-quotes.
create Chumbawamba Tubthumping
No quotes needed for single-word artist or title name.
"""
url = get_url(self.name, self.port)
args = parse_quoted_strings(arg)
payload = {
'Artist': args[0],
'SongTitle': args[1]
}
r = requests.post(
url,
json=payload,
headers={'Authorization': DEFAULT_AUTH}
)
print(r.json())
def do_delete(self, arg):
"""
Delete a song.
Parameters
----------
song: music_id
The music_id of the song to delete.
Examples
--------
delete 6ecfafd0-8a35-4af6-a9e2-cbd79b3abeea
Delete "The Last Great American Dynasty".
"""
url = get_url(self.name, self.port)
r = requests.delete(
url+arg.strip(),
headers={'Authorization': DEFAULT_AUTH}
)
if r.status_code != 200:
print("Non-successful status code:", r.status_code)
def do_quit(self, arg):
"""
Quit the program.
"""
return True
def do_test(self, arg):
"""
Run a test stub on the music server.
"""
url = get_url(self.name, self.port)
r = requests.get(
url+'test',
headers={'Authorization': DEFAULT_AUTH}
)
if r.status_code != 200:
print("Non-successful status code:", r.status_code)
def do_shutdown(self, arg):
"""
Tell the music cerver to shut down.
"""
url = get_url(self.name, self.port)
r = requests.get(
url+'shutdown',
headers={'Authorization': DEFAULT_AUTH}
)
if r.status_code != 200:
print("Non-successful status code:", r.status_code)
if __name__ == '__main__':
args = parse_args()
Mcli(args).cmdloop()