File: ValueObject.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 (41 lines) | stat: -rw-r--r-- 1,215 bytes parent folder | download | duplicates (2)
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
35
36
37
38
39
40
41
# SPDX-License-Identifier: BSD-3-Clause
#
# Copyright 2013 Raritan Inc. All rights reserved.

import raritan.rpc

class ValueObject(object):
    def __init__(self):
        pass

    @staticmethod
    def decode(json, agent):
        if not json:
            return None
        class_ = raritan.rpc.TypeInfo.decode(json['type'])
        obj = class_.decode(json['value'], agent)
        return obj

    @staticmethod
    def encode(obj):
      json = {}
      json['type'] = obj.idlType
      json['value'] = obj.encode()
      return json

    def listValues(self):
        return [getattr(self, e) for e in self.listElements()]

    def __str__(self):
        elements = self.listElements()
        l = max([len(e) for e in elements])
        pretty = "\n".join([
            raritan.rpc.Utils.indent("* %-*s = %s" % (l, e, raritan.rpc.Utils.rprint(getattr(self, e))), 4) for e in elements
        ])
        return "%s:\n%s" % (raritan.rpc.TypeInfo.typeBaseName(self.idlType), pretty)

    def __eq__(self, other):
        return (other != None and self.idlType == other.idlType and self.listValues() == other.listValues())

    def __hash__(self):
        return hash((self.idlType, tuple(self.listValues())))