Author: aconway
Date: Tue Jul 8 20:18:15 2014
New Revision: 1608937
URL: http://svn.apache.org/r1608937
Log:
WIP: Moved out exception/status codes to error.py, renamed node.py to client.py.
Added:
qpid/dispatch/trunk/python/qpid_dispatch_internal/management/client.py
- copied, changed from r1608890,
qpid/dispatch/trunk/python/qpid_dispatch_internal/management/node.py
qpid/dispatch/trunk/python/qpid_dispatch_internal/management/error.py
Removed:
qpid/dispatch/trunk/python/qpid_dispatch_internal/management/node.py
Modified:
qpid/dispatch/trunk/python/qpid_dispatch_internal/management/__init__.py
qpid/dispatch/trunk/python/qpid_dispatch_internal/management/agent.py
Modified:
qpid/dispatch/trunk/python/qpid_dispatch_internal/management/__init__.py
URL:
http://svn.apache.org/viewvc/qpid/dispatch/trunk/python/qpid_dispatch_internal/management/__init__.py?rev=1608937&r1=1608936&r2=1608937&view=diff
==============================================================================
--- qpid/dispatch/trunk/python/qpid_dispatch_internal/management/__init__.py
(original)
+++ qpid/dispatch/trunk/python/qpid_dispatch_internal/management/__init__.py
Tue Jul 8 20:18:15 2014
@@ -20,7 +20,8 @@
from .entity import Entity, EntityList
-from .node import ManagementError, Node, Url
+from .error import ManagementError
+from .client import Node, Url
from .qdrouter import QdSchema
from .config import Config, configure_dispatch
from .schema import Type, BooleanType, EnumType, EnumValue, AttributeType,
EntityType, Schema, schema_file, ValidationError
Modified: qpid/dispatch/trunk/python/qpid_dispatch_internal/management/agent.py
URL:
http://svn.apache.org/viewvc/qpid/dispatch/trunk/python/qpid_dispatch_internal/management/agent.py?rev=1608937&r1=1608936&r2=1608937&view=diff
==============================================================================
--- qpid/dispatch/trunk/python/qpid_dispatch_internal/management/agent.py
(original)
+++ qpid/dispatch/trunk/python/qpid_dispatch_internal/management/agent.py Tue
Jul 8 20:18:15 2014
@@ -17,10 +17,10 @@
# under the License
#
-import httplib, re
+import re
from qpid_dispatch_internal import dispatch_c
from dispatch import IoAdapter, LogAdapter, LOG_DEBUG, LOG_ERROR
-from node import ManagementError
+from error import ManagementError, OK, BAD_REQUEST, INTERNAL_SERVER_ERROR,
NOT_IMPLEMENTED
from schema import ValidationError
from entity import Entity, EntityList
from qdrouter import QdSchema
@@ -38,7 +38,7 @@ class Agent(object):
self.log = LogAdapter("AGENT").log
self.schema = QdSchema()
- def respond(self, request, status=httplib.OK, description="OK", body=None):
+ def respond(self, request, status=OK, description="OK", body=None):
response = Message(
address=request.reply_to,
correlation_id=request.correlation_id,
@@ -60,18 +60,18 @@ class Agent(object):
except ManagementError, e:
error(e, format_exc())
except ValidationError, e:
- error(ManagementError(httplib.BAD_REQUEST, str(e)), format_exc())
+ error(ManagementError(BAD_REQUEST, str(e)), format_exc())
except Exception, e:
- error(ManagementError(httplib.INTERNAL_SERVER_ERROR,
+ error(ManagementError(INTERNAL_SERVER_ERROR,
"%s: %s"%(type(e).__name__ , e)),
format_exc())
def handle(self, request):
op = request.properties.get('operation')
if not op:
- raise ManagementError(httplib.BAD_REQUEST, "No 'operation'
property on %s"%request)
+ raise ManagementError(BAD_REQUEST, "No 'operation' property on
%s"%request)
method = getattr(self, op.lower(), None)
if not method:
- raise ManagementError(httplib.NOT_IMPLEMENTED, op)
+ raise ManagementError(NOT_IMPLEMENTED, op)
method(request)
def create(self, request):
@@ -79,10 +79,10 @@ class Agent(object):
entity = Entity(request.body)
for a in ['type', 'name']:
if a not in request.properties:
- raise ManagementError(httplib.BAD_REQUEST,
+ raise ManagementError(BAD_REQUEST,
"No value for '%s' in request
properties"%a)
if a in entity and entity[a] != request.properties[a]:
- raise ManagementError(httplib.BAD_REQUEST, "Conflicting values
for '%s'"%a)
+ raise ManagementError(BAD_REQUEST, "Conflicting values for
'%s'"%a)
entity[a] = request.properties[a]
self.schema.validate(EntityList([entity]), full=False)
@@ -114,6 +114,6 @@ class Agent(object):
getattr(Creator(), re.sub('-', '_', entity.type))()
except AttributeError:
raise ManagementError(
- httplib.BAD_REQUEST, "Cannot create entity of type
'%s'"%entity.type)
+ BAD_REQUEST, "Cannot create entity of type '%s'"%entity.type)
self.respond(request, body=dict(entity))
Copied: qpid/dispatch/trunk/python/qpid_dispatch_internal/management/client.py
(from r1608890,
qpid/dispatch/trunk/python/qpid_dispatch_internal/management/node.py)
URL:
http://svn.apache.org/viewvc/qpid/dispatch/trunk/python/qpid_dispatch_internal/management/client.py?p2=qpid/dispatch/trunk/python/qpid_dispatch_internal/management/client.py&p1=qpid/dispatch/trunk/python/qpid_dispatch_internal/management/node.py&r1=1608890&r2=1608937&rev=1608937&view=diff
==============================================================================
--- qpid/dispatch/trunk/python/qpid_dispatch_internal/management/node.py
(original)
+++ qpid/dispatch/trunk/python/qpid_dispatch_internal/management/client.py Tue
Jul 8 20:18:15 2014
@@ -18,36 +18,13 @@
##
"""
-AMQP management tools for Qpid dispatch.
+AMQP management client for Qpid dispatch.
"""
-import proton, re, threading, httplib
+import proton, re, threading
from entity import Entity, EntityList
+from error import ManagementError, OK, NOT_FOUND
-class ManagementError(Exception):
- """An AMQP management error. str() gives a string with status code and
text.
- @ivar status: integer status code
- @ivar description: description text
- """
- def __init__(self, status, description):
- self.status, self.description = status, description
-
- def __str__(self):
- status_str = httplib.responses.get(self.status)
- if status_str in self.description: return self.description
- else: return "%s: %s"%(status_str, self.description)
-
- @classmethod
- def is_ok(cls, status):
- return status == httplib.OK
-
- @classmethod
- def raise_if(cls, status, response):
- if not cls.is_ok(status):
- raise ManagementError(status, response)
-
-
-# TODO aconway 2014-06-03: proton URL class, conditional import?
class Url:
"""Simple AMQP URL parser/constructor"""
@@ -66,18 +43,21 @@ class Url:
"""
fields = ['scheme', 'user', 'password', 'host', 'port', 'path']
- for field in fields: setattr(self, field, None)
- if isinstance(url, Url): # Copy from url
- for field in fields:
- setattr(self, field, getattr(url, field))
- elif url is not None: # Parse from url
+ for f in fields: setattr(self, f, None)
+ for k in kwargs: getattr(self, k) # Check for invalid kwargs
+
+ if isinstance(url, Url): # Copy from another Url instance.
+ self.__dict__.update(url.__dict__)
+
+ elif url is not None: # Parse from url
match = Url.RE.match(url)
if match is None:
raise ValueError("Invalid AMQP URL: %s"%url)
self.scheme, self.user, self.password, host4, host6, port,
self.path = match.groups()
self.host = host4 or host6
self.port = port and int(port)
+
# Let kwargs override values previously set from url
for field in fields:
setattr(self, field, kwargs.get(field, getattr(self, field)))
@@ -193,10 +173,11 @@ class Node(object):
"""
Check a manaement response message for errors and correlation ID.
"""
- properties = response.properties
- ManagementError.raise_if(properties.get('statusCode'),
properties.get('statusDescription'))
+ code = response.properties.get('statusCode')
+ if code != OK:
+ raise ManagementError(code,
response.properties.get('statusDescription'))
if response.correlation_id != request.correlation_id:
- raise ManagementError(httplib.NOT_FOUND, "Bad correlation id
request=%s, response=%s"%(
+ raise ManagementError(NOT_FOUND, "Bad correlation id request=%s,
response=%s"%(
request.correlation_id, response.correlation_id))
Added: qpid/dispatch/trunk/python/qpid_dispatch_internal/management/error.py
URL:
http://svn.apache.org/viewvc/qpid/dispatch/trunk/python/qpid_dispatch_internal/management/error.py?rev=1608937&view=auto
==============================================================================
--- qpid/dispatch/trunk/python/qpid_dispatch_internal/management/error.py
(added)
+++ qpid/dispatch/trunk/python/qpid_dispatch_internal/management/error.py Tue
Jul 8 20:18:15 2014
@@ -0,0 +1,44 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License
+#
+
+# HTTP status codes used by AMQP (extend as needed)
+
+STATUS_CODES=['OK', 'BAD_REQUEST', 'UNAUTHORIZED', 'FORBIDDEN', 'NOT_FOUND',
'REQUEST_TIMEOUT', 'INTERNAL_SERVER_ERROR', 'NOT_IMPLEMENTED']
+
+__all__ = STATUS_CODES + ['ManagementError', 'STATUS_TEXT']
+
+import httplib
+for code in STATUS_CODES: exec("%s=httplib.%s"%(code, code))
+from httplib import responses as STATUS_TEXT
+del httplib
+
+class ManagementError(Exception):
+ """
+ An AMQP management error.
+ str() gives a string with status code and text.
+ @ivar status: integer status code.
+ @ivar description: detailed description of error.
+ """
+ def __init__(self, status, description):
+ self.status, self.description = status, description
+
+ def __str__(self):
+ text = STATUS_TEXT.get(self.status) or "Unknown status %s"%self.status
+ if text in self.description: return self.description
+ else: return "%s: %s"%(text, self.description)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]