Skip to content
Snippets Groups Projects
Commit c83c7c52 authored by Guillaume Chanel's avatar Guillaume Chanel
Browse files

Add possibility to create groups of size > 1

Several options where added including:
--group-size to specify the size of the groups (default 1)
--big-group to attribute remaining emails to the last group
--prefix to add a prefix to all group names
parent 248a0f6d
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,8 @@ import argparse
import yaml
import re
import logging
import itertools
import collections
email_regex = re.compile(r"^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$")
......@@ -17,10 +19,27 @@ def is_valid_email(email: str) -> bool:
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Repos file generator - create the PWM repos file from a list of emails')
parser.add_argument('in_file', type=argparse.FileType('r'), default='-', help='name of the file containing a list of email adresses (- for stdin)')
parser.add_argument('repos_file', type=argparse.FileType('w'), default='repos_file.yml', help='name of the repos file')
parser.add_argument('repos_file', type=argparse.FileType('w'), default='-', help='name of the repos file')
parser.add_argument('--group-size', '-s', type=int, default=1, help='number of persons per group, if the number of emails is not divisible by the group-size, one group will have a smaller number of participant (unless --big-group is specified).')
parser.add_argument('--prefix', '-p', type=str, default=None, help='add a prefix to group names, automatically create group names from first email')
parser.add_argument('--big-group', '-b', action='store_true', help='if the number of emails is not divisible by the group-size, make a last group with more emails')
# In the case we want to implement input file parsers:
# parser.add_argument('-f', '--field', help='name of the field containing emails if the input as several fields (e.g. CSV)')
args = parser.parse_args()
email_list = [line.strip() for line in args.in_file if is_valid_email(line.strip())] # I do not get why I need to strip for validating email -> the regexp should take care of line return with $
group_list = [{'emails': [email]} for email in email_list]
# Get the email list and filter invalid emails
email_list = [line.strip() for line in args.in_file if is_valid_email(line.strip())] # TODO: I do not get why I need to strip for validating email -> the regexp should take care of line return with $
# Group emails according to group sizes
group_list = [list(itertools.islice(email_list, i, i+args.group_size)) for i in range(0, len(email_list), args.group_size)]
if args.big_group and len(group_list[-1]) < args.group_size and len(group_list) > 1:
group_list[-2].extend(group_list[-1])
group_list.pop()
# Format to YAML
if args.prefix:
group_list = [{'name': args.prefix + g[0].split('@',1)[0] ,'emails': g} for g in group_list]
else:
group_list = [{'emails': g} for g in group_list]
yaml.safe_dump(group_list, args.repos_file)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment