<commit_before>#
# Copyright 2015 <NAME>, Opera Software ASA
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License.  You may obtain a copy of
# the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations under
# the License.

from base import NetworkDriver
from exceptions import ReplaceConfigException, MergeConfigException
from bnclient import bnclient
import difflib
import sys
from StringIO import StringIO


class IBMDriver(NetworkDriver):

    def __init__(self, hostname, username, password, timeout=60):
        self.hostname = hostname
        self.username = username
        self.password = password
        self.timeout = timeout
        self.argv = ['', '-u', username, '-p', password, hostname]
        self.bnc = bnclient.bnclient(self.argv)
        self.config_replace = False
        self.config = {"running": "", "candidate": "", "rollback": ""}
        self.filename_running = "/tmp/" + hostname + "-running.conf"
        self.filename_candidate = None
        self.filename_rollback = "/tmp/" + hostname + "-rollback.conf"
        self.error = StringIO()

    def str2argv(self, str=''):
        return str.split(' ')

    def open(self):
        self.bnc.connect()
        self.bnc.sendhello()
        self._get_config(self.filename_rollback)

    def close(self):
        self.bnc.close()

    def _bnc_cmd(self, file, operation, error_option):
        cmd = "-o edit-config -t running -f "
        cmd += file
        cmd += " -d " + operation
        cmd += " -r " + error_option
        return cmd

    def _write_memory(self):
        cmd = "-o copy-config -t startup -s running"
        return cmd

    def _send_rpc(self, cmd):
        self.error = StringIO()
        old_stdout = sys.stdout
        sys.stdout = self.error
        self.bnc.sendrpc(self.str2argv(cmd))
        sys.stdout = old_stdout

    def _get_config(self, filename):
        self.bnc.sendrpc(self.str2argv("-o get -f " + filename))

    def _load_config(self, filename, config):
        self._get_config(self.filename_rollback)
        if filename is None:
            configuration = config<commit_msg>timeout implemented in IBM module<commit_after>#
# Copyright 2015 <NAME>, Opera Software ASA
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License.  You may obtain a copy of
# the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations under
# the License.

from base import NetworkDriver
from exceptions import ReplaceConfigException, MergeConfigException
from bnclient import bnclient
import difflib
import sys
from StringIO import StringIO


class IBMDriver(NetworkDriver):

    def __init__(self, hostname, username, password, timeout=60):
        self.hostname = hostname
        self.username = username
        self.password = password
        self.timeout = timeout
        self.argv = ['', '-u', username, '-p', password, hostname]
        self.bnc = bnclient.bnclient(self.argv)
        self.config_replace = False
        self.config = {"running": "", "candidate": "", "rollback": ""}
        self.filename_running = "/tmp/" + hostname + "-running.conf"
        self.filename_candidate = None
        self.filename_rollback = "/tmp/" + hostname + "-rollback.conf"
        self.error = StringIO()

    def str2argv(self, str=''):
        return str.split(' ')

    def open(self):
        self.bnc.connect(self.timeout)
        self.bnc.sendhello()
        self._get_config(self.filename_rollback)

    def close(self):
        self.bnc.close()

    def _bnc_cmd(self, file, operation, error_option):
        cmd = "-o edit-config -t running -f "
        cmd += file
        cmd += " -d " + operation
        cmd += " -r " + error_option
        return cmd

    def _write_memory(self):
        cmd = "-o copy-config -t startup -s running"
        return cmd

    def _send_rpc(self, cmd):
        self.error = StringIO()
        old_stdout = sys.stdout
        sys.stdout = self.error
        self.bnc.sendrpc(self.str2argv(cmd))
        sys.stdout = old_stdout

    def _get_config(self, filename):
        self.bnc.sendrpc(self.str2argv("-o get -f " + filename))

    def _load_config(self, filename, config):
        self._get_config(self.filename_rollback)
        if filename is None:
            configuration = config
{'fraction_non_alphanumeric': 0.10415833666533386, 'fraction_numerical': 0.005197920831667333, 'mean_word_length': 2.7447604790419162, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1819523', 'n_tokens_mistral': 1443, 'n_tokens_neox': 1410, 'n_words': 449}