1
+ """Defines the skeleton of different command structures.
2
+
3
+ Classes:
4
+ Command -- A simple command that can make requests to a path.
5
+ ArgCommand -- A Command subclass for commands with arguments.
6
+ FileCommand -- A Command subclass for file-manipulation commands.
7
+ DownloadCommand -- A Command subclass for file download commands.
8
+ """
9
+
1
10
from __future__ import absolute_import
2
11
3
12
import os
10
19
11
20
12
21
class Command (object ):
22
+ """Defines a command.
23
+
24
+ Public methods:
25
+ __init__ -- creates a Command that will make requests to a given path
26
+ request -- make a request to this command's path
27
+
28
+ Instance variables:
29
+ path -- the url path that this Command will make requests to
30
+ """
13
31
14
32
def __init__ (self , path ):
33
+ """Creates a Command.
34
+
35
+ Keyword arguments:
36
+ path -- the url path that this Command makes requests to
37
+ """
15
38
self .path = path
16
39
17
40
def request (self , client , * args , ** kwargs ):
41
+ """Makes a request to the client with arguments.
42
+
43
+ Keyword arguments:
44
+ client -- the HTTP client to use for the request
45
+ args -- unused unnamed arguments
46
+ kwargs -- additional arguments to HTTP client's request
47
+ """
18
48
return client .request (self .path , ** kwargs )
19
49
20
50
21
51
class ArgCommand (Command ):
52
+ """Defines a command that takes arguments.
53
+
54
+ Subclass of Command.
55
+
56
+ Public methods:
57
+ __init__ -- extends Command constructor to also take a number of required
58
+ arguments
59
+ request -- makes a request to the ArgCommand's path with given arguments
60
+
61
+ Instance variables:
62
+ path -- the url path of that this command will send data to
63
+ argc -- the number of arguments required by this command
64
+ """
22
65
23
66
def __init__ (self , path , argc = None ):
67
+ """Creates an ArgCommand.
68
+
69
+ Keyword arguments:
70
+ path -- the url path to which the command with send data
71
+ argc -- the number of arguments required by this command
72
+ """
24
73
Command .__init__ (self , path )
25
74
self .argc = argc
26
75
27
76
def request (self , client , * args , ** kwargs ):
77
+ """Makes a request to the client with arguments.
78
+
79
+ Can raise an InvalidArgument if the wrong number of arguments is
80
+ provided.
81
+
82
+ Keyword arguments:
83
+ client -- the HTTP client to use for the request
84
+ args -- the arguments to the HTTP client's request
85
+ kwargs -- additional arguments to HTTP client's request
86
+ """
28
87
if self .argc and len (args ) != self .argc :
29
88
raise InvalidArguments ("[%s] command requires %d arguments." % (
30
89
self .path , self .argc ))
31
90
return client .request (self .path , args = args , ** kwargs )
32
91
33
92
34
93
class FileCommand (Command ):
94
+ """Defines a command for manipulating files.
95
+
96
+ Subclass of Command.
97
+
98
+ Public methods:
99
+ request -- overrides Command's request to access a file or files
100
+ files -- adds file-like objects as a multipart request to IPFS
101
+ directory -- loads a directory recursively into IPFS
102
+
103
+ Instance variables:
104
+ path -- the path to make the file requests to
105
+ """
35
106
36
107
def request (self , client , args , f , ** kwargs ):
37
- """
38
- Takes either a file object, a filename, an iterable of filenames, an
39
- iterable of file objects, or a heterogeneous iterable of file objects
40
- and filenames. Can only take one directory at a time, which will be
108
+ """Makes a request for a file or files.
109
+
110
+ Can only take one directory at a time, which will be
41
111
traversed (optionally recursive).
112
+
113
+ Keyword arguments:
114
+ client -- the http client to send requests to
115
+ args -- the arguments to the HTTP client's request
116
+ f -- a file object, a filename, an iterable of filenames, an
117
+ iterable of file objects, or a heterogeneous iterable of file
118
+ objects and filenames
119
+ kwargs -- additional arguments (include 'recursive' if recursively
120
+ copying a directory)
42
121
"""
43
122
if kwargs .pop ('recursive' , False ):
44
123
return self .directory (client , args , f , recursive = True , ** kwargs )
@@ -47,29 +126,67 @@ def request(self, client, args, f, **kwargs):
47
126
else :
48
127
return self .files (client , args , f , ** kwargs )
49
128
50
- def files (self , client , args , files , chunk_size = default_chunk_size , ** kwargs ):
51
- """
52
- Adds file-like objects as a multipart request to IPFS.
129
+ def files (self , client , args , files ,
130
+ chunk_size = default_chunk_size , ** kwargs ):
131
+ """Adds file-like objects as a multipart request to IPFS.
132
+
133
+ Keyword arguments:
134
+ client -- the http client to send requests to
135
+ args -- the arguments to the HTTP client's request
136
+ files -- the files being requested
137
+ chunk_size -- the size of the chunks to break the files into
138
+ kwargs -- additional arguments to HTTP client's request
53
139
"""
54
140
body , headers = multipart .stream_files (files ,
55
141
chunk_size = chunk_size )
56
- return client .request (self .path , args = args , data = body , headers = headers , ** kwargs )
142
+ return client .request (self .path , args = args , data = body ,
143
+ headers = headers , ** kwargs )
57
144
58
145
def directory (self , client , args , dirname ,
59
146
match = '*' , recursive = False ,
60
147
chunk_size = default_chunk_size , ** kwargs ):
61
- """
62
- Loads a directory recursively into IPFS, files are matched against the
63
- given pattern.
148
+ """Loads a directory recursively into IPFS.
149
+
150
+ Files are matched against the given pattern.
151
+
152
+ Keyword arguments:
153
+ client -- the http client to send requests to
154
+ args -- the arguments to the HTTP client's request
155
+ dirname -- the name of the directory being requested
156
+ match -- a pattern to match the files against
157
+ recursive -- boolean for whether to load contents recursively
158
+ chunk_size -- the size of the chunks to break the files into
159
+ kwargs -- additional arguments to HTTP client's request
64
160
"""
65
161
body , headers = multipart .stream_directory (dirname ,
66
162
fnpattern = match ,
67
163
recursive = recursive ,
68
164
chunk_size = chunk_size )
69
- return client .request (self .path , args = args , data = body , headers = headers , ** kwargs )
165
+ return client .request (self .path , args = args , data = body ,
166
+ headers = headers , ** kwargs )
70
167
71
168
72
169
class DownloadCommand (Command ):
170
+ """Downloads requested files.
171
+
172
+ Subclass of Command
173
+
174
+ Public methods:
175
+ request -- make a request to this DownloadCommand's path to download a
176
+ given file
177
+
178
+ Instance variables:
179
+ path -- the url path to send requests to
180
+ """
73
181
74
182
def request (self , client , * args , ** kwargs ):
183
+ """Requests a download from the HTTP Client.
184
+
185
+ See the HTTP client's doc for details of what to pass in.
186
+
187
+ Keyword arguments:
188
+ client -- the http client to send requests to
189
+ args -- the arguments to the HTTP client
190
+ kwargs -- additional arguments to the HTTP client
191
+ """
75
192
return client .download (self .path , args = args , ** kwargs )
0 commit comments