Patchwork [4,of,5] templater: move verbosity-to-template matcher to constructor

login
register
mail settings
Submitter Matt Mackall
Date Aug. 25, 2015, 7:56 p.m.
Message ID <1b6f85c04ba5de786ea0.1440532600@ruin.waste.org>
Download mbox | patch
Permalink /patch/10277/
State Accepted
Headers show

Comments

Matt Mackall - Aug. 25, 2015, 7:56 p.m.
# HG changeset patch
# User Matt Mackall <mpm@selenic.com>
# Date 1440527900 18000
#      Tue Aug 25 13:38:20 2015 -0500
# Node ID 1b6f85c04ba5de786ea02badb8bd4bab81ac58bc
# Parent  976f1ba20c1c0cf289ffddde694442dbadee7896
templater: move verbosity-to-template matcher to constructor

We're going to reuse this and it's silly for it to be done per-cset anyway.
Augie Fackler - Aug. 27, 2015, 4:56 p.m.
On Tue, Aug 25, 2015 at 02:56:40PM -0500, Matt Mackall wrote:
> # HG changeset patch
> # User Matt Mackall <mpm@selenic.com>
> # Date 1440527900 18000
> #      Tue Aug 25 13:38:20 2015 -0500
> # Node ID 1b6f85c04ba5de786ea02badb8bd4bab81ac58bc
> # Parent  976f1ba20c1c0cf289ffddde694442dbadee7896
> templater: move verbosity-to-template matcher to constructor

I've gone ahead and queued the first 4 patches here, but I'm unclear
on what the plan is for patch 5. These 4 seem like good cleanups in
their own right.

>
> We're going to reuse this and it's silly for it to be done per-cset anyway.
>
> diff -r 976f1ba20c1c -r 1b6f85c04ba5 mercurial/cmdutil.py
> --- a/mercurial/cmdutil.py	Tue Aug 25 12:59:08 2015 -0500
> +++ b/mercurial/cmdutil.py	Tue Aug 25 13:38:20 2015 -0500
> @@ -1414,6 +1414,23 @@
>
>          self.cache = {}
>
> +        # find correct templates for current mode
> +        tmplmodes = [
> +            (True, None),
> +            (self.ui.verbose, 'verbose'),
> +            (self.ui.quiet, 'quiet'),
> +            (self.ui.debugflag, 'debug'),
> +        ]
> +
> +        self._parts = {'header': '', 'footer': '', 'changeset': 'changeset'}
> +        for mode, postfix in tmplmodes:
> +            for t in self._parts:
> +                cur = t
> +                if postfix:
> +                    cur += "_" + postfix
> +                if mode and cur in self.t:
> +                    self._parts[t] = cur
> +
>      def _show(self, ctx, copies, matchfn, props):
>          '''show a single changeset or file revision'''
>
> @@ -1440,27 +1457,10 @@
>          props['revcache'] = {'copies': copies}
>          props['cache'] = self.cache
>
> -        # find correct templates for current mode
> -        tmplmodes = [
> -            (True, None),
> -            (self.ui.verbose, 'verbose'),
> -            (self.ui.quiet, 'quiet'),
> -            (self.ui.debugflag, 'debug'),
> -        ]
> -
> -        types = {'header': '', 'footer': '', 'changeset': 'changeset'}
> -        for mode, postfix in tmplmodes:
> -            for t in types:
> -                cur = t
> -                if postfix:
> -                    cur += "_" + postfix
> -                if mode and cur in self.t:
> -                    types[t] = cur
> -
>          try:
>              # write header
> -            if types['header']:
> -                h = templater.stringify(self.t(types['header'], **props))
> +            if self._parts['header']:
> +                h = templater.stringify(self.t(self._parts['header'], **props))
>                  if self.buffered:
>                      self.header[ctx.rev()] = h
>                  else:
> @@ -1469,14 +1469,14 @@
>                          self.ui.write(h)
>
>              # write changeset metadata, then patch if requested
> -            key = types['changeset']
> +            key = self._parts['changeset']
>              self.ui.write(templater.stringify(self.t(key, **props)))
>              self.showpatch(ctx.node(), matchfn)
>
> -            if types['footer']:
> +            if self._parts['footer']:
>                  if not self.footer:
> -                    self.footer = templater.stringify(self.t(types['footer'],
> -                                                      **props))
> +                    self.footer = templater.stringify(
> +                        self.t(self._parts['footer'], **props))
>          except KeyError as inst:
>              msg = _("%s: no key named '%s'")
>              raise util.Abort(msg % (self.t.mapfile, inst.args[0]))
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> https://selenic.com/mailman/listinfo/mercurial-devel

Patch

diff -r 976f1ba20c1c -r 1b6f85c04ba5 mercurial/cmdutil.py
--- a/mercurial/cmdutil.py	Tue Aug 25 12:59:08 2015 -0500
+++ b/mercurial/cmdutil.py	Tue Aug 25 13:38:20 2015 -0500
@@ -1414,6 +1414,23 @@ 
 
         self.cache = {}
 
+        # find correct templates for current mode
+        tmplmodes = [
+            (True, None),
+            (self.ui.verbose, 'verbose'),
+            (self.ui.quiet, 'quiet'),
+            (self.ui.debugflag, 'debug'),
+        ]
+
+        self._parts = {'header': '', 'footer': '', 'changeset': 'changeset'}
+        for mode, postfix in tmplmodes:
+            for t in self._parts:
+                cur = t
+                if postfix:
+                    cur += "_" + postfix
+                if mode and cur in self.t:
+                    self._parts[t] = cur
+
     def _show(self, ctx, copies, matchfn, props):
         '''show a single changeset or file revision'''
 
@@ -1440,27 +1457,10 @@ 
         props['revcache'] = {'copies': copies}
         props['cache'] = self.cache
 
-        # find correct templates for current mode
-        tmplmodes = [
-            (True, None),
-            (self.ui.verbose, 'verbose'),
-            (self.ui.quiet, 'quiet'),
-            (self.ui.debugflag, 'debug'),
-        ]
-
-        types = {'header': '', 'footer': '', 'changeset': 'changeset'}
-        for mode, postfix in tmplmodes:
-            for t in types:
-                cur = t
-                if postfix:
-                    cur += "_" + postfix
-                if mode and cur in self.t:
-                    types[t] = cur
-
         try:
             # write header
-            if types['header']:
-                h = templater.stringify(self.t(types['header'], **props))
+            if self._parts['header']:
+                h = templater.stringify(self.t(self._parts['header'], **props))
                 if self.buffered:
                     self.header[ctx.rev()] = h
                 else:
@@ -1469,14 +1469,14 @@ 
                         self.ui.write(h)
 
             # write changeset metadata, then patch if requested
-            key = types['changeset']
+            key = self._parts['changeset']
             self.ui.write(templater.stringify(self.t(key, **props)))
             self.showpatch(ctx.node(), matchfn)
 
-            if types['footer']:
+            if self._parts['footer']:
                 if not self.footer:
-                    self.footer = templater.stringify(self.t(types['footer'],
-                                                      **props))
+                    self.footer = templater.stringify(
+                        self.t(self._parts['footer'], **props))
         except KeyError as inst:
             msg = _("%s: no key named '%s'")
             raise util.Abort(msg % (self.t.mapfile, inst.args[0]))