def _load_mailcaps(self): mailcap_file = self.config.MAILCAP if not os.path.exists(mailcap_file): return mailcap.getcaps() mailcaps_env = 'MAILCAPS' caps = dict() # Prepends application-specific mailcap file to MAILCAPS env # This allows mailcap.getcaps to read it before other mailcap files # Previous value of MAILCAPS is restored after usage prev_mailcaps = os.getenv(mailcaps_env) if prev_mailcaps is None: # start with default system caps files; will be overwritten by local file caps = mailcap.getcaps() new_mailcaps = mailcap_file else: new_mailcaps = os.pathsep.join((mailcap_file, prev_mailcaps)) os.environ[mailcaps_env] = new_mailcaps # merge local mailcaps file with default caps # settings in local file will override defaults for k, v in mailcap.getcaps().items(): caps[k] = v if prev_mailcaps is None: del os.environ[mailcaps_env] else: os.environ[mailcaps_env] = prev_mailcaps return caps
def __init__(self, stdscr, config): self.stdscr = stdscr self.config = config self.loader = LoadScreen(self) self._display = None self._mailcap_dict = mailcap.getcaps()
def test_mock_getcaps(self): # Test mailcap.getcaps() using mock mailcap file in this dir. # Temporarily override any existing system mailcap file by pointing the # MAILCAPS environment variable to our mock file. os.environ["MAILCAPS"] = MAILCAPFILE caps = mailcap.getcaps() self.assertDictEqual(caps, MAILCAPDICT)
def test_extended(self): os.environ['MAILCAPS'] = ':'.join([TRIVIAL, EXTENDED]) # The line numbers should increment between files d = mailcap_fix.getcaps() self.assertEqual(d['image/*'][0]['lineno'], 0) self.assertEqual(d['text/plain'][0]['lineno'], 3) command, entry = mailcap_fix.findmatch(d, 'image/jpeg', filename='a') self.assertEqual(command, 'feh a')
def __init__(self, stdscr, config): self.stdscr = stdscr self.config = config self.loader = LoadScreen(self) self._display = None self._mailcap_dict = mailcap.getcaps() self._term = os.environ.get('TERM') # This is a hack, the MIME parsers should be stateless # but we need to load the imgur credentials from the config mime_parsers.ImgurApiMIMEParser.CLIENT_ID = config['imgur_client_id']
def test_trivial(self): os.environ['MAILCAPS'] = TRIVIAL d = mailcap_fix.getcaps() self.assertEqual(d['image/*'][0]['lineno'], 0) command, entry = mailcap_fix.findmatch(d, 'image/png', filename='a') self.assertEqual(command, 'feh a') command, entry = mailcap_fix.findmatch(d, 'image/jpeg', filename='a') self.assertEqual(command, 'feh a') command, entry = mailcap_fix.findmatch(d, 'music/mp3', filename='a') self.assertEqual(command, 'play a')
def test_backwards_compatible(self): os.environ['MAILCAPS'] = TRIVIAL d = mailcap.getcaps() d_lineno = mailcap_fix.getcaps() # Note: Both of these cases should not break, but they will exhibit the # old, incorrect behavior and return the second entry # Call the patched findmatch() using an dict without ``lineno`` command, entry = mailcap_fix.findmatch(d, 'image/jpeg', filename='a') self.assertEqual(command, 'eog a') # Call the original findmatch() using a dict with the added ``lineno`` command, entry = mailcap.findmatch(d_lineno, 'image/jpeg', filename='a') self.assertEqual(command, 'eog a')
def test_system_mailcap(self): # Test mailcap.getcaps() with mailcap file(s) on system, if any. caps = mailcap.getcaps() self.assertIsInstance(caps, dict) mailcapfiles = mailcap.listmailcapfiles() existingmcfiles = [mcf for mcf in mailcapfiles if os.path.exists(mcf)] if existingmcfiles: # At least 1 mailcap file exists, so test that. for (k, v) in caps.items(): self.assertIsInstance(k, str) self.assertIsInstance(v, list) for e in v: self.assertIsInstance(e, dict) else: # No mailcap files on system. getcaps() should return empty dict. self.assertEqual({}, caps)
elapsed_time = time.time() - start print(' %s ms per loop' % (elapsed_time / float(n) * 1000.0)) def lookup_all(lookup_func, d): # Loop through the mailcap dict and run a lookup for every MIME type for key in d: lookup_func(d, key) if __name__ == '__main__': for filename in (TRIVIAL, EXTENDED): os.environ['MAILCAPS'] = filename print('MAICAPS=%s, mailcap.getcaps()' % filename) timer(mailcap.getcaps) print('MAICAPS=%s, mailcap_fix.getcaps()' % filename) timer(mailcap_fix.getcaps) d = mailcap.getcaps() print('MAICAPS=%s, mailcap.lookup(d, ...), %s entries' % (filename, len(d))) timer(lookup_all, mailcap.lookup, d) d_fix = mailcap_fix.getcaps() print('MAICAPS=%s, mailcap_fix.lookup(d_fix, ...), %s entries' % (filename, len(d_fix))) timer(lookup_all, mailcap_fix.lookup, d_fix)