mailcap.py
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# GNU Mailutils -- a suite of utilities for electronic mail
# Copyright (C) 2009-2012, 2014-2016 Free Software Foundation, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General
# Public License along with this library. If not, see
# <http://www.gnu.org/licenses/>.
from mailutils.c_api import mailcap
from mailutils.error import MailcapError
class Mailcap:
def __init__ (self, stream):
self.mc = mailcap.MailcapType ()
status = mailcap.create (self.mc, stream.stm)
if status:
raise MailcapError (status)
def __del__ (self):
mailcap.destroy (self.mc)
del self.mc
def __len__ (self):
return self.entries_count ()
def __getitem__ (self, item):
return self.get_entry (item)
def __iter__ (self):
self.__count = 0
self.__len = self.entries_count ()
return self
def next (self):
if self.__count >= self.__len:
self.__count = 0
raise StopIteration
else:
self.__count += 1
return self.__getitem__ (self.__count)
def entries_count (self):
"""Return the number of entries found in the mailcap."""
status, count = mailcap.entries_count (self.mc)
if status:
raise MailcapError (status)
return count
def get_entry (self, item):
"""Return in MailcapEntry the mailcap entry of 'item'."""
status, entry = mailcap.get_entry (self.mc, item)
if status:
raise MailcapError (status)
return MailcapEntry (entry)
class MailcapEntry:
def __init__ (self, entry):
self.entry = entry
def __len__ (self):
return self.fields_count ()
def __getitem__ (self, item):
return self.get_field (item)
def __iter__ (self):
self.__count = 0
self.__len = self.fields_count ()
return self
def next (self):
if self.__count >= self.__len:
self.__count = 0
raise StopIteration
else:
self.__count += 1
return self.__getitem__ (self.__count)
def fields_count (self):
status, count = mailcap.entry_fields_count (self.entry)
if status:
raise MailcapEntry (status)
return count
def get_field (self, i):
status, field = mailcap.entry_get_field (self.entry, i)
if status:
raise MailcapEntry (status)
return field
def get_typefield (self):
status, typefield = mailcap.entry_get_typefield (self.entry)
if status:
raise MailcapEntry (status)
return typefield
def get_viewcommand (self):
status, viewcommand = mailcap.entry_get_viewcommand (self.entry)
if status:
raise MailcapEntry (status)
return viewcommand