nntp.utils¶
Parsing and formatting helpers for NNTP arguments and responses. Copyright (C) 2013-2020 Byron Platt
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
Attributes¶
Functions¶
|
Format an article range as a command argument. |
|
Format a message-id or article range as a command argument. |
|
Parse a LIST ACTIVE or NEWGROUPS line. |
|
Parse a LIST ACTIVE.TIMES line. |
|
Parse a LIST NEWSGROUPS line. |
|
Parse a LIST OVERVIEW.FMT line (RFC 3977 section 8.4). |
|
Parse an HDR, XHDR or XPAT line: an article number, then a value. |
|
Parse header lines up to the first blank line. |
|
Format headers as an article header block, ending with a blank line. |
|
Parse a date as returned by the DATE command. |
|
Parse a Unix timestamp, as used by LIST ACTIVE.TIMES. |
Module Contents¶
- nntp.utils._HEADER_NAME_RE¶
- nntp.utils._HEADER_FOLD_RE¶
- nntp.utils.unparse_range(obj: nntp.types.Range) str¶
Format an article range as a command argument.
- Parameters:
obj – An article number, (first,) for first onwards, or (first, last).
- Returns:
The range, e.g. “4678”, “4245-” or “4245-5234”.
- Raises:
ValueError – If obj is not one of those forms.
- nntp.utils.unparse_msgid_range(obj: str | nntp.types.Range) str¶
Format a message-id or article range as a command argument.
- Parameters:
obj – A message-id, or a range as for unparse_range().
- Returns:
The message-id unchanged, or the formatted range.
- Raises:
ValueError – If obj is not a valid range.
- nntp.utils.parse_newsgroup(line: str) nntp.types.Newsgroup¶
Parse a LIST ACTIVE or NEWGROUPS line.
The fields are name, high water mark, low water mark and status (RFC 3977 section 7.6.3).
- Parameters:
line – The response line.
- Returns:
The newsgroup.
- Raises:
ValueError – If the line cannot be parsed.
- nntp.utils.parse_active_time(line: str) tuple[str, datetime.datetime, str]¶
Parse a LIST ACTIVE.TIMES line.
- Parameters:
line – The response line: name, creation time (seconds since the epoch) and creator.
- Returns:
The newsgroup name, its creation time in UTC and its creator.
- Raises:
ValueError – If the line cannot be parsed.
- nntp.utils.parse_newsgroup_description(line: str) tuple[str, str]¶
Parse a LIST NEWSGROUPS line.
- Parameters:
line – The response line: name, then an optional description.
- Returns:
The newsgroup name and description (”” if there is none).
- Raises:
ValueError – If the line is empty.
- nntp.utils.parse_overview_fmt(line: str) tuple[str, bool]¶
Parse a LIST OVERVIEW.FMT line (RFC 3977 section 8.4).
- Parameters:
line – The response line, e.g. “Subject:”, “:bytes” or “Xref:full”.
- Returns:
The field name and whether the field is “full” (its values include the header name).
- Raises:
ValueError – If the line cannot be parsed.
- nntp.utils.parse_number_value(line: str) tuple[int, str]¶
Parse an HDR, XHDR or XPAT line: an article number, then a value.
A line starting with a message-id (older XHDR by message-id) gets number 0, as HDR gives.
- Parameters:
line – The response line.
- Returns:
The article number and the value (”” if there is none).
- Raises:
ValueError – If the line cannot be parsed.
- nntp.utils.parse_headers(obj: str | collections.abc.Iterable[str]) nntp.headerdict.HeaderDict¶
Parse header lines up to the first blank line.
Continuation lines are unfolded. CRLF and LF line endings are accepted.
- Parameters:
obj – A string, or an iterable of lines such as a file.
- Returns:
The headers. If a header is repeated the last value wins.
- Raises:
ValueError – If the first line is a continuation line or a line is not a header.
- nntp.utils.unparse_headers(hdrs: collections.abc.Mapping[str, str]) str¶
Format headers as an article header block, ending with a blank line.
Values may be folded (RFC 5322 section 2.2.3): a line break followed by a space or tab. LF line breaks in folds are sent as CRLF.
- Parameters:
hdrs – The headers.
- Returns:
The header block.
- Raises:
ValueError – If a name is not printable ASCII without “:”, or a value contains NUL or a CR/LF that is not part of a fold. Either would let the header end early and inject extra headers or body text.
- nntp.utils.parse_date(value: str | int) datetime.datetime¶
Parse a date as returned by the DATE command.
- Parameters:
value – A date in the format YYYYMMDDHHMMSS.
- Returns:
The date, in UTC.
- Raises:
ValueError – If the value is not a valid date in that format.
- nntp.utils.parse_epoch(value: str | int) datetime.datetime¶
Parse a Unix timestamp, as used by LIST ACTIVE.TIMES.
- Parameters:
value – Seconds since the epoch.
- Returns:
The time, in UTC.
- Raises:
ValueError – If the value is not an integer, or is out of range.