From 23593ac4778729b1c09ba1bcda1788a0e3219772 Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Mon, 5 Dec 2022 21:26:16 -0300 Subject: [PATCH] [KiKit Present] Added Template member to list resources - To make easier the get_targets implementation --- kibot/PcbDraw/README.md | 1 + kibot/PcbDraw/present.py | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/kibot/PcbDraw/README.md b/kibot/PcbDraw/README.md index ba724000..990e405f 100644 --- a/kibot/PcbDraw/README.md +++ b/kibot/PcbDraw/README.md @@ -204,4 +204,5 @@ This file comes from KiKit, but it has too much in common with `populate.py`. - Added source_front, source_back and source_gerbers to the boards. So now the images and gerbers come from outside. - Adapted Template._renderBoards to just copy the files (keeping the extensions) +- Added listResources, with some changes to copyRelativeTo and _copyResources diff --git a/kibot/PcbDraw/present.py b/kibot/PcbDraw/present.py index 82c30d5c..aceda045 100644 --- a/kibot/PcbDraw/present.py +++ b/kibot/PcbDraw/present.py @@ -48,13 +48,16 @@ def readTemplate(path): except KeyError: raise RuntimeError("Unknown template type '{}'".format(tType)) -def copyRelativeTo(sourceTree, sourceFile, outputDir): +def copyRelativeTo(sourceTree, sourceFile, outputDir, dry=False): sourceTree = os.path.abspath(sourceTree) sourceFile = os.path.abspath(sourceFile) relPath = os.path.relpath(sourceFile, sourceTree) outputDir = os.path.join(outputDir, os.path.dirname(relPath)) - Path(outputDir).mkdir(parents=True, exist_ok=True) - shutil.copy(sourceFile, outputDir) + dest = os.path.join(outputDir, os.path.basename(sourceFile)) + if not dry: + Path(outputDir).mkdir(parents=True, exist_ok=True) + shutil.copy(sourceFile, outputDir) + return dest class Template: def __init__(self, directory): @@ -66,17 +69,25 @@ class Template: self.name = None self.repository = None - def _copyResources(self, outputDirectory): + def _copyResources(self, outputDirectory, dry=False): """ Copy all resource files specified by template.json and further specified by addResource to the output directory. """ + files = [] for pattern in self.parameters["resources"]: for path in glob.glob(os.path.join(self.directory, pattern), recursive=True): - copyRelativeTo(self.directory, path, outputDirectory) + files.append(copyRelativeTo(self.directory, path, outputDirectory, dry)) for pattern in self.extraResources: for path in glob.glob(pattern, recursive=True): - copyRelativeTo(".", path, outputDirectory) + files.append(copyRelativeTo(".", path, outputDirectory, dry)) + return files + + def listResources(self, outputDirectory): + """ + Returns a list of resources that we will copy. + """ + return self._copyResources(outputDirectory, dry=True) def addResource(self, resource): """