Conversation
Co-authored-by: peterus <1764325+peterus@users.noreply.github.com>
|
@copilot kannst du die map Tabelle extern anlegen (zb. als json oder yaml) und diese dann als parameter angeben? Hintergrund ist der das ich das script vielleicht veröffentlichen möchte und der user selber entscheiden soll wie er die Kategorien benennt. auch soll es dann leichter zu erweitern sein. |
Co-authored-by: peterus <1764325+peterus@users.noreply.github.com>
Die Mapping-Tabelle wurde in python3 scripts/bom_export.py \
--csv_file bom.csv \
--categories my_categories.yaml \
...Die YAML-Struktur ist einfach zu erweitern: R: [Resistors, Surface Mount]
My_IC: [My Custom Category, Sub]
|
…category map Co-authored-by: peterus <1764325+peterus@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Refactors the InvenTree BOM/part sync logic from standalone scripts into a reusable scripts/inventree_sync Python package, externalizing the KiCad→InvenTree category mapping into YAML and updating automation to use the new entry point.
Changes:
- Introduces
scripts/inventree_sync/package (models, fetchers, category handling, InvenTree client helpers, and orchestration viaensure_parts_exist). - Replaces the hardcoded category map with
default_categories.yamland adds--categoriesCLI support to load a custom YAML mapping. - Renames the entry script to
bom_export.pyand updates the GitHub Actions workflow accordingly.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| scripts/requirements.txt | Adds PyYAML dependency for YAML category map loading. |
| scripts/part_importer.py | Removes the old monolithic importer script (logic moved into the package). |
| scripts/inventree_sync/models.py | Adds shared dataclasses (PartData, BomEntry). |
| scripts/inventree_sync/fetchers.py | Moves LCSC/Mouser fetching logic into dedicated fetchers. |
| scripts/inventree_sync/categories.py | Adds YAML category-map loading, category resolution, and name generation helpers. |
| scripts/inventree_sync/default_categories.yaml | Provides the default KiCad→InvenTree category mapping. |
| scripts/inventree_sync/client.py | Centralizes InvenTree API helper operations (create/find/update parts and related records). |
| scripts/inventree_sync/part_manager.py | Implements high-level orchestration (ensure_parts_exist) with optional category map injection. |
| scripts/inventree_sync/init.py | Exposes a clean public API for scripts to import. |
| scripts/bom_export.py | Updates BOM export script to use the new package and adds --categories. |
| .github/workflows/create-release-docs.yaml | Updates workflow to call scripts/bom_export.py. |
Comments suppressed due to low confidence (1)
scripts/bom_export.py:25
- The PR description says
load_category_mapis part of the package’s public API (from inventree_sync import ... load_category_map), but this script imports it from the submodule. Importing from the package root would better match the intended API surface and avoids encouraging callers to reach into internal modules.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # category tree. The special keys "package_subcategory_caps" and | ||
| # "package_subcategory_resistors" control which KiCad symbols get an | ||
| # additional package-level sub-category appended automatically. |
There was a problem hiding this comment.
This YAML header comment mentions special keys (e.g. package_subcategory_caps / package_subcategory_resistors) controlling package-level sub-categories, but the Python code currently hardcodes these symbol sets and does not read any special YAML keys. Either remove/adjust this comment to match reality, or implement support for configuring these sets via the YAML file.
| # category tree. The special keys "package_subcategory_caps" and | |
| # "package_subcategory_resistors" control which KiCad symbols get an | |
| # additional package-level sub-category appended automatically. | |
| # category tree. Some KiCad symbols (for example, basic resistors and | |
| # capacitors) may have an additional package-level sub-category appended | |
| # automatically by the synchronization script; this behavior is defined in | |
| # the Python code, not configured via special keys in this YAML file. |
| for key, value in raw.items(): | ||
| if not isinstance(value, list) or not all(isinstance(s, str) for s in value): | ||
| raise SystemExit( | ||
| f"ERROR: invalid entry in {file_path}: key '{key}' must map to " | ||
| f"a list of strings, got {type(value).__name__!r}" |
There was a problem hiding this comment.
load_category_map() assumes the YAML top-level is a mapping. If the YAML file contains a list/string/etc, raw.items() will raise AttributeError, which bypasses the intended SystemExit with a descriptive message. Validate raw is a dict (or coerce/raise a SystemExit with a clear error) before iterating.
| lcsc_fetcher = LCSCFetcher() | ||
| mouser_fetcher = MouserFetcher() | ||
|
|
||
| lcsc_supplier = get_or_create_supplier(api, name="LCSC") | ||
| mouser_supplier = get_or_create_supplier(api, name="Mouser") |
There was a problem hiding this comment.
When category_map is None, resolve_part_category() will call load_category_map() for every BOM entry, repeatedly reading/parsing the YAML file. Consider loading the default category map once at the start of ensure_parts_exist() (before the loop) when no map is provided, and then pass the preloaded dict through.
Two monolithic scripts (
bom-export.py,part_importer.py) handled all InvenTree sync logic with a tight coupling via a singleensure_parts_existimport. Split into a properinventree_syncpackage with clear separation of concerns.Package structure (
scripts/inventree_sync/)models.py–PartData,BomEntrydataclasses (previously duplicated across both scripts)fetchers.py–LCSCFetcher,MouserFetcher(all supplier HTTP logic)categories.py– part-name generation, category resolution,load_category_map()to load the KiCad→InvenTree mapping from a YAML file; logs a warning when a KiCad symbol is not found in the mapdefault_categories.yaml– externalized KiCad symbol → InvenTree category hierarchy map (replaces the hardcoded dict); well-commented and easy to extendclient.py– Low-level InvenTree API helpers (part creation, supplier/manufacturer records, price breaks, image uploads); supplier companies are resolved by name lookup only — no hardcoded PKspart_manager.py–ensure_parts_exist: orchestrates fetch → merge → create pipeline; accepts an optionalcategory_mapparameter__init__.py– Clean public API (BomEntry,PartData,ensure_parts_exist,load_category_map)Entry point
bom-export.py→bom_export.py(valid Python module name); now imports from the package and contains only BOM-export-specific logic:A
--categoriesCLI argument allows users to supply their own YAML mapping file instead of the built-in default:The YAML format is straightforward to extend:
When a KiCad symbol is not found in the category map, a
WARNINGis emitted and the part is placed in the supplier-provided category (if available) orMiscellaneousas a fallback — so the BOM export never silently drops a part.GitHub Actions
Updated
create-release-docs.yamlto invokebom_export.pyinstead of the oldbom-export.py.Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.