Skip to content

Commit 3171b63

Browse files
author
devbisme
committed
Moved checking of overwrite and merge operations into the row_file_to_symbol_file function.
1 parent baa86ad commit 3171b63

File tree

1 file changed

+27
-36
lines changed

1 file changed

+27
-36
lines changed

kipart/kipart.py

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,6 +1534,7 @@ def row_file_to_symbol_lib_file(
15341534
default_style=DEFAULT_STYLE,
15351535
alt_pin_delim=None,
15361536
overwrite=False,
1537+
merge=False,
15371538
bundle=False,
15381539
scrunch=False,
15391540
ccw=False,
@@ -1573,6 +1574,8 @@ def row_file_to_symbol_lib_file(
15731574
alternatives. Defaults to None (no splitting).
15741575
overwrite (bool, optional): Allow overwriting or merging with existing output file.
15751576
Defaults to False.
1577+
merge (bool, optional): If overwriting, merge new symbols with existing library.
1578+
Defaults to False.
15761579
bundle (int, optional): Bundle identically-named power or ground pins into single pins.
15771580
If bundle > 1, bundles NC pins as well.
15781581
Defaults to 0.
@@ -1600,15 +1603,23 @@ def row_file_to_symbol_lib_file(
16001603
FileNotFoundError: If the input file doesn't exist.
16011604
"""
16021605

1603-
# Determine output filename for the symbol library
16041606
if not symbol_lib_file:
1607+
# If there's no output file specified, use the input filename with .kicad_sym extension
16051608
symbol_lib_file = os.path.splitext(row_file)[0] + ".kicad_sym"
1609+
else:
1610+
# Make sure given symbol file is a KiCad symbol file
1611+
if os.path.splitext(symbol_lib_file)[1] != ".kicad_sym":
1612+
raise ValueError(f"Output file {symbol_lib_file} must have a .kicad_sym extension")
16061613

1607-
# Check for an existing file
1608-
if os.path.exists(symbol_lib_file) and not overwrite:
1609-
raise ValueError(
1610-
f"Output file {symbol_lib_file} already exists and overwriting has not been enabled."
1611-
)
1614+
# Check overwriting and merging operations for an existing file
1615+
if os.path.exists(symbol_lib_file):
1616+
if not overwrite:
1617+
raise ValueError(
1618+
f"Output file {symbol_lib_file} already exists and overwriting has not been enabled."
1619+
)
1620+
if not merge:
1621+
# Clear out the file if it exists and we're not merging
1622+
os.truncate(symbol_lib_file, 0)
16121623

16131624
# Read rows of symbol pin data from CSV or Excel file.
16141625
rows = read_row_file(row_file)
@@ -1642,7 +1653,7 @@ def row_file_to_symbol_lib_file(
16421653
# Merge the existing library with the new one
16431654
symbol_lib = merge_symbol_libs(existing_lib, symbol_lib, overwrite=True)
16441655
except Exception as e:
1645-
print(f"Warning: Could not merge with existing library: {str(e)}")
1656+
print(f"Warning: Could not merge {row_file} with existing library {symbol_lib_file}: {str(e)}")
16461657
print("Creating a new library instead.")
16471658
# Continue with the original symbol_lib
16481659

@@ -1652,6 +1663,10 @@ def row_file_to_symbol_lib_file(
16521663
# Store the symbol library as an S-expression in the output file.
16531664
with open(symbol_lib_file, "w") as f:
16541665
f.write(str(symbol_lib))
1666+
if merge:
1667+
print(f"Merged symbols from {row_file} into existing symbol library {symbol_lib_file}")
1668+
else:
1669+
print(f"Created symbol library {symbol_lib_file} from {row_file}")
16551670

16561671
return symbol_lib_file
16571672

@@ -1901,27 +1916,8 @@ def kipart():
19011916
if args.merge and not args.output:
19021917
args.output = os.path.splitext(args.input_files[0])[0] + ".kicad_sym"
19031918

1904-
# Do some checks on the output file if given.
1905-
if args.output:
1906-
1907-
# Make sure it's a KiCad symbol file.
1908-
if os.path.splitext(args.output)[1] != ".kicad_sym":
1909-
print(f"Error: Output file {args.output} must have a .kicad_sym extension")
1910-
sys.exit(1)
1911-
1912-
# If the output file already exists and we're not overwriting, exit.
1913-
if os.path.exists(args.output) and not args.overwrite:
1914-
print(
1915-
f"Error: Output file {args.output} already exists. Use -w to allow changes to it."
1916-
)
1917-
sys.exit(1)
1918-
1919-
# Clear out the file if it exists and we're not merging
1920-
if os.path.exists(args.output) and not args.merge:
1921-
os.truncate(args.output, 0)
1922-
19231919
# Process each input file containing rows of symbol pin data
1924-
error_flag = False
1920+
num_errors = 0
19251921
for row_file in args.input_files:
19261922
try:
19271923
symbol_lib_file = row_file_to_symbol_lib_file(
@@ -1934,6 +1930,7 @@ def kipart():
19341930
default_style=default_style,
19351931
alt_pin_delim=args.alt_delimiter,
19361932
overwrite=args.overwrite,
1933+
merge=args.merge,
19371934
bundle=args.bundle,
19381935
scrunch=args.scrunch,
19391936
ccw=args.ccw,
@@ -1944,12 +1941,6 @@ def kipart():
19441941
justify=args.justify,
19451942
)
19461943

1947-
if args.merge:
1948-
print(f"Merged symbols from {row_file} into {symbol_lib_file}")
1949-
args.overwrite = True # Allow overwriting for subsequent files
1950-
else:
1951-
print(f"Created {symbol_lib_file} successfully from {row_file}")
1952-
19531944
# If output is going to a single file, any subsequent files
19541945
# will be merged into it. So set the merge flag to True.
19551946
if args.output:
@@ -1959,11 +1950,11 @@ def kipart():
19591950
except Exception as e:
19601951
# raise
19611952
print(f"Error: Failed while processing file '{row_file}': {str(e)}")
1962-
error_flag = True
1953+
num_errors += 1
19631954
continue
19641955

1965-
if error_flag:
1966-
print("Errors occurred during processing. Please check the output above.")
1956+
if num_errors:
1957+
print(f"A total of {num_errors} errors occurred during processing. Please check the output above.")
19671958
sys.exit(1)
19681959

19691960

0 commit comments

Comments
 (0)