]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
netcmd: move method print_json to command base class
authorRob van der Linde <rob@catalyst.net.nz>
Tue, 16 May 2023 02:35:41 +0000 (14:35 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Sun, 25 Jun 2023 23:29:32 +0000 (23:29 +0000)
This is used in quite a few commands, move to base class.

This ensures the correct encoder class and settings are always used,
and they are only defined in one place.

Signed-off-by: Rob van der Linde <rob@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Joseph Sutton <josephsutton@catalyst.net.nz>
python/samba/netcmd/__init__.py
python/samba/netcmd/domain/auth/base.py
python/samba/netcmd/domain/claim/base.py

index 1caafa54a6238cf4cc45a5142e71ed24264d9094..bb3736e21178709b350957820087e657560789ae 100644 (file)
@@ -16,6 +16,7 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
+import json
 import optparse
 import sys
 import textwrap
@@ -29,6 +30,7 @@ from samba.getopt import SambaOption, OptionError
 from samba.logger import get_samba_logger
 from samba.samdb import SamDB
 
+from .encoders import JSONEncoder
 from .validators import ValidationError
 
 
@@ -151,6 +153,15 @@ class Command(object):
         return SamDB(ldap_url, credentials=creds,
                      session_info=system_session(lp), lp=lp)
 
+    def print_json(self, data):
+        """Print json on the screen using consistent formatting and sorting.
+
+        A custom JSONEncoder class is used to help with serializing unknown
+        objects such as Dn for example.
+        """
+        json.dump(data, self.outf, cls=JSONEncoder, indent=2, sort_keys=True)
+        self.outf.write("\n")
+
     def show_command_error(self, e):
         """display a command error"""
         if isinstance(e, CommandError):
index 14301e5f83e9a70c4a6c06dc414f4b7d927cdd5c..1a3633d9f3b8dc0ecc18273fe008140536717e04 100644 (file)
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
-import json
-
 from samba.netcmd import Command, CommandError
-from samba.netcmd.encoders import JSONEncoder
 from samba.netcmd.domain.models import AuthenticationPolicy
 
 
@@ -32,15 +29,6 @@ class SiloCommand(Command):
         super().__init__(*args, **kwargs)
         self.ldb = None
 
-    def print_json(self, data):
-        """Print json on the screen using consistent formatting and sorting.
-
-        A custom JSONEncoder class is used to help with serializing unknown
-        objects such as Dn for example.
-        """
-        json.dump(data, self.outf, cls=JSONEncoder, indent=2, sort_keys=True)
-        self.outf.write("\n")
-
     def get_policy(self, name):
         """Helper function to return auth policy or raise CommandError.
 
index e398c945b661900533fcec33fed9c04156df5140..2e0dd12e645f2ced6ae9d3c90c84e8b0e4d3b834 100644 (file)
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
-import json
-
 from ldb import SCOPE_ONELEVEL
 from samba.netcmd import Command
-from samba.netcmd.encoders import JSONEncoder
 
 
 class ClaimCommand(Command):
@@ -34,15 +31,6 @@ class ClaimCommand(Command):
         super().__init__(*args, **kwargs)
         self.ldb = None
 
-    def print_json(self, data):
-        """Print json on the screen using consistent formatting and sorting.
-
-        A custom JSONEncoder class is used to help with serializing unknown
-        objects such as Dn for example.
-        """
-        json.dump(data, self.outf, cls=JSONEncoder, indent=2, sort_keys=True)
-        self.outf.write("\n")
-
     def get_attribute_from_schema(self, name):
         """Find DN by name in attribute schema.