def arg_list()

in pygenie/jobs/utils.py [0:0]


def arg_list(func):
    """
    Decorator for accepting a string or list arg into a method and adding it to
    an object's attribute as a list (with duplicates removed). The object's
    attribute name is specified via the method's arg name when the method
    is defined.
    """

    @_remove_wrapper_from_args
    def wrapper(*args, **kwargs):
        """Add arg to object's attribute as a list."""

        assert len(args) == 2, 'incorrect arguments to {}()'.format(func.__name__)

        attr_name = getargspec(func).args[1]
        self = args[0]
        value = args[1]

        assert isinstance(value, list) or is_str(value), \
            '{}() argument value should be a string or list of strings' \
                .format(func.__name__)

        attr = getattr(self, attr_name)
        attr.extend(str_to_list(value))

        final = list()
        for item in [json.dumps(a, sort_keys=True) for a in attr]:
            if item not in final:
                final.append(item)

        setattr(self, attr_name, [json.loads(i) for i in final])

        return func(*args, **kwargs) or self

    return decorator(wrapper, func)