Patchwork D2959: stringutil: add isauthorwellformed function

login
register
mail settings
Submitter phabricator
Date March 27, 2018, 4:04 p.m.
Message ID <differential-rev-PHID-DREV-ugoaadxcmv3ypu7wqrkj-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/29909/
State Superseded
Headers show

Comments

phabricator - March 27, 2018, 4:04 p.m.
sheehan created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  The regular expression for this function formerly lived at
  https://hg.mozilla.org/hgcustom/version-control-tools/file/tip/hghooks/mozhghooks/author_format.py#l13

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D2959

AFFECTED FILES
  mercurial/utils/stringutil.py

CHANGE DETAILS




To: sheehan, #hg-reviewers
Cc: mercurial-devel
phabricator - March 28, 2018, 5:04 a.m.
av6 added inline comments.

INLINE COMMENTS

> stringutil.py:310
> +    '''
> +    return bool(_correctauthorformat.match(author))

Nit: `is not None` is more pythonic.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D2959

To: sheehan, #hg-reviewers
Cc: av6, mercurial-devel
phabricator - March 28, 2018, 1:17 p.m.
yuja requested changes to this revision.
yuja added a comment.
This revision now requires changes to proceed.


  Looks mostly good, but can you fix these nits?

INLINE COMMENTS

> stringutil.py:290
> +
> +_correctauthorformat = remod.compile('^[^<]+\s\<[^<>]+@[^<>]+\>$')
> +def isauthorwellformed(author):

Nit: add br'' for Python 3 compatibility.

> stringutil.py:295
> +
> +    >>> isauthorwellformed('Good Author <good@author.com>')
> +    True

b'' for Python 3 compatibility.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D2959

To: sheehan, #hg-reviewers, yuja
Cc: yuja, av6, mercurial-devel

Patch

diff --git a/mercurial/utils/stringutil.py b/mercurial/utils/stringutil.py
--- a/mercurial/utils/stringutil.py
+++ b/mercurial/utils/stringutil.py
@@ -286,3 +286,25 @@ 
     If s is not a valid boolean, returns None.
     """
     return _booleans.get(s.lower(), None)
+
+_correctauthorformat = remod.compile('^[^<]+\s\<[^<>]+@[^<>]+\>$')
+def isauthorwellformed(author):
+    '''Return True if the author field is well formed
+    (ie "Contributor Name <contrib@email.dom>")
+
+    >>> isauthorwellformed('Good Author <good@author.com>')
+    True
+    >>> isauthorwellformed('Author <good@author.com>')
+    True
+    >>> isauthorwellformed('Bad Author')
+    False
+    >>> isauthorwellformed('Bad Author <author@author.com')
+    False
+    >>> isauthorwellformed('Bad Author author@author.com')
+    False
+    >>> isauthorwellformed('<author@author.com>')
+    False
+    >>> isauthorwellformed('Bad Author <author>')
+    False
+    '''
+    return bool(_correctauthorformat.match(author))