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

Command Line Interface for LittleFS-Python #50

Merged
merged 5 commits into from
Sep 13, 2023
Merged

Command Line Interface for LittleFS-Python #50

merged 5 commits into from
Sep 13, 2023

Conversation

jrast
Copy link
Owner

@jrast jrast commented Sep 7, 2023

No description provided.

@ianlevesque
Copy link

Being able to specify the total size instead of number of blocks would be more convenient. If it could accept those parameters as either integers or hexadecimal that would also make it easier to send over parameters right out of partitions.csv on esp-idf.

@BrianPugh
Copy link
Collaborator

per @ianlevesque , you can add something like:

# Dictionary mapping suffixes to their size in bytes
_suffix_map = {
    "kb": 1024,
    "mb": 1024**2,
    "gb": 1024**3,
}


def int_parser(size_str):
    size_str = str(size_str).lower()

    # Check if the string starts with '0x', which indicates a hexadecimal number
    if size_str.startswith("0x"):
        base = 16
        size_str = size_str[2:]
    else:
        base = 10

    # Separate the size number and suffix
    for suffix, multiplier in _suffix_map.items():
        if size_str.endswith(suffix):
            num_part = size_str[: -len(suffix)]
            return int(num_part, base) * multiplier

    # Handle base units; remove base suffix
    if size_str.endswith("b"):
        size_str = size_str[:-1]

    # If no suffix, assume it's in bytes
    return int(size_str, base)

And then in the add_argument call, specify type=int_parser. This should handle vanilla integers, hexadecimal, as well as numbers with suffixes like 10MB.

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.

Added a few comments, mostly just alternative ways to accomplish similar goals with argparse. Feel free to disregard, as most of the comments are more stylistic than functionality. Functionality-wise, I think this PR is great. Thank you!

src/littlefs/__main__.py Show resolved Hide resolved
src/littlefs/__main__.py Outdated Show resolved Hide resolved
src/littlefs/__main__.py Outdated Show resolved Hide resolved
src/littlefs/__main__.py Show resolved Hide resolved
src/littlefs/__main__.py Outdated Show resolved Hide resolved
src/littlefs/__main__.py Show resolved Hide resolved
@BrianPugh
Copy link
Collaborator

To accomplish @ianlevesque feature request of providing --block-size plus a total --size, we can use the add_mutually_exclusive_group method with --size and --block-count as members of it.

@jrast
Copy link
Owner Author

jrast commented Sep 10, 2023

With the latest commit most everything mentioned above should be available.

@BrianPugh
Copy link
Collaborator

I haven't actually ran the code, but it all looks very good!

@BrianPugh
Copy link
Collaborator

@ianlevesque have you had a chance to play around with it to see if we can now use this as a suitable mklittlefs replacement in esp_littlefs?

@ianlevesque
Copy link

I very quickly updated the PR joltwallet/esp_littlefs#140 to use this, and it works. Only caveat is that there are two config parameters, CONFIG_LITTLEFS_OBJ_NAME_LEN & CONFIG_LITTLEFS_PAGE_SIZE, that can no longer be passed in. If they were added as parameters then this would have all of the functionality from mklittlefs before. littlefs-python does have them, just not exposed as CLI parameters on this new script

@BrianPugh
Copy link
Collaborator

I'm like 99% sure mklittlefs doesn't do anything with it's passed in --page paramenter (CONFIG_LITTLEFS_PAGE_SIZE), and littlefs doesn't exactly care about it either, so I think we can safely ignore that.

Supporting a custom LFS_NAME_MAX is a bit more difficult, since that would require recompilation of C code.

I think we should just assert it to be the default 255 in esp_littlefs if a custom image is used, there's very little benefit in specifying a smaller value. The only benefit is reducing the struct size of lfs_info, which is populated by lfs_stat and lfs_dir_read.

@ianlevesque
Copy link

I'm like 99% sure mklittlefs doesn't do anything with it's passed in --page paramenter (CONFIG_LITTLEFS_PAGE_SIZE), and littlefs doesn't exactly care about it either, so I think we can safely ignore that.
...
I think we should just assert it to be the default 255 in esp_littlefs if a custom image is used

No other concerns here then.

@jrast jrast merged commit f049525 into master Sep 13, 2023
15 checks passed
@jrast jrast deleted the cli-interface branch September 13, 2023 20:53
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.

3 participants