Skip to content

Commit

Permalink
use % formatting support for bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Good committed Sep 23, 2015
1 parent fc8fa24 commit 98553a9
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 31 deletions.
6 changes: 3 additions & 3 deletions pymap/parsing/command/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def __init__(self, buf, tag, command):
def __bytes__(self):
if hasattr(self, '_raw'):
return self._raw
self._raw = raw = self.command.command + b': ' + \
super().__bytes__()
self._raw = raw = b'%b: %b' % (self.command.command,
super().__bytes__())
return raw

def __str__(self):
Expand All @@ -60,7 +60,7 @@ def __init__(self, buf, tag, command=None):

def __bytes__(self):
if self.command:
return b'Command Not Found: ' + self.command
return b'Command Not Found: %b' % self.command
else:
return b'Command Not Given'

Expand Down
7 changes: 3 additions & 4 deletions pymap/parsing/primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def escape_quoted_specials(match):
return b'\\' + match.group(0)
pat = self._quoted_specials_pattern
quoted_string = pat.sub(escape_quoted_specials, self.value)
self._raw = b'"' + quoted_string + b'"'
self._raw = b'"%b"' % quoted_string
return self._raw


Expand Down Expand Up @@ -226,8 +226,7 @@ def __bytes__(self):
if self._raw is not None:
return bytes(self._raw)
length_bytes = bytes(str(len(self.value)), 'ascii')
literal_header = b'{' + length_bytes + b'}\r\n'
self._raw = literal_header + self.value
self._raw = b'{%b}\r\n%b' % (length_bytes, self.value)
return self._raw


Expand Down Expand Up @@ -266,4 +265,4 @@ def parse(cls, buf, list_expected=None, **kwargs):
items.append(item)

def __bytes__(self):
return b'(' + b' '.join([bytes(item) for item in self.value]) + b')'
return b'(%b)' % b' '.join([bytes(item) for item in self.value])
6 changes: 3 additions & 3 deletions pymap/parsing/response/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __bytes__(self):
if self._raw is not None:
return self._raw
raw_lines = [bytes(data) for data in self.data]
raw_lines.append(b''.join((self.tag, b' ', self.text, b'\r\n')))
raw_lines.append(b'%b %b\r\n' % (self.tag, self.text))
self._raw = b''.join(raw_lines)
return self._raw

Expand All @@ -73,9 +73,9 @@ class ConditionResponse(Response):

def __init__(self, tag, text, code):
if code:
text = b' '.join((self.condition, bytes(code), text))
text = b'%b %b %b' % (self.condition, code, text)
else:
text = b' '.join((self.condition, text))
text = b'%b %b' % (self.condition, text)
super().__init__(tag, text)


Expand Down
12 changes: 6 additions & 6 deletions pymap/parsing/response/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# THE SOFTWARE.
#

from ..primitives import Number, List
from ..primitives import List

__all__ = ['ResponseCode', 'Alert', 'BadCharset', 'Capability', 'Parse',
'PermanentFlags', 'ReadOnly', 'ReadWrite', 'TryCreate', 'UidNext',
Expand Down Expand Up @@ -85,7 +85,7 @@ def to_response(self):
return Response(b'*', self.string)

def __bytes__(self):
return b'[' + self.string + b']'
return b'[%b]' % self.string


class Parse(ResponseCode):
Expand All @@ -100,7 +100,7 @@ class PermanentFlags(ResponseCode):
def __init__(self, flags):
super().__init__()
self.flags = flags
self._raw = b'[PERMANENTFLAGS ' + bytes(List(flags)) + b']'
self._raw = b'[PERMANENTFLAGS %b]' % List(flags)

def __bytes__(self):
return self._raw
Expand Down Expand Up @@ -136,7 +136,7 @@ class UidNext(ResponseCode):
def __init__(self, next):
super().__init__()
self.next = next
self._raw = b'[UIDNEXT ' + bytes(Number(next)) + b']'
self._raw = b'[UIDNEXT %i]' % next

def __bytes__(self):
return self._raw
Expand All @@ -148,7 +148,7 @@ class UidValidity(ResponseCode):
def __init__(self, validity):
super().__init__()
self.validity = validity
self._raw = b'[UIDVALIDITY ' + bytes(Number(validity)) + b']'
self._raw = b'[UIDVALIDITY %i]' % validity

def __bytes__(self):
return self._raw
Expand All @@ -163,7 +163,7 @@ class Unseen(ResponseCode):
def __init__(self, next):
super().__init__()
self.next = next
self._raw = b'[UNSEEN ' + bytes(Number(next)) + b']'
self._raw = b'[UNSEEN %i]' % next

def __bytes__(self):
return self._raw
18 changes: 8 additions & 10 deletions pymap/parsing/response/specials.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class FlagsResponse(Response):
"""

def __init__(self, flags):
text = b'FLAGS ' + bytes(List(flags))
text = b'FLAGS %b' % List(flags)
super().__init__(b'*', text)


Expand All @@ -54,7 +54,7 @@ class ExistsResponse(Response):
"""

def __init__(self, num):
text = bytes(str(num), 'utf-8') + b' EXISTS'
text = b'%i EXISTS' % num
super().__init__(b'*', text)


Expand All @@ -67,7 +67,7 @@ class RecentResponse(Response):
"""

def __init__(self, num):
text = bytes(str(num), 'utf-8') + b' RECENT'
text = b'%i RECENT' % num
super().__init__(b'*', text)


Expand All @@ -79,7 +79,7 @@ class ExpungeResponse(Response):
"""

def __init__(self, seq):
text = bytes(str(seq), 'utf-8') + b' EXPUNGE'
text = b'%i EXPUNGE' % seq
super().__init__(b'*', text)


Expand All @@ -96,9 +96,8 @@ class FetchResponse(Response):
"""

def __init__(self, seq, data):
seq_raw = bytes(str(seq), 'utf-8')
data_list = List(chain.from_iterable(data.items()))
text = b' '.join((seq_raw, b'FETCH', bytes(data_list)))
text = b'%i FETCH %b' % (seq, data_list)
super().__init__(b'*', text)


Expand All @@ -110,7 +109,7 @@ class SearchResponse(Response):
"""

def __init__(self, seqs):
seqs_raw = [bytes(str(seq), 'utf-8') for seq in seqs]
seqs_raw = [b'%i' % seq for seq in seqs]
text = b' '.join([b'SEARCH'] + seqs_raw)
super().__init__(b'*', text)

Expand Down Expand Up @@ -141,9 +140,8 @@ def __init__(self, name, sep, marked=False, no_inferior=False,
name_attrs.value.append(br'\Noinferior')
if no_select:
name_attrs.value.append(br'\Noselect')
sep_raw = bytes(QuotedString(sep))
name_raw = bytes(Mailbox(name))
text = b' '.join((self.name, bytes(name_attrs), sep_raw, name_raw))
text = b'%b %b %b %b' % (self.name, name_attrs, QuotedString(sep),
Mailbox(name))
super().__init__(b'*', text)


Expand Down
2 changes: 1 addition & 1 deletion pymap/parsing/specials/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ def parse(cls, buf, **kwargs):
return cls(when, string.value), after

def __bytes__(self):
return b'"' + self._raw + b'"'
return b'"%b"' % self._raw
3 changes: 1 addition & 2 deletions pymap/parsing/specials/fetchattr.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ def raw(self):
if self.section:
parts.append(b'[')
if self.section[0]:
part_raw = b'.'.join([bytes(str(num), 'ascii')
for num in self.section[0]])
part_raw = b'.'.join([b'%i' % num for num in self.section[0]])
parts.append(part_raw)
if self.section[1]:
parts.append(b'.')
Expand Down
2 changes: 1 addition & 1 deletion pymap/parsing/specials/mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def _modified_b64encode(cls, src):
def _modified_b64decode(cls, src):
# Inspired by Twisted Python's implementation:
# https://twistedmatrix.com/trac/browser/trunk/LICENSE
src_utf7 = b'+' + src.replace(b',', b'/') + b'-'
src_utf7 = b'+%b-' % src.replace(b',', b'/')
return src_utf7.decode('utf-7')

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion pymap/parsing/specials/sequenceset.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def __bytes__(self):
if isinstance(group, tuple):
left = bytes(str(group[0]), 'ascii')
right = bytes(str(group[1]), 'ascii')
parts.append(left + b':' + right)
parts.append(b'%b:%b' % (left, right))
else:
parts.append(bytes(str(group), 'ascii'))
self._raw = raw = b','.join(parts)
Expand Down

0 comments on commit 98553a9

Please sign in to comment.