Skip to content

Commit

Permalink
Better schematics filename handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MitjaNemec committed Apr 3, 2023
1 parent 54f46b2 commit d859cc4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
13 changes: 10 additions & 3 deletions save_restore_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,9 @@ def __init__(self, board, dont_parse_schematics=False):
# TODO check if there is any other footprint fit same ID as anchor footprint

def parse_schematic_files(self, filename, dict_of_sheets):
with open(filename) as f:
with open(filename, encoding='utf-8') as f:
contents = f.read().split("\n")
filename_dir = os.path.dirname(filename)
# find (sheet (at and then look in next few lines for new schematics file
for i in range(len(contents)):
line = contents[i]
Expand All @@ -199,6 +200,7 @@ def parse_schematic_files(self, filename, dict_of_sheets):
sn_found = False
sf_found = False
for j in range(i,i+10):
line_con = contents[j]
if "(uuid " in contents[j]:
path = contents[j].replace("(uuid ", '').rstrip(")").upper().strip()
sheet_id = path.replace('00000000-0000-0000-0000-0000', '')
Expand All @@ -223,10 +225,15 @@ def parse_schematic_files(self, filename, dict_of_sheets):
raise LookupError(f'Did not found sheetfile and/or sheetname properties in the schematic file '
f'in {filename} line:{str(i)}. Unsupported schematics file format')

sheetfilepath = os.path.join(filename_dir, sheetfile)
# here I should find all sheet data
dict_of_sheets[sheet_id] = [sheetname, sheetfile]
dict_of_sheets[sheet_id] = [sheetname, sheetfilepath]
# test if newfound file can be opened
if not os.path.exists(sheetfilepath):
raise LookupError(f'File {sheetfilepath} does not exists. This is either due to error in parsing'
f' schematics files, missing schematics file or an error within the schematics')
# open a newfound file and look for nested sheets
self.parse_schematic_files(sheetfile, dict_of_sheets)
self.parse_schematic_files(sheetfilepath, dict_of_sheets)
return

def get_fp_by_ref(self, ref):
Expand Down
21 changes: 21 additions & 0 deletions test_save_restore_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@

class TestSave(unittest.TestCase):

def test_path(self):
prj_dir = os.path.normpath(os.path.dirname(os.path.join(os.path.dirname(os.path.realpath(__file__)),
"SaveRestoreSourceProject_path_issue/")))
source_file = os.path.join(prj_dir, 'save_restore_source_project.kicad_pcb')

board = pcbnew.LoadBoard(source_file)
src_anchor_fp_ref = 'L401'
save_layout = SaveLayout(board, src_anchor_fp_ref)

# get the level from user
level = 1

data_file = os.path.join(prj_dir, 'source_layout_test_shallow.pckl')
save_layout.save_layout(save_layout.src_anchor_fp.sheet_id[0:level + 1], data_file,
True, True, True, True, True)

@unittest.SkipTest
def test_save_shallow(self):
prj_dir = os.path.normpath(os.path.dirname(os.path.join(os.path.dirname(os.path.realpath(__file__)),
"SaveRestoreSourceProject/")))
Expand All @@ -28,6 +45,7 @@ def test_save_shallow(self):
save_layout.save_layout(save_layout.src_anchor_fp.sheet_id[0:level + 1], data_file,
True, True, True, True, True)

@unittest.SkipTest
def test_save_deep(self):
prj_dir = os.path.normpath(os.path.dirname(os.path.join(os.path.dirname(os.path.realpath(__file__)),
"SaveRestoreSourceProject/")))
Expand All @@ -44,6 +62,7 @@ def test_save_deep(self):
save_layout.save_layout(save_layout.src_anchor_fp.sheet_id[0:level + 1], data_file,
True, True, True, True, True)

@unittest.SkipTest
def test_restore_shallow_different_level(self):
prj_dir = os.path.normpath(os.path.dirname(os.path.join(os.path.dirname(os.path.realpath(__file__)),
"SaveRestoreDestinationProject/")))
Expand All @@ -59,6 +78,7 @@ def test_restore_shallow_different_level(self):

saved = pcbnew.SaveBoard(destination_file.replace(".kicad_pcb", "_shallow_different.kicad_pcb"), board)

@unittest.SkipTest
def test_restore_shallow_same_level(self):
prj_dir = os.path.normpath(os.path.dirname(os.path.join(os.path.dirname(os.path.realpath(__file__)),
"SaveRestoreDestinationProject/")))
Expand All @@ -74,6 +94,7 @@ def test_restore_shallow_same_level(self):

saved = pcbnew.SaveBoard(destination_file.replace(".kicad_pcb", "_shallow_same.kicad_pcb"), board)

@unittest.SkipTest
def test_restore_deep(self):
prj_dir = os.path.normpath(os.path.dirname(os.path.join(os.path.dirname(os.path.realpath(__file__)),
"SaveRestoreDestinationProject/")))
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.6
2.0.7

0 comments on commit d859cc4

Please sign in to comment.