diff --git a/lib/jnpr/jsnapy/check.py b/lib/jnpr/jsnapy/check.py index cd586b08..c174f99a 100755 --- a/lib/jnpr/jsnapy/check.py +++ b/lib/jnpr/jsnapy/check.py @@ -27,16 +27,15 @@ class Comparator: - def __init__(self,**kwargs): + def __init__(self, **kwargs): """ Comparator object constructor. :param int port. """ - self.port = kwargs.get('port', None) self.logger_check = logging.getLogger(__name__) self.log_detail = {'hostname': None} - + self.port = kwargs.get('port', None) def is_op(self, op): """ @@ -70,7 +69,7 @@ def generate_snap_file(self, device, prefix, name, reply_format): This function generates name of snapshot files """ if self.port is not None: - device = device + "_" + str(self.port) + device = "{}_{}".format(device, self.port) if os.path.isfile(prefix): return prefix else: diff --git a/lib/jnpr/jsnapy/jsnapy.py b/lib/jnpr/jsnapy/jsnapy.py index 6d567a12..103e3a63 100755 --- a/lib/jnpr/jsnapy/jsnapy.py +++ b/lib/jnpr/jsnapy/jsnapy.py @@ -369,7 +369,7 @@ def get_hosts(self): sys.exit(1) self.login(output_file) - def generate_rpc_reply(self, dev, output_file, hostname, config_data,**kwargs): + def generate_rpc_reply(self, dev, output_file, hostname, config_data, **kwargs): """ Generates rpc-reply based on command/rpc given and stores them in snap_files :param dev: device handler @@ -377,7 +377,6 @@ def generate_rpc_reply(self, dev, output_file, hostname, config_data,**kwargs): :param hostname: hostname of device :param config_data : data of main config file """ - port = kwargs.get('port', None) val = None test_files = [] for tfile in config_data.get('tests'): @@ -488,18 +487,19 @@ def login(self, output_file): gp = first_entry.get('group', 'all') dgroup = [i.strip().lower() for i in gp.split(',')] - iter = 0; for dgp in dev_file: if dgroup[0].lower() == 'all' or dgp.lower() in dgroup: - for val in dev_file[dgp]: + for counter, val in enumerate(dev_file[dgp]): + # There can be multiple values of device/hostname + # The values can have same hostname but different port + # key for the dictionary modified from hostname to enumerate value to keep distinction hostname = list(val)[0] - iter+=1 self.log_detail = {'hostname': hostname} if val.get(hostname) is not None and hostname not in self.host_list: #host_dict[hostname] = deepcopy(val.get(hostname)) self.host_list.append(hostname) - host_dict[iter] = deepcopy(val.get(hostname)) - host_dict[iter]["device"] = hostname + host_dict[counter] = deepcopy(val.get(hostname)) + host_dict[counter]["device"] = hostname # login credentials are given in main config file, can connect to multiple devices else: @@ -542,7 +542,7 @@ def login(self, output_file): key_value = {'port': port} if port is not None else {} self.connect(hostname, username, password, output_file, **key_value) - def get_test(self, config_data, hostname, snap_file, post_snap, action,**kwargs): + def get_test(self, config_data, hostname, snap_file, post_snap, action, **kwargs): """ Analyse testfile and return object of operator.Operator containing test details called by connect() function and other functions of Jsnapy module functions @@ -1016,9 +1016,8 @@ def get_hosts_list(self, hosts_val, host_dict): :param hosts_val: has the list of hosts to be parsed :param host_dict: The dictionary to be created to store the parsed values """ - iter = -1 #iterator keeps count of number of hosts - for host in hosts_val: - iter += 1 + + for counter, host in enumerate(hosts_val): try: hostname = host['device'] self.log_detail = {'hostname': hostname} @@ -1036,7 +1035,10 @@ def get_hosts_list(self, hosts_val, host_dict): else: if hostname not in self.host_list: self.host_list.append(hostname) - host_dict[iter] = deepcopy(host) + # There can be multiple values of device/hostname + # The values can have same hostname but different port + # key for the dictionary modified from hostname to enumerate value to keep distinction + host_dict[counter] = deepcopy(host) ####### generate init folder ###### ''' diff --git a/lib/jnpr/jsnapy/snap.py b/lib/jnpr/jsnapy/snap.py index 2946d499..aaa2cf01 100755 --- a/lib/jnpr/jsnapy/snap.py +++ b/lib/jnpr/jsnapy/snap.py @@ -28,13 +28,13 @@ def __init__(self, **kwargs): :param int port. """ - self.port = kwargs.get('port', None) self.logger_snap = logging.getLogger(__name__) self.log_detail = {'hostname': None} self.reply = {} self.command_list = [] self.rpc_list = [] self.test_included = [] + self.port = kwargs.get('port', None) def _write_file(self, rpc_reply, format, output_file): """ @@ -96,7 +96,7 @@ def generate_snap_file(self, output_file, hostname, name, cmd_format): :return: return output file """ if self.port is not None: - hostname = hostname + "_" + str(self.port) + hostname = "{}_{}".format(hostname, self.port) name = name.split('|')[0].strip() cmd_rpc = re.sub('/|\*|\.|-|\|', '_', name) if os.path.isfile(output_file):