Move MessageRange to message.py
This commit is contained in:
parent
97050eafe4
commit
f7f4569594
2 changed files with 65 additions and 65 deletions
|
@ -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
|
||||
|
|
|
@ -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 = [
|
||||
|
|
Loading…
Add table
Reference in a new issue