How can I modify a source attribute in a designspace file? I'm so close!
See original GitHub issueIām not sure if this is the right place to ask a usage question, but the forum mentioned in the contribution guidelines is either down or doesnāt exist (https://github.com/fonttools/fonttools/issues/1629). Iām also not able to find anything through Google. However, please let me know if I should move elsewhere with this question.
How I might set source filename
attributes in a designspace file?
Context:
I have a script that takes in a designspace file and preps sources for interpolation into a prototype variable font by simply copying the UFOs into a timestamped folder and chucking out all incompatible glyphs. This is a āstupidā way to prep fonts for building, but itās pretty useful to allow me to check a variable font in browsers, etc, well before it the project has been totally finished.
However, this script currently only works if the designspace only points to UFOs in its own, same directory. Iād like to be able to go through and update the filename
attribute of sources to correctly point to the new timestamped folder, if they have been copied in from other directories. But, once I have set source attributes to edited values, Iām having trouble making the attributes āstickā while I write a new designspace file.
Hereās my current copy and edit function (from here):
def copyDesignSpace(designspacePath):
# duplicate designspace into new folder
inputDStail = os.path.split(designspacePath)[1]
outputDSpath = newFolderPath + "/" + inputDStail
shutil.copyfile(designspacePath, outputDSpath)
# update source & instance paths in designspace as needed
outputDS = DesignSpaceDocument.fromfile(outputDSpath)
for source in outputDS.sources:
print(source.filename) # prints the original value
fontFilename = os.path.split(source.path)[1]
setattr(source, 'filename', fontFilename)
print(source.filename) # this does change to the value I want
# this doesn't overwrite the designspace file...
outputDS.write(outputDSpath.replace(
".designspace", ".prepped.designspace"))
copyDesignSpace(designspacePath)
I think Iām close, but just missing something. If I find it, Iāll update this thread. If someone else is able to point out what Iām missing, I would really appreciate the nudge in the right direction!
Issue Analytics
- State:
- Created 4 years ago
- Comments:6
Top GitHub Comments
This just came in handy. Thanks for asking this question, past me! And thanks again to Just for his help in answering it. š¤
You can replace
setattr(source, 'path', newFontPath)
by
source.path = newFontPath