Example #1
0
 def __rshift__(self, obj):
     """Implement append-to-file pipeline stage.
     
     This behaves as __gt__, but opens the file in append mode.
     """
     if isinstance(obj, PipelineEntry):
         raise ValueError("Cannot write to existing pipeline entry.")
     obj = filelike.to_filelike(obj, "a")
     return self._create(obj, "a")
Example #2
0
 def __rshift__(self,obj):
     """Implement append-to-file pipeline stage.
     
     This behaves as __gt__, but opens the file in append mode.
     """
     if isinstance(obj,PipelineEntry):
         raise ValueError("Cannot write to existing pipeline entry.")
     obj = filelike.to_filelike(obj,"a")
     return self._create(obj,"a")
Example #3
0
 def test_tofilelike_read(self):
     """Test behavior of to_filelike for mode "r-"."""
     class F:
         def read(self,sz=-1):
             return ""
     f = to_filelike(F(),"r-")
     self.assertEquals(f.__class__,wrappers.FileWrapper)
     self.assertEquals(f.read(),"")
     self.assertRaises(ValueError,to_filelike,F(),"r")
     self.assertRaises(ValueError,to_filelike,F(),"w-")
     self.assertRaises(ValueError,to_filelike,F(),"rw")
Example #4
0
 def test_tofilelike_writeseek(self):
     """Test behavior of to_filelike for mode "w"."""
     class F:
         def write(self,data):
             pass
         def seek(self,offset,whence):
             pass
     f = to_filelike(F(),"w")
     self.assertEquals(f.__class__,wrappers.FileWrapper)
     self.assertRaises(ValueError,to_filelike,F(),"r")
     self.assertRaises(ValueError,to_filelike,F(),"r-")
Example #5
0
    def test_tofilelike_write(self):
        """Test behavior of to_filelike for mode "w-"."""
        class F:
            def write(self, data):
                pass

        f = to_filelike(F(), "w-")
        self.assertEquals(f.__class__, wrappers.FileWrapper)
        self.assertRaises(ValueError, to_filelike, F(), "w")
        self.assertRaises(ValueError, to_filelike, F(), "r")
        self.assertRaises(ValueError, to_filelike, F(), "r-")
        self.assertRaises(ValueError, to_filelike, F(), "rw")
Example #6
0
    def test_tofilelike_read(self):
        """Test behavior of to_filelike for mode "r-"."""
        class F:
            def read(self, sz=-1):
                return ""

        f = to_filelike(F(), "r-")
        self.assertEquals(f.__class__, wrappers.FileWrapper)
        self.assertEquals(f.read(), "")
        self.assertRaises(ValueError, to_filelike, F(), "r")
        self.assertRaises(ValueError, to_filelike, F(), "w-")
        self.assertRaises(ValueError, to_filelike, F(), "rw")
Example #7
0
 def test_tofilelike_readwrite(self):
     """Test behavior of to_filelike for mode "rw"."""
     class F:
         def write(self,data):
             pass
         def read(self,sz=-1):
             return ""
         def seek(self,offset,whence):
             pass
     f = to_filelike(F(),"rw")
     self.assertEquals(f.__class__,wrappers.FileWrapper)
     self.assertEquals(f.read(),"")
Example #8
0
 def __lt__(self, obj):
     """Implement read-from-file pipeline stage.
     
     'obj' must not be a PipelineEntry, and must be coercable
     to a file-like object using filelike.to_filelike() with
     mode "r".
     
     A new file-like object will be returned that reads from the
     opened file-like object.
     """
     if isinstance(obj, PipelineEntry):
         raise ValueError("Cannot read from existing pipeline entry.")
     obj = filelike.to_filelike(obj, "r")
     return self._create(obj, "r")
Example #9
0
 def __gt__(self,obj):
     """Implement write-to-file pipeline stage.
     
     'obj' must not be a PipelineEntry, and must be coercable
     to a file-like object using filelike.to_filelike() with
     mode "w".
     
     A new file-like wrapper will be returned that writes to
     the given object.
     """
     if isinstance(obj,PipelineEntry):
         raise ValueError("Cannot write to existing pipeline entry.")
     obj = filelike.to_filelike(obj,"w")
     return self._create(obj,"w")
Example #10
0
 def __gt__(self, obj):
     """Implement write-to-file pipeline stage.
     
     'obj' must not be a PipelineEntry, and must be coercable
     to a file-like object using filelike.to_filelike() with
     mode "w".
     
     A new file-like wrapper will be returned that writes to
     the given object.
     """
     if isinstance(obj, PipelineEntry):
         raise ValueError("Cannot write to existing pipeline entry.")
     obj = filelike.to_filelike(obj, "w")
     return self._create(obj, "w")
Example #11
0
 def __lt__(self,obj):
     """Implement read-from-file pipeline stage.
     
     'obj' must not be a PipelineEntry, and must be coercable
     to a file-like object using filelike.to_filelike() with
     mode "r".
     
     A new file-like object will be returned that reads from the
     opened file-like object.
     """
     if isinstance(obj,PipelineEntry):
         raise ValueError("Cannot read from existing pipeline entry.")
     obj = filelike.to_filelike(obj,"r")
     return self._create(obj,"r")
Example #12
0
    def test_tofilelike_readwrite(self):
        """Test behavior of to_filelike for mode "rw"."""
        class F:
            def write(self, data):
                pass

            def read(self, sz=-1):
                return ""

            def seek(self, offset, whence):
                pass

        f = to_filelike(F(), "rw")
        self.assertEquals(f.__class__, wrappers.FileWrapper)
        self.assertEquals(f.read(), "")
Example #13
0
 def __lt__(self, obj):
     """Implement read-from-file pipeline stage.
     
     'obj' must not be a PipelineEntry, and must be coercable
     to a file-like object using filelike.to_filelike() with
     mode "w".
     
     A new file-like wrapper will be returned that writes to
     the given object.
     """
     if isinstance(obj, PipelineEntry):
         raise ValueError("Cannot read from existing pipeline entry.")
     obj = filelike.to_filelike(obj, "r")
     while len(self._stages) > 0:
         next = self._stages.pop()
         obj = next._create(obj, "r")
     return obj
Example #14
0
 def __lt__(self,obj):
     """Implement read-from-file pipeline stage.
     
     'obj' must not be a PipelineEntry, and must be coercable
     to a file-like object using filelike.to_filelike() with
     mode "w".
     
     A new file-like wrapper will be returned that writes to
     the given object.
     """
     if isinstance(obj,PipelineEntry):
         raise ValueError("Cannot read from existing pipeline entry.")
     obj = filelike.to_filelike(obj,"r")
     while len(self._stages) > 0:
         next = self._stages.pop()
         obj = next._create(obj,"r")
     return obj
Example #15
0
 def test_tofilelike_string(self):
     """Test behaviour of to_filelike on strings."""
     f = to_filelike("testing")
     self.assert_(isinstance(f,StringIO))
     self.assertEquals(f.read(),"testing")
Example #16
0
 def test_tofilelike_stringio(self):
     """Test behaviour of to_filelike on StringIO instances."""
     f = to_filelike(StringIO())
     self.assert_(isinstance(f,StringIO))
Example #17
0
 def test_tofilelike_stringio(self):
     """Test behaviour of to_filelike on StringIO instances."""
     f = to_filelike(StringIO())
     self.assert_(isinstance(f, StringIO))
Example #18
0
 def test_tofilelike_string(self):
     """Test behaviour of to_filelike on strings."""
     f = to_filelike("testing")
     self.assert_(isinstance(f, StringIO))
     self.assertEquals(f.read(), "testing")