From f7f4569594d5bb3fe95eb2882a0b47a511d2b534 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Wed, 27 Nov 2024 19:28:28 -0500 Subject: [PATCH] Move MessageRange to message.py --- lib/nntp/tiny/message.py | 64 ++++++++++++++++++++++++++++++++++++++ lib/nntp/tiny/session.py | 66 +--------------------------------------- 2 files changed, 65 insertions(+), 65 deletions(-) diff --git a/lib/nntp/tiny/message.py b/lib/nntp/tiny/message.py index bfc1f31..c6fa86b 100644 --- a/lib/nntp/tiny/message.py +++ b/lib/nntp/tiny/message.py @@ -45,6 +45,70 @@ def parse_timestamp(timestamp: str): return parsedate_to_datetime(timestamp) +class MessageRange(): + __slots__ = 'id', 'min', 'max', + + RE_NUM = re.compile(r'^(\d+)$') + RE_RANGE = re.compile(r'^(\d+)-(\d+)$') + RE_RANGE_LOWER = re.compile(r'^(\d+$)-$') + RE_RANGE_UPPER = re.compile(r'^-(\d+$)$') + + def __init__(self): + self.id: int = None + self.min: int = None + self.max: int = None + + def __str__(self): + if self.id is not None: + return str(self.id) + + if self.min is not None and self.max is None: + return "%d-" % (self.min) + elif self.min is not None and self.max is not None: + return "%d-%d" % (self.min, self.max) + elif self.min is None and self.max is not None: + return "-%d" % (self.max) + + return "?" + + def where(self): + if self.id is not None: + return "id = %d" % (self.id) + + if self.min is not None and self.max is None: + return "id >= %d" % (self.min) + elif self.min is not None and self.max is not None: + return "id >= %d and id <= %d" % (self.min, self.max) + elif self.min is None and self.max is not None: + return "id <= %d" % (self.max) + + @staticmethod + def parse(r: str): + match = __class__.RE_NUM.match(r) + if match: + obj = __class__() + obj.id = int(match[1]) + return obj + + match = __class__.RE_RANGE.match(r) + if match: + obj = __class__() + obj.min = int(match[1]) + obj.max = int(match[2]) + return obj + + match = __class__.RE_RANGE_LOWER.match(r) + if match: + obj = __class__() + obj.min = int(match[1]) + return obj + + match = __class__.RE_RANGE_UPPER.match(r) + if match: + obj = __class__() + obj.max = int(match[1]) + return obj + class MessageState(enum.Enum): EMPTY = 0 HEADER = 1 diff --git a/lib/nntp/tiny/session.py b/lib/nntp/tiny/session.py index a8a4271..1ff6f84 100644 --- a/lib/nntp/tiny/session.py +++ b/lib/nntp/tiny/session.py @@ -13,7 +13,7 @@ from nntp.tiny.buffer import LineBuffer, OutputBuffer, BufferOverflow from nntp.tiny.db import Database from nntp.tiny.response import Response, ResponseCode from nntp.tiny.newsgroup import Newsgroup -from nntp.tiny.message import Message +from nntp.tiny.message import Message, MessageRange class SessionState(enum.Flag): ACTIVE = 1 @@ -28,70 +28,6 @@ class MessagePart(enum.Enum): BODY = enum.auto() WHOLE = enum.auto() -class MessageRange(): - __slots__ = 'id', 'min', 'max', - - RE_NUM = re.compile(r'^(\d+)$') - RE_RANGE = re.compile(r'^(\d+)-(\d+)$') - RE_RANGE_LOWER = re.compile(r'^(\d+$)-$') - RE_RANGE_UPPER = re.compile(r'^-(\d+$)$') - - def __init__(self): - self.id: int = None - self.min: int = None - self.max: int = None - - def __str__(self): - if self.id is not None: - return str(self.id) - - if self.min is not None and self.max is None: - return "%d-" % (self.min) - elif self.min is not None and self.max is not None: - return "%d-%d" % (self.min, self.max) - elif self.min is None and self.max is not None: - return "-%d" % (self.max) - - return "?" - - def where(self): - if self.id is not None: - return "id = %d" % (self.id) - - if self.min is not None and self.max is None: - return "id >= %d" % (self.min) - elif self.min is not None and self.max is not None: - return "id >= %d and id <= %d" % (self.min, self.max) - elif self.min is None and self.max is not None: - return "id <= %d" % (self.max) - - @staticmethod - def parse(r: str): - match = __class__.RE_NUM.match(r) - if match: - obj = __class__() - obj.id = int(match[1]) - return obj - - match = __class__.RE_RANGE.match(r) - if match: - obj = __class__() - obj.min = int(match[1]) - obj.max = int(match[2]) - return obj - - match = __class__.RE_RANGE_LOWER.match(r) - if match: - obj = __class__() - obj.min = int(match[1]) - return obj - - match = __class__.RE_RANGE_UPPER.match(r) - if match: - obj = __class__() - obj.max = int(match[1]) - return obj - class Session(): NNTP_VERSION = 2 NNTP_CAPABILITIES = [