1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-24 21:34:56 +03:00

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>
This commit is contained in:
Rob van der Linde 2023-05-16 14:35:41 +12:00 committed by Andrew Bartlett
parent 15440c6d6b
commit 2842ed824a
3 changed files with 11 additions and 24 deletions

View 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):

View File

@ -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.

View File

@ -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.