Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _test(self, to_parse, expected, wrap=True):
if wrap:
# convenience - expected value should be wrapped in another tuple
expected = (expected,)
if not isinstance(to_parse, list):
to_parse = [to_parse]
output = parse_response(to_parse)
self.assertSequenceEqual(output, expected)
def _test_parse_error(self, to_parse, expected_msg):
if not isinstance(to_parse, list):
to_parse = [to_parse]
self.assertRaisesRegex(ProtocolError, expected_msg,
parse_response, to_parse)
def _parse_untagged_response(text):
assert_imap_protocol(text.startswith(b'* '))
text = text[2:]
if text.startswith((b'OK ', b'NO ')):
return tuple(text.split(b' ', 1))
return parse_response([text])
def _parse_quota(quota_rep):
quota_rep = parse_response(quota_rep)
rv = list()
for quota_root, quota_resource_infos in as_pairs(quota_rep):
for quota_resource_info in as_triplets(quota_resource_infos):
rv.append(Quota(
quota_root=to_unicode(quota_root),
resource=to_unicode(quota_resource_info[0]),
usage=quota_resource_info[1],
limit=quota_resource_info[2]
))
return rv
def main():
i = IMAPClient('localhost')
i.login('mailtest', 'foobar')
i.select_folder('INBOX')
msgs = i.search()
i._imap.debug = 5
lines = i.altfetch(msgs, ['ENVELOPE', 'BODYSTRUCTURE'])
body = '\r\n'.join(lines)
#body += '\r\n'
for x in parse_response(body):
print x
def namespace(self):
"""Return the namespace for the account as a (personal, other,
shared) tuple.
Each element may be None if no namespace of that type exists,
or a sequence of (prefix, separator) pairs.
For convenience the tuple elements may be accessed
positionally or using attributes named *personal*, *other* and
*shared*.
See :rfc:`2342` for more details.
"""
data = self._command_and_check('namespace')
parts = []
for item in parse_response(data):
if item is None:
parts.append(item)
else:
converted = []
for prefix, separator in item:
if self.folder_encode:
prefix = decode_utf7(prefix)
converted.append((prefix, to_unicode(separator)))
parts.append(tuple(converted))
return Namespace(*parts)
*parameters* should be specified as a dictionary of field/value pairs,
for example: ``{"name": "IMAPClient", "version": "0.12"}``
"""
if parameters is None:
args = 'NIL'
else:
if not isinstance(parameters, dict):
raise TypeError("'parameters' should be a dictionary")
args = seq_to_parenstr(
_quote(v) for v in
itertools.chain.from_iterable(parameters.items()))
typ, data = self._imap._simple_command('ID', args)
self._checkok('id', typ, data)
typ, data = self._imap._untagged_response(typ, data, 'ID')
return parse_response(data)
The *criteria* and *charset* arguments are as per
:py:meth:`.search`.
See :rfc:`5256` for more details.
"""
algorithm = to_bytes(algorithm)
if not self.has_capability(b'THREAD=' + algorithm):
raise exceptions.CapabilityError(
'The server does not support %s threading algorithm' % algorithm
)
args = [algorithm, to_bytes(charset)] + \
_normalise_search_criteria(criteria, charset)
data = self._raw_command_untagged(b'THREAD', args)
return parse_response(data)
*what* should be a sequence of status items to query. This
defaults to ``('MESSAGES', 'RECENT', 'UIDNEXT', 'UIDVALIDITY',
'UNSEEN')``.
Returns a dictionary of the status items for the folder with
keys matching *what*.
"""
if what is None:
what = ('MESSAGES', 'RECENT', 'UIDNEXT', 'UIDVALIDITY', 'UNSEEN')
else:
what = normalise_text_list(what)
what_ = '(%s)' % (' '.join(what))
fname = self._normalise_folder(folder)
data = self._command_and_check('status', fname, what_)
response = parse_response(data)
status_items = response[-1]
return dict(as_pairs(status_items))