Skip to content

Commit

Permalink
new position of generator file
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Apr 19, 2018
1 parent 6043df4 commit a0e83ab
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/appier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
from .smtp import message, message_base, message_netius, smtp_engine, multipart, plain,\
html, header
from .storage import StorageEngine, BaseEngine, FsEngine
from .structures import OrderedDict, GeneratorFile, LazyDict, LazyValue, lazy_dict, lazy
from .structures import OrderedDict, LazyDict, LazyValue, GeneratorFile, lazy_dict, lazy
from .typesf import AbstractType, Type, File, Files, ImageFile, ImageFiles, image, images, Reference,\
reference, References, references, Encrypted, encrypted, secure
from .util import is_iterable, is_mobile, is_tablet, is_browser, is_bot, browser_info, email_parts, email_mime,\
Expand Down
70 changes: 35 additions & 35 deletions src/appier/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,41 +205,6 @@ def _verify(self, force = False):
self._list.append(item)
self._items[key] = item

class GeneratorFile(object):
"""
File like class that encapsulates an underlying
stream generator (first yield is size) into a
file, to be used as a normal file.
Notice that there are certain limitation to this
strategy like the fact that the read operation
(chunk) size parameter is not respected.
"""

def __init__(self, generator):
self._generator = generator
self._size = next(generator)
self._position = 0

def seek(self, offset, whence = os.SEEK_SET):
if whence == os.SEEK_SET:
self._position = offset
if whence == os.SEEK_CUR:
self._position += offset
if whence == os.SEEK_END:
self._position = self._size

def tell(self):
return self._position

def read(self, size):
try: data = next(self._generator)
except StopIteration: data = b""
return data

def close(self):
self._generator.close()

class LazyDict(dict):

def __getitem__(self, key, force = False, resolve = False):
Expand Down Expand Up @@ -275,5 +240,40 @@ def resolve(self, force = False):
def call(self):
return self.resolve()

class GeneratorFile(object):
"""
File like class that encapsulates an underlying
stream generator (first yield is size) into a
file, to be used as a normal file.
Notice that there are certain limitation to this
strategy like the fact that the read operation
(chunk) size parameter is not respected.
"""

def __init__(self, generator):
self._generator = generator
self._size = next(generator)
self._position = 0

def seek(self, offset, whence = os.SEEK_SET):
if whence == os.SEEK_SET:
self._position = offset
if whence == os.SEEK_CUR:
self._position += offset
if whence == os.SEEK_END:
self._position = self._size

def tell(self):
return self._position

def read(self, size):
try: data = next(self._generator)
except StopIteration: data = b""
return data

def close(self):
self._generator.close()

lazy_dict = LazyDict
lazy = LazyValue

0 comments on commit a0e83ab

Please sign in to comment.