ftp_fs¶
FTP FileSystem implementation using ftputil.
This module provides FtpFs, a FileSystem backend accessing remote files over FTP. Connections (FTPHost objects) are pooled through the object cache and file contents are read through the block-cache / writeback machinery since FTP does not support random access reads.
Example
import os
import tempfile
import threading
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
from py_misc_utils import cached_file as chf
from py_misc_utils.fs import ftp_fs
root = tempfile.mkdtemp()
# Start a local FTP server so the example is runnable as-is.
authorizer = DummyAuthorizer()
authorizer.add_user('user', 'pass', root, perm='elradfmwMT')
handler = FTPHandler
handler.authorizer = authorizer
server = FTPServer(('127.0.0.1', 0), handler)
stop = threading.Event()
def serve():
while not stop.wait(0.01):
server.serve_forever(timeout=0.01, blocking=False, handle_exit=False)
threading.Thread(target=serve, daemon=True).start()
host, port = server.address
fs = ftp_fs.FtpFs(cache_iface=chf.CacheInterface(os.path.join(root, 'cache')))
url = f'ftp://user:pass@{host}:{port}'
with fs.open(url + '/hello.txt', mode='wb') as fd:
fd.write(b'ftp write')
with fs.open(url + '/hello.txt', mode='rb') as fd:
print(fd.read())
stop.set()
server.close_all()
CacheHandler
¶
Bases: Handler
Object-cache handler creating and keeping alive ftputil.FTPHost objects.
Source code in py_misc_utils/fs/ftp_fs.py
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 | |
create()
¶
Create a new FTPHost connection.
Returns:
| Type | Description |
|---|---|
|
A ftputil.FTPHost connected with the handler arguments. |
Source code in py_misc_utils/fs/ftp_fs.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
is_alive(obj)
¶
Return True if the connection still responds to keep_alive().
Source code in py_misc_utils/fs/ftp_fs.py
100 101 102 103 104 105 106 107 108 109 | |
close(obj)
¶
Close the FTPHost connection.
Source code in py_misc_utils/fs/ftp_fs.py
111 112 113 | |
FtpReader
¶
Reader downloading whole files over an FTP connection.
Source code in py_misc_utils/fs/ftp_fs.py
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 | |
tag(sres)
classmethod
¶
Build a cache tag from a stat result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sres
|
ftputil stat result of the file. |
required |
Returns:
| Type | Description |
|---|---|
|
The cache tag embedding size and modification time. |
Source code in py_misc_utils/fs/ftp_fs.py
130 131 132 133 134 135 136 137 138 139 140 | |
support_blocks()
¶
Return False since FTP transfers do not support partial reads.
Source code in py_misc_utils/fs/ftp_fs.py
142 143 144 | |
read_block(bpath, offset, size)
¶
Copy the whole remote file into a local cache file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bpath
|
Local path of the cache file to write the contents to. |
required | |
offset
|
Must be CachedBlockFile.WHOLE_OFFSET since partial reads are not supported. |
required | |
size
|
Number of bytes to read; ignored since the whole file is read. |
required |
Returns:
| Type | Description |
|---|---|
|
The number of bytes written into the cache file. |
Source code in py_misc_utils/fs/ftp_fs.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
FtpSession
¶
Bases: FTP
FTP session connecting and logging in upon creation.
Source code in py_misc_utils/fs/ftp_fs.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
FtpFs
¶
Bases: FsBase
FileSystem implementation for URLs with the ftp:// scheme.
Connections are pooled per (host, port, user) via the object cache and remote contents are staged through the block-cache and writeback-file helpers, so repeated accesses are served from the local cache.
Source code in py_misc_utils/fs/ftp_fs.py
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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 | |
remove(url)
¶
Remove a remote file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
ftp:// URL of the file to remove. |
required |
Source code in py_misc_utils/fs/ftp_fs.py
272 273 274 275 276 277 278 279 | |
rename(src_url, dest_url)
¶
Rename a remote file within the same host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src_url
|
ftp:// URL of the source file. |
required | |
dest_url
|
ftp:// URL of the destination file. |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
When the source and destination URLs are not on the same host. |
Source code in py_misc_utils/fs/ftp_fs.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |
mkdir(url, mode=None)
¶
Create a remote directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
ftp:// URL of the directory to create. |
required | |
mode
|
Unused, since remote permissions are not supported. |
None
|
Source code in py_misc_utils/fs/ftp_fs.py
303 304 305 306 307 308 309 310 311 | |
makedirs(url, mode=None, exist_ok=None)
¶
Create a remote directory and all its parents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
ftp:// URL of the directory to create. |
required | |
mode
|
Unused, since remote permissions are not supported. |
None
|
|
exist_ok
|
If True, do not raise when the directory already exists (default: False). |
None
|
Source code in py_misc_utils/fs/ftp_fs.py
313 314 315 316 317 318 319 320 321 322 323 324 | |
rmdir(url)
¶
Remove an empty remote directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
ftp:// URL of the directory to remove. |
required |
Source code in py_misc_utils/fs/ftp_fs.py
326 327 328 329 330 331 332 333 | |
rmtree(url, ignore_errors=None)
¶
Recursively remove a remote directory tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
ftp:// URL of the directory tree to remove. |
required | |
ignore_errors
|
If True, errors raised while removing the tree are ignored (default: False). |
None
|
Source code in py_misc_utils/fs/ftp_fs.py
335 336 337 338 339 340 341 342 343 344 345 | |
stat(url)
¶
Stat a remote path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
ftp:// URL to stat. |
required |
Returns:
| Type | Description |
|---|---|
|
The DirEntry of the path. |
Source code in py_misc_utils/fs/ftp_fs.py
369 370 371 372 373 374 375 376 377 378 379 380 | |
list(url)
¶
Enumerate the entries of a remote directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
ftp:// URL of the directory to enumerate. |
required |
Yields:
| Type | Description |
|---|---|
|
A DirEntry for every file and subdirectory of the directory. |
Source code in py_misc_utils/fs/ftp_fs.py
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | |
open(url, mode, **kwargs)
¶
Open a URL for reading or writing.
Read modes are served through the block-cache interface, while write modes download the existing contents when truncation is not requested and wrap the stream into a WritebackFile uploading on close.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
ftp:// URL to open. |
required | |
mode
|
Open mode string. |
required | |
**kwargs
|
Extra keyword arguments forwarded to the cache interface. |
{}
|
Returns:
| Type | Description |
|---|---|
|
A file-like object, wrapped into a TextIOWrapper in text modes. |
Source code in py_misc_utils/fs/ftp_fs.py
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | |
put_file(url, data_gen)
¶
Write data chunks into a remote file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
ftp:// URL of the file to write. |
required | |
data_gen
|
Iterable of bytes chunks to write. |
required |
Source code in py_misc_utils/fs/ftp_fs.py
463 464 465 466 467 468 469 470 471 472 473 474 | |
get_file(url)
¶
Read the contents of a remote file as chunks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
ftp:// URL of the file to read. |
required |
Yields:
| Type | Description |
|---|---|
|
The file contents as bytes chunks. |
Source code in py_misc_utils/fs/ftp_fs.py
476 477 478 479 480 481 482 483 484 485 486 487 488 | |
as_local(url, **kwargs)
¶
Return a local file with the contents of a URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
ftp:// URL of the file to access. |
required | |
**kwargs
|
Extra keyword arguments forwarded to the cache interface. |
{}
|
Returns:
| Type | Description |
|---|---|
|
The path of the local file, downloaded through the cache interface. |
Source code in py_misc_utils/fs/ftp_fs.py
490 491 492 493 494 495 496 497 498 499 500 501 502 503 | |
link(src_url, dest_url)
¶
Copy the file, since FTP has no hard link support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src_url
|
ftp:// URL of the source file. |
required | |
dest_url
|
ftp:// URL of the destination file. |
required |
Source code in py_misc_utils/fs/ftp_fs.py
505 506 507 508 509 510 511 512 513 | |
symlink(src_url, dest_url)
¶
Copy the file, since FTP has no symbolic link support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src_url
|
ftp:// URL of the source file. |
required | |
dest_url
|
ftp:// URL of the destination file. |
required |
Source code in py_misc_utils/fs/ftp_fs.py
515 516 517 518 519 520 521 522 | |