Python email.errors() Examples
The following are 21
code examples of email.errors().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
email
, or try the search function
.
Example #1
Source File: packaging.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def _get_version_from_pkg_metadata(package_name): """Get the version from package metadata if present. This looks for PKG-INFO if present (for sdists), and if not looks for METADATA (for wheels) and failing that will return None. """ pkg_metadata_filenames = ['PKG-INFO', 'METADATA'] pkg_metadata = {} for filename in pkg_metadata_filenames: try: pkg_metadata_file = open(filename, 'r') except (IOError, OSError): continue try: pkg_metadata = email.message_from_file(pkg_metadata_file) except email.errors.MessageError: continue # Check to make sure we're in our own dir if pkg_metadata.get('Name', None) != package_name: return None return pkg_metadata.get('Version', None)
Example #2
Source File: packaging.py From keras-lambda with MIT License | 6 votes |
def _get_version_from_pkg_metadata(package_name): """Get the version from package metadata if present. This looks for PKG-INFO if present (for sdists), and if not looks for METADATA (for wheels) and failing that will return None. """ pkg_metadata_filenames = ['PKG-INFO', 'METADATA'] pkg_metadata = {} for filename in pkg_metadata_filenames: try: pkg_metadata_file = open(filename, 'r') except (IOError, OSError): continue try: pkg_metadata = email.message_from_file(pkg_metadata_file) except email.errors.MessageError: continue # Check to make sure we're in our own dir if pkg_metadata.get('Name', None) != package_name: return None return pkg_metadata.get('Version', None)
Example #3
Source File: contentmanager.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def get_text_content(msg, errors='replace'): content = msg.get_payload(decode=True) charset = msg.get_param('charset', 'ASCII') return content.decode(charset, errors=errors)
Example #4
Source File: test_packaging.py From keras-lambda with MIT License | 5 votes |
def test_email_parsing_errors_are_handled(self): mocked_open = mock.mock_open() with mock.patch('pbr.packaging.open', mocked_open): with mock.patch('email.message_from_file') as message_from_file: message_from_file.side_effect = [ email.errors.MessageError('Test'), {'Name': 'pbr_testpackage'}] version = packaging._get_version_from_pkg_metadata( 'pbr_testpackage') self.assertTrue(message_from_file.called) self.assertIsNone(version)
Example #5
Source File: contentmanager.py From android_universal with MIT License | 5 votes |
def _prepare_set(msg, maintype, subtype, headers): msg['Content-Type'] = '/'.join((maintype, subtype)) if headers: if not hasattr(headers[0], 'name'): mp = msg.policy headers = [mp.header_factory(*mp.header_source_parse([header])) for header in headers] try: for header in headers: if header.defects: raise header.defects[0] msg[header.name] = header except email.errors.HeaderDefect as exc: raise ValueError("Invalid header: {}".format( header.fold(policy=msg.policy))) from exc
Example #6
Source File: contentmanager.py From android_universal with MIT License | 5 votes |
def get_text_content(msg, errors='replace'): content = msg.get_payload(decode=True) charset = msg.get_param('charset', 'ASCII') return content.decode(charset, errors=errors)
Example #7
Source File: contentmanager.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _prepare_set(msg, maintype, subtype, headers): msg['Content-Type'] = '/'.join((maintype, subtype)) if headers: if not hasattr(headers[0], 'name'): mp = msg.policy headers = [mp.header_factory(*mp.header_source_parse([header])) for header in headers] try: for header in headers: if header.defects: raise header.defects[0] msg[header.name] = header except email.errors.HeaderDefect as exc: raise ValueError("Invalid header: {}".format( header.fold(policy=msg.policy))) from exc
Example #8
Source File: contentmanager.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def get_text_content(msg, errors='replace'): content = msg.get_payload(decode=True) charset = msg.get_param('charset', 'ASCII') return content.decode(charset, errors=errors)
Example #9
Source File: contentmanager.py From odoo13-x64 with GNU General Public License v3.0 | 5 votes |
def _prepare_set(msg, maintype, subtype, headers): msg['Content-Type'] = '/'.join((maintype, subtype)) if headers: if not hasattr(headers[0], 'name'): mp = msg.policy headers = [mp.header_factory(*mp.header_source_parse([header])) for header in headers] try: for header in headers: if header.defects: raise header.defects[0] msg[header.name] = header except email.errors.HeaderDefect as exc: raise ValueError("Invalid header: {}".format( header.fold(policy=msg.policy))) from exc
Example #10
Source File: contentmanager.py From odoo13-x64 with GNU General Public License v3.0 | 5 votes |
def get_text_content(msg, errors='replace'): content = msg.get_payload(decode=True) charset = msg.get_param('charset', 'ASCII') return content.decode(charset, errors=errors)
Example #11
Source File: message.py From OrangeAssassin with Apache License 2.0 | 5 votes |
def _iter_parts(msg): """Extract and decode the text parts from the parsed email message. For non-text parts the payload will be None. Yields (payload, part) """ for part in msg.walk(): if part.get_content_maintype() == "text": payload = part.get_payload(decode=True) charset = part.get_content_charset() errors = "ignore" if not charset: charset = "ascii" elif charset.lower().replace("_", "-") in STRICT_CHARSETS: errors = "strict" try: payload = payload.decode(charset, errors) except (LookupError, UnicodeError, AssertionError): try: payload = payload.decode("ascii", "ignore") except UnicodeError: continue yield payload, part else: yield None, part
Example #12
Source File: contentmanager.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def _prepare_set(msg, maintype, subtype, headers): msg['Content-Type'] = '/'.join((maintype, subtype)) if headers: if not hasattr(headers[0], 'name'): mp = msg.policy headers = [mp.header_factory(*mp.header_source_parse([header])) for header in headers] try: for header in headers: if header.defects: raise header.defects[0] msg[header.name] = header except email.errors.HeaderDefect as exc: raise ValueError("Invalid header: {}".format( header.fold(policy=msg.policy))) from exc
Example #13
Source File: contentmanager.py From ironpython3 with Apache License 2.0 | 5 votes |
def _prepare_set(msg, maintype, subtype, headers): msg['Content-Type'] = '/'.join((maintype, subtype)) if headers: if not hasattr(headers[0], 'name'): mp = msg.policy headers = [mp.header_factory(*mp.header_source_parse([header])) for header in headers] try: for header in headers: if header.defects: raise header.defects[0] msg[header.name] = header except email.errors.HeaderDefect as exc: raise ValueError("Invalid header: {}".format( header.fold(policy=msg.policy))) from exc
Example #14
Source File: contentmanager.py From ironpython3 with Apache License 2.0 | 5 votes |
def get_text_content(msg, errors='replace'): content = msg.get_payload(decode=True) charset = msg.get_param('charset', 'ASCII') return content.decode(charset, errors=errors)
Example #15
Source File: contentmanager.py From Imogen with MIT License | 5 votes |
def _prepare_set(msg, maintype, subtype, headers): msg['Content-Type'] = '/'.join((maintype, subtype)) if headers: if not hasattr(headers[0], 'name'): mp = msg.policy headers = [mp.header_factory(*mp.header_source_parse([header])) for header in headers] try: for header in headers: if header.defects: raise header.defects[0] msg[header.name] = header except email.errors.HeaderDefect as exc: raise ValueError("Invalid header: {}".format( header.fold(policy=msg.policy))) from exc
Example #16
Source File: contentmanager.py From Imogen with MIT License | 5 votes |
def get_text_content(msg, errors='replace'): content = msg.get_payload(decode=True) charset = msg.get_param('charset', 'ASCII') return content.decode(charset, errors=errors)
Example #17
Source File: contentmanager.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def _prepare_set(msg, maintype, subtype, headers): msg['Content-Type'] = '/'.join((maintype, subtype)) if headers: if not hasattr(headers[0], 'name'): mp = msg.policy headers = [mp.header_factory(*mp.header_source_parse([header])) for header in headers] try: for header in headers: if header.defects: raise header.defects[0] msg[header.name] = header except email.errors.HeaderDefect as exc: raise ValueError("Invalid header: {}".format( header.fold(policy=msg.policy))) from exc
Example #18
Source File: contentmanager.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def get_text_content(msg, errors='replace'): content = msg.get_payload(decode=True) charset = msg.get_param('charset', 'ASCII') return content.decode(charset, errors=errors)
Example #19
Source File: test_packaging.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def test_email_parsing_errors_are_handled(self): mocked_open = mock.mock_open() with mock.patch('pbr.packaging.open', mocked_open): with mock.patch('email.message_from_file') as message_from_file: message_from_file.side_effect = [ email.errors.MessageError('Test'), {'Name': 'pbr_testpackage'}] version = packaging._get_version_from_pkg_metadata( 'pbr_testpackage') self.assertTrue(message_from_file.called) self.assertIsNone(version)
Example #20
Source File: http.py From terraform-templates with Apache License 2.0 | 5 votes |
def _http_request_requests(self, url, headers, data, params): kwargs = { 'verify': self.verify_cert, } if url is not None: kwargs['url'] = url if headers is not None: kwargs['headers'] = headers if data is not None: kwargs['data'] = data if params is not None: kwargs['params'] = params if self.timeout is not None: kwargs['timeout'] = self.timeout try: if data is None: r = requests.get(**kwargs) else: r = requests.post(**kwargs) except requests.exceptions.RequestException as e: raise PanHttpError('RequestException: ' + str(e)) self.code = r.status_code self.reason = r.reason x = ['%s: %s' % (k, v) for k, v in r.headers.items()] try: self.headers = email.message_from_string('\n'.join(x)) except (TypeError, email.errors.MessageError) as e: raise PanHttpError('email.message_from_string() %s' % e) self.encoding = self.headers.get_content_charset('utf8') self.content_type = self.headers.get_content_type() self.content = r.content self.text = r.text # allow non-2XX error codes # see http://bugs.python.org/issue18543 for why we can't just # install a new HTTPErrorProcessor()
Example #21
Source File: http.py From terraform-templates with Apache License 2.0 | 4 votes |
def _http_request_urllib(self, url, headers, data, params): if params is not None: url += '?' + urlencode(params) kwargs = {} if url is not None: kwargs['url'] = url if headers is not None: kwargs['headers'] = headers if data is not None: kwargs['data'] = data if headers is not None: x = set(k.lower() for k in headers) if not 'content-type' in x: kwargs['data'] = urlencode(data) # data must by type 'bytes' for 3.x if _isunicode(kwargs['data']): kwargs['data'] = kwargs['data'].encode() request = Request(**kwargs) kwargs = { 'url': request, } if self.timeout is not None: kwargs['timeout'] = self.timeout if not self.verify_cert and \ (sys.version_info.major == 2 and sys.hexversion >= 0x02070900 or sys.version_info.major == 3 and sys.hexversion >= 0x03040300): context = ssl._create_unverified_context() kwargs['context'] = context try: response = self._urlopen(**kwargs) except (URLError, IOError, ssl.CertificateError) as e: # IOError for urllib timeouts # ssl.CertificateError for mismatching hostname raise PanHttpError(str(e)) self.code = response.getcode() if hasattr(response, 'reason'): # 3.2 self.reason = response.reason elif hasattr(response, 'msg'): # 2.7 self.reason = response.msg try: self.headers = email.message_from_string(str(response.info())) except (TypeError, email.errors.MessageError) as e: raise PanHttpError('email.message_from_string() %s' % e) self.encoding = self.headers.get_content_charset('utf8') self.content_type = self.headers.get_content_type() self.content = response.read() self.text = self.content.decode(self.encoding)