tfatool icon indicating copy to clipboard operation
tfatool copied to clipboard

Delete FlashAir files from the command line

Open TadLeonard opened this issue 7 years ago • 12 comments

Delete files via flashair-util

Previously, the upload entrypoint's DEL was used internally to sync directories with FlashAir. @thopiekar has requested (issue #8) that deletion be a first class citizen.

Help! This needs functional testing!

I no longer have a FlashAir SD card, so I'd appreciate it if somebody could help me test this! Ambitious contributors might also add a small test to test_functional.py.

How to use this (from the command line)

$ flashair-util --delete hork.jpg  # delete hork.jpg in the default /DCIM/100__TSB directory
$ flashair-util --delete /path/to/hork.jpg -r ''  # if you know your absolute remote path, make the remote directory blank

How to use this (in your code)

from tfatool import upload

# delete a remote file in the default directory
upload.delete_file_in("hork.png")

# delete a remote file in another directory
upload.delete_file_in("hork.png", remote_dir="/DCIM/special_dir/")

# delete a remote file with a fully specified, absolute path
upload.delete_file_in("/DCIM/special_dir/hork.jpg", remote_dir=None)

# use `delete_file` as a shortcut for `upload.delete_file_in(..., remote_dir=None)`
upload.delete_file("/DCIM/special_dir/hork.jpg")  # the same as the above call

How this was tested

Very minimal unit testing for request URL construction and some command line usage.

TadLeonard avatar Sep 23 '17 22:09 TadLeonard

Great @TadLeonard ! I will test it ASAP.

I just recently bought an FlashAir for this: https://github.com/thopiekar/CuraFlashAirPlugin Normally you have to remove an SD, take it to your PC, save your GCode and reinsert it into your 3D printer. So I decided to experiment with this FlashAir device and found a 2nd choice with 8GB on Amazon for 15EUR, which is a great price for a device to experiment with.

I'm sorry to hear, that you don't own an FlashAir anymore. However, many thanks for your module here. It makes the development of my plugin much easier!

PS: Had issues to run your module on Windows. Will send a PR with my changes as soon as I have time!

Regards!

thopiekar avatar Sep 23 '17 23:09 thopiekar

That's a very cool application of FlashAir! I look forward to your Windows PR. Thanks!

TadLeonard avatar Sep 23 '17 23:09 TadLeonard

Is anybody available for functional testing of this branch? @daanzu? @thopiekar?

TadLeonard avatar Nov 29 '17 22:11 TadLeonard

@TadLeonard Sorry, I had very busy days. Will try to test it today! I defined a test procedure for it, but do you know whether removing empty folders is also possible? I would like to include it also in my test case.

Thanks and regards!

thopiekar avatar Feb 14 '18 11:02 thopiekar

Fetched the sources, but can't test it, because I get complains on Windows that a PosixPath module is missing. Was the tfatool ever meant to be Linux-only?

thopiekar avatar Feb 14 '18 20:02 thopiekar

Thanks for attempting to test things, @thopiekar! There was a PR (#10) for Windows support that removed usage of PosixPath. However, this doesn't seem like a usage issue, but possibly an issue with your version of python. The pathlib module was added in 3.4. Is it possible that you're using a version of python that's older? Maybe that's not the issue at all, but perhaps you could copy/paste your error message so I can make sense of it?

Thanks again!

TadLeonard avatar Feb 15 '18 00:02 TadLeonard

Also, @thopiekar, removing folders is fairly tricky with FlashAir. You have to remove every single file in a folder. Then you perform a standard delete on the folder. If you don't remove everything within the folder first, the files will become inaccessible (and therefore unreadable and not deletable). So, theoretically it's possible with tfatool already, but it could be more convenient by having a function handle that for you.

TadLeonard avatar Feb 15 '18 00:02 TadLeonard

@TadLeonard When looking into https://github.com/TadLeonard/tfatool/branches it seems that this PR and its branch is 4 commits behind. I will git pull from master and test again :)

thopiekar avatar Feb 15 '18 09:02 thopiekar

Oh, and I'm using Python 3.6 here

thopiekar avatar Feb 15 '18 09:02 thopiekar

Looks like the guy from #10 missed one PosixPath mentioned in upload.py. Will make a PR for that including the contents from here 😉

thopiekar avatar Feb 15 '18 09:02 thopiekar

Now running the code works, but:

Traceback (most recent call last):
  File "C:\Users\Thomas Pietrowski\GitLab\tfatool\test_delete.py", line 4, in <module>
    upload.delete_file_in("/foo.txt")
  File "C:\Users\Thomas Pietrowski\GitLab\tfatool\tfatool\upload.py", line 45, in delete_file_in
    raise UploadError("Failed to delete file", response)
tfatool.upload.UploadError: Failed to delete file: <Response [404]>

When using my test script. (The files are there of course.)

from tfatool import upload

# delete a remote file in the default directory
upload.delete_file_in("/foo.txt")

# delete a remote file in another directory
upload.delete_file_in("foo.txt", remote_dir="/special_dir1/")

# delete a remote file with a fully specified, absolute path
upload.delete_file_in("/special_dir2/foo.txt", remote_dir=None)

# use `delete_file` as a shortcut for `upload.delete_file_in(..., remote_dir=None)`
upload.delete_file("/special_dir3/foo.txt")  # the same as the above call

thopiekar avatar Feb 15 '18 09:02 thopiekar

@thopiekar thanks for testing this out! Much appreciated.

Do you think the errors you're seeing have to do with the path separators on your local machine vs the Posix-style separators required by FlashAir? If that's true, tfatool may still need PosixPath or something like it to construct paths for the FlashAir host.

TadLeonard avatar Jul 10 '18 23:07 TadLeonard