def profile(self, request): """Start/stop the python profiler, returns profile results""" profile = self.__dict__.get("_profile") if "start" in request.properties: if not profile: profile = self.__dict__["_profile"] = Profile() profile.enable() self._log(LOG_INFO, "Started python profiler") return (OK, None) if not profile: raise BadRequestStatus("Profiler not started") if "stop" in request.properties: profile.create_stats() self._log(LOG_INFO, "Stopped python profiler") out = StringIO() stats = pstats.Stats(profile, stream=out) try: stop = request.properties["stop"] if stop == "kgrind": # Generate kcachegrind output using pyprof2calltree from pyprof2calltree import convert convert(stats, out) elif stop == "visualize": # Start kcachegrind using pyprof2calltree from pyprof2calltree import visualize visualize(stats) else: stats.print_stats() # Plain python profile stats return (OK, out.getvalue()) finally: out.close() raise BadRequestStatus("Bad profile request %s" % (request))
def create_entity(self, attributes): """Create an instance of the implementation class for an entity""" if attributes.get('identity') is not None: raise BadRequestStatus("'identity' attribute cannot be specified %s" % attributes) if attributes.get('type') is None: raise BadRequestStatus("No 'type' attribute in %s" % attributes) entity_type = self.schema.entity_type(attributes['type']) return self.entity_class(entity_type)(self, entity_type, attributes)
def receive(self, request, unused_link_id, unused_cost): """Called when a management request is received.""" def error(e, trace): """Raise an error""" self.log( LOG_ERROR, "Error performing %s: %s" % (request.properties.get('operation'), e.description)) self.respond(request, e.status, e.description) # If there's no reply_to, don't bother to process the request. if not request.reply_to: return # Coarse locking, handle one request at a time. with self.request_lock: try: self.entities.refresh_from_c() self.log(LOG_DEBUG, "Agent request %s" % request) status, body = self.handle(request) self.respond(request, status=status, body=body) except ManagementError as e: error(e, format_exc()) except ValidationError as e: error(BadRequestStatus(str(e)), format_exc()) except Exception as e: error( InternalServerErrorStatus("%s: %s" % (type(e).__name__, e)), format_exc())
def find_entity(self, request): """Find the entity addressed by request""" requested_type = request.properties.get('type') if requested_type: requested_type = self.schema.entity_type(requested_type) # ids is a map of identifying attribute values ids = dict((k, request.properties.get(k)) for k in ['name', 'identity'] if k in request.properties) # Special case for management object: if no name/id and no conflicting type # then assume this is for "self" if not ids: if not requested_type or self.management.entity_type.is_a( requested_type): return self.management else: raise BadRequestStatus("%s: No name or identity provided" % requested_type) def attrvals(): """String form of the id attribute values for error messages""" return " ".join(["%s=%r" % (k, v) for k, v in ids.iteritems()]) k, v = ids.iteritems().next() # Get the first id attribute found = self.entities.map_filter(None, lambda e: e.attributes.get(k) == v) if len(found) == 1: entity = found[0] elif len(found) > 1: raise InternalServerErrorStatus( "Duplicate (%s) entities with %s=%r" % (len(found), k, v)) else: raise NotFoundStatus("No entity with %s" % attrvals()) for k, v in ids.iteritems(): if entity[k] != v: raise BadRequestStatus("Conflicting %s" % attrvals()) if requested_type: if not entity.entity_type.is_a(requested_type): raise BadRequestStatus( "Entity type '%s' does not extend requested type '%s'" % (entity.entity_type.name, requested_type)) return entity
def create(self, request): """ Create operation called from an external client. Create is special: it is directed at an entity but the entity does not yet exist so it is handled initially by the agent and then delegated to the new entity. """ attributes = request.body for a in ['type', 'name']: prop = request.properties.get(a) if prop: old = attributes.setdefault(a, prop) if old is not None and old != prop: raise BadRequestStatus("Conflicting values for '%s'" % a) attributes[a] = prop if attributes.get('type') is None: raise BadRequestStatus("No 'type' attribute in %s" % attributes) et = self.schema.entity_type(attributes['type']) et.allowed("CREATE", attributes) et.create_check(attributes) return (CREATED, self._create(attributes).attributes)
def receive(self, request, link_id): """Called when a management request is received.""" def error(e, trace): """Raise an error""" self.log(LOG_ERROR, "Error dispatching %s: %s\n%s" % (request, e, trace)) self.respond(request, e.status, e.description) # If there's no reply_to, don't bother to process the request. if not request.reply_to: return # Coarse locking, handle one request at a time. with self.request_lock: try: self.entities.refresh_from_c() self.log(LOG_DEBUG, "Agent request %s on link %s" % (request, link_id)) status, body = self.handle(request) self.respond(request, status=status, body=body) except ManagementError, e: error(e, format_exc()) except ValidationError, e: error(BadRequestStatus(str(e)), format_exc())
def required_property(prop, request): """Raise exception if required property is missing""" if not request.properties or prop not in request.properties: raise BadRequestStatus("No '%s' property: %s"%(prop, request)) return request.properties[prop]
def _delete(self): if self.http: raise BadRequestStatus("HTTP listeners cannot be deleted") self._qd.qd_connection_manager_delete_listener( self._dispatch, self._implementations[0].key)
def _delete(self): if self.http and qpid_dispatch_site.SKIP_DELETE_HTTP_LISTENER: raise BadRequestStatus("HTTP listeners cannot be deleted") self._qd.qd_connection_manager_delete_listener( self._dispatch, self._implementations[0].key)