koenvervloesem / bluetooth-numbers

An up-to-date listing of all the various Bluetooth Specification-related elements (Company IDs, Service UUIDs, Characteristic UUIDs and Descriptor UUIDs), that you can use in your Python projects instead of rolling your own.
https://bluetooth-numbers.readthedocs.io
MIT License
16 stars 3 forks source link

Update utility and using oui.csv #46

Open vincentdavis opened 11 months ago

vincentdavis commented 11 months ago

I would like to add the ability to numbers update locally. I am not sure how that looks in the end. I don't think trying to update an installed package is the right thing to do. It seems maybe there is some desire to have no dependencies other than the std python. With this in mind, I am suggesting adding an update_numbers script. If nothing else maybe it makes it easier to update the number for the project.

Data sources:

An update function would be something like this below. It would have an option to use a local file or download from the above source. An option to save the downloaded file. This example retunr a plan Dict, optionally it could return a OUIDICT or can be called from the generate_oui_module.

What are your thoughts?

draft code:

def update_ieee(source_csv: str = '', save: bool = False) -> dict:
    """Update the IEEE OUI database from a file or download."""
    if not source_csv:
        url = "https://standards-oui.ieee.org/oui/oui.csv"
        out_csv = f"{DATA_DIR}/ieee/oui.csv"
        response = get(url)
        if response.status_code == 200:
            # Get the content of the response
            if save:
                with open(out_csv, "wb") as f:
                    # Write the content to the file
                    f.write(response.content)
        else:
            print(f"Error downloading file: {response.status_code}")
            exit()
    if not source_csv:
        reader = csv.DictReader(io.StringIO(response.content.decode("utf-8")))
    else:
        with open(source_csv, "r") as f:
            reader = csv.DictReader(f)
    oui_dictionary: dict[int, str] = {}
    for row in reader:
        assert len(row["Assignment"]) == 6
        oui_hex = ":".join([row["Assignment"][i:i + 2] for i in range(0, 6, 2)])
        oui_dictionary[oui_hex] = row["Organization Name"]

    return oui_dictionary

if __name__ == "__main__":
    ouis = update_ieee()
    examples = list(ouis.keys())[50:60]
    for example in examples:
        print(f"{example} - {ouis[example]}")
koenvervloesem commented 11 months ago

I'm wary of adding functionality for updating the numbers locally. If this project is used more, I think it's better to just create regular minor releases with updated data. And people who really need the latest data can always install this package from the Git repository after generating the modules with the latest data themselves.

I also want users to contribute changes to the data as much as possible to the upstream sources, as explained here: https://bluetooth-numbers.readthedocs.io/en/stable/contributing.html#code-contributions

That said, updating this project's sources is at the moment done manually, so this could be automated a bit more with scripts. Because the Bluetooth SIG now provides their data again in machine-readable format, this is definitely something I'm planning.