5
5
import sys
6
6
7
7
8
- class NoDocument (Exception ):
9
- pass
8
+ config_path = os .path .join (os .path .expanduser ('~' ), '.coreapi' )
9
+
10
+ store_path = os .path .join (config_path , 'document.json' )
11
+ credentials_path = os .path .join (config_path , 'credentials.json' )
12
+ headers_path = os .path .join (config_path , 'headers.json' )
10
13
11
14
12
15
def coerce_key_types (doc , keys ):
@@ -36,63 +39,44 @@ def coerce_key_types(doc, keys):
36
39
return ret
37
40
38
41
39
- def get_credentials_path ():
40
- directory = os .path .join (os .path .expanduser ('~' ), '.coreapi' )
41
- if os .path .isfile (directory ):
42
- os .remove (directory )
43
- os .mkdir (directory )
44
- elif not os .path .exists (directory ):
45
- os .mkdir (directory )
46
- return os .path .join (directory , 'credentials.json' )
47
-
48
-
49
- def get_store_path ():
50
- directory = os .path .join (os .path .expanduser ('~' ), '.coreapi' )
51
- if os .path .isfile (directory ):
52
- os .remove (directory )
53
- os .mkdir (directory )
54
- elif not os .path .exists (directory ):
55
- os .mkdir (directory )
56
- return os .path .join (directory , 'document.json' )
42
+ def get_session ():
43
+ credentials = get_credentials ()
44
+ headers = get_headers ()
45
+ return coreapi .get_session (credentials , headers )
57
46
58
47
59
- def get_session ():
60
- path = get_credentials_path ()
61
- if os .path .exists (path ) and os .path .isfile (path ):
62
- store = open (path , 'rb' )
63
- credentials = json .loads (store .read ())
64
- store .close ()
65
- return coreapi .get_session (credentials )
66
- return coreapi .get_default_session ()
48
+ def read_from_store ():
49
+ if not os .path .exists (store_path ):
50
+ return None
51
+ store = open (store_path , 'rb' )
52
+ content = store .read ()
53
+ store .close ()
54
+ return coreapi .load (content )
67
55
68
56
69
57
def write_to_store (doc ):
70
- path = get_store_path ()
71
58
content_type , content = coreapi .dump (doc )
72
- store = open (path , 'wb' )
59
+ store = open (store_path , 'wb' )
73
60
store .write (content )
74
61
store .close ()
75
62
76
63
77
- def read_from_store ():
78
- path = get_store_path ()
79
- if not os .path .exists (path ):
80
- raise NoDocument ()
81
- store = open (path , 'rb' )
82
- content = store .read ()
83
- store .close ()
84
- return coreapi .load (content )
85
-
86
-
87
64
def dump_to_console (doc ):
88
65
codec = coreapi .codecs .PlainTextCodec ()
89
66
return codec .dump (doc , colorize = True )
90
67
91
68
69
+ # Core commands
70
+
92
71
@click .group (invoke_without_command = True , help = 'Command line client for interacting with CoreAPI services.\n \n Visit http://www.coreapi.org for more information.' )
93
72
@click .option ('--version' , is_flag = True , help = 'Display the package version number.' )
94
73
@click .pass_context
95
74
def client (ctx , version ):
75
+ if os .path .isfile (config_path ):
76
+ os .remove (config_path )
77
+ if not os .path .isdir (config_path ):
78
+ os .mkdir (config_path )
79
+
96
80
if ctx .invoked_subcommand is not None :
97
81
return
98
82
@@ -113,21 +97,18 @@ def get(url):
113
97
114
98
@click .command (help = 'Remove the current document, and any stored credentials.' )
115
99
def clear ():
116
- path = get_store_path ()
117
- if os .path .exists (path ):
118
- os .remove (path )
119
- path = get_credentials_path ()
120
- if os .path .exists (path ):
121
- os .remove (path )
100
+ if os .path .exists (store_path ):
101
+ os .remove (store_path )
102
+ if os .path .exists (credentials_path ):
103
+ os .remove (credentials_path )
122
104
click .echo ('Cleared.' )
123
105
124
106
125
107
@click .command (help = 'Display the current document, or element at the given PATH.' )
126
108
@click .argument ('path' , nargs = - 1 )
127
109
def show (path ):
128
- try :
129
- doc = read_from_store ()
130
- except NoDocument :
110
+ doc = read_from_store ()
111
+ if doc is None :
131
112
click .echo ('No current document. Use `coreapi get` to fetch a document first.' )
132
113
return
133
114
@@ -156,9 +137,8 @@ def action(path, param):
156
137
157
138
params = dict ([tuple (item .split ('=' , 1 )) for item in param ])
158
139
159
- try :
160
- doc = read_from_store ()
161
- except NoDocument :
140
+ doc = read_from_store ()
141
+ if doc is None :
162
142
click .echo ('No current document. Use `coreapi get` to fetch a document first.' )
163
143
return
164
144
@@ -169,11 +149,138 @@ def action(path, param):
169
149
write_to_store (doc )
170
150
171
151
152
+ # Credentials
153
+
154
+ def get_credentials ():
155
+ if not os .path .isfile (credentials_path ):
156
+ return {}
157
+ store = open (credentials_path , 'rb' )
158
+ credentials = json .loads (store .read ())
159
+ store .close ()
160
+ return credentials
161
+
162
+
163
+ def set_credentials (credentials ):
164
+ store = open (credentials_path , 'wb' )
165
+ store .write (json .dumps (credentials ))
166
+ store .close
167
+
168
+
169
+ @click .group (help = 'Configure credentials using in request "Authorization:" headers.' )
170
+ def credentials ():
171
+ pass
172
+
173
+
174
+ @click .command (help = "List stored credentials." )
175
+ def credentials_show ():
176
+ credentials = get_credentials ()
177
+ if credentials :
178
+ width = max ([len (key ) for key in credentials .keys ()])
179
+ fmt = '{domain:%d} "{header}"' % width
180
+
181
+ click .echo (click .style ('Credentials' , bold = True ))
182
+ for key , value in sorted (credentials .items ()):
183
+ click .echo (fmt .format (domain = key , header = value ))
184
+
185
+
186
+ @click .command (help = "Add CREDENTIALS string for the given DOMAIN." )
187
+ @click .argument ('domain' , nargs = 1 )
188
+ @click .argument ('header' , nargs = 1 )
189
+ def credentials_add (domain , header ):
190
+ credentials = get_credentials ()
191
+ credentials [domain ] = header
192
+ set_credentials (credentials )
193
+
194
+ click .echo (click .style ('Added credentials' , bold = True ))
195
+ click .echo ('%s "%s"' % (domain , header ))
196
+
197
+
198
+ @click .command (help = "Remove credentials for the given DOMAIN." )
199
+ @click .argument ('domain' , nargs = 1 )
200
+ def credentials_remove (domain ):
201
+ credentials = get_credentials ()
202
+ credentials .pop (domain , None )
203
+ set_credentials (credentials )
204
+
205
+ click .echo (click .style ('Removed credentials' , bold = True ))
206
+ click .echo (domain )
207
+
208
+
209
+ # Headers
210
+
211
+ def get_headers ():
212
+ if not os .path .isfile (headers_path ):
213
+ return {}
214
+ headers_file = open (headers_path , 'rb' )
215
+ headers = json .loads (headers_file .read ())
216
+ headers_file .close ()
217
+ return headers
218
+
219
+
220
+ def set_headers (headers ):
221
+ headers_file = open (headers_path , 'wb' )
222
+ headers_file .write (json .dumps (headers ))
223
+ headers_file .close ()
224
+
225
+
226
+ def titlecase (header ):
227
+ return '-' .join ([word .title () for word in header .split ('-' )])
228
+
229
+
230
+ @click .group (help = "Configure custom request headers." )
231
+ def headers ():
232
+ pass
233
+
234
+
235
+ @click .command (help = "List custom request headers." )
236
+ def headers_show ():
237
+ headers = get_headers ()
238
+
239
+ click .echo (click .style ('Headers' , bold = True ))
240
+ for key , value in sorted (headers .items ()):
241
+ click .echo (key + ': ' + value )
242
+
243
+
244
+ @click .command (help = "Add custom request HEADER with given VALUE." )
245
+ @click .argument ('header' , nargs = 1 )
246
+ @click .argument ('value' , nargs = 1 )
247
+ def headers_add (header , value ):
248
+ header = titlecase (header )
249
+ headers = get_headers ()
250
+ headers [header ] = value
251
+ set_headers (headers )
252
+
253
+ click .echo (click .style ('Added header' , bold = True ))
254
+ click .echo ('%s: %s' % (header , value ))
255
+
256
+
257
+ @click .command (help = "Remove custom request HEADER." )
258
+ @click .argument ('header' , nargs = 1 )
259
+ def headers_remove (header ):
260
+ header = titlecase (header )
261
+ headers = get_headers ()
262
+ headers .pop (header , None )
263
+ set_headers (headers )
264
+
265
+ click .echo (click .style ('Removed header' , bold = True ))
266
+ click .echo (header )
267
+
268
+
172
269
client .add_command (get )
173
270
client .add_command (show )
174
271
client .add_command (action )
175
272
client .add_command (clear )
176
273
274
+ client .add_command (credentials )
275
+ credentials .add_command (credentials_add , name = 'add' )
276
+ credentials .add_command (credentials_remove , name = 'remove' )
277
+ credentials .add_command (credentials_show , name = 'show' )
278
+
279
+ client .add_command (headers )
280
+ headers .add_command (headers_add , name = 'add' )
281
+ headers .add_command (headers_remove , name = 'remove' )
282
+ headers .add_command (headers_show , name = 'show' )
283
+
177
284
178
285
if __name__ == '__main__' :
179
286
client ()
0 commit comments