def __call__(self, message_id, json=False): try: conn = get_db_conn() cursor = conn.cursor() query = "SELECT `message`,`format` FROM p2p_message " \ "WHERE `message_id`='%s'" % message_id cursor.execute(query) fetched_data = cursor.fetchone() finally: cursor.close() if fetched_data: msg = Message() msg_format = fetched_data[1] if msg_format == 'json': msg.fromjson(fetched_data[0]) else: msg.fromxml(fetched_data[0]) mdict = encode({'id': msg.id, 'name': msg.name, 'meta': msg.meta, 'body': msg.body}) if json: print json_module.dumps(mdict, indent=4, sort_keys=True, ensure_ascii=False) else: print yaml.dump(mdict, default_flow_style=False, allow_unicode=True) else: raise CommandError('Message not found')
def _stop_service(self, service, **kwds): api = service_apis[service]() for key, value in kwds.items(): if value == None: kwds.pop(key) try: print 'Stopping %s' % service api.stop_service(**kwds) except (BaseException, Exception), e: print 'Service stop failed.\n%s' % e return int(CommandError())
def __call__(self, behavior=None): # if behavior not in behavior_apis: # raise CommandError('Unknown behavior.') bus.queryenv_service = new_queryenv(autoretry=False) api = ServiceAPI() # api.init_service() if behavior and behavior not in behavior_apis: raise CommandError('Unknown behavior.') if behavior: print "Reconfiguring behavior %s..." % behavior else: print "Reconfiguring..." behavior_params = {behavior: None} if behavior else None try: api.reconfigure(behavior_params=behavior_params, async=False) except (BaseException, Exception), e: raise CommandError('Reconfigure failed.\n%s' % e)
def _run_queryenv_method(self, method, kwds, kwds_mapping=None): """ Executes queryenv method with given `kwds`. `kwds` can contain excessive key-value pairs, this method filters it and passes only acceptable kwds by target method. `kwds_mapping` defines how `kwds` keys will be renamed when passed to queryenv method. """ if not method: return if not kwds_mapping: kwds_mapping = {} method = method.replace('-', '_') try: m = getattr(self.queryenv(), method) except AttributeError: raise CommandError('Unknown QueryEnv method\n') argspec = inspect.getargspec(m) argnames = argspec.args filtered_kwds = {} for k, v in kwds.items(): arg_name = kwds_mapping.get(k, k) if argspec.keywords or arg_name in argnames: filtered_kwds[arg_name] = v if method == 'fetch': del kwds[k] if method == 'fetch': filtered_kwds['params'] = kwds try: return m(**filtered_kwds) except (QueryEnvError, HTTPError) as e: if isinstance(e, HTTPError) and method == 'fetch': message = '%s method is not supported' % filtered_kwds[ 'command'] else: message = str(e) raise CommandError(message)
def _display_fetch(self, out, format='xml'): if format == 'xml': print minidom.parseString(out).toprettyxml(encoding='utf-8') elif format == 'json': out_dict = xml2dict(ET.XML(out)) print json_module.dumps(out_dict, indent=4, sort_keys=True, ensure_ascii=False) elif format == 'yaml': out_dict = xml2dict(ET.XML(out)) print yaml.dump(out_dict, default_flow_style=False, allow_unicode=True) else: raise CommandError( 'Unknown output format.\nAvailable formats: xml, json, yaml')
def _format_status(self, status_struct, format='xml'): if format == 'xml': if isinstance(status_struct, dict): doc = self._dict_to_xml(status_struct, 'status') else: doc = self._dict_list_to_xml(status_struct, 'statuses', 'status') return doc.toprettyxml(encoding='utf-8') elif format == 'json': return json_module.dumps(status_struct, indent=4, sort_keys=True, ensure_ascii=False) elif format == 'yaml': return yaml.dump(status_struct, default_flow_style=False, allow_unicode=True) else: raise CommandError( 'Unknown output format.\nAvailable formats: xml, json, yaml')