File: debug.py

package info (click to toggle)
python-softlayer 6.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,100 kB
  • sloc: python: 53,771; makefile: 289; sh: 57
file content (62 lines) | stat: -rw-r--r-- 1,762 bytes parent folder | download
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
    SoftLayer.transports.debug
    ~~~~~~~~~~~~~~~~~~~~
    Debugging transport. Will print out verbose logging information.

    :license: MIT, see LICENSE for more details.
"""

import json
import logging
import time

from SoftLayer import exceptions


class DebugTransport(object):
    """Transport that records API call timings."""

    def __init__(self, transport):
        self.transport = transport

        #: List All API calls made during a session
        self.requests = []
        self.logger = logging.getLogger(__name__)

    def __call__(self, call):
        call.start_time = time.time()

        self.pre_transport_log(call)
        try:
            call.result = self.transport(call)
        except (exceptions.SoftLayerAPIError, exceptions.TransportError) as ex:
            call.exception = ex

        self.post_transport_log(call)

        call.end_time = time.time()
        self.requests.append(call)

        if call.exception is not None:
            self.logger.debug(self.print_reproduceable(call))
            raise call.exception

        return call.result

    def pre_transport_log(self, call):
        """Prints a warning before calling the API """
        output = "Calling: {})".format(call)
        self.logger.warning(output)

    def post_transport_log(self, call):
        """Prints the result "Returned Data: \n%s" % (call.result)of an API call"""
        output = "Returned Data: \n{}".format(json.dumps(call.result))
        self.logger.debug(output)

    def get_last_calls(self):
        """Returns all API calls for a session"""
        return self.requests

    def print_reproduceable(self, call):
        """Prints a reproduceable debugging output"""
        return self.transport.print_reproduceable(call)