Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def try_cast_number(value):
if type(value) in (tuple, int, float, bool):
# Already a type recognized by Tk
return value
try:
return pyats.tcl.cast_int(value)
except (TypeError, ValueError):
pass
try:
return pyats.tcl.cast_double(value)
except (TypeError, ValueError):
pass
return tclstr(value)
def clean_cli_output(output, cmd=None, os=None,
return_dict=False,
remove_prompt=True, remove_timestamp=True):
d = {
'cmd': None,
'timestamp': None,
'prompt': None,
}
try:
output = tclstr(output)
except NameError:
pass
from genie.libs.conf.utils import ansi
output = re.sub(r'\t', r' ', output)
output = re.sub(r'\r+\n', r'\n', output)
output = re.sub(r'.*\r', r'', output, re.MULTILINE)
output = re.sub(ansi.re_generic, r'', output)
output = re.sub(r'\s+$', r'', output, re.MULTILINE)
if cmd:
m = re.match(r'^(?P(?:do )?' + re.escape(cmd) + r')(?:\n|$)', output)
if m:
d.update(m.groupdict())
output = output[:m.start(0)] + output[m.end(0):]
)
from enum import Enum
import collections
import contextlib
import functools
import logging
import re
import statistics
import time
import types
try:
from pyats.tcl import tclstr
import pyats.tcl
str_type = tclstr
except Exception:
str_type = str
from genie.decorator import managedattribute
from genie.conf.base.attributes import AttributesHelper
import genie.conf.base.attributes
from genie.libs.conf.device.hltapi import Device as HltapiDevice
import genie.libs.conf.device
import genie.libs.conf.interface.hltapi
from genie.libs.conf.stream import Stream, StreamStats
from genie.libs.conf.base import IPv4Address, IPv6Address, MAC
logger = logging.getLogger(__name__)
#logger.setLevel(logging.DEBUG)
intf_kwargs['vlan_id'] = emulated_interface.eth_encap_val1 # REGEXP ^[0-9]{1,4}(,[0-9]{1,4})*$
intf_kwargs['vlan_id_mode'] = 'fixed' # REGEXP ^(fixed|increment)(,(fixed|increment))*$
# -vlan_id_step REGEXP ^[0-9]{1,4}(,[0-9]{1,4})*$
# -vlan_tpid REGEXP ^0x[0-9a-fA-F]+(,0x[0-9a-fA-F]+)*$
# -vlan_user_priority REGEXP ^[0-7](,[0-7])*$
# -vlan_user_priority_step REGEXP ^[0-7](,[0-7])*$
kl = tcl.cast_keyed_list(
hltapi.ixNetworkProtocolIntfCfg(**intf_kwargs),
item_cast=tclstr)
if int(kl.get('status', 0)) == 0:
raise ValueError(kl.get('log', 'Unknown message'))
self.tgen_handle = tcl.cast_list(
kl.connected_interfaces,
item_cast=tclstr)
# TODO self.name
# TODO emulated_interface.ipv6_link_local
return ''
kwargs['mac_dst2_step'] = str(MAC(kwargs['mac_dst2_step']))
except KeyError:
pass
# Pagent HLTAPI does not support l3_protocol=none, not sending it is equivalent.
if kwargs.get('l3_protocol', None) == 'none':
del kwargs['l3_protocol']
# Pagent HLTAPI does not support l4_protocol=none, not sending it is equivalent.
if kwargs.get('l4_protocol', None) == 'none':
del kwargs['l4_protocol']
# Pagent does not support -mac_discovery_gw
kwargs.pop('mac_discovery_gw', None)
# Pagent does not support -name
kwargs.pop('name', None)
# Pagent only supports -mpls_labels in this format: <label>,,,
if 'mpls_labels' in kwargs:
mpls_labels = self.tcl.cast_list(kwargs['mpls_labels'], item_cast=tclstr)
for i, mpls_label in enumerate(mpls_labels):
try:
mpls_label = int(mpls_label)
except ValueError:
continue
else:
mpls_label = '{mpls_label},{cos},{bottom},{ttl}'.format(
mpls_label=mpls_label,
cos=0,
bottom=int(i == len(mpls_labels) - 1),
ttl=0)
mpls_labels[i] = mpls_label
# Pagent does not support -gateway and -ipv6_gateway
kwargs.pop('gateway', None)
kwargs.pop('ipv6_gateway', None)
# Pagent does not support -mpls_labels_mode, support is equivalent to "fixed"</label>
def tcl_invoke_ats_cmd(cmd, *, cast_=None, **kwargs):
cmd = TclCommand(cmd, keywords=kwargs, cast=tcl.cast_list)
result = cmd()
result_code = result[0]
result_msg = result[1] if len(result) == 2 else result[1:]
try:
result_code = tcl.cast_int(result[0])
except ValueError:
result_code = tclstr(result[0])
if result_code in ('passed', 1):
if cast_:
result_msg = cast_(result_msg)
return result_msg
else:
raise RuntimeError(tclstr(result_msg))
def traffic_stats(self, **kwargs):
if 'streams' in kwargs:
streams_ids = self.tcl.cast_list(kwargs['streams'], item_cast=tclstr)
Pagent_GET_STREAMS_STATS_proc = TclCommand(
'::Pagent::GET_STREAMS_STATS',
tcl=self.tcl)
if len(streams_ids) > 1 \
and Pagent_GET_STREAMS_STATS_proc.exists() \
and 'lsearch $stream_id $streams' in Pagent_GET_STREAMS_STATS_proc.proc_body():
# Due to a bug in ::Pagent::GET_STREAMS_STATS where stream
# IDs are incorrectly matched (wrong order of [lsearch]
# parameters), revert to getting all the streams on all or
# only the specified ports
del kwargs['streams']
hltkl = self.pyats_connection.traffic_stats(**kwargs)
return hltkl
def tcl_invoke_ats_cmd(cmd, *, cast_=None, **kwargs):
cmd = TclCommand(cmd, keywords=kwargs, cast=tcl.cast_list)
result = cmd()
result_code = result[0]
result_msg = result[1] if len(result) == 2 else result[1:]
try:
result_code = tcl.cast_int(result[0])
except ValueError:
result_code = tclstr(result[0])
if result_code in ('passed', 1):
if cast_:
result_msg = cast_(result_msg)
return result_msg
else:
raise RuntimeError(tclstr(result_msg))