all repos — listfix @ 798bc41ae5e67c136e8983a80d933f83aefd68a8

Postfix Mailing List Software; Maintained on behalf the of Agency Economy Incorporated NFP.

Implement an args module for checking and parsing command arguments.

	modified:   listfix.py
	modified:   listfix/__init__.py
	new file:   listfix/args.py
Brian Barto bartobrian@gmail.com
Tue, 29 Mar 2022 16:49:05 -0400
commit

798bc41ae5e67c136e8983a80d933f83aefd68a8

parent

0d94cacc881493397a1795c6f19ff0bbaf2cc4a0

3 files changed, 67 insertions(+), 73 deletions(-)

jump to
M listfix.pylistfix.py

@@ -1,9 +1,8 @@

#!/usr/bin/python3 import sys -import re import os -from listfix import DB, Email +from listfix import Args, DB, Email ######################## ## Function Defs

@@ -32,32 +31,20 @@ ########################

## Main Program ######################## -re_email_arg = re.compile("([^<>\"\s]+)@(\S+\.[^<>\"\s]+)") - ## Connect to DB (create DB if needed) and check tables. listfix_dir = os.path.dirname(os.path.realpath(__file__)) db = DB(listfix_dir + "/listfix.sqlite3") -## Get command +## Get args -command = None -if (len(sys.argv) >= 1): - command = sys.argv[1] -else: - raise ValueError("Missing Command Argument") +args = Args(sys.argv) +command = args.get_command() ## Evaluate command if (command == "filter"): - - if (len(sys.argv) >= 3): - if (not re_email_arg.match(sys.argv[2])): - raise ValueError(f"Error while executing filter.") - else: - raise ValueError(f"Error while executing filter.") - - list_email = sys.argv[2] + list_email = args.get_list_email() content = [] for line in sys.stdin:

@@ -89,89 +76,43 @@ for r in list_recipients:

email_out.send(r) elif (command == "lists"): - lists = db.get_lists() for l in lists: list_name = db.get_list_name(l) print(f"{l} ({list_name})") elif (command == "dump"): - - if (len(sys.argv) >= 3): - if (not re_email_arg.match(sys.argv[2])): - raise ValueError(f"Error while executing dump.") - else: - raise ValueError(f"Error while executing dump.") - - list_email = sys.argv[2] - + list_email = args.get_list_email() recipients = db.get_list_recipients(list_email) for r in recipients: recipient_name = db.get_recipient_name(list_email, r) print(f"{r} ({recipient_name})") elif (command == "create"): - - if (len(sys.argv) >= 4): - if (not re_email_arg.match(sys.argv[2])): - raise ValueError(f"Error while executing create.") - else: - raise ValueError(f"Error while executing create.") - - list_email = sys.argv[2] - list_name = sys.argv[3] - + list_email = args.get_list_email() + list_name = args.get_list_name() db.create_list(list_email, list_name) - print(f"New list ({list_email}) added.") elif (command == "destroy"): - - if (len(sys.argv) >= 3): - if (not re_email_arg.match(sys.argv[2])): - raise ValueError(f"Error while executing destroy.") - else: - raise ValueError(f"Error while executing destroy.") - - list_email = sys.argv[2] - + list_email = args.get_list_email() db.destroy_list(list_email) - print(f"Email list ({list_email}) destroyed.") elif (command == "add"): - - if (len(sys.argv) >= 5): - if (not re_email_arg.match(sys.argv[2]) or not re_email_arg.match(sys.argv[3])): - raise ValueError(f"Error while executing add.") - else: - raise ValueError(f"Error while executing add.") - - list_email = sys.argv[2] - recipient_email = sys.argv[3] - recipient_name = sys.argv[4] - + list_email = args.get_list_email() + recipient_email = args.get_recipient_email() + recipient_name = args.get_recipient_name() db.create_recipient(list_email, recipient_email, recipient_name) - print(f"New recipient ({recipient_email}) added to list ({list_email})") elif (command == "remove"): - - if (len(sys.argv) >= 4): - if (not re_email_arg.match(sys.argv[2]) or not re_email_arg.match(sys.argv[3])): - raise ValueError(f"Error while executing remove.") - else: - raise ValueError(f"Error while executing remove.") - - list_email = sys.argv[2] - recipient_email = sys.argv[3] - + list_email = args.get_list_email() + recipient_email = args.get_recipient_email() db.destroy_recipient(list_email, recipient_email) - print(f"Recipient ({recipient_email}) removed from list ({list_email})") else: - raise ValueError(f"Unknown Command: {command}") ## Disconnect from DB
M listfix/__init__.pylistfix/__init__.py

@@ -1,3 +1,4 @@

from .db import DB from .email import Email +from .args import Args
A listfix/args.py

@@ -0,0 +1,52 @@

+import re + +re_email_arg = re.compile("([^<>\"\s]+)@(\S+\.[^<>\"\s]+)") + +class Args: + + def __init__(self, args): + self.args = [] + self.command = None + + if (type(args) is list): + if (len(args) >= 2): + self.args = args + self.command = args[1] + else: + return False + else: + return False + + def get_command(self): + return self.command + + def get_list_email(self): + if (len(self.args) < 3): + return False + + if (not re_email_arg.match(self.args[2])): + return False + + return self.args[2] + + def get_list_name(self): + if (len(self.args) < 4): + return False + + return self.args[3] + + def get_recipient_email(self): + if (len(self.args) < 4): + return False + + if (not re_email_arg.match(self.args[3])): + return False + + return self.args[3] + + def get_recipient_name(self): + if (len(self.args) < 5): + return False + + return self.args[4] +