<commit_before>from fabric.api import task


class Nexus(object):

    def __init__(self, n9k_ip, n9k_username, n9k_password, ucsm_ports=None, peer_link=None):
        self.n9k_ip = n9k_ip
        self.n9k_username = n9k_username
        self.n9k_password = <PASSWORD>
        self.ucsm_ports = ucsm_ports
        self.peer_link = peer_link
        self.osp_ports = set(self.ucsm_ports + self.peer_link)

    def _allow_feature_nxapi(self):
        from fabric.api import settings, run

        with settings(host_string='{user}@{ip}'.format(user=self.n9k_username, ip=self.n9k_ip), password=<PASSWORD>):
            if 'disabled'in run('sh feature | i nxapi', shell=False):
                run('conf t ; feature nxapi', shell=False)

    def _rest_api(self, commands):
        import requests
        import json

        body = [{"jsonrpc": "2.0", "method": "cli", "params": {"cmd": command, "version": 1}, "id": 1} for command in commands]
        try:
            return requests.post('http://{0}/ins'.format(self.n9k_ip), auth=(self.n9k_username, self.n9k_password),
                                 headers={'content-type': 'application/json-rpc'}, data=json.dumps(body), timeout=2).json()
        except requests.exceptions.ConnectionError:
            self._allow_feature_nxapi()
            return self._rest_api(commands=commands)
        except requests.exceptions.ReadTimeout:
            return 'timeout' if len(commands) == 1 else ['timeout'] * len(commands)

    def get_hostname(self):
        res = self.cmd(['sh switchname'])
        return res[0]['result']['body']['hostname']

    def cmd(self, commands):
        if isinstance(commands, basestring):  # it might be provided as a string where commands are separated by ','
            commands = commands.strip('[]')
            commands = commands.split(',')

        results = self._rest_api(commands=commands)
        if len(commands) == 1:
            results = [results]
        for i, x in enumerate(results, start=0):
            if 'error' in x:
                raise NameError('{cmd} : {msg}'.format(msg=x['error']['data']['msg'].strip('%\n'), cmd=commands[i]))
        return results

    def change_port_state(self, port_no, port_state="no shut"):
        """
        Change port state of the port
        :param port_no: should be in full format like e1/3 or po1
        :param port_state: 'shut' or 'no shut'
        """
        self.cmd(['conf t', 'int {port}'.format(port=port_no), '{0}'.format(port_state)])

    def show_port_channel_summary(self):
        res = self.cmd(['show port-channel summary'])
        if res[0] == 'timeout':
            return []
        return [x['port-channel'] for x in res[0]['result']['body']['TABLE_channel']['ROW_channel']]

    def get_pc_for_osp(self):
        res = self.cmd(['show port-channel summary'])
        if res[0] == 'timeout':
            raise Exception('Connection to N9K {ip} timed out. Response: {res}'.format(ip=self.n9k_ip, res=res[0]))
        pc = []
        try:
            all_pc_w_ports = [x for x in res[0]['result']['body']['TABLE_channel']['ROW_channel'] if 'TABLE_member' in x]
            for entry in all_pc_w_ports:
                ports = entry['TABLE_member']['ROW_member']
                if isinstance(ports, list):
                    port_list = set([x['port'].split('Ethernet')[1] for x in ports])
                    if port_list.issubset(self.osp_ports):
                        pc.append(entry['port-channel'])
                else:
                    port = ports['port']
                    if port.split('Ethernet')[1] in self.osp_ports:
                         pc.append(entry['port-channel'])
        except Exception:
            raise Exception('Error in parsing response from N9K {ip}. Response: {res}'.format(ip=self.n9k_ip, res=res))
        return pc

    def show_interface_switchport(self, name):
        res = self.cmd(['show interface {0} switchport'.format(name)])
        if res[0] == 'timeout':
            return []
        vlans_str = res[0]['result']['body']['TABLE_interface']['ROW_interface']['trunk_vlans']
        vlans = set()
        for vlan_range in vlans_str.split(','):  # from  1,2,5-7  to (1, 2, 5, 6, 7)
            se = vlan_range.split('-')
            if len(se) == 2:
                vlans = vlans | set(range(int(se[0]), int(se[1]) + 1))
            elif len(se) == 1:
                vlans.add(int(se[0]))
        return sorted(vlans)<commit_msg>Minor fixes to n9k. Part 2<commit_after>from fabric.api import task


class Nexus(object):

    def __init__(self, n9k_ip, n9k_username, n9k_password, ucsm_ports=None, peer_link=None):
        self.n9k_ip = n9k_ip
        self.n9k_username = n9k_username
        self.n9k_password = <PASSWORD>
        self.ucsm_ports = ucsm_ports
        self.peer_link = peer_link
        self.osp_ports = set(self.ucsm_ports + self.peer_link)

    def _allow_feature_nxapi(self):
        from fabric.api import settings, run

        with settings(host_string='{user}@{ip}'.format(user=self.n9k_username, ip=self.n9k_ip), password=self.n9k_password):
            if 'disabled'in run('sh feature | i nxapi', shell=False):
                run('conf t ; feature nxapi', shell=False)

    def _rest_api(self, commands, timeout=2):
        import requests
        import json

        body = [{"jsonrpc": "2.0", "method": "cli", "params": {"cmd": command, "version": 1}, "id": 1} for command in commands]
        try:
            return requests.post('http://{0}/ins'.format(self.n9k_ip), auth=(self.n9k_username, self.n9k_password),
                                 headers={'content-type': 'application/json-rpc'}, data=json.dumps(body), timeout=timeout).json()
        except requests.exceptions.ConnectionError:
            self._allow_feature_nxapi()
            return self._rest_api(commands=commands, timeout=timeout)
        except requests.exceptions.ReadTimeout:
            return 'timeout' if len(commands) == 1 else ['timeout'] * len(commands)

    def get_hostname(self):
        res = self.cmd(['sh switchname'])
        return res[0]['result']['body']['hostname']

    def cmd(self, commands, timeout=2):
        if isinstance(commands, basestring):  # it might be provided as a string where commands are separated by ','
            commands = commands.strip('[]')
            commands = commands.split(',')

        results = self._rest_api(commands=commands, timeout=timeout)
        if len(commands) == 1:
            results = [results]
        for i, x in enumerate(results, start=0):
            if 'error' in x:
                raise NameError('{cmd} : {msg}'.format(msg=x['error']['data']['msg'].strip('%\n'), cmd=commands[i]))
        return results

    def change_port_state(self, port_no, port_state="no shut"):
        """
        Change port state of the port
        :param port_no: should be in full format like e1/3 or po1
        :param port_state: 'shut' or 'no shut'
        """
        self.cmd(['conf t', 'int {port}'.format(port=port_no), '{0}'.format(port_state)])

    def show_port_channel_summary(self):
        res = self.cmd(['show port-channel summary'])
        if res[0] == 'timeout':
            return []
        return [x['port-channel'] for x in res[0]['result']['body']['TABLE_channel']['ROW_channel']]

    def get_pc_for_osp(self):
        res = self.cmd(['show port-channel summary'], timeout=15)
        if res[0] == 'timeout':
            raise Exception('Connection to N9K {ip} timed out.'.format(ip=self.n9k_ip))
        pc = []
        try:
            all_pc_w_ports = [x for x in res[0]['result']['body']['TABLE_channel']['ROW_channel'] if 'TABLE_member' in x]
            for entry in all_pc_w_ports:
                ports = entry['TABLE_member']['ROW_member']
                if isinstance(ports, list):
                    port_list = set([x['port'].split('Ethernet')[1] for x in ports])
                    if port_list.issubset(self.osp_ports):
                        pc.append(entry['port-channel'])
                else:
                    port = ports['port']
                    if port.split('Ethernet')[1] in self.osp_ports:
                         pc.append(entry['port-channel'])
        except Exception:
            raise Exception('Error in parsing response from N9K {ip}. Response: {res}'.format(ip=self.n9k_ip, res=res))
        return pc

    def show_interface_switchport(self, name):
        res = self.cmd(['show interface {0} switchport'.format(name)])
        if res[0] == 'timeout':
            return []
        vlans_str = res[0]['result']['body']['TABLE_interface']['ROW_interface']['trunk_vlans']
        vlans = set()
        for vlan_range in vlans_str.split(','):  # from  1,2,5-7  to (1, 2, 5, 6, 7)
            se = vlan_range.split('-')
            if len(se) == 2:
                vlans = vlans | set(range(int(se[0]), int(se[1]) + 1))
            elif len(se) == 1:
                vlans.add(int(se[0]))
        return sorted(vlans)
{'fraction_non_alphanumeric': 0.14593088071348942, 'fraction_numerical': 0.013043478260869565, 'mean_word_length': 2.4136225266362255, 'pattern_counts': {'":': 12, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1315858', 'n_tokens_mistral': 2811, 'n_tokens_neox': 2728, 'n_words': 632}