-
Notifications
You must be signed in to change notification settings - Fork 1
/
docs.py
executable file
·244 lines (196 loc) · 6.84 KB
/
docs.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python3
# Deejayd, a media player daemon
# Copyright (C) 2007-2017 Mickael Royer <[email protected]>
# Alexandre Rossi <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
Use to create documentation of the protocol
"""
# init translation
import gettext
import inspect
from deejayd.ui.i18n import DeejaydTranslations
from deejayd.db.models import And, Equals, Or, Contains, NotEquals
from deejayd.jsonrpc import interfaces
from deejayd.jsonrpc.jsonbuilders import JSONRPCResponse, JSONRPCRequest
try:
t = gettext.translation("deejayd", class_=DeejaydTranslations)
except IOError:
t = DeejaydTranslations()
t.install()
common_request = [
{"prefix": "", "desc": "General Commands",
"object": interfaces.CoreModule},
{"prefix": "introspection.", "desc": "Introspection Commands",
"object": interfaces.IntrospectionModule},
{"prefix": "player.", "desc": "Player Commands",
"object": interfaces.PlayerModule},
{"prefix": "audiolib.", "desc": "Audio Library Commands",
"object": interfaces.AudioLibraryModule},
{"prefix": "videolib.", "desc": "Video Library Commands",
"object": interfaces.LibraryModule},
{"prefix": "audiopls.", "desc": "Playlist Mode Commands",
"object": interfaces.AudioSourceModule},
{"prefix": "videopls.", "desc": "Video Mode Commands",
"object": interfaces.VideoSourceModule},
{"prefix": "audioqueue.", "desc": "Queue Commands",
"object": interfaces.QueueSourceModule},
{"prefix": "webradio.", "desc": "Webradio Mode Commands",
"object": interfaces.WebradioModule},
{"prefix": "recpls.", "desc": "Recorded Playlist Commands",
"object": interfaces.RecordedPlaylistModule},
{"prefix": "signal.", "desc": "Signal commands",
"object": interfaces.SignalModule},
]
class WikiFormat:
def commandDoc(self):
return """
As written in specification, request is like that :
{{{
`%(request)s`
}}}
""" % {
"request": JSONRPCRequest("method_name",
["params1", "params2"], id="id").to_pretty_json(),
}
def answerDoc(self):
return """
As written in specification, response is like that :
{{{
%s
}}}
For deejayd, result parameter has always the same syntax :
{{{
`{
"type": answer_type,
"answer": the real answer value
}`
}}}
With response types equals to:
* ack
* list
* dict
* mediaList
* dvdInfo
* fileAndDirList
""" % JSONRPCResponse("deejayd_response", "id").to_pretty_json()
def formatSectionDoc(self, section):
cmd_docs = []
for c_name in dir(section["object"]):
if not c_name.startswith("__"):
try:
c = getattr(section["object"], c_name)
except AttributeError:
continue
if inspect.isclass(c):
cmd_docs.append(self.formatCommandDoc(c, section["prefix"]))
return """
=== `%(section)s` ===
%(commands)s
""" % {
"section": section["desc"],
"commands": "\n\n".join(cmd_docs)
}
def formatCommandDoc(self, cmd, prefix=""):
args = ''
command_args = getattr(cmd, "args", [])
for arg in command_args:
props = []
# An argument is optional by default
if 'req' in arg and arg['req']:
props.append('Mandatory')
else:
props.append('Optional')
args += " * {{{%(name)s}}} (%(props)s) : %(type)s\n"\
% { 'name': arg['name'],
'props': ' and '.join(props),
'type' : arg['type'] }
if len(command_args) == 0:
args = " * ''This command does not accept any argument.''\n"
rvalues = None
try:
if isinstance(cmd.answer, list):
rvalues = cmd.answer
else:
rvalues = [cmd.answer]
except AttributeError:
rvalues = ['ack']
return """==== `%(name)s` ====
%(desc)s
Arguments :
%(args)s
Expected return value : ''`%(rvalues)s`''
""" % { 'name' : prefix+cmd.__name__,
'desc' : cmd.__doc__.strip('\n'),
'args' : args,
'rvalues' : rvalues }
def build(self, sections):
filter = And(Equals("artist", "artist_name"),
Or(Contains("genre", "Rock"), NotEquals("Rating", 4)))
j_sig = {
"type": "signal",
"answer": {
"name": "signal_name",
"attrs": {"attr1": "value1"},
}
}
signal = JSONRPCResponse(j_sig, None).to_json()
return """= deejayd - JSON-RPC Protocol =
Deejayd protocol follows JSON-RPC 1.0 specification available
[http://json-rpc.org/wiki/specification here].
All data between the client and server is encoded in UTF-8.
== Commands Format ==
%(cmd_format)s
== Response Format ==
%(answer)s
== Separator ==
Commands and Responses always finish with the separator `ENDJSON\\n`.
So a command is interpreted by deejayd server only if it finish with this
separator.
== Specific Objects ==
=== Mediafilter Objects ===
Mediafilter object has been serialized in a specific way to be passed as
an method argument or receive with an answer. An example is given here.
{{{
`%(filter)s`
}}}
=== Sort Objects ===
Sort object has been serialized in a specific way to be passed as
an method argument or receive with an answer. An example is given here.
{{{
`[["tag1", "ascending"], ["tag2", "descending"]]`
}}}
Possible sort values are : "ascending" and "descending".
=== Signal Objects ===
Signal is available for TCP connection only.
Signal object has been serialized in a specific way to be send to client.
An example is given here.
{{{
`%(signal)s`
}}}
== Common Available Commands ==
%(commands)s
""" % {
"cmd_format": self.commandDoc(),
"answer": self.answerDoc(),
"filter": filter.to_json(),
"commands": "\n\n".join([self.formatSectionDoc(s) for s in sections]),
"signal": signal,
}
if __name__ == "__main__":
docs = WikiFormat().build(common_request)
with open("doc/deejayd_rpc_protocol", "w") as f:
f.write(docs)