robotools / fontParts

The replacement for RoboFab
MIT License
133 stars 44 forks source link

info.update and info.copy #676

Closed typesupply closed 1 year ago

typesupply commented 1 year ago

There needs to be an easier way to copy data from one info object to another.

https://robofontcommunity.slack.com/archives/C039PGQAZ71/p1668114026079679

typesupply commented 1 year ago

Follow the method names here:

https://github.com/robotools/fontParts/blob/master/Lib/fontParts/base/base.py#L258

roberto-arista commented 1 year ago

great idea!

typemytype commented 1 year ago

Im not a fan to add full dict support for info, just info.update

def update(self, otherInfo):
        """
        Update the info object with an other info object.
        """
        if isinstance(otherInfo, self.__class__):
            otherInfo = otherInfo.asDict()
        if not isinstance(otherInfo, dict):
            raise TypeError("Not a valid info object or dictionary")
        selfData = self.asDict()
        selfData.update(otherInfo)
        for name, value in selfData.items():
            setattr(self, name, value)
typesupply commented 1 year ago

That sounds good to me. Though... that implementation requires the info object to have an asDict method, right?

typemytype commented 1 year ago

just guidelines must be ignored... in fontParts they arent part of an info object!

from fontTools.ufoLib import fontInfoAttributesVersion3

def asDict(self):
    return {attr: getattr(self, attr) for attr in fontInfoAttributesVersion3 if attr != "guidelines"}