Skip to content

Commit

Permalink
Merge branch 'release/v1.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Jul 19, 2014
2 parents d29366f + 0b7952c commit 3823292
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
Binary file modified alfred-workflow.zip
Binary file not shown.
1 change: 1 addition & 0 deletions tests/fubar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fübar
21 changes: 21 additions & 0 deletions tests/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def setUp(self):
'address': 'Hürterstr. 42\nEssen'}
self.test_file = os.path.join(os.path.dirname(__file__),
'cönfüsed.gif')
self.fubar_url = 'http://deanishe.net/fubar.txt'
self.fubar_bytes = b'fübar'
self.fubar_unicode = 'fübar'

def tearDown(self):
pass
Expand Down Expand Up @@ -191,6 +194,24 @@ def test_json_encoding(self):
data = r.json()
self.assertEqual(data[0], 'münchen')

def test_iter_content(self):
"""iter_content returns content"""
r = web.get(self.fubar_url)
self.assertEqual(r.status_code, 200)
contents = b''
for s in r.iter_content(chunk_size=1):
contents += s
self.assertEqual(contents, self.fubar_bytes)

def test_iter_content_decoded(self):
"""iter_content returns content"""
r = web.get(self.fubar_url)
self.assertEqual(r.status_code, 200)
contents = ''
for u in r.iter_content(chunk_size=1, decode_unicode=True):
contents += u
self.assertEqual(contents, self.fubar_unicode)


if __name__ == '__main__': # pragma: no cover
unittest.main()
2 changes: 1 addition & 1 deletion workflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def main(wf):
"""

__version__ = '1.5.1'
__version__ = '1.6'

from .workflow import Workflow, PasswordNotFound, KeychainError
from .workflow import (ICON_ERROR, ICON_WARNING, ICON_NOTE, ICON_INFO,
Expand Down
50 changes: 48 additions & 2 deletions workflow/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import json
import re
import unicodedata
import codecs


USER_AGENT = u'alfred-workflow-0.1'
Expand Down Expand Up @@ -150,7 +151,6 @@ def __init__(self, request):
self.url = None
self.raw = None
self.encoding = None
self.content = None
self.error = None
self.status_code = None
self.reason = None
Expand All @@ -171,7 +171,6 @@ def __init__(self, request):
else:
self.status_code = self.raw.getcode()
self.url = self.raw.geturl()
self.content = self.raw.read()
self.reason = RESPONSES.get(self.status_code)

# Parse additional info if request succeeded
Expand All @@ -193,6 +192,16 @@ def json(self):

return json.loads(self.content, self.encoding or 'utf-8')

@property
def content(self):
"""Return raw content of response (i.e. bytes)
:returns: ``str``
"""

return self.raw.read()

@property
def text(self):
"""Return unicode-decoded content of response.
Expand All @@ -206,6 +215,43 @@ def text(self):
self.encoding))
return self.content

def iter_content(self, chunk_size=1, decode_unicode=False):
"""Iterate over response data.
:param chunk_size: Number of bytes to read into memory
:type chunk_size: ``int``
:param decode_unicode: Decode to Unicode using detected encoding
:type decode_unicode: ``Boolean``
:returns: iterator
"""

def decode_stream(iterator, r):

decoder = codecs.getincrementaldecoder(r.encoding)(errors='replace')

for chunk in iterator:
rv = decoder.decode(chunk)
if rv:
yield rv
rv = decoder.decode(b'', final=True)
if rv:
yield rv # pragma: nocover

def generate():
while True:
chunk = self.raw.read(chunk_size)
if not chunk:
break
yield chunk

chunks = generate()

if decode_unicode and self.encoding:
chunks = decode_stream(chunks, self)

return chunks

def raise_for_status(self):
"""Raise stored error if one occurred.
Expand Down

0 comments on commit 3823292

Please sign in to comment.