A comprehensive Python library for parsing and manipulating KiCad file formats
Project description
KiCadFiles
A comprehensive Python library for parsing and manipulating KiCad file formats.
📚 View Full Documentation - Complete API reference and examples
Features
- Complete KiCad S-expression support: >200 classes representing KiCad tokens (missing tokens? create an issue with examples)
- Type-safe parsing: Full Python type hints for all classes and methods
- Flexible error handling: Three strictness modes (STRICT, FAILSAFE, SILENT)
- Round-trip parsing: Parse KiCad files and convert back to S-expressions
- Minimal dependencies: Self-contained S-expression parsing (no external dependencies)
- Extensive testing: Comprehensive test suite ensuring reliability
Installation
pip install kicadfiles
Quick Start
Basic Usage
from kicadfiles import At, Layer, Footprint, ParseStrictness
# Create objects using dataclass syntax
position = At(x=10.0, y=20.0, angle=90.0)
layer = Layer(name="F.Cu")
# Parse from S-expression
at_obj = At.from_sexpr("(at 10.0 20.0 90.0)", ParseStrictness.STRICT)
# Convert back to S-expression string
sexpr_str = at_obj.to_sexpr_str(pretty_print=False)
print(sexpr_str) # Output: (at 10.0 20.0 90.0)
Parsing with Different Strictness Modes
from kicadfiles import At, ParseStrictness
# STRICT mode: Raises exceptions for any parsing errors
try:
at_obj = At.from_sexpr("(at 10.0 20.0)", ParseStrictness.STRICT)
except ValueError as e:
print(f"Parsing failed: {e}")
# FAILSAFE mode: Logs warnings and uses defaults for missing fields
at_obj = At.from_sexpr("(at 10.0 20.0)", ParseStrictness.FAILSAFE)
print(f"Angle defaulted to: {at_obj.angle}") # Output: None
# SILENT mode: Silently uses defaults for missing fields
at_obj = At.from_sexpr("(at 10.0 20.0)", ParseStrictness.SILENT)
Working with Complex Objects
from kicadfiles import Footprint, Pad, At, Size
# Create a footprint with pads
footprint = Footprint(
library_link="Resistor_SMD:R_0603",
at=At(x=50.0, y=30.0, angle=0.0),
pads=[
Pad(
number="1",
type="smd",
shape="roundrect",
at=At(x=-0.8, y=0.0),
size=Size(width=0.7, height=0.9)
),
Pad(
number="2",
type="smd",
shape="roundrect",
at=At(x=0.8, y=0.0),
size=Size(width=0.7, height=0.9)
)
]
)
# Convert to S-expression
sexpr = footprint.to_sexpr_str(pretty_print=True)
print(sexpr)
API Overview
Core Classes
- KiCadObject: Base class for all KiCad objects
- ParseStrictness: Enum controlling error handling (STRICT, FAILSAFE, SILENT)
Main File Format Classes
These classes represent complete KiCad file formats and support both from_file() and save_to_file() methods:
- KicadSymbolLib: Symbol library files (.kicad_sym)
- KicadPcb: PCB board files (.kicad_pcb)
- KicadSch: Schematic files (.kicad_sch)
- KicadWks: Worksheet files (.kicad_wks)
- Footprint: Individual footprint files (.kicad_mod)
- KicadProject: Project settings files (.kicad_pro)
Main Object Categories
- Base Types (37 classes): At, Size, Layer, Stroke, etc.
- Text and Documents (27 classes): TitleBlock, Page, Comment, etc.
- Pad and Drill (18 classes): Pad, Drill, Via, etc.
- Graphics (28 classes): Line, Circle, Arc, Polygon, etc.
- Symbol Library (15 classes): Symbol, Pin, Property, etc.
- Footprint Library (12 classes): Footprint, Model, Tags, etc.
- Zone System (28 classes): Zone, Hatch, FilledPolygon, etc.
- Board Layout (15 classes): General, Layers, Nets, etc.
- Schematic System (13 classes): Wire, Junction, Label, etc.
Error Handling
The library provides three levels of error handling:
- STRICT: Raises
ValueErrorfor any parsing errors - FAILSAFE: Logs warnings and uses default values for missing fields
- SILENT: Silently uses default values without warnings
# Handle parsing errors gracefully
from kicadfiles import Footprint, ParseStrictness
import logging
logging.basicConfig(level=logging.WARNING)
# This will log warnings but continue parsing
footprint = Footprint.from_sexpr(
"(footprint incomplete_data)",
ParseStrictness.FAILSAFE
)
For a complete overview of all classes and their module organization, see kicadfiles/CLASSES.md.
Development
Setting up Development Environment
git clone https://github.com/Steffen-W/KiCadFiles.git
cd KiCadFiles
pip install -e ".[dev]"
Running Tests
pytest tests/ -v
Code Formatting
# Complete pipeline (recommended)
black .
isort .
flake8 kicadfiles
pyright kicadfiles
mypy kicadfiles
Linting
flake8 kicadfiles/
Type Checking
mypy kicadfiles/
pyright kicadfiles/
Documentation
cd docs && make clean && make html && cd ..
# Open docs/build/html/index.html in browser
Coverage
pytest --cov=kicadfiles --cov-report=html tests/
# Open htmlcov/index.html in browser
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
If you encounter any problems or have questions, please open an issue on the GitHub repository.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kicadfiles-1.0.0.tar.gz.
File metadata
- Download URL: kicadfiles-1.0.0.tar.gz
- Upload date:
- Size: 80.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1bcdae65d4a9ce04eaa9700ebeed8bbd88244a126a97ed3d9060ba1724a783f5
|
|
| MD5 |
313c9549d5db4a6dd2d2d8e0d840f347
|
|
| BLAKE2b-256 |
fe899f1992c84c2ee92f2d6cb70a9b574d26bb685ab4ebd0625984f1984ee4fe
|
Provenance
The following attestation bundles were made for kicadfiles-1.0.0.tar.gz:
Publisher:
publish.yml on Steffen-W/KiCadFiles
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kicadfiles-1.0.0.tar.gz -
Subject digest:
1bcdae65d4a9ce04eaa9700ebeed8bbd88244a126a97ed3d9060ba1724a783f5 - Sigstore transparency entry: 557430705
- Sigstore integration time:
-
Permalink:
Steffen-W/KiCadFiles@0e3a4849e8195b062a3311aca60e036d5ec7843e -
Branch / Tag:
refs/heads/master - Owner: https://github.com/Steffen-W
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@0e3a4849e8195b062a3311aca60e036d5ec7843e -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file kicadfiles-1.0.0-py3-none-any.whl.
File metadata
- Download URL: kicadfiles-1.0.0-py3-none-any.whl
- Upload date:
- Size: 76.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eee6bd0c7469bf16643a4f336b5663a4fdb2cd2015aa470cd524e8da88b15216
|
|
| MD5 |
5f9692b64b981c584c9dc1ed7ecbb0b7
|
|
| BLAKE2b-256 |
5a5d9bd26438bb0684a085e85cc87d8e3e9f87af90603b09c84f363aee67ee9c
|
Provenance
The following attestation bundles were made for kicadfiles-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on Steffen-W/KiCadFiles
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kicadfiles-1.0.0-py3-none-any.whl -
Subject digest:
eee6bd0c7469bf16643a4f336b5663a4fdb2cd2015aa470cd524e8da88b15216 - Sigstore transparency entry: 557430716
- Sigstore integration time:
-
Permalink:
Steffen-W/KiCadFiles@0e3a4849e8195b062a3311aca60e036d5ec7843e -
Branch / Tag:
refs/heads/master - Owner: https://github.com/Steffen-W
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@0e3a4849e8195b062a3311aca60e036d5ec7843e -
Trigger Event:
workflow_dispatch
-
Statement type: