[KiKit Present] Added Template member to list resources

- To make easier the get_targets implementation
This commit is contained in:
Salvador E. Tropea 2022-12-05 21:26:16 -03:00
parent eb4514f570
commit 23593ac477
2 changed files with 18 additions and 6 deletions

View File

@ -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. - Added source_front, source_back and source_gerbers to the boards.
So now the images and gerbers come from outside. So now the images and gerbers come from outside.
- Adapted Template._renderBoards to just copy the files (keeping the extensions) - Adapted Template._renderBoards to just copy the files (keeping the extensions)
- Added listResources, with some changes to copyRelativeTo and _copyResources

View File

@ -48,13 +48,16 @@ def readTemplate(path):
except KeyError: except KeyError:
raise RuntimeError("Unknown template type '{}'".format(tType)) raise RuntimeError("Unknown template type '{}'".format(tType))
def copyRelativeTo(sourceTree, sourceFile, outputDir): def copyRelativeTo(sourceTree, sourceFile, outputDir, dry=False):
sourceTree = os.path.abspath(sourceTree) sourceTree = os.path.abspath(sourceTree)
sourceFile = os.path.abspath(sourceFile) sourceFile = os.path.abspath(sourceFile)
relPath = os.path.relpath(sourceFile, sourceTree) relPath = os.path.relpath(sourceFile, sourceTree)
outputDir = os.path.join(outputDir, os.path.dirname(relPath)) outputDir = os.path.join(outputDir, os.path.dirname(relPath))
Path(outputDir).mkdir(parents=True, exist_ok=True) dest = os.path.join(outputDir, os.path.basename(sourceFile))
shutil.copy(sourceFile, outputDir) if not dry:
Path(outputDir).mkdir(parents=True, exist_ok=True)
shutil.copy(sourceFile, outputDir)
return dest
class Template: class Template:
def __init__(self, directory): def __init__(self, directory):
@ -66,17 +69,25 @@ class Template:
self.name = None self.name = None
self.repository = 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 Copy all resource files specified by template.json and further specified
by addResource to the output directory. by addResource to the output directory.
""" """
files = []
for pattern in self.parameters["resources"]: for pattern in self.parameters["resources"]:
for path in glob.glob(os.path.join(self.directory, pattern), recursive=True): 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 pattern in self.extraResources:
for path in glob.glob(pattern, recursive=True): 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): def addResource(self, resource):
""" """