Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Direct Browsing of LittleFS Storage Device on Windows #67

Merged
merged 9 commits into from
Dec 2, 2023

Conversation

Jiajun-Huang
Copy link
Contributor

@Jiajun-Huang Jiajun-Huang commented Nov 28, 2023

This is an excellent Python interface for LittleFS; however, there is currently no support for users to directly browse the storage device with LittleFS on Windows. This pull request aims to address this by introducing support for browsing through the storage device. This is achieved by implementing a new UserContext with a device/file path arrangement.

Testing has only been conducted manually using an SD card adapter and USB flash.

A small sample code to show how it works.

disk_path = r"\\.\F: " # assume F drive is your storage device for littlefs
fs = LittleFS(block_size=512, block_count=30228480, mount=False, context=UserContextWinDisk(disk_path))
fs.mount()
print(fs.listdir('/'))
with fs.open('deadbeef.txt', 'rb') as fh:
    data = fh.read()

@Jiajun-Huang Jiajun-Huang changed the title Add support for Windows user to direct Add support for Windows user to direct browse storage device with littlefs Nov 28, 2023
@Jiajun-Huang Jiajun-Huang marked this pull request as draft November 28, 2023 04:35
@Jiajun-Huang Jiajun-Huang changed the title Add support for Windows user to direct browse storage device with littlefs Enable Direct Browsing of LittleFS Storage Device on Windows Nov 28, 2023
@Jiajun-Huang Jiajun-Huang marked this pull request as ready for review November 28, 2023 04:53


# if the user has win32file installed then define this class
try:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to avoid large try/excepts, as you could catch unintended exceptions. I would:

import ctypes
try:
    import win32file
except ImportError:
    pass
else:
    # Put the UserContextWinDisk implementation here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I want to do! Didn't know I could do that. Also, instead of pass, I think it might be better to add a message to warn the user.

def __init__(self, disk_path:str) -> None:
self.device = win32file.CreateFile(disk_path, win32file.GENERIC_READ, win32file.FILE_SHARE_READ, None, win32file.OPEN_EXISTING, 0, None)
if self.device == win32file.INVALID_HANDLE_VALUE:
raise Exception("Error opening disk")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a OSError or IOError might be more appropriate.

try:
import win32file
except ImportError:
raise ImportError("Unable to import 'win32file'. This module is required for Windows-specific functionality. Please ensure you are running on a Windows platform or install 'pywin32' using: 'pip install pywin32'.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so hold on, this now makes win32file a hard requirement. We should only raise an error if the user attempts to instantiate UserContextWinDisk. The following is probably closer to what we want:

try:
    import win32file
except ImportError:
    win32file = None

class UserContextWinDisk(UserContext):
     def __init__(self, disk_path:str) -> None:
         if win32file is None:
             raise ImportError(...)
         # rest of WinDisk implementation

import win32file
except ImportError:
win32file = None
else:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so sorry, with this new implementation (checking win32file in __init__), then we no longer need this else:

try:  
     import win32file  
 except ImportError:
     win32file = None
 
class UserContextWinDisk(UserContext):
    ...

Otherwise right now we get ImportErrors if win32file is not available.

Copy link
Collaborator

@BrianPugh BrianPugh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great to me! thank you!

@BrianPugh BrianPugh merged commit a286289 into jrast:master Dec 2, 2023
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants