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+
110from __future__ import absolute_import
211
312import os
1019
1120
1221class 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+ """
1331
1432 def __init__ (self , path ):
33+ """Creates a Command.
34+
35+ Keyword arguments:
36+ path -- the url path that this Command makes requests to
37+ """
1538 self .path = path
1639
1740 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+ """
1848 return client .request (self .path , ** kwargs )
1949
2050
2151class 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+ """
2265
2366 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+ """
2473 Command .__init__ (self , path )
2574 self .argc = argc
2675
2776 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+ """
2887 if self .argc and len (args ) != self .argc :
2988 raise InvalidArguments ("[%s] command requires %d arguments." % (
3089 self .path , self .argc ))
3190 return client .request (self .path , args = args , ** kwargs )
3291
3392
3493class 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+ """
35106
36107 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
41111 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)
42121 """
43122 if kwargs .pop ('recursive' , False ):
44123 return self .directory (client , args , f , recursive = True , ** kwargs )
@@ -47,29 +126,67 @@ def request(self, client, args, f, **kwargs):
47126 else :
48127 return self .files (client , args , f , ** kwargs )
49128
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
53139 """
54140 body , headers = multipart .stream_files (files ,
55141 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 )
57144
58145 def directory (self , client , args , dirname ,
59146 match = '*' , recursive = False ,
60147 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
64160 """
65161 body , headers = multipart .stream_directory (dirname ,
66162 fnpattern = match ,
67163 recursive = recursive ,
68164 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 )
70167
71168
72169class 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+ """
73181
74182 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+ """
75192 return client .download (self .path , args = args , ** kwargs )
0 commit comments