skpy/util.py
def convertIds(*types, **kwargs):
        """
        Class decorator: add helper methods to convert identifier properties into SkypeObjs.

        Args:
            types (str list): simple field types to add properties for (``user``, ``users`` or ``chat``)
            user (str list): attribute names to treat as single user identifier fields
            users (str list): attribute names to treat as user identifier lists
            chat (str list): attribute names to treat as chat identifier fields

        Returns:
            method: decorator function, ready to apply to other methods
        """
        user = kwargs.get("user", ())
        users = kwargs.get("users", ())
        chat = kwargs.get("chat", ())

        def userObj(self, field):
            return self.skype.contacts[getattr(self, field)]

        def userObjs(self, field):
            return (self.skype.contacts[id] for id in getattr(self, field))

        def chatObj(self, field):
            return self.skype.chats[getattr(self, field)]

        def attach(cls, fn, field, idField):
            """
            Generate the property object and attach it to the class.

            Args:
                cls (type): class to attach the property to
                fn (method): function to be attached
                field (str): attribute name for the new property
                idField (str): reference field to retrieve identifier from
            """
            setattr(cls, field, property(functools.wraps(fn)(functools.partial(fn, field=idField))))

        def wrapper(cls):
            # Shorthand identifiers, e.g. @convertIds("user", "chat").
            for type in types:
                if type == "user":
                    attach(cls, userObj, "user", "userId")
                elif type == "users":
                    attach(cls, userObjs, "users", "userIds")
                elif type == "chat":
                    attach(cls, chatObj, "chat", "chatId")
            # Custom field names, e.g. @convertIds(user=["creator"]).
            for field in user:
                attach(cls, userObj, field, "{0}Id".format(field))
            for field in users:
                attach(cls, userObjs, "{0}s".format(field), "{0}Ids".format(field))
            for field in chat:
                attach(cls, chatObj, field, "{0}Id".format(field))
            return cls

        return wrapper
def convertIds(*types, **kwargs):
        """
        Class decorator: add helper methods to convert identifier properties into SkypeObjs.

        Args:
            types (str list): simple field types to add properties for (``user``, ``users`` or ``chat``)
            user (str list): attribute names to treat as single user identifier fields
            users (str list): attribute names to treat as user identifier lists
            chat (str list): attribute names to treat as chat identifier fields

        Returns:
            method: decorator function, ready to apply to other methods
        """
        user = kwargs.get("user", ())
        users = kwargs.get("users", ())
        chat = kwargs.get("chat", ())

        def userObj(self, field):
            return self.skype.contacts[getattr(self, field)]

        def userObjs(self, field):
            return (self.skype.contacts[id] for id in getattr(self, field))

        def chatObj(self, field):
            return self.skype.chats[getattr(self, field)]

        def attach(cls, fn, field, idField):
            """
            Generate the property object and attach it to the class.

            Args:
                cls (type): class to attach the property to
                fn (method): function to be attached
                field (str): attribute name for the new property
                idField (str): reference field to retrieve identifier from
            """
            setattr(cls, field, property(functools.wraps(fn)(functools.partial(fn, field=idField))))

        def wrapper(cls):
            # Shorthand identifiers, e.g. @convertIds("user", "chat").
            for type in types:
                if type == "user":
                    attach(cls, userObj, "user", "userId")
                elif type == "users":
                    attach(cls, userObjs, "users", "userIds")
                elif type == "chat":
                    attach(cls, chatObj, "chat", "chatId")
            # Custom field names, e.g. @convertIds(user=["creator"]).
            for field in user:
                attach(cls, userObj, field, "{0}Id".format(field))
            for field in users:
                attach(cls, userObjs, "{0}s".format(field), "{0}Ids".format(field))
            for field in chat:
                attach(cls, chatObj, field, "{0}Id".format(field))
            return cls

        return wrapper
Class decorator: add helper methods to convert identifier properties into SkypeObjs.

        Args:
            types (str list): simple field types to add properties for (``user``, ``users`` or ``chat``)
            user (str list): attribute names to treat as single user identifier fields
            users (str list): attribute names to treat as user identifier lists
            chat (str list): attribute names to treat as chat identifier fields

        Returns:
            method: decorator function, ready to apply to other methods