def __init_subclass__(cls, base_url: str): cls._default_base_url = base_url requests = { name: request for name, request in cls.__annotations__.items() if isinstance(request, Request) } for name, request in requests.items(): cls.__annotations__[name] = request.annotation() setattr(cls, _REQUESTS, requests) for name in requests: def _binder(name: str) -> Callable[[Radegast], BoundRequest]: @property # type: ignore def _property(self: Radegast) -> BoundRequest: return BoundRequest(self, name, requests[name]) return _property setattr(cls, name, _binder(name))
def sanitize_command(self, cmd): """ Removes None values, Validates all required params are present @param cmd: Cmd object eg: createPhysicalNetwork @return: """ requests = {} required = [] for attribute in dir(cmd): if attribute != "__doc__" and attribute != "__init__" and\ attribute != "__module__": if attribute == "isAsync": isAsync = getattr(cmd, attribute) elif attribute == "required": required = getattr(cmd, attribute) else: requests[attribute] = getattr(cmd, attribute) cmdname = cmd.__class__.__name__.replace("Cmd", "") for requiredPara in required: if requests[requiredPara] is None: raise cloudstackException.cloudstackAPIException( cmdname, "%s is required" % requiredPara) for param, value in requests.items(): if value is None: requests.pop(param) elif isinstance(value, list): if len(value) == 0: requests.pop(param) else: if not isinstance(value[0], dict): requests[param] = ",".join(value) else: requests.pop(param) i = 0 for val in value: for k, v in val.iteritems(): requests["%s[%d].%s" % (param, i, k)] = v i = i + 1 return cmdname, isAsync, requests
def _request_loop(self, force_sync=False): """The greenlet that requests needed metadata/pages """ self.update(force_sync=force_sync) self._updated.set() requests = {} while True: self.log('Request loop active') self.update() while True: while not self.request_queue.empty(): self.request_queue.get().insert_into(requests) gevent.sleep(0) try: request = self.request_queue.get( timeout=self._sleep_seconds()) except Empty: break else: request.insert_into(requests) request_list = [(k, v) for k, v in requests.items() if v] request_list.sort(key=lambda k_v: -len(k_v[1])) requests = dict(request_list) if request_list: for k, v in request_list[0][1].items(): v.run(requests) break if not request_list: self._sleep_before_request() if self.request_queue.empty(): self._updated.clear() self.log('Request loop exiting') return