diff python/fileobj.py @ 498:95ba5770d2f0

file decorator
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 21 Aug 2013 15:54:09 -0700
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/fileobj.py	Wed Aug 21 15:54:09 2013 -0700
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+
+"""
+filename -> file-like object as a decorator
+"""
+
+import optparse
+import os
+import sys
+
+string = (basestring,)
+
+class fileobj(object):
+    def __init__(self, arg, *args, **kwargs):
+        self._args = [arg] + list(args)
+        self._kwargs = kwargs
+        # mode, filename, ...
+
+        # function
+        self.func = arg if not args else None
+    def __call__(self, *args, **kwargs):
+        if self.func is None:
+            raise NotImplementedError
+        else:
+            if len(args) and isinstance(args[0], string):
+                args = list(args)
+                with file(args[0], 'w') as fp:
+                    args[0] =fp
+                    return self.func(*args, **kwargs)
+            return self.func(*args, **kwargs)
+
+if __name__ == '__main__':
+    # test code
+    import os
+    import tempfile
+
+    @fileobj
+    def test1(fp):
+        fp.write('foo')
+
+    filename = tempfile.mktemp()
+    print filename
+    assert not os.path.exists(filename)
+    test1(filename)
+    assert os.path.exists(filename)
+    print file(filename).read()