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
e894fc0e
Commit
e894fc0e
authored
Jun 11, 1998
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support new overridable method, isheader() (ESR).
Also implement __setitem__(), more-or-less correctly (GvR).
parent
444d0f87
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
24 deletions
+48
-24
Lib/rfc822.py
Lib/rfc822.py
+48
-24
No files found.
Lib/rfc822.py
View file @
e894fc0e
...
@@ -13,13 +13,13 @@ sys.stdin or call os.popen().
...
@@ -13,13 +13,13 @@ sys.stdin or call os.popen().
Then pass the open file object to the Message() constructor:
Then pass the open file object to the Message() constructor:
m = Message(fp)
m = Message(fp)
This class can work with any input object that supports
read and seek
This class can work with any input object that supports
a readline
method
s. The initialization method which parses the message will work
method
. If the input object has seek and tell capability, the
even without seek capability, but in that case the final seek to the
rewindbody method will work; also illegal lines will be pushed back
start of the delimiter line won't take place. However, if the input
onto the input stream. If the input object lacks seek but has an
object has an `unread' method that can push back a line of input,
`unread' method that can push back a line of input, Message will use
Message will use that to push back the delimiter line. Thus this class
that to push back illegal lines. Thus this class can be used to parse
can be used to parse
messages coming from a buffered stream.
messages coming from a buffered stream.
The optional `seekable' argument is provided as a workaround for
The optional `seekable' argument is provided as a workaround for
certain stdio libraries in which tell() discards buffered data before
certain stdio libraries in which tell() discards buffered data before
...
@@ -134,29 +134,30 @@ class Message:
...
@@ -134,29 +134,30 @@ class Message:
self
.
unixfrom
=
self
.
unixfrom
+
line
self
.
unixfrom
=
self
.
unixfrom
+
line
continue
continue
firstline
=
0
firstline
=
0
if
self
.
islast
(
line
):
if
headerseen
and
line
[
0
]
in
'
\
t
'
:
break
elif
headerseen
and
line
[
0
]
in
'
\
t
'
:
# It's a continuation line.
# It's a continuation line.
list
.
append
(
line
)
list
.
append
(
line
)
x
=
(
self
.
dict
[
headerseen
]
+
"
\
n
"
+
x
=
(
self
.
dict
[
headerseen
]
+
"
\
n
"
+
string
.
strip
(
line
))
string
.
strip
(
line
))
self
.
dict
[
headerseen
]
=
string
.
strip
(
x
)
self
.
dict
[
headerseen
]
=
string
.
strip
(
x
)
elif
':'
in
line
:
continue
# It's a header line.
list
.
append
(
line
)
i
=
string
.
find
(
line
,
':'
)
headerseen
=
string
.
lower
(
line
[:
i
])
self
.
dict
[
headerseen
]
=
string
.
strip
(
line
[
i
+
1
:])
elif
self
.
iscomment
(
line
):
elif
self
.
iscomment
(
line
):
pass
# It's a comment. Ignore it.
continue
elif
self
.
islast
(
line
):
# Note! No pushback here! The delimiter line gets eaten.
break
headerseen
=
self
.
isheader
(
line
)
if
headerseen
:
# It's a legal header line, save it.
list
.
append
(
line
)
self
.
dict
[
headerseen
]
=
string
.
strip
(
line
[
len
(
headerseen
)
+
2
:])
continue
else
:
else
:
# It's not a header line; stop here.
# It's not a header line;
throw it back and
stop here.
if
not
headerseen
:
if
not
self
.
dict
:
self
.
status
=
'No headers'
self
.
status
=
'No headers'
else
:
else
:
self
.
status
=
'
Bad header
'
self
.
status
=
'
Non-header line where header expected
'
# Try to undo the read.
# Try to undo the read.
if
getattr
(
self
.
fp
,
'unread'
):
if
getattr
(
self
.
fp
,
'unread'
):
self
.
fp
.
unread
(
line
)
self
.
fp
.
unread
(
line
)
...
@@ -165,6 +166,19 @@ class Message:
...
@@ -165,6 +166,19 @@ class Message:
else
:
else
:
self
.
status
=
self
.
status
+
'; bad seek'
self
.
status
=
self
.
status
+
'; bad seek'
break
break
def
isheader
(
self
,
line
):
"""Determine whether a given line is a legal header.
This method should return the header name, suitably canonicalized.
You may override this method in order to use Message parsing
on tagged data in RFC822-like formats with special header formats.
"""
i
=
string
.
find
(
line
,
':'
)
if
i
>
0
:
return
string
.
lower
(
line
[:
i
])
else
:
return
None
def
islast
(
self
,
line
):
def
islast
(
self
,
line
):
"""Determine whether a line is a legal end of RFC-822 headers.
"""Determine whether a line is a legal end of RFC-822 headers.
...
@@ -323,6 +337,15 @@ class Message:
...
@@ -323,6 +337,15 @@ class Message:
def
__getitem__
(
self
,
name
):
def
__getitem__
(
self
,
name
):
"""Get a specific header, as from a dictionary."""
"""Get a specific header, as from a dictionary."""
return
self
.
dict
[
string
.
lower
(
name
)]
return
self
.
dict
[
string
.
lower
(
name
)]
def
__setitem__
(
self
,
name
,
value
):
"""Set the value of a header."""
del
self
[
name
]
# Won't fail if it doesn't exist
self
.
dict
[
string
.
lower
(
name
)]
=
value
text
=
name
+
": "
+
value
lines
=
string
.
split
(
text
,
"
\
n
"
)
for
line
in
lines
:
self
.
headers
.
append
(
line
+
"
\
n
"
)
def
__delitem__
(
self
,
name
):
def
__delitem__
(
self
,
name
):
"""Delete all occurrences of a specific header, if it is present."""
"""Delete all occurrences of a specific header, if it is present."""
...
@@ -646,7 +669,8 @@ class AddrlistClass:
...
@@ -646,7 +669,8 @@ class AddrlistClass:
"""Parse a sequence of RFC-822 phrases.
"""Parse a sequence of RFC-822 phrases.
A phrase is a sequence of words, which are in turn either
A phrase is a sequence of words, which are in turn either
RFC-822 atoms or quoted-strings.
RFC-822 atoms or quoted-strings. Phrases are canonicalized
by squeezing all runs of continuous whitespace into one space.
"""
"""
plist
=
[]
plist
=
[]
...
...
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