13
|
1 try:
|
|
2 # python2
|
|
3 string = (str, unicode)
|
|
4 except NameError:
|
|
5 # python3
|
|
6 string = (str,)
|
|
7
|
|
8
|
|
9 class fileobj(object):
|
|
10 """
|
|
11 fileobj decorator:
|
|
12 inner function may take a file-like object or a string as path to file
|
|
13 The decorated function must take this as the first argument
|
|
14 """
|
|
15
|
|
16
|
|
17 def __init__(self, function):
|
|
18 self.wrapped = function
|
|
19
|
|
20 def __call__(self, fp, *args, **kwargs):
|
|
21
|
|
22 if isinstance(fp, string):
|
|
23 with open(fp) as f:
|
|
24 return self.wrapped(f, *args, **kwargs)
|
|
25
|
|
26 return self.wrapped(fp, *args, **kwargs)
|