File: Utils.py

package info (click to toggle)
raritan-json-rpc-sdk 4.0.20%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 57,236 kB
  • sloc: cs: 223,121; perl: 117,786; python: 26,872; javascript: 6,544; makefile: 27
file content (34 lines) | stat: -rw-r--r-- 1,104 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# SPDX-License-Identifier: BSD-3-Clause
#
# Copyright 2013 Raritan Inc. All rights reserved.

# Avoid name clash with raritan.rpc.sys
from __future__ import absolute_import
import sys

class Utils(object):

    @staticmethod
    def indent(string, indent):
        return "\n".join([(" " * indent) + l for l in string.splitlines()])

    @staticmethod
    def rprint(object):
        if sys.version_info.major < 3:
            if isinstance(object, basestring):
                return '"' + str(object) + '"'
        else:
            if isinstance(object, str):
                return '"' + str(object) + '"'

        if isinstance(object, list):
            if len(object) == 0:
                return '[]'
            else:
                return '[\n' + ',\n'.join(Utils.indent(Utils.rprint(x), 4) for x in object) + '\n]'
        elif isinstance(object, dict):
            if len(object) == 0:
                return '{}'
            else:
                return '{\n' + "\n".join(Utils.indent("%s = %s" % (x, Utils.rprint(object[x])), 4) for x in sorted(object)) + '\n}'
        return str(object)