Patchwork [2,of,3] registrar: add a method to merge registrar instances

login
register
mail settings
Submitter Matt Harbison
Date Dec. 31, 2018, 3:55 a.m.
Message ID <94d4ffb62049c77f4631.1546228508@Envy>
Download mbox | patch
Permalink /patch/37401/
State Accepted
Headers show

Comments

Matt Harbison - Dec. 31, 2018, 3:55 a.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1546224746 18000
#      Sun Dec 30 21:52:26 2018 -0500
# Node ID 94d4ffb62049c77f4631ae76732765b17033d2dd
# Parent  a567c612e90996a1a6410c0400634447405df996
registrar: add a method to merge registrar instances

This provides sanity checking beyond simply merging the underlying dictionaries.
Yuya Nishihara - Jan. 2, 2019, 12:48 a.m.
On Sun, 30 Dec 2018 22:55:08 -0500, Matt Harbison wrote:
> # HG changeset patch
> # User Matt Harbison <matt_harbison@yahoo.com>
> # Date 1546224746 18000
> #      Sun Dec 30 21:52:26 2018 -0500
> # Node ID 94d4ffb62049c77f4631ae76732765b17033d2dd
> # Parent  a567c612e90996a1a6410c0400634447405df996
> registrar: add a method to merge registrar instances

> +    def _merge(self, registrarbase):
> +        """Merge the entries of the given registrar object into this one.
> +
> +        The other registrar object must not contain any entries already in the
> +        current one, or a ProgrammmingError is raised.  Additionally, the types
> +        of the two registrars must match.
> +        """
> +        if type(self) != type(registrarbase):

Nit: isinstance(registrarbase, type(self)) is preferred unless we have to
require exact match.

> +        dups = set(registrarbase._table.keys()).intersection(self._table.keys())

Nit: keys() can be omitted.

Patch

diff --git a/mercurial/registrar.py b/mercurial/registrar.py
--- a/mercurial/registrar.py
+++ b/mercurial/registrar.py
@@ -73,6 +73,25 @@  class _funcregistrarbase(object):
 
         return func
 
+    def _merge(self, registrarbase):
+        """Merge the entries of the given registrar object into this one.
+
+        The other registrar object must not contain any entries already in the
+        current one, or a ProgrammmingError is raised.  Additionally, the types
+        of the two registrars must match.
+        """
+        if type(self) != type(registrarbase):
+            msg = "cannot merge different types of registrar"
+            raise error.ProgrammingError(msg)
+
+        dups = set(registrarbase._table.keys()).intersection(self._table.keys())
+
+        if dups:
+            msg = 'duplicate registration for names: "%s"' % '", "'.join(dups)
+            raise error.ProgrammingError(msg)
+
+        self._table.update(registrarbase._table)
+
     def _parsefuncdecl(self, decl):
         """Parse function declaration and return the name of function in it
         """