view lemuriformes/decorators.py @ 16:9b1bb9eee962

misc functions related to filesystem paths
author Jeff Hammel <k0scist@gmail.com>
date Sun, 10 Dec 2017 17:19:30 -0800
parents 2227ff372388
children
line wrap: on
line source

try:
    # python2
    string = (str, unicode)
except NameError:
    # python3
    string = (str,)


class fileobj(object):
    """
    fileobj decorator:
    inner function may take a file-like object or a string as path to file
    The decorated function must take this as the first argument
    """


    def __init__(self, function):
        self.wrapped = function

    def __call__(self, fp, *args, **kwargs):

        if isinstance(fp, string):
            with open(fp) as f:
                return self.wrapped(f, *args, **kwargs)

        return self.wrapped(fp, *args, **kwargs)