Skip to content

Add Shell.append_if_missing() helper#39

Merged
makermelissa merged 1 commit into
adafruit:mainfrom
makermelissa-piclaw:add-append-if-missing
Jun 16, 2026
Merged

Add Shell.append_if_missing() helper#39
makermelissa merged 1 commit into
adafruit:mainfrom
makermelissa-piclaw:add-append-if-missing

Conversation

@makermelissa-piclaw

Copy link
Copy Markdown
Contributor

Adds Shell.append_if_missing(file, line) — the common "add a line only if it isn't already there" idiom that the installer scripts repeat constantly for /etc/modules, modprobe blacklists, and config.txt entries.

It's the Python equivalent of:

grep -qxF "line" file || echo line >> file

Complements the existing reconfig() (which is uncomment-or-append for a regex pattern). append_if_missing is for the simpler exact-line case where you just want to guarantee a literal line is present without disturbing anything else.

Uses re.escape so lines containing regex metacharacters (e.g. dtoverlay=i2s-mmap) are matched literally rather than as patterns.

Context

Suggested by @makermelissa on adafruit/Raspberry-Pi-Installer-Scripts#398, where several converted installers each define a private copy of this helper. Landing it here lets those scripts share one implementation.

Testing

  • ruff format + ruff check + reuse lint (pre-commit) all pass.
  • Manually verified: no duplicate when the line is already present (anywhere in the file), appends when absent, and metacharacter-containing lines dedup correctly.

Adds the common grep-or-append idiom (grep -qxF line file || echo
line >> file) as a reusable method, complementing reconfig(). The
installer scripts repeat this pattern constantly for /etc/modules,
blacklist, and config.txt entries. Uses re.escape so lines with
regex metacharacters match literally.

@makermelissa makermelissa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is similar to reconfig, but it won't update an existing string.

@makermelissa makermelissa merged commit e119361 into adafruit:main Jun 16, 2026
1 check passed
makermelissa-piclaw added a commit to makermelissa-piclaw/Raspberry-Pi-Installer-Scripts that referenced this pull request Jun 16, 2026
Drop the local append_if_missing helper now that it's available in the
shared library (adafruit/Adafruit_Python_Shell#39).
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