From e1e58926b8315450f9d95fa59047244b9c0690e5 Mon Sep 17 00:00:00 2001 From: jochan Date: Fri, 10 Nov 2017 09:32:43 -0800 Subject: [PATCH] * Added firstname.lastname case * Allowed hyphens in names * Converted all fullnames to usernames --- authors.py | 35 +++++++++++++++++++++++------------ pattern-author.txt | 7 ++++--- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/authors.py b/authors.py index c31ebab..01a69d8 100644 --- a/authors.py +++ b/authors.py @@ -5,15 +5,16 @@ import os failed_pattern = re.compile("^\*\*\*.*$") null_author = re.compile("^<>$") visier_prepended = re.compile("^VISIER\\\.*$") -full_name_no_email = re.compile("^([A-Z]\w*\s?)+$") -full_name_null_email = re.compile("^([A-Z]\w*\s?)+<>$") -full_name_with_email = re.compile("^([A-Z]\w*\s?)+<.+>$") -username_no_email = re.compile("^\w*$") -username_null_email = re.compile("^\w*\s?<>$") -username_with_email = re.compile("^\w*\s?<.+>$") -username_sqr_email = re.compile("^\w*\s?\[.*\]$") -username_rnd_name = re.compile("^\w*\s?\(.*\)$") -username_address = re.compile("^\w*@.*$") +full_name_no_email = re.compile("^([A-Z][\w\-]*\s?)+$") +full_name_null_email = re.compile("^([A-Z][\w\-]*\s?)+<>$") +full_name_with_email = re.compile("^([A-Z][\w\-]*\s?)+<.+>$") +username_no_email = re.compile("^[\w\-]*$") +username_null_email = re.compile("^[\w\-]*\s?<>$") +username_with_email = re.compile("^[\w\-]*\s?<.+>$") +username_sqr_email = re.compile("^[\w\-]*\s?\[.*\]$") +username_rnd_name = re.compile("^[\w\-]*\s?\(.*\)$") +username_address = re.compile("^[\w\-]*@.*$") +first_last_name = re.compile("^[\w\-]*\.[\w\-]*$") any_any = re.compile("^.+<.*>$") null_any = re.compile("^<.*>$") any_email = re.compile("^.+\s\S+@\S+$") @@ -32,6 +33,12 @@ def email_from_username(author): def username_from_email(author): return next((user["username"] for user in users if user["email"] == author), "") +def username_from_fullname(author): + return next((user["username"] for user in users if user["name"] == author), "") + +def username_from_firstname_lastname(author): + return next((user["username"] for user in users if user["name"].casefold() == author.replace(".", " ").casefold()), "") + def replace_author(author): if failed_pattern.match(author): return "nulluser <>" @@ -40,12 +47,13 @@ def replace_author(author): if visier_prepended.match(author): return replace_author(author[7:]) if full_name_with_email.match(author): - return author + fullname = author.split("<")[0].strip() + return username_from_fullname(fullname) + " <{}>".format(email_from_fullname(fullname)) if full_name_null_email.match(author): fullname = author.strip()[:-2].strip() - return fullname + " <{}>".format(email_from_fullname(fullname)) + return username_from_fullname(fullname) + " <{}>".format(email_from_fullname(fullname)) if full_name_no_email.match(author): - return author.strip() + " <{}>".format(email_from_fullname(author.strip())) + return username_from_fullname(author.strip()) + " <{}>".format(email_from_fullname(author.strip())) if username_with_email.match(author): return author if username_null_email.match(author): @@ -61,6 +69,9 @@ def replace_author(author): if username_address.match(author): username = author.split("@")[0] return username + " <{}>".format(email_from_username(username)) + if first_last_name.match(author): + username = username_from_firstname_lastname(author) + return username + " <{}>".format(email_from_username(username)) if any_any.match(author): return author if null_any.match(author): diff --git a/pattern-author.txt b/pattern-author.txt index 5b01a9e..ba7045f 100644 --- a/pattern-author.txt +++ b/pattern-author.txt @@ -2,9 +2,9 @@ format matches replace ------ ------- ------- *** failed to import... ^\*\*\*.*$ "nulluser <>" <> ^<>$ prepend with "nulluser " -Full Name ^([A-Z]\w*\s?)+$ append with -Full Name <> ^([A-Z]\w*\s?)+<>$ <> with -Full Name ^([A-Z]\w*\s?)+<.*>$ do nothing +Full Name ^([A-Z]\w*\s?)+$ fullname with username and append with +Full Name <> ^([A-Z]\w*\s?)+<>$ fullname with username and <> with +Full Name ^([A-Z]\w*\s?)+<.*>$ fullname with username VISIER\username ^VISIER\\.*$ drop 7 and recurse username ^\w*$ append with username <> ^\w*\s?<>$ <> with @@ -12,6 +12,7 @@ username ^\w*\s?<.*>$ do nothing username [name@email.com] ^\w*\s?\[.*\]$ [ with < and ] with > username (Full Name) ^\w*\s?\(.*\)$ \s?\(.*\) with \s username@address.com ^\w*@.*$ @.* with \s +firstname.lastname ^\w*\.\w*$ username from fullname from firstname.lastname anything ^.+<.*>$ do nothing ^<.*>$ prepend with user anything name@email.com ^.+\s\S+@\S+$ wrap email with <>