def test_lookup(self): # Test without key expected = [{ 'view': 'animate %s', 'lineno': 12 }, { 'view': 'mpeg_play %s', 'lineno': 13 }] actual = mailcap.lookup(MAILCAPDICT, 'video/mpeg') self.assertListEqual(expected, actual) # Test with key key = 'compose' expected = [{ 'edit': 'audiocompose %s', 'compose': 'audiocompose %s', 'description': '"An audio fragment"', 'view': 'showaudio %s', 'lineno': 6 }] actual = mailcap.lookup(MAILCAPDICT, 'audio/basic', key) self.assertListEqual(expected, actual) # Test on user-defined dicts without line numbers expected = [{'view': 'mpeg_play %s'}, {'view': 'animate %s'}] actual = mailcap.lookup(MAILCAPDICT_DEPRECATED, 'video/mpeg') self.assertListEqual(expected, actual)
def test_lookup(self): # Test without key expected = [{'view': 'mpeg_play %s'}, {'view': 'animate %s'}] actual = mailcap.lookup(MAILCAPDICT, 'video/mpeg') self.assertListEqual(expected, actual) # Test with key key = 'compose' expected = [{'edit': 'audiocompose %s', 'compose': 'audiocompose %s', 'description': '"An audio fragment"', 'view': 'showaudio %s'}] actual = mailcap.lookup(MAILCAPDICT, 'audio/basic', key) self.assertListEqual(expected, actual)
def decode_body(msg, s): "Decode body to plain text using first copiousoutput filter from mailcap" import mailcap import tempfile global caps if caps is None: caps = mailcap.getcaps() content_type = msg.get_content_type() if content_type.startswith('text/'): charset = msg.get_content_charset() else: charset = None tmpfile = tempfile.NamedTemporaryFile() command = None entries = mailcap.lookup(caps, content_type, "view") for entry in entries: if 'copiousoutput' in entry: if 'test' in entry: test = mailcap.subst(entry['test'], content_type, tmpfile.name) if test and os.system(test) != 0: continue command = mailcap.subst(entry["view"], content_type, tmpfile.name) break if not command: return s if charset and bytes is not str and isinstance(s, bytes): # Python3 s = s.decode(charset, "replace") if not isinstance(s, bytes): s = s.encode(g.default_encoding, "replace") tmpfile.write(s) tmpfile.flush() pipe = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) new_s = pipe.stdout.read() pipe.stdout.close() if pipe.wait() == 0: # result=0, Ok s = new_s if bytes is not str and isinstance(s, bytes): # Python3 s = s.decode(g.default_encoding, "replace") if charset and not isinstance(s, bytes): s = s.encode(charset, "replace") set_content_type(msg, "text/plain") msg["X-MIME-Autoconverted"] = \ "from %s to text/plain by %s id %s" \ % (content_type, g.host_name, command.split()[0]) else: msg["X-MIME-Autoconverted"] = \ "failed conversion from %s to text/plain by %s id %s" \ % (content_type, g.host_name, command.split()[0]) tmpfile.close() # Will be removed on close return s
def test_lookup(self): # Test without key expected = [{'view': 'animate %s', 'lineno': 12}, {'view': 'mpeg_play %s', 'lineno': 13}] actual = mailcap.lookup(MAILCAPDICT, 'video/mpeg') self.assertListEqual(expected, actual) # Test with key key = 'compose' expected = [{'edit': 'audiocompose %s', 'compose': 'audiocompose %s', 'description': '"An audio fragment"', 'view': 'showaudio %s', 'lineno': 6}] actual = mailcap.lookup(MAILCAPDICT, 'audio/basic', key) self.assertListEqual(expected, actual) # Test on user-defined dicts without line numbers expected = [{'view': 'mpeg_play %s'}, {'view': 'animate %s'}] actual = mailcap.lookup(MAILCAPDICT_DEPRECATED, 'video/mpeg') self.assertListEqual(expected, actual)
def update_event(self, inp=-1): self.set_output_val( 0, mailcap.lookup(self.input(0), self.input(1), self.input(2)))