annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 try:
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 # python2
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 string = (str, unicode)
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 except NameError:
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 # python3
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 string = (str,)
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 class fileobj(object):
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 """
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 fileobj decorator:
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 inner function may take a file-like object or a string as path to file
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 The decorated function must take this as the first argument
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 """
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 def __init__(self, function):
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 self.wrapped = function
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 def __call__(self, fp, *args, **kwargs):
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 if isinstance(fp, string):
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 with open(fp) as f:
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 return self.wrapped(f, *args, **kwargs)
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25
2227ff372388 decorators module
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 return self.wrapped(fp, *args, **kwargs)