import os

from waterbutler.core.streams import BaseStream


class FileStreamReader(BaseStream):

    def __init__(self, file_pointer):
        super().__init__()
        self.file_gen = None
        self.file_pointer = file_pointer
        self.read_size = None
        self.content_type = 'application/octet-stream'

    @property
    def size(self):
        cursor = self.file_pointer.tell()
        self.file_pointer.seek(0, os.SEEK_END)
        ret = self.file_pointer.tell()
        self.file_pointer.seek(cursor)
        return ret

    def close(self):
        self.file_pointer.close()
        self.feed_eof()

    def read_as_gen(self):
        self.file_pointer.seek(0)
        while True:
            chunk = self.file_pointer.read(self.read_size)
            if not chunk:
                self.feed_eof()
                chunk = b''
            yield chunk

    async def _read(self, size):
        self.file_gen = self.file_gen or self.read_as_gen()
        self.read_size = size
        return next(self.file_gen)

import os
# import asyncio

from waterbutler.core.streams import BaseStream


class FileStreamReader(BaseStream):

    def __init__(self, file_pointer):
        super().__init__()
        self.file_gen = None
        self.file_pointer = file_pointer
        self.read_size = None
        self.content_type = 'application/octet-stream'

    @property
    def size(self):
        cursor = self.file_pointer.tell()
        self.file_pointer.seek(0, os.SEEK_END)
        ret = self.file_pointer.tell()
        self.file_pointer.seek(cursor)
        return ret

    def close(self):
        self.file_pointer.close()
        self.feed_eof()

    class read_chunks:
        def __init__(self, read_size, fp):
            self.done = False
            self.read_size = read_size
            self.fp = fp

        async def __aiter__(self):
            return self

        async def __anext__(self):
            if self.done:
                raise StopAsyncIteration
            return await self.get_chunk()

        async def get_chunk(self):
            while True:
                chunk = self.fp.read(self.read_size)
                if not chunk:
                    chunk = b''
                    self.done = True
                return chunk

    async def _read(self, read_size):
        async for chunk in self.read_chunks(read_size, self.file_pointer):
            if chunk == b'':
                self.feed_eof()
            return chunk

Update FileStreamReader to use async generator
Update FileStreamReader to use async generator

  import os
+ # import asyncio
  
  from waterbutler.core.streams import BaseStream
  
  
  class FileStreamReader(BaseStream):
  
      def __init__(self, file_pointer):
          super().__init__()
          self.file_gen = None
          self.file_pointer = file_pointer
          self.read_size = None
          self.content_type = 'application/octet-stream'
  
      @property
      def size(self):
          cursor = self.file_pointer.tell()
          self.file_pointer.seek(0, os.SEEK_END)
          ret = self.file_pointer.tell()
          self.file_pointer.seek(cursor)
          return ret
  
      def close(self):
          self.file_pointer.close()
          self.feed_eof()
  
-     def read_as_gen(self):
-         self.file_pointer.seek(0)
+     class read_chunks:
+         def __init__(self, read_size, fp):
+             self.done = False
+             self.read_size = read_size
+             self.fp = fp
+ 
+         async def __aiter__(self):
+             return self
+ 
+         async def __anext__(self):
+             if self.done:
+                 raise StopAsyncIteration
+             return await self.get_chunk()
+ 
+         async def get_chunk(self):
-         while True:
+             while True:
-             chunk = self.file_pointer.read(self.read_size)
+                 chunk = self.fp.read(self.read_size)
-             if not chunk:
+                 if not chunk:
+                     chunk = b''
+                     self.done = True
+                 return chunk
+ 
+     async def _read(self, read_size):
+         async for chunk in self.read_chunks(read_size, self.file_pointer):
+             if chunk == b'':
                  self.feed_eof()
-                 chunk = b''
-             yield chunk
+             return chunk
  
-     async def _read(self, size):
-         self.file_gen = self.file_gen or self.read_as_gen()
-         self.read_size = size
-         return next(self.file_gen)
- 
Update FileStreamReader to use async generator
## Code Before:
import os

from waterbutler.core.streams import BaseStream


class FileStreamReader(BaseStream):

    def __init__(self, file_pointer):
        super().__init__()
        self.file_gen = None
        self.file_pointer = file_pointer
        self.read_size = None
        self.content_type = 'application/octet-stream'

    @property
    def size(self):
        cursor = self.file_pointer.tell()
        self.file_pointer.seek(0, os.SEEK_END)
        ret = self.file_pointer.tell()
        self.file_pointer.seek(cursor)
        return ret

    def close(self):
        self.file_pointer.close()
        self.feed_eof()

    def read_as_gen(self):
        self.file_pointer.seek(0)
        while True:
            chunk = self.file_pointer.read(self.read_size)
            if not chunk:
                self.feed_eof()
                chunk = b''
            yield chunk

    async def _read(self, size):
        self.file_gen = self.file_gen or self.read_as_gen()
        self.read_size = size
        return next(self.file_gen)

## Instruction:
Update FileStreamReader to use async generator
## Code After:
import os
# import asyncio

from waterbutler.core.streams import BaseStream


class FileStreamReader(BaseStream):

    def __init__(self, file_pointer):
        super().__init__()
        self.file_gen = None
        self.file_pointer = file_pointer
        self.read_size = None
        self.content_type = 'application/octet-stream'

    @property
    def size(self):
        cursor = self.file_pointer.tell()
        self.file_pointer.seek(0, os.SEEK_END)
        ret = self.file_pointer.tell()
        self.file_pointer.seek(cursor)
        return ret

    def close(self):
        self.file_pointer.close()
        self.feed_eof()

    class read_chunks:
        def __init__(self, read_size, fp):
            self.done = False
            self.read_size = read_size
            self.fp = fp

        async def __aiter__(self):
            return self

        async def __anext__(self):
            if self.done:
                raise StopAsyncIteration
            return await self.get_chunk()

        async def get_chunk(self):
            while True:
                chunk = self.fp.read(self.read_size)
                if not chunk:
                    chunk = b''
                    self.done = True
                return chunk

    async def _read(self, read_size):
        async for chunk in self.read_chunks(read_size, self.file_pointer):
            if chunk == b'':
                self.feed_eof()
            return chunk
