<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
  listservers class

  This class implements methods that will list servers within a
  membase cluster

"""

import pprint
import restclient;
from membase_info import usage


class Listservers:

    def __init__(self):
        """
      constructor
    """

        self.rest_cmd = '/pools/default'
        self.method = 'GET'
        self.debug = False
        self.verbose = False
        self.output = 'standard'
        self.user = ''
        self.password = ''
        self.error = ''

    def runCmd(
        self,
        cmd,
        server,
        port,
        user,
        password,
        opts,
        ):

        self.user = user
        self.password = password

        for (o, a) in opts:
            if o in  ('-o', '--output'):
                self.output = a
            if o in  ('-d', '--debug'):
                self.debug = True
            if o in  ('-v', '--verbose'):
                self.verbose = True

        data = self.getData(server,
                            port,
                            user,
                            password)

        if (self.output == 'json'):
            print data
        else:
            if self.verbose:
                print 'List of servers within the server %s:%s' % (server, port)
            # obtain dict of nodes. If not dict, is error message
            nodes = self.getNodes(data)
            if type(nodes) == type(list()):
                self.printNodes(nodes)
            else:
                print self.error



    def getData(self,
                server,
                port,
                user='',
                password=''):
        """
        get the raw json output from the server
    """

        if not user and not password:
            user = self.user
            password = self.password

        self.rest = restclient.RestClient(server, port, {'debug':self.debug})
        response = self.rest.sendCmd(self.method,
                                     self.rest_cmd,
                                     user,
                                     password)

        if response.status == 200:
            data = response.read()
        else:
            data = '"ERROR: %d %s"' % (response.status, response.reason)

        return data

    def getNodes(self, data):
        """
        deserialize the raw json output and return just the nodes
    """

        json = self.rest.getJson(data)
        #pp = pprint.PrettyPrinter(indent=4)
        #pp.pprint(json['nodes'])
        # this means an error occured
        if type(json) == type(unicode()):
            self.error = json
            return None
        elif type(json) == type(list()):
            self.error = json[0]
            return None
        return json['nodes']

    def printNodes(self, nodes):
        """
        print the nodes
    """
        for node in nodes:
            print '%s\t%s' % (node['otpNode'], node['status' ])<commit_msg>BUG #1596: Added clusterMembership to output of server list<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
  listservers class

  This class implements methods that will list servers within a
  membase cluster

"""

import pprint
import restclient
from membase_info import usage


class Listservers:

    def __init__(self):
        """
      constructor
    """

        self.rest_cmd = '/pools/default'
        self.method = 'GET'
        self.debug = False
        self.verbose = False
        self.output = 'standard'
        self.user = ''
        self.password = ''
        self.error = ''

    def runCmd(
        self,
        cmd,
        server,
        port,
        user,
        password,
        opts,
        ):

        self.user = user
        self.password = password

        for (o, a) in opts:
            if o in  ('-o', '--output'):
                self.output = a
            if o in  ('-d', '--debug'):
                self.debug = True
            if o in  ('-v', '--verbose'):
                self.verbose = True

        data = self.getData(server,
                            port,
                            user,
                            password)

        if (self.output == 'json'):
            print data
        else:
            if self.verbose:
                print 'List of servers within the server %s:%s' % (server, port)
            # obtain dict of nodes. If not dict, is error message
            nodes = self.getNodes(data)
            if type(nodes) == type(list()):
                self.printNodes(nodes)
            else:
                print self.error



    def getData(self,
                server,
                port,
                user='',
                password=''):
        """
        get the raw json output from the server
    """

        if not user and not password:
            user = self.user
            password = <PASSWORD>

        self.rest = restclient.RestClient(server, port, {'debug':self.debug})
        response = self.rest.sendCmd(self.method,
                                     self.rest_cmd,
                                     user,
                                     password)

        if response.status == 200:
            data = response.read()
        else:
            data = '"ERROR: %d %s"' % (response.status, response.reason)

        return data

    def getNodes(self, data):
        """
        deserialize the raw json output and return just the nodes
    """

        json = self.rest.getJson(data)
        #pp = pprint.PrettyPrinter(indent=4)
        #pp.pprint(json['nodes'])
        # this means an error occured
        if type(json) == type(unicode()):
            self.error = json
            return None
        elif type(json) == type(list()):
            self.error = json[0]
            return None
        return json['nodes']

    def printNodes(self, nodes):
        """
        print the nodes
    """
        for node in nodes:
            print '%s %s %s' % (node['otpNode'],
                                node['status'],
                                node['clusterMembership'])
{'fraction_non_alphanumeric': 0.10735294117647058, 'fraction_numerical': 0.00261437908496732, 'mean_word_length': 1.2478883584282041, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '453805', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1581, 'n_words': 461}