From e13dac3c5322c70cf6c994b65d47be17b78d22ac Mon Sep 17 00:00:00 2001 From: kerem Date: Thu, 23 Nov 2023 22:26:59 +0100 Subject: [PATCH] New revision of the library import tool --- project_init_tool/KicED_libLinker.py | 129 ++++++++++++++++----------- project_init_tool/linkKicEDlib.py | 23 ----- 2 files changed, 75 insertions(+), 77 deletions(-) delete mode 100644 project_init_tool/linkKicEDlib.py diff --git a/project_init_tool/KicED_libLinker.py b/project_init_tool/KicED_libLinker.py index c4369b0..e0e0584 100644 --- a/project_init_tool/KicED_libLinker.py +++ b/project_init_tool/KicED_libLinker.py @@ -1,82 +1,102 @@ import os import glob -def getFootprinLibrarys(pathTofootprints): - cwd = os.getcwd() - os.chdir(footprintPath) +scriptDir = os.getcwd() + +def getKickedAbsPath(): + ''' + Returns Kicked's absolute path and goes back to the scrip's path + ''' + os.chdir('..') + path = os.getcwd() + os.chdir(scriptDir) + return path + +def getFootprinLibraries(): + ''' + Returns the list of all .pretty footprint files and goes back to the scrip's path + ''' + footpintPath = os.path.join(getKickedAbsPath(), "footprints") + os.chdir(footpintPath) + footprintList = [] - files = os.listdir() + prettyFiles = os.listdir() # append all .pretty folders - for f in files: + for f in prettyFiles: if f.endswith(".pretty"): footprintList.append(f) - - os.chdir(cwd) - return footprintList -def getSymbolsLibrarys(pathToSymbols): - search_pattern = os.path.join(pathToSymbols, "*.kicad_sym") - return glob.glob(search_pattern) + os.chdir(scriptDir) + return footprintList -pathToProjectFolder = input("enter path to KiCad project folder:") -# move one directory back into the main KicED library folder -# https://stackoverflow.com/questions/12280143/how-to-move-to-one-folder-back-in-python -os.path.normpath(os.getcwd() + os.sep + os.pardir) +def getSymbolLibraries(): + ''' + Returns the list of all .kicad_sym simbol files and goes back to the scrip's path + ''' + symbolPath = os.path.join(getKickedAbsPath(), "symbols") + os.chdir(symbolPath) -cwd = os.getcwd() + symboList = [] + symFiles = os.listdir() + symbolList = [] + # append all .pretty folders + for f in symFiles: + if f.endswith(".kicad_sym"): + symbolList.append(f) -print(pathToProjectFolder) -print("Curent Working Path") -print(cwd) + os.chdir(scriptDir) + return symbolList -# Kiced Folder -os.chdir("..") -cwdOneBack = os.getcwd() -os.chdir(cwd) -# find common path -commonPath = os.path.commonprefix([cwd, pathToProjectFolder]) +# Gets the inital paths required +footprintLibraries = getFootprinLibraries() +symbolLibraries = getSymbolLibraries() -# KicED library paths -footprintPath = os.path.join(cwdOneBack, "footprints") -symbolsPath = os.path.join(cwdOneBack, "symbols") +# User input to get the path to the project +pathToProjectFolder = input("enter path to KiCad project folder:") # create relative path from KiCad project and the KicED library -relative_path = os.path.relpath(cwd, pathToProjectFolder) -print(relative_path) +relative_path = os.path.relpath(getKickedAbsPath(),pathToProjectFolder) +# Translates the relative paht ot what kicad project needs from kiced baseRelativeBasePath = os.path.join("${KIPRJMOD}", relative_path) -print(baseRelativeBasePath) - -footprintLibPaths = getFootprinLibrarys(footprintPath) -symbolLibPaths = getSymbolsLibrarys(symbolsPath) temp = [] -for f in footprintLibPaths: - pth = os.path.join(baseRelativeBasePath, "symbols") - temp.append(os.path.join(pth, f)) +for item in footprintLibraries: + pth = os.path.join(baseRelativeBasePath, "footprints") + tmp = os.path.join(pth, item) + temp.append(tmp) footprintLibPaths = temp temp = [] -for f in symbolLibPaths: - pth = os.path.relpath(f,cwd) - pth = os.path.join(baseRelativeBasePath,pth) - temp.append(pth) +for item in symbolLibraries: + pth = os.path.join(baseRelativeBasePath, "symbols") + tmp = os.path.join(pth,item) + temp.append(tmp) symbolLibPaths = temp -if 1: - for f in footprintLibPaths: - print(f) +# INFO PRINT # +print("Selected Project Path:") +print(pathToProjectFolder) +print("Curent Working Path:") +print(scriptDir) +print("Relative path from Project to Kiced:") +print(relative_path) +print("Formated relative path to kiced library relative to project directory") +print(baseRelativeBasePath) +print("Including following libraries:") +for f in footprintLibPaths: + print(f) + +for f in symbolLibPaths: + print(f) - for f in symbolLibPaths: - print(f) # creating footprint lib link file os.chdir(pathToProjectFolder) -#print(pathToProjectFolder) try: with open("fp-lib-table",'w') as file: file.write("(fp_lib_table\n (version 7)\n") @@ -89,16 +109,15 @@ try: file.write(" (lib (name \"") temp = os.path.basename(p) file.write(temp[:-len(".pretty")]) - file.write("\")(type \"KiCad\")(uri \"") file.write(p) file.write("\")(options \"\")(descr \"\"))\n") - file.write(")") - os.chdir(cwd) + file.write(")") + os.chdir(scriptDir) except FileNotFoundError: print("fail") - os.chdir(cwd) + os.chdir(scriptDir) # creating symbol lib link file os.chdir(pathToProjectFolder) @@ -118,8 +137,10 @@ try: file.write(p) file.write("\")(options \"\")(descr \"\"))\n") - file.write(")") - os.chdir(cwd) + file.write(")") + os.chdir(scriptDir) except FileNotFoundError: print("fail") - os.chdir(cwd) + os.chdir(scriptDir) + +print("Import Done!") diff --git a/project_init_tool/linkKicEDlib.py b/project_init_tool/linkKicEDlib.py deleted file mode 100644 index c3efad6..0000000 --- a/project_init_tool/linkKicEDlib.py +++ /dev/null @@ -1,23 +0,0 @@ -import tkinter as tk -from tkinter import filedialog - -def select_folder(): - folder_path = filedialog.askdirectory() - folder_label.config(text="Selected Folder: " + folder_path) - -# Create the main window -root = tk.Tk() -root.title("KicED library linker") - -# Create a label to display the selected folder -folder_label = tk.Label(root, text="Selected KiCad project folder: ") -folder_label.pack(pady=10) - -# Create a button to open the file dialog -select_button = tk.Button(root, text="Select Folder", command=select_folder) -select_button.pack() - - -# Start the GUI application -root.mainloop() -