Skip to content

Instantly share code, notes, and snippets.

@bengalih
Created January 9, 2022 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bengalih/8e5d0d2f49f913539def7376165708c8 to your computer and use it in GitHub Desktop.
Save bengalih/8e5d0d2f49f913539def7376165708c8 to your computer and use it in GitHub Desktop.
F:\>python
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from FATtools.Volume import *
>>> root = vopen('F:', 'r+b')
>>> root.listdir()
['MP3S', 'System Volume Information', 'C.mp3', 'B.mp3', 'A.mp3']
>>> root.sort()
(7, 121)
>>> root.listdir()
['MP3S', 'System Volume Information', 'C.mp3', 'B.mp3', 'A.mp3']
>>> root.flush()
>>> root.listdir()
['MP3S', 'System Volume Information', 'C.mp3', 'B.mp3', 'A.mp3']
>>> root.close()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: close() missing 1 required positional argument: 'handle'
@maxpat78
Copy link

I've just fixed my Dirtable.sort() code: now it uses default Python sorting algorithm if no user defined sorting func is passed.

Since I always use the "programmed" version, I forgot to test the default sort operation :D

To close the volume and commit changes, just exit Python (close member in Dirtable objects, like your root, is designed to close a file handle belonging to the Dirtable itself, not the disk).

@bengalih
Copy link
Author

@maxpat78 thanks for the fix.
But what if I need to perform additional functions on the files in the volume with my code? Sorting is the first thing I need to perform and if the Volume is locked until I exit Python...?

@maxpat78
Copy link

maxpat78 commented Jan 10, 2022

What do you mean? While a disk device or volume is "locked" by FATtools (or by any other "low level" app), you MUST NOT access that file system with other apps, or FS integrity could be compromised.
While FATtools has had access to the FS, obviously you can continue to use FATtools to manipulate your files at an higher level (create, read/write, delete, etc.): the low level sort operation does not prevent you in doing so.

@bengalih
Copy link
Author

@maxpat78 Right - I understand that while FATtools has the volume locked you must not access it, however I want to be able to have FATtools close down the open volume so that I can then continue to access it with more python code. I have several steps I need to complete in my program,, the first requires your sort mechanism (which I tested with the update and now seems to work, thanks!).
But let's say for instance that after the sort from FATtools, I want to then enumerate the files I just sorted, read some data, create some new files on that volume. How can I do that if the only way for FATtools to release the volume is to exit from my Python script?

There doesn't appear to be a way to close the volume. I also tried unsuccessfully with something like:

with vopen('F:', 'r+b') as root:
    root.sort()

Sorry if I am misunderstanding or if this is a basic question I just don't grasp, but when working with files for instance you open and then close them or use a with like above so that the file handle is released and then it can be accessed by another process. I don't see a way to use FATtools without running it in a separate python program and then putting all my additional code into a second program I have to run only after the first python process terminates and unlocks the volume.

thanks.

@maxpat78
Copy link

But let's say for instance that after the sort from FATtools, I want to then enumerate the files I just sorted, read some data, create some new files on that volume. How can I do that if the only way for FATtools to release the volume is to exit from my Python script?

It seems to me that my samples subdirectory and the main utilities (cp.py, ls.py, rm.py) give a clear idea about how such operations can be done: using FATtools inside your more complex Python program.

In fact, enumerating, creating, reading... in FATtools is no way different from sorting (they are all low level operations, since FATtools access the raw FAT file-system directly, in each case).
Obviously, I/O operations emulated by FATtools do not offer (and do not intend to offer) all facilities and checks provided by the O.S. file-system manager nor the Python regular file object.

Or you can adopt the yourself-imagined approach: calling a simple Python script (FATtools-enhanced) for sorting from inside another "main" script (as soon as the called script terminates, all handles to FAT FS are implicitly closed by dead Python process).

A third way: write a close_disk() member for my Dirtable object that 1) flushes the FATtools cache to the (real or emulated) disk or disk image and 2) closes and invalidates all FATtools handles.

@bengalih
Copy link
Author

@maxpat78 Thanks for the clarification. I just wanted to make sure that I wasn't missing something and that once I opened the volume for sorting with FATTools that I would no longer be able to access the volume through any other means until the python code exited.
I am not very experienced with Python but hadn't encountered something like this before.

The code I am working on has to do the following:

  1. Sort all files alphabetically on the disk
  2. Enumerate the files in this order and then write a JSON text file for each MP3 file found in the root that contains data found within the ID3 tags of each file (something else I need to find a library to do...).
  3. Set all those files read-only
  4. Possibly some other small housekeeping I haven't realized yet

I supposed I could move everything after 1 to before 1 if I first read the file list in normally, sort them in memory and do all the other steps before actually using FATTools to open the volume and sort on disk. It is just that I would not have thought to do it this way if I could read them in first in the correct order after sorting on disk (a necessary step anyway so would be less code to write).

So, if I understand you correctly while FATtools has some options like cp, ls, rm on a low level, my ability to use another library to access say, ID3 tags on these files would be much more complex (if not impossible) to do once I open the Volume with FATtools.

I would love to do your option #3, and perhaps when my Python skill gets closer to approaching yours I might have the ability to do so.
In the meantime, I will work on either thinking on refactoring the order of my proposed code, or go with my original option of nested scripts.

Thanks for your assistance and your work in developing these tools. Hopefully they will work out for my needs when all is said and done.

@maxpat78
Copy link

Thanks.

In fact, "closing while running" is not so easy nor a quick fix - perhaps Dirtable.flush() could suit to your needs, but it probably will conflict (in a dangerous, destructive way...?) with some atexit.registered functions.
Perhaps one day I'll introduce some sort of Volume.vclose() that carry on the magic...

Hint: if ID3 library or something needs only to read data, and you can pass them as bytes, FATtools can open and read from files, before and after sorting.

@maxpat78
Copy link

Hello, my latest code should allow you to perform the requested operations in "mixed mode".

@bengalih
Copy link
Author

bengalih commented Jan 19, 2022

@maxpat78 Just tested the following:

Type "help", "copyright", "credits" or "license" for more information.
>>> from FATtools.Volume import *
>>> root = vopen('H:', 'r+b')
>>> root.listdir()
['MP3S', 'System Volume Information', 'C.mp3', 'B.mp3', 'A.mp3']
>>> root.sort()
(7, 121)
>>> root.listdir()
['A.mp3', 'B.mp3', 'C.mp3', 'MP3S', 'System Volume Information']
>>> vclose(root)

I was successfully able to access the drive by letter from within the OS after issuing the vclose, so it appears to function for me.

Thanks very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment