<commit_before>from base import NetworkDriver

from ncclient import manager


def __execute_rpc__(conn, rpc_command):
    output = conn.rpc(str(rpc_command))

    if len(output.xpath('/rpc-reply/error')) > 0:
        raise Exception(output.tostring)

    return output


class JunOSDriver(NetworkDriver):

    def __init__(self, hostname, user, password):
        self.hostname = hostname
        self.user = user
        self.password = password

    def open(self):
        self.device = manager.connect(
            host=self.hostname,
            port=830,
            username=self.user,
            password=<PASSWORD>,
            timeout=10,
            hostkey_verify=False,
            device_params={'name':'junos'},
        )
        rpc_command = '<lock><target><candidate/></target></lock>'
        __execute_rpc__(self.device, rpc_command)

    def close(self):
        rpc_command = '<unlock><target><candidate/></target></unlock>'
        __execute_rpc__(self.device, rpc_command)
        self.device.close_session()

    def load_candidate_config(self, filename=None, config=None):

        if filename is None:
            configuration = config
        else:
            with open(filename) as f:
                configuration = f.read()

        rpc_command = '''
        <load-configuration format="text" action="replace">
            <configuration-text>
                %s
            </configuration-text>
        </load-configuration>
        ''' % configuration

        __execute_rpc__(self.device, rpc_command)

    def compare_config(self):
        rpc_command = '''
        <get-configuration changed="changed" database= "candidate" format="text" compare="rollback" rollback="0">
        </get-configuration>'''
        conf = __execute_rpc__(self.device, rpc_command)
        return conf.xpath('/rpc-reply/configuration-information/configuration-output')[0].text

    def commit_config(self):
        rpc_command = '<commit/>'
        __execute_rpc__(self.device, rpc_command)

    def discard_config(self):
        rpc_command = '<discard-changes/>'
        __execute_rpc__(self.device, rpc_command)

    def rollback(self):
        rpc_command = '<load-configuration rollback="1"/>'
        __execute_rpc__(self.device, rpc_command)
        self.commit_config()

<commit_msg>Migrated from ncclient to pyEZ<commit_after>from base import NetworkDriver

from jnpr.junos import Device
from jnpr.junos.utils.config import Config

def __execute_rpc__(conn, rpc_command):
    output = conn.rpc(str(rpc_command))

    if len(output.xpath('/rpc-reply/error')) > 0:
        raise Exception(output.tostring)

    return output


class JunOSDriver(NetworkDriver):

    def __init__(self, hostname, user, password):
        self.hostname = hostname
        self.user = user
        self.password = password
        self.device = Device(hostname, user=user, password=password)

    def open(self):
        self.device.open()
        self.device.bind( cu=Config )
        self.device.cu.lock()

    def close(self):
        self.device.cu.unlock()
        self.device.close()

    def load_candidate_config(self, filename=None, config=None):
        if filename is None:
            configuration = config
        else:
            with open(filename) as f:
                configuration = f.read()

        self.device.cu.load(configuration, format='text', overwrite=True)

    def compare_config(self):
        return self.device.cu.diff()

    def commit_config(self):
        self.device.cu.commit()

    def discard_config(self):
        self.device.cu.rollback(rb_id=0)

    def rollback(self):
        self.device.cu.rollback(rb_id=1)
        self.commit_config()

{'fraction_non_alphanumeric': 0.11382113821138211, 'fraction_numerical': 0.0032520325203252032, 'mean_word_length': 2.53544061302682, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4178354', 'n_tokens_mistral': 1095, 'n_tokens_neox': 1050, 'n_words': 208}