# HG changeset patch # User Jeff Hammel # Date 1512954449 28800 # Node ID 2227ff372388faa25f746ade3be18527a324fd96 # Parent 82cd4e0b66cffca8c642fce56c49eea4734721cb decorators module diff -r 82cd4e0b66cf -r 2227ff372388 lemuriformes/decorators.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lemuriformes/decorators.py Sun Dec 10 17:07:29 2017 -0800 @@ -0,0 +1,26 @@ +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)