summaryrefslogtreecommitdiff
path: root/python/samba
diff options
context:
space:
mode:
authorRob van der Linde <rob@catalyst.net.nz>2023-05-16 14:35:41 +1200
committerAndrew Bartlett <abartlet@samba.org>2023-06-25 23:29:32 +0000
commit2842ed824ae41aa96673bcbebd309b90813d1ef2 (patch)
treea9126fb21a8dd35d89cebf4d23e23feebc241da5 /python/samba
parent15440c6d6bfd23cd4756511ec3abb891f3d7f8a3 (diff)
downloadsamba-2842ed824ae41aa96673bcbebd309b90813d1ef2.tar.gz
samba-2842ed824ae41aa96673bcbebd309b90813d1ef2.tar.bz2
samba-2842ed824ae41aa96673bcbebd309b90813d1ef2.zip
netcmd: move method print_json to command base class
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>
Diffstat (limited to 'python/samba')
-rw-r--r--python/samba/netcmd/__init__.py11
-rw-r--r--python/samba/netcmd/domain/auth/base.py12
-rw-r--r--python/samba/netcmd/domain/claim/base.py12
3 files changed, 11 insertions, 24 deletions
diff --git a/python/samba/netcmd/__init__.py b/python/samba/netcmd/__init__.py
index 1caafa54a62..bb3736e2117 100644
--- a/python/samba/netcmd/__init__.py
+++ b/python/samba/netcmd/__init__.py
@@ -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):
diff --git a/python/samba/netcmd/domain/auth/base.py b/python/samba/netcmd/domain/auth/base.py
index 14301e5f83e..1a3633d9f3b 100644
--- a/python/samba/netcmd/domain/auth/base.py
+++ b/python/samba/netcmd/domain/auth/base.py
@@ -20,10 +20,7 @@
# 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.
diff --git a/python/samba/netcmd/domain/claim/base.py b/python/samba/netcmd/domain/claim/base.py
index e398c945b66..2e0dd12e645 100644
--- a/python/samba/netcmd/domain/claim/base.py
+++ b/python/samba/netcmd/domain/claim/base.py
@@ -20,11 +20,8 @@
# 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.