Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
bfc3944b
Commit
bfc3944b
authored
Mar 25, 1997
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change by Andrew Kuchling (edited by Guido):
Removed unused import tempfile. Added some docstrings.
parent
31ef35b8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
17 deletions
+32
-17
Lib/mailcap.py
Lib/mailcap.py
+32
-17
No files found.
Lib/mailcap.py
View file @
bfc3944b
# Mailcap file handling. See RFC 1524.
"""Mailcap file handling. See RFC 1524."""
import
os
import
string
import
tempfile
# Part 1: top-level interface.
def
getcaps
():
"""Return a dictionary containing the mailcap database.
The dictionary maps a MIME type (in all lowercase,
e.g. 'text/plain') to a list of corresponding mailcap entries.
"""
caps
=
{}
for
mailcap
in
listmailcapfiles
():
try
:
...
...
@@ -24,6 +29,7 @@ def getcaps():
return
caps
def
listmailcapfiles
():
"""Return a list of all mailcap files found on the system."""
# XXX Actually, this is Unix-specific
if
os
.
environ
.
has_key
(
'MAILCAPS'
):
str
=
os
.
environ
[
'MAILCAPS'
]
...
...
@@ -112,30 +118,39 @@ def parsefield(line, i, n):
# Part 3: using the database.
def
findmatch
(
caps
,
type
,
key
=
'view'
,
filename
=
"/dev/null"
,
plist
=
[]):
entries
=
lookup
(
caps
,
type
,
key
)
def
findmatch
(
caps
,
MIMEtype
,
key
=
'view'
,
filename
=
"/dev/null"
,
plist
=
[]):
"""Find a match for a mailcap entry.
Return a tuple containing the command line, and the mailcap entry
used; (None, None) if no match is found. This may invoke the
'test' command of several matching entries before deciding which
entry to use.
"""
entries
=
lookup
(
caps
,
MIMEtype
,
key
)
# XXX This code should somehow check for the needsterminal flag.
for
e
in
entries
:
if
e
.
has_key
(
'test'
):
test
=
subst
(
e
[
'test'
],
filename
,
plist
)
if
test
and
os
.
system
(
test
)
!=
0
:
continue
command
=
subst
(
e
[
key
],
type
,
filename
,
plist
)
command
=
subst
(
e
[
key
],
MIME
type
,
filename
,
plist
)
return
command
,
e
return
None
,
None
def
lookup
(
caps
,
type
,
key
=
None
):
def
lookup
(
caps
,
MIME
type
,
key
=
None
):
entries
=
[]
if
caps
.
has_key
(
type
):
entries
=
entries
+
caps
[
type
]
types
=
string
.
splitfields
(
type
,
'/'
)
type
=
types
[
0
]
+
'/*'
if
caps
.
has_key
(
type
):
entries
=
entries
+
caps
[
type
]
if
caps
.
has_key
(
MIME
type
):
entries
=
entries
+
caps
[
MIME
type
]
MIMEtypes
=
string
.
splitfields
(
MIME
type
,
'/'
)
MIMEtype
=
MIME
types
[
0
]
+
'/*'
if
caps
.
has_key
(
MIME
type
):
entries
=
entries
+
caps
[
MIME
type
]
if
key
is
not
None
:
entries
=
filter
(
lambda
e
,
key
=
key
:
e
.
has_key
(
key
),
entries
)
return
entries
def
subst
(
field
,
type
,
filename
,
plist
=
[]):
def
subst
(
field
,
MIME
type
,
filename
,
plist
=
[]):
# XXX Actually, this is Unix-specific
res
=
''
i
,
n
=
0
,
len
(
field
)
...
...
@@ -152,7 +167,7 @@ def subst(field, type, filename, plist=[]):
elif
c
==
's'
:
res
=
res
+
filename
elif
c
==
't'
:
res
=
res
+
type
res
=
res
+
MIME
type
elif
c
==
'{'
:
start
=
i
while
i
<
n
and
field
[
i
]
<>
'}'
:
...
...
@@ -187,11 +202,11 @@ def test():
for
i
in
range
(
1
,
len
(
sys
.
argv
),
2
):
args
=
sys
.
argv
[
i
:
i
+
2
]
if
len
(
args
)
<
2
:
print
"usage: mailcap [type file] ..."
print
"usage: mailcap [
MIME
type file] ..."
return
type
=
args
[
0
]
MIME
type
=
args
[
0
]
file
=
args
[
1
]
command
,
e
=
findmatch
(
caps
,
type
,
'view'
,
file
)
command
,
e
=
findmatch
(
caps
,
MIME
type
,
'view'
,
file
)
if
not
command
:
print
"No viewer found for"
,
type
else
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment