skidl.netlist_to_skidl module

Convert a KiCad netlist into equivalent hierarchical SKiDL programs.

This module enables the conversion of KiCad netlists to SKiDL Python scripts, preserving the hierarchical structure. The resulting SKiDL scripts can be used to regenerate the circuit or as a starting point for circuit modifications.

class skidl.netlist_to_skidl.HierarchicalConverter(src)[source]

Bases: object

Converts a KiCad netlist into hierarchical SKiDL Python scripts.

This class analyzes a KiCad netlist and generates equivalent SKiDL scripts that preserve the hierarchical structure of the original schematic.

analyze_nets()[source]

Analyze net usage to determine origins and required connections.

This method examines how nets are used across sheets to determine which sheets need to pass nets to their children, which nets are local, and which are imported.

assign_components_to_sheets()[source]

Assign each component from the netlist to its appropriate sheet.

This method assigns components to their respective sheets based on the sheet path information in the netlist.

component_to_skidl(comp: object) str[source]

Return a SKiDL instantiation string for a component.

This method generates SKiDL code to instantiate a component with all its relevant properties preserved.

Parameters:

comp (object) – A component object from the netlist

Returns:

SKiDL code to instantiate the component

Return type:

str

convert(output_dir: str = None)[source]

Run the complete conversion and write files if output_dir is provided.

This method performs the full netlist-to-SKiDL conversion process and optionally writes the generated Python files to a directory.

Parameters:

output_dir (str, optional) – Directory to write the generated Python files. If None, files are not written.

Returns:

If output_dir is None, returns the main sheet code as a string.

Otherwise, returns an empty string after writing files.

Return type:

str

cull_from_top()[source]

Remove top-level sheets that are not needed.

This method simplifies the hierarchy by removing unnecessary empty top-level sheets, making the resulting SKiDL code more concise.

extract_sheet_info()[source]

Populate self.sheets with Sheet objects built from the netlist.

This method examines the netlist to identify all sheets and their hierarchical relationships, creating Sheet objects to represent them.

find_lowest_common_ancestor(sheet1, sheet2)[source]

Return the lowest common ancestor (LCA) of two sheets.

This method finds the sheet that is the closest common parent of two sheets in the schematic hierarchy.

Parameters:
  • sheet1 (str) – Path to the first sheet

  • sheet2 (str) – Path to the second sheet

Returns:

Path to the lowest common ancestor sheet

Return type:

str

generate_main_code()[source]

Generate the code for the main.py file that creates nets and calls subcircuits.

This method creates the top-level Python script that imports and calls the top-level subcircuit and generates the final netlist.

Returns:

SKiDL Python code for the main script

Return type:

str

generate_sheet_code(sheet: Sheet) str[source]

Generate the SKiDL code for a given sheet.

This method creates a complete SKiDL subcircuit function for a sheet, including component instantiation, connections, and calls to child subcircuits.

Parameters:

sheet (Sheet) – The sheet to generate code for

Returns:

Complete SKiDL Python code for the sheet as a subcircuit

Return type:

str

net_to_skidl(net: object, sheet: Sheet) str[source]

Return a SKiDL connection string for a net within a given sheet.

This method generates SKiDL code to connect pins to a net within a sheet.

Parameters:
  • net (object) – A net object from the netlist

  • sheet (Sheet) – The sheet context for this net connection

Returns:

SKiDL code to connect pins to the net, or an empty string if

no connections are needed in this sheet

Return type:

str

class skidl.netlist_to_skidl.NetSexp(sexp)[source]

Bases: object

This class delivers attributes from a net S-expression.

class skidl.netlist_to_skidl.NetlistSexp(sexp)[source]

Bases: object

Represents a KiCad netlist.

This class encapsulates the structure of a KiCad netlist, including its parts, nets, and sheets. It provides methods to access and manipulate the netlist data.

parts

List of components in the netlist

Type:

List

nets

List of electrical nets in the netlist

Type:

List

sheets

List of hierarchical sheets in the netlist

Type:

List

class skidl.netlist_to_skidl.PartSexp(sexp)[source]

Bases: object

This class delivers attributes from a component S-expression.

class skidl.netlist_to_skidl.PinSexp(sexp)[source]

Bases: object

This class delivers attributes from a pin S-expression.

class skidl.netlist_to_skidl.PropertySexp(sexp)[source]

Bases: object

This class delivers attributes from a property S-expression.

class skidl.netlist_to_skidl.Sheet(**attribs)[source]

Bases: object

Represents a hierarchical sheet from a KiCad schematic.

This object stores information about a sheet in the schematic hierarchy, including its components, nets, and relationship to other sheets.

number

Sheet number in the hierarchy

Type:

str

name

Sheet name

Type:

str

path

Full hierarchical path to the sheet

Type:

str

components

Components contained in this sheet

Type:

List

local_nets

Nets that originate in this sheet

Type:

Set[str]

imported_nets

Nets that are imported from parent/other sheets

Type:

Set[str]

parent

Path to the parent sheet, or None for top-level sheets

Type:

str

children

Paths to the child sheets of this sheet

Type:

List[str]

class skidl.netlist_to_skidl.SheetSexp(sexp)[source]

Bases: object

This class delivers attributes from a sheet S-expression.

skidl.netlist_to_skidl.find_common_path_prefix(path1: str, path2: str) str[source]

Return the common path prefix of two paths.

This function finds the longest common hierarchical path prefix between two paths.

Parameters:
  • path1 (str) – First path

  • path2 (str) – Second path

Returns:

The common path prefix including the trailing separator

Return type:

str

Examples

>>> find_common_path_prefix("/path/to/sheet1/", "/path/to/sheet2/")
"/path/to/"
skidl.netlist_to_skidl.legalize_name(name: str, is_filename: bool = False) str[source]

Return a version of name that is a legal Python identifier.

This function converts arbitrary strings to valid Python identifiers by replacing illegal characters with underscores and handling special cases for names starting with numbers or containing special symbols.

Parameters:
  • name (str) – The original name to convert

  • is_filename (bool, optional) – Whether the name will be used as a filename. Defaults to False.

Returns:

A legal Python identifier derived from the input name

Return type:

str

Examples

>>> legalize_name("1wire")
"_1wire"
>>> legalize_name("5V+")
"_5V_p"
skidl.netlist_to_skidl.netlist_to_skidl(netlist_src: str, output_dir: str = None)[source]

Convert a KiCad netlist to hierarchical SKiDL Python files.

This function creates a HierarchicalConverter and uses it to convert a KiCad netlist into SKiDL Python scripts.

Parameters:
  • netlist_src (str) – Path to a KiCad netlist file or a string containing netlist data

  • output_dir (str, optional) – Directory to write the generated Python files. If None, files are not written.

Returns:

If output_dir is None, returns the main sheet code as a string.

Otherwise, returns an empty string after writing files.

Return type:

str