annotate python/fileobj.py @ 699:72d2a2e09c6a

stub: slicing
author Jeff Hammel <k0scist@gmail.com>
date Tue, 12 Aug 2014 16:49:58 -0700
parents 95ba5770d2f0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
498
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 filename -> file-like object as a decorator
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 """
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 import optparse
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 import os
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 import sys
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 string = (basestring,)
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 class fileobj(object):
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 def __init__(self, arg, *args, **kwargs):
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 self._args = [arg] + list(args)
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 self._kwargs = kwargs
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 # mode, filename, ...
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 # function
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 self.func = arg if not args else None
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 def __call__(self, *args, **kwargs):
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 if self.func is None:
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 raise NotImplementedError
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 else:
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 if len(args) and isinstance(args[0], string):
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 args = list(args)
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 with file(args[0], 'w') as fp:
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 args[0] =fp
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 return self.func(*args, **kwargs)
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 return self.func(*args, **kwargs)
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 if __name__ == '__main__':
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 # test code
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 import os
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 import tempfile
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 @fileobj
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 def test1(fp):
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 fp.write('foo')
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 filename = tempfile.mktemp()
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 print filename
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 assert not os.path.exists(filename)
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 test1(filename)
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 assert os.path.exists(filename)
95ba5770d2f0 file decorator
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 print file(filename).read()