Skip to content

Preserve pre-existing user templates on save#1375

Merged
amilcarlucas merged 1 commit intomasterfrom
component_template_save_fix
Mar 12, 2026
Merged

Preserve pre-existing user templates on save#1375
amilcarlucas merged 1 commit intomasterfrom
component_template_save_fix

Conversation

@amilcarlucas
Copy link
Copy Markdown
Collaborator

When saving a component template to the user templates file, existing templates for other component types were silently overwritten. Now the file is read before writing and merged so only the saved component's entry is updated while all others are retained.

Also adds broader OSError/Exception handling to _load_system_templates and _load_user_templates so read failures degrade gracefully to an empty dict instead of propagating an unhandled exception.

Copilot AI review requested due to automatic review settings March 12, 2026 22:16
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates vehicle component template persistence so saving a template updates only the relevant component entry while retaining other existing templates in the same JSON file, and improves robustness when reading template files by handling more I/O failure modes gracefully.

Changes:

  • Merge existing user templates from disk when writing updated component templates to avoid overwriting other component types.
  • Add broader OSError / Exception handling in system/user template load helpers to degrade to {} on read failures.
  • Add a regression test ensuring pre-existing user component templates are preserved on save.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
ardupilot_methodic_configurator/backend_filesystem_vehicle_components.py Reads existing templates before writing and expands error handling in template loaders.
tests/test_backend_filesystem_vehicle_components.py Adds a test verifying saving one component template preserves other user templates.
Comments suppressed due to low confidence (1)

ardupilot_methodic_configurator/backend_filesystem_vehicle_components.py:244

  • In save_component_templates_to_file() when save_component_to_system_templates is enabled, filepath is always set to the package resource path (.../vehicle_templates/system_vehicle_components_template.json). If that file doesn’t exist (or the resource directory is read-only), the code still proceeds to write to that resource path because there’s no fallback assignment to os_path.join(templates_dir, templates_filename). This can cause writes to fail or go to the wrong location, and it also means existing system templates in the fallback location won’t be loaded/merged. Set filepath (and existing_templates) to the default templates directory when the local resource file isn’t present, before opening the file for writing.
                local_dir = importlib_files("ardupilot_methodic_configurator") / "vehicle_templates"
                filepath = str(local_dir / templates_filename)
                if os_path.exists(filepath):
                    existing_templates = self._load_system_templates(filepath)
            except (OSError, ValueError) as e:
                logging_debug("Failed to check local template file: %s", e)
                # Fall back to default templates_dir
        else:

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +229 to +231
# Save to "developer" system templates file, only for AMC developers with local a local git repository
# copy of the software who want to add new templates to the system templates file in their local git
# repository and create a github pull-request with the newly added component templates.
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

Typo/grammar in the new comment: “with local a local git repository” should be corrected (duplicate “local”), and “github” should be capitalized as “GitHub”.

Suggested change
# Save to "developer" system templates file, only for AMC developers with local a local git repository
# copy of the software who want to add new templates to the system templates file in their local git
# repository and create a github pull-request with the newly added component templates.
# Save to "developer" system templates file, only for AMC developers with a local git repository
# copy of the software who want to add new templates to the system templates file in their local git
# repository and create a GitHub pull request with the newly added component templates.

Copilot uses AI. Check for mistakes.
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 12, 2026

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
11708 10824 92% 89% 🟢

New Files

No new covered files...

Modified Files

No covered modified files...

updated for commit: 2c4184c by action🐍

@github-actions
Copy link
Copy Markdown
Contributor

Test Results

     4 files  ±0       4 suites  ±0   41m 34s ⏱️ -1s
 3 200 tests +1   3 198 ✅ +1   2 💤 ±0  0 ❌ ±0 
12 592 runs  +4  12 572 ✅ +4  20 💤 ±0  0 ❌ ±0 

Results for commit 147d431. ± Comparison against base commit 59eb235.

When saving a component template to the user templates file, existing
templates for other component types were silently overwritten. Now the
file is read before writing and merged so only the saved component's
entry is updated while all others are retained.

Also adds broader OSError/Exception handling to _load_system_templates
and _load_user_templates so read failures degrade gracefully to an empty
dict instead of propagating an unhandled exception.
@amilcarlucas amilcarlucas force-pushed the component_template_save_fix branch from 147d431 to 2c4184c Compare March 12, 2026 22:49
@amilcarlucas amilcarlucas merged commit 3e02840 into master Mar 12, 2026
27 checks passed
@amilcarlucas amilcarlucas deleted the component_template_save_fix branch March 12, 2026 22: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.

2 participants