From 1801fe5c5a5d42cb30de0803377ed4e682fcea3c Mon Sep 17 00:00:00 2001 From: Wolfgang Müller Date: Tue, 27 Apr 2021 18:00:47 +0200 Subject: Move Actions to their own file --- quarg/main.py | 76 +++++++---------------------------------------------------- 1 file changed, 8 insertions(+), 68 deletions(-) (limited to 'quarg/main.py') diff --git a/quarg/main.py b/quarg/main.py index 541c4d6..410306a 100644 --- a/quarg/main.py +++ b/quarg/main.py @@ -2,76 +2,16 @@ import argparse import configparser import os import sys -from abc import ABCMeta, abstractmethod from timeit import default_timer as timer -import dateutil.relativedelta from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker +import quarg.actions as actions import quarg.database.filters as filters from quarg.database.tables import Backlog, Buffer, Network, QuasselUser, Sender from quarg.quassel.formatter import format_from -from quarg.quassel.types import BufferType, MessageFlag, MessageType -from quarg.utils import errx, parse_isodate - -# TODO Find out what to do about passing INVALID or GROUP as BufferType -# TODO Find out what to do about passing various message flags - -class ParseEnum(argparse.Action, metaclass=ABCMeta): - def __call__(self, parser, namespace, value, option_string=None): - key = value.upper() - if key not in self.enumclass.__members__: - errx(f'Not a valid {self.enumclass.describe()}: {value}') - - saved = getattr(namespace, self.dest) or [] - saved.append(self.enumclass[key]) - setattr(namespace, self.dest, saved) - - @property - @abstractmethod - def enumclass(self): - pass - -class ParseMessageType(ParseEnum): - @property - def enumclass(self): - return MessageType - -class ParseMessageFlag(ParseEnum): - @property - def enumclass(self): - return MessageFlag - -class ParseBufferType(ParseEnum): - @property - def enumclass(self): - return BufferType - -# FIXME make sure pylint disables are actually still needed everywhere - -class ParseDate(argparse.Action): - # pylint: disable=too-few-public-methods, unsupported-membership-test - def __call__(self, parser, namespace, datespec, option_string=None): - setattr(namespace, self.dest, parse_isodate(datespec)) - -class ParseAround(argparse.Action): - # pylint: disable=too-few-public-methods, unsupported-membership-test - def __call__(self, parser, namespace, aroundspec, option_string=None): - if '/' in aroundspec: - # FIXME / fine here? - datespec, rangespec = aroundspec.split('/', 1) - try: - hour_range = int(rangespec) - except ValueError as err: - errx(err) - else: - datespec, hour_range = (aroundspec, 12) - - date = parse_isodate(datespec) - - offset = dateutil.relativedelta.relativedelta(hours=hour_range) - setattr(namespace, self.dest, (date - offset, date + offset)) +from quarg.utils import errx # TODO Make --after/--before and --around mutually exclusive # FIXME why need default=None for --joined/--no-joined? @@ -82,18 +22,18 @@ cli.add_argument('query', nargs='*', help='match messages containing this query' cli.add_argument('-d', action='store_true', dest='debug', help='print SQL query information') cli.add_argument('-e', action='store_true', dest='expr', help='interpret query as LIKE expression') cli.add_argument('-b', action='append', dest='buffer', help='match messages sent to this buffer') -cli.add_argument('-B', action=ParseBufferType, dest='buftype', help='match messages sent to buffers of this type') +cli.add_argument('-B', action=actions.ParseBufferType, dest='buftype', help='match messages sent to buffers of this type') cli.add_argument('-n', action='append', dest='nick', help='match messages sent by this nickname') cli.add_argument('-N', action='append', dest='network', help='match messages sent to this network') cli.add_argument('-u', action='append', dest='user', help='match messages received by this quassel user') -cli.add_argument('-t', action=ParseMessageType, dest='msgtype', help='match messages of this message type') -cli.add_argument('-f', action=ParseMessageFlag, dest='msgflag', help='match messages with this flag') +cli.add_argument('-t', action=actions.ParseMessageType, dest='msgtype', help='match messages of this message type') +cli.add_argument('-f', action=actions.ParseMessageFlag, dest='msgflag', help='match messages with this flag') cli.add_argument('-p', action='append', dest='prefix', help='match nicks with this prefix') cli.add_argument('--joined', default=None, action='store_true', dest='joined', help='match messages sent to channels which are currently joined') cli.add_argument('--no-joined', default=None, action='store_false', dest='joined', help='match messages sent to channels which are not currently joined') -cli.add_argument('--after', action=ParseDate, metavar='DATE', help='match messages sent after this date') -cli.add_argument('--before', action=ParseDate, metavar='DATE', help='match messages sent before this date') -cli.add_argument('--around', action=ParseAround, metavar='DATE', help='match messages sent within 12 hours of this date') +cli.add_argument('--after', action=actions.ParseDate, metavar='DATE', help='match messages sent after this date') +cli.add_argument('--before', action=actions.ParseDate, metavar='DATE', help='match messages sent before this date') +cli.add_argument('--around', action=actions.ParseAround, metavar='DATE', help='match messages sent within 12 hours of this date') # pylint: enable=line-too-long Session = sessionmaker() -- cgit v1.2.3-2-gb3c3