Patchwork [2,of,8] ui: add the possiblity to get a date config field

login
register
mail settings
Submitter Boris Feld
Date May 22, 2017, 6:46 p.m.
Message ID <1738d2fb32fe1ccf2f54.1495478782@FB>
Download mbox | patch
Permalink /patch/20828/
State Accepted
Headers show

Comments

Boris Feld - May 22, 2017, 6:46 p.m.
# HG changeset patch
# User Boris Feld <boris.feld@octobus.net>
# Date 1495188461 -7200
#      Fri May 19 12:07:41 2017 +0200
# Node ID 1738d2fb32fe1ccf2f54ef2382df0a929c50be77
# Parent  ba781657de9f2e5b735e8a207cdf0b858047190c
# EXP-Topic develforcedate
ui: add the possiblity to get a date config field

Add the method configdate to read a date from configuration. It uses the
util.rawparsedate refactored earlier to support all standard date formats.
Yuya Nishihara - May 23, 2017, 12:43 p.m.
On Mon, 22 May 2017 20:46:22 +0200, Boris Feld wrote:
> # HG changeset patch
> # User Boris Feld <boris.feld@octobus.net>
> # Date 1495188461 -7200
> #      Fri May 19 12:07:41 2017 +0200
> # Node ID 1738d2fb32fe1ccf2f54ef2382df0a929c50be77
> # Parent  ba781657de9f2e5b735e8a207cdf0b858047190c
> # EXP-Topic develforcedate
> ui: add the possiblity to get a date config field
> 
> Add the method configdate to read a date from configuration. It uses the
> util.rawparsedate refactored earlier to support all standard date formats.
> 
> diff -r ba781657de9f2e5b735e8a207cdf0b858047190c -r 1738d2fb32fe1ccf2f54ef2382df0a929c50be77 mercurial/ui.py
> --- a/mercurial/ui.py	Fri May 19 12:07:23 2017 +0200
> +++ b/mercurial/ui.py	Fri May 19 12:07:41 2017 +0200
> @@ -598,6 +598,19 @@
>          return self.configwith(config.parselist, section, name, default or [],
>                                 'list', untrusted)
>  
> +    def configdate(self, section, name, default=None, untrusted=False):
> +        """parse a configuration element as a tuple of ints
> +
> +        >>> u = ui(); s = 'foo'
> +        >>> u.setconfig(s, 'date', '0 0')
> +        >>> u.configdate(s, 'date')
> +        (0, 0)
> +        """
> +        if self.config(section, name, default, untrusted):
> +            return self.configwith(util.parsedate, section, name, default,
> +                                   'date', untrusted)

Perhaps this should be util.rawparsedate.
Boris Feld - May 23, 2017, 1:23 p.m.
On Tue, 2017-05-23 at 21:43 +0900, Yuya Nishihara wrote:
> On Mon, 22 May 2017 20:46:22 +0200, Boris Feld wrote:
> > # HG changeset patch
> > # User Boris Feld <boris.feld@octobus.net>
> > # Date 1495188461 -7200
> > #      Fri May 19 12:07:41 2017 +0200
> > # Node ID 1738d2fb32fe1ccf2f54ef2382df0a929c50be77
> > # Parent  ba781657de9f2e5b735e8a207cdf0b858047190c
> > # EXP-Topic develforcedate
> > ui: add the possiblity to get a date config field
> > 
> > Add the method configdate to read a date from configuration. It
> > uses the
> > util.rawparsedate refactored earlier to support all standard date
> > formats.
> > 
> > diff -r ba781657de9f2e5b735e8a207cdf0b858047190c -r
> > 1738d2fb32fe1ccf2f54ef2382df0a929c50be77 mercurial/ui.py
> > --- a/mercurial/ui.py	Fri May 19 12:07:23 2017 +0200
> > +++ b/mercurial/ui.py	Fri May 19 12:07:41 2017 +0200
> > @@ -598,6 +598,19 @@
> >          return self.configwith(config.parselist, section, name,
> > default or [],
> >                                 'list', untrusted)
> >  
> > +    def configdate(self, section, name, default=None,
> > untrusted=False):
> > +        """parse a configuration element as a tuple of ints
> > +
> > +        >>> u = ui(); s = 'foo'
> > +        >>> u.setconfig(s, 'date', '0 0')
> > +        >>> u.configdate(s, 'date')
> > +        (0, 0)
> > +        """
> > +        if self.config(section, name, default, untrusted):
> > +            return self.configwith(util.parsedate, section, name,
> > default,
> > +                                   'date', untrusted)
> 
> Perhaps this should be util.rawparsedate.

Thank you for the catch, it worked in the happy path but not in case of
invalid date value. I've done the fix and add a test for an invalid
date in the v2 version of the patch serie.
Yuya Nishihara - May 23, 2017, 2:12 p.m.
On Tue, 23 May 2017 15:23:12 +0200, Boris Feld wrote:
> On Tue, 2017-05-23 at 21:43 +0900, Yuya Nishihara wrote:
> > On Mon, 22 May 2017 20:46:22 +0200, Boris Feld wrote:
> > > # HG changeset patch
> > > # User Boris Feld <boris.feld@octobus.net>
> > > # Date 1495188461 -7200
> > > #      Fri May 19 12:07:41 2017 +0200
> > > # Node ID 1738d2fb32fe1ccf2f54ef2382df0a929c50be77
> > > # Parent  ba781657de9f2e5b735e8a207cdf0b858047190c
> > > # EXP-Topic develforcedate
> > > ui: add the possiblity to get a date config field
> > > 
> > > Add the method configdate to read a date from configuration. It
> > > uses the
> > > util.rawparsedate refactored earlier to support all standard date
> > > formats.
> > > 
> > > diff -r ba781657de9f2e5b735e8a207cdf0b858047190c -r
> > > 1738d2fb32fe1ccf2f54ef2382df0a929c50be77 mercurial/ui.py
> > > --- a/mercurial/ui.py	Fri May 19 12:07:23 2017 +0200
> > > +++ b/mercurial/ui.py	Fri May 19 12:07:41 2017 +0200
> > > @@ -598,6 +598,19 @@
> > >          return self.configwith(config.parselist, section, name,
> > > default or [],
> > >                                 'list', untrusted)
> > >  
> > > +    def configdate(self, section, name, default=None,
> > > untrusted=False):
> > > +        """parse a configuration element as a tuple of ints
> > > +
> > > +        >>> u = ui(); s = 'foo'
> > > +        >>> u.setconfig(s, 'date', '0 0')
> > > +        >>> u.configdate(s, 'date')
> > > +        (0, 0)
> > > +        """
> > > +        if self.config(section, name, default, untrusted):
> > > +            return self.configwith(util.parsedate, section, name,
> > > default,
> > > +                                   'date', untrusted)
> > 
> > Perhaps this should be util.rawparsedate.
> 
> Thank you for the catch, it worked in the happy path but not in case of
> invalid date value. I've done the fix and add a test for an invalid
> date in the v2 version of the patch serie.

BTW, if we introduce new exception type, maybe we wouldn't have to split
rawparsedate() from parsedate().

Patch

diff -r ba781657de9f2e5b735e8a207cdf0b858047190c -r 1738d2fb32fe1ccf2f54ef2382df0a929c50be77 mercurial/ui.py
--- a/mercurial/ui.py	Fri May 19 12:07:23 2017 +0200
+++ b/mercurial/ui.py	Fri May 19 12:07:41 2017 +0200
@@ -598,6 +598,19 @@ 
         return self.configwith(config.parselist, section, name, default or [],
                                'list', untrusted)
 
+    def configdate(self, section, name, default=None, untrusted=False):
+        """parse a configuration element as a tuple of ints
+
+        >>> u = ui(); s = 'foo'
+        >>> u.setconfig(s, 'date', '0 0')
+        >>> u.configdate(s, 'date')
+        (0, 0)
+        """
+        if self.config(section, name, default, untrusted):
+            return self.configwith(util.parsedate, section, name, default,
+                                   'date', untrusted)
+        return default
+
     def hasconfig(self, section, name, untrusted=False):
         return self._data(untrusted).hasitem(section, name)