Patchwork [7,of,8] logtoprocess: extract logger class from ui wrapper

login
register
mail settings
Submitter Yuya Nishihara
Date Nov. 15, 2018, 1:02 p.m.
Message ID <cae8cb38be59c8ec66f6.1542286968@mimosa>
Download mbox | patch
Permalink /patch/36610/
State Accepted
Headers show

Comments

Yuya Nishihara - Nov. 15, 2018, 1:02 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1541925918 -32400
#      Sun Nov 11 17:45:18 2018 +0900
# Node ID cae8cb38be59c8ec66f6be3fa9c2db432409497d
# Parent  f644f2f01fcb5444809756e121c0fafe46189f10
logtoprocess: extract logger class from ui wrapper

Just like the blackbox extension. The ui wrapper will be removed soon.

Patch

diff --git a/hgext/logtoprocess.py b/hgext/logtoprocess.py
--- a/hgext/logtoprocess.py
+++ b/hgext/logtoprocess.py
@@ -38,6 +38,7 @@  import os
 
 from mercurial import (
     pycompat,
+    util,
 )
 from mercurial.utils import (
     procutil,
@@ -49,27 +50,50 @@  from mercurial.utils import (
 # leave the attribute unspecified.
 testedwith = 'ships-with-hg-core'
 
+class processlogger(object):
+    """Map log events to external commands
+
+    Arguments are passed on as environment variables.
+    """
+
+    def __init__(self, ui):
+        self._scripts = dict(ui.configitems(b'logtoprocess'))
+
+    def tracked(self, event):
+        return bool(self._scripts.get(event))
+
+    def log(self, ui, event, msg, opts):
+        script = self._scripts.get(event)
+        if not script:
+            return
+        env = {
+            b'EVENT': event,
+            b'HGPID': os.getpid(),
+            b'MSG1': msg[0] % msg[1:],
+        }
+        # keyword arguments get prefixed with OPT_ and uppercased
+        env.update((b'OPT_%s' % key.upper(), value)
+                   for key, value in pycompat.byteskwargs(opts).items())
+        fullenv = procutil.shellenviron(env)
+        procutil.runbgcommand(script, fullenv, shell=True)
+
 def uisetup(ui):
 
     class logtoprocessui(ui.__class__):
-        def log(self, event, *msg, **opts):
-            """Map log events to external commands
-
-            Arguments are passed on as environment variables.
+        def __init__(self, src=None):
+            super(logtoprocessui, self).__init__(src)
+            if src and r'_ltplogger' in src.__dict__:
+                self._ltplogger = src._ltplogger
 
-            """
-            script = self.config('logtoprocess', event)
-            if script:
-                env = {
-                    b'EVENT': event,
-                    b'HGPID': os.getpid(),
-                    b'MSG1': msg[0] % msg[1:],
-                }
-                # keyword arguments get prefixed with OPT_ and uppercased
-                env.update((b'OPT_%s' % key.upper(), value)
-                           for key, value in pycompat.byteskwargs(opts).items())
-                fullenv = procutil.shellenviron(env)
-                procutil.runbgcommand(script, fullenv, shell=True)
+        # trick to initialize logger after configuration is loaded, which
+        # can be replaced later with processlogger(ui) in uisetup(), where
+        # both user and repo configurations should be available.
+        @util.propertycache
+        def _ltplogger(self):
+            return processlogger(self)
+
+        def log(self, event, *msg, **opts):
+            self._ltplogger.log(self, event, msg, opts)
             return super(logtoprocessui, self).log(event, *msg, **opts)
 
     # Replace the class for this instance and all clones created from it: