Skip to content

PR #65 broke the MagTag constructor in some circumstances #79

Closed
@denschub

Description

@denschub

As far as I can tell, PR #65 broke the MagTag() constructor.

From the "Simple test" example in the docs, you are instructed to use the MagTag constructor to get an instance to the MagTag class, which you need to access MagTag-specific APIs:

from adafruit_magtag.magtag import MagTag

magtag = MagTag()
magtag.add_text(
    text_position=(
        50,
        (magtag.graphics.display.height // 2) - 1,
    ),
    text_scale=3,
)

that constructor ends up calling the Network class, which then ends up calling the constructor from PortalBase' Network:

https://github.com/adafruit/Adafruit_CircuitPython_MagTag/blob/32ec2d95131c5dea8ba33093cb3c5ad92133c9d5/adafruit_magtag/network.py#L70-L74

This is called without any secrets, and that's fine because PortalBase defaults to None. However, if you don't pass None, this piece of code will still be executed:

self._secrets_network = [
{
"ssid": self._secrets["ssid"],
"password": self._secrets["password"],
}
]

secrets here is the global secrets import. This code assumes that ssid and password will always be defined at the root level of the secrets. However, this assumption isn't really documented anywhere, and I've been previously fine with having a secrets structure of, for example,

secrets = {
  "api_root": "https://example.com",
  "wifi": {
    "ssid": "example",
    "password": "example"
  }
}

If you have a structure like mine, the aforementioned access of self._secrets["ssid"] and self._secrets["password"] will fail with a KeyError.

In my very humble opinion, this whole code should make sure that the secrets are actually there, and if not, it probably should just not attempt to connect to a network. Users can connect manually via wifi.radio.connect anyway, and provide the credentials there. Moreover, even if "you need to provide the SSID and password in the secrets root if you want to use networking" would be a acceptable compromise, this would still break the library for users who want to use MagTag-specific things, but aren't actually interested in networking. So either way, a bit of extra protection probably doesn't hurt. :)

I would submit a PR, but unfortunately I'm not set up to build CircuitPython myself, and it looks like it's quite a task to get that done... :(

/cc @tekktrik

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions