-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path__init__.py
More file actions
187 lines (133 loc) · 5.18 KB
/
__init__.py
File metadata and controls
187 lines (133 loc) · 5.18 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# -*- coding: utf-8 -*-
# Copyright (C) 2014-2015 MUJIN Inc.
from .version import __version__ # noqa: F401
import six
from typing import Optional
try:
import ujson as json # noqa: F401
except ImportError:
import json # noqa: F401
try:
from urllib import parse as urlparse # noqa: F401
except ImportError:
import urlparse # noqa: F401
# use GetMonotonicTime if possible
try:
from mujincommon import GetMonotonicTime
except ImportError:
import time
if hasattr(time, 'monotonic'):
def GetMonotonicTime():
return time.monotonic()
else:
def GetMonotonicTime():
return time.time()
import logging
log = logging.getLogger(__name__)
try:
from mujincommon import i18n
ugettext, ungettext = i18n.GetDomain('mujinwebstackclientpy').GetTranslationFunctions()
except ImportError:
def ugettext(message):
return message
def ungettext(singular, plural, n):
return singular if n == 1 else plural
_ = ugettext
@six.python_2_unicode_compatible
class ClientExceptionBase(Exception):
"""client base exception"""
_message = None # the error message, should be unicode
def __init__(self, message: Optional[str]='') -> None:
if message is not None and not isinstance(message, six.text_type):
message = message.decode('utf-8', 'ignore')
self._message = message
def __str__(self) -> str:
return u'%s: %s' % (self.__class__.__name__, self._message)
def __repr__(self) -> str:
return '<%s(message=%r)>' % (self.__class__.__name__, self._message)
@six.python_2_unicode_compatible
class APIServerError(ClientExceptionBase):
_message = None # the error. should be unicode
_errorcode = None # the error code coming from the server
_detailInfoType = None # str, the detailed error type given errorcode
_detailInfo = None # dcit, the detailed info
_inputcommand = None # the command sent to the server
def __init__(self, message: str, errorcode: Optional[int]=None, inputcommand: Optional[str]=None, detailInfoType: Optional[str]=None, detailInfo: Optional[dict]=None) -> None:
if message is not None and not isinstance(message, six.text_type):
message = message.decode('utf-8', 'ignore')
self._message = message
self._errorcode = errorcode
self._inputcommand = inputcommand
self._detailInfoType = detailInfoType
self._detailInfo = detailInfo
def __str__(self) -> str:
if self._message is not None:
return _('API Server Error: %s') % self._message
return _('API Server Error: Unknown')
def __repr__(self):
return '<%s(message=%r, errorcode=%r, inputcommand=%r, detailInfoType=%r, detailInfo=%r)>' % (self.__class__.__name__, self._message, self._errorcode, self._inputcommand, self._detailInfoType, self._detailInfo)
@property
def message(self) -> str:
"""The error message from server."""
return self._message
@property
def errorcode(self):
"""The error code from server. Could be None."""
return self._errorcode
@property
def stacktrace(self) -> str:
"""The stack trace for the error"""
return ''
@property
def inputcommand(self) -> Optional[str]:
"""The command that was sent to the server. Could be None."""
return self._inputcommand
@property
def detailInfoType(self) -> Optional[str]:
"""string for the detai info type"""
return self._detailInfoType
@property
def detailInfo(self):
"""string for the detai info type"""
return self._detailInfo
class TimeoutError(ClientExceptionBase):
pass
class AuthenticationError(ClientExceptionBase):
pass
class WebstackClientError(ClientExceptionBase):
"""Exeption class for errors returned by the web stack client"""
_response = None # http response that resulted in the error
def __init__(self, message='', response=None):
super(WebstackClientError, self).__init__(message)
self._response = response
@property
def response(self):
"""The http response that resulted in the error"""
return self._response
class URIError(ClientExceptionBase):
pass
class UserInterrupt(ClientExceptionBase):
pass
class ControllerGraphClientException(ClientExceptionBase):
_statusCode = None # the HTTP status code
_content = None # the body of the response (dict, JSON decoded)
_response = None # the raw requests.Response object
_errorCode = None # the error code from the server, can be 'not-found', 'conflict', etc.
def __init__(self, message='', statusCode=None, content=None, response=None, errorCode=None):
super(ControllerGraphClientException, self).__init__(message)
self._statusCode = statusCode
self._content = content
self._response = response
self._errorCode = errorCode
@property
def statusCode(self):
return self._statusCode
@property
def content(self):
return self._content
@property
def response(self):
return self._response
@property
def errorCode(self):
return self._errorCode