Skip to content

Commit

Permalink
Remove dependency on click-default-group package (#903)
Browse files Browse the repository at this point in the history
  • Loading branch information
jleveque authored May 9, 2020
1 parent 45eb491 commit f57b487
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 166 deletions.
43 changes: 16 additions & 27 deletions clear/bgp_frr_v6.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,23 @@ def bgp():
"""Clear IPv6 BGP (Border Gateway Protocol) information"""
pass


# Default 'bgp' command (called if no subcommands or their aliases were passed)
@bgp.command(default=True)
def default():
"""Clear all BGP peers"""
command = 'sudo vtysh -c "clear bgp ipv6 *"'
run_command(command)


@bgp.group()
def neighbor():
"""Clear specific BGP peers"""
pass


@neighbor.command(default=True)
# 'all' subcommand
@neighbor.command('all')
@click.argument('ipaddress', required=False)
def default(ipaddress):
def neigh_all(ipaddress):
"""Clear all BGP peers"""

if ipaddress is not None:
command = 'sudo vtysh -c "clear bgp ipv6 {} "'.format(ipaddress)
command = 'sudo vtysh -c "clear bgp ipv6 {}"'.format(ipaddress)
else:
command = 'sudo vtysh -c "clear bgp ipv6 *"'
run_command(command)


# 'in' subcommand
@neighbor.command('in')
@click.argument('ipaddress', required=False)
Expand Down Expand Up @@ -72,19 +62,6 @@ def soft():
"""Soft reconfig BGP's inbound/outbound updates"""
pass


@soft.command(default=True)
@click.argument('ipaddress', required=False)
def default(ipaddress):
"""Clear BGP neighbors soft configuration"""

if ipaddress is not None:
command = 'sudo vtysh -c "clear bgp ipv6 {} soft "'.format(ipaddress)
else:
command = 'sudo vtysh -c "clear bgp ipv6 * soft"'
run_command(command)


# 'soft in' subcommand
@soft.command('in')
@click.argument('ipaddress', required=False)
Expand All @@ -98,6 +75,18 @@ def soft_in(ipaddress):
run_command(command)


# 'soft all' subcommand
@neighbor.command('all')
@click.argument('ipaddress', required=False)
def soft_all(ipaddress):
"""Clear BGP neighbors soft configuration"""

if ipaddress is not None:
command = 'sudo vtysh -c "clear bgp ipv6 {} soft"'.format(ipaddress)
else:
command = 'sudo vtysh -c "clear bgp ipv6 * soft"'
run_command(command)

# 'soft out' subcommand
@soft.command('out')
@click.argument('ipaddress', required=False)
Expand Down
20 changes: 6 additions & 14 deletions clear/bgp_quagga_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,19 @@ def bgp():
pass


# Default 'bgp' command (called if no subcommands or their aliases were passed)
@bgp.command(default=True)
def default():
"""Clear all BGP peers"""
command = 'sudo vtysh -c "clear ip bgp *"'
run_command(command)


@bgp.group()
def neighbor():
"""Clear specific BGP peers"""
pass


@neighbor.command(default=True)
@neighbor.command('all')
@click.argument('ipaddress', required=False)
def default(ipaddress):
def neigh_all(ipaddress):
"""Clear all BGP peers"""

if ipaddress is not None:
command = 'sudo vtysh -c "clear ip bgp {} "'.format(ipaddress)
command = 'sudo vtysh -c "clear ip bgp {}"'.format(ipaddress)
else:
command = 'sudo vtysh -c "clear ip bgp *"'
run_command(command)
Expand Down Expand Up @@ -73,13 +65,13 @@ def soft():
pass


@soft.command(default=True)
@soft.command('all')
@click.argument('ipaddress', required=False)
def default(ipaddress):
def soft_all(ipaddress):
"""Clear BGP neighbors soft configuration"""

if ipaddress is not None:
command = 'sudo vtysh -c "clear ip bgp {} soft "'.format(ipaddress)
command = 'sudo vtysh -c "clear ip bgp {} soft"'.format(ipaddress)
else:
command = 'sudo vtysh -c "clear ip bgp * soft"'
run_command(command)
Expand Down
22 changes: 6 additions & 16 deletions clear/bgp_quagga_v6.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,18 @@ def bgp():
"""Clear IPv6 BGP (Border Gateway Protocol) information"""
pass


# Default 'bgp' command (called if no subcommands or their aliases were passed)
@bgp.command(default=True)
def default():
"""Clear all BGP peers"""
command = 'sudo vtysh -c "clear ipv6 bgp *"'
run_command(command)


@bgp.group()
def neighbor():
"""Clear specific BGP peers"""
pass


@neighbor.command(default=True)
@neighbor.command('all')
@click.argument('ipaddress', required=False)
def default(ipaddress):
def neigh_all(ipaddress):
"""Clear all BGP peers"""

if ipaddress is not None:
command = 'sudo vtysh -c "clear ipv6 bgp {} "'.format(ipaddress)
command = 'sudo vtysh -c "clear ipv6 bgp {}"'.format(ipaddress)
else:
command = 'sudo vtysh -c "clear ipv6 bgp *"'
run_command(command)
Expand Down Expand Up @@ -73,13 +63,13 @@ def soft():
pass


@soft.command(default=True)
@soft.command('all')
@click.argument('ipaddress', required=False)
def default(ipaddress):
def soft_all(ipaddress):
"""Clear BGP neighbors soft configuration"""

if ipaddress is not None:
command = 'sudo vtysh -c "clear ipv6 bgp {} soft "'.format(ipaddress)
command = 'sudo vtysh -c "clear ipv6 bgp {} soft"'.format(ipaddress)
else:
command = 'sudo vtysh -c "clear ipv6 bgp * soft"'
run_command(command)
Expand Down
20 changes: 7 additions & 13 deletions clear/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import click
import os
import subprocess
from click_default_group import DefaultGroup

try:
# noinspection PyPep8Naming
Expand Down Expand Up @@ -35,12 +34,10 @@ def read_config(self, filename):
_config = None


# This aliased group has been modified from click examples to inherit from DefaultGroup instead of click.Group.
# DefaultFroup is a superclass of click.Group which calls a default subcommand instead of showing
# a help message if no subcommand is passed
class AliasedGroup(DefaultGroup):
"""This subclass of a DefaultGroup supports looking up aliases in a config
file and with a bit of magic.

class AliasedGroup(click.Group):
"""This subclass of click.Group supports abbreviations and
looking up aliases in a config file with a bit of magic.
"""

def get_command(self, ctx, cmd_name):
Expand Down Expand Up @@ -71,12 +68,9 @@ def get_command(self, ctx, cmd_name):
matches = [x for x in self.list_commands(ctx)
if x.lower().startswith(cmd_name.lower())]
if not matches:
# No command name matched. Issue Default command.
ctx.arg0 = cmd_name
cmd_name = self.default_cmd_name
return DefaultGroup.get_command(self, ctx, cmd_name)
return None
elif len(matches) == 1:
return DefaultGroup.get_command(self, ctx, matches[0])
return click.Group.get_command(self, ctx, matches[0])
ctx.fail('Too many matches: %s' % ', '.join(sorted(matches)))


Expand Down Expand Up @@ -384,7 +378,7 @@ def line(linenum):
# 'nat' group ("clear nat ...")
#

@cli.group(cls=AliasedGroup, default_if_no_args=False)
@cli.group(cls=AliasedGroup)
def nat():
"""Clear the nat info"""
pass
Expand Down
18 changes: 5 additions & 13 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import ipaddress
from swsssdk import ConfigDBConnector, SonicV2Connector, SonicDBConfig
from minigraph import parse_device_desc_xml
from click_default_group import DefaultGroup

import aaa
import mlnx
Expand Down Expand Up @@ -63,12 +62,8 @@ def log_error(msg):
syslog.closelog()


# This aliased group has been modified from click examples to inherit from DefaultGroup instead of click.Group.
# DefaultGroup is a superclass of click.Group which calls a default subcommand instead of showing
# a help message if no subcommand is passed
class AbbreviationGroup(DefaultGroup):
"""This subclass of a DefaultGroup supports looking up aliases in a config
file and with a bit of magic.
class AbbreviationGroup(click.Group):
"""This subclass of click.Group supports abbreviated subgroup/subcommand names
"""

def get_command(self, ctx, cmd_name):
Expand All @@ -93,18 +88,15 @@ def get_command(self, ctx, cmd_name):
shortest = x

if not matches:
# No command name matched. Issue Default command.
ctx.arg0 = cmd_name
cmd_name = self.default_cmd_name
return DefaultGroup.get_command(self, ctx, cmd_name)
return None
elif len(matches) == 1:
return DefaultGroup.get_command(self, ctx, matches[0])
return click.Group.get_command(self, ctx, matches[0])
else:
for x in matches:
if not x.startswith(shortest):
break
else:
return DefaultGroup.get_command(self, ctx, shortest)
return click.Group.get_command(self, ctx, shortest)

ctx.fail('Too many matches: %s' % ', '.join(sorted(matches)))

Expand Down
17 changes: 5 additions & 12 deletions connect/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import click
import os
import pexpect
from click_default_group import DefaultGroup

try:
# noinspection PyPep8Naming
Expand Down Expand Up @@ -35,12 +34,9 @@ def read_config(self, filename):
_config = None


# This aliased group has been modified from click examples to inherit from DefaultGroup instead of click.Group.
# DefaultGroup is a superclass of click.Group which calls a default subcommand instead of showing
# a help message if no subcommand is passed
class AliasedGroup(DefaultGroup):
"""This subclass of a DefaultGroup supports looking up aliases in a config
file and with a bit of magic.
class AliasedGroup(click.Group):
"""This subclass of click.Group supports abbreviations and
looking up aliases in a config file with a bit of magic.
"""

def get_command(self, ctx, cmd_name):
Expand Down Expand Up @@ -71,12 +67,9 @@ def get_command(self, ctx, cmd_name):
matches = [x for x in self.list_commands(ctx)
if x.lower().startswith(cmd_name.lower())]
if not matches:
# No command name matched. Issue Default command.
ctx.arg0 = cmd_name
cmd_name = self.default_cmd_name
return DefaultGroup.get_command(self, ctx, cmd_name)
return None
elif len(matches) == 1:
return DefaultGroup.get_command(self, ctx, matches[0])
return click.Group.get_command(self, ctx, matches[0])
ctx.fail('Too many matches: %s' % ', '.join(sorted(matches)))

def run_command(command, display_cmd=False):
Expand Down
19 changes: 7 additions & 12 deletions debug/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import click
import subprocess
from click_default_group import DefaultGroup

def run_command(command, pager=False):
click.echo(click.style("Command: ", fg='cyan') + click.style(command, fg='green'))
Expand Down Expand Up @@ -192,17 +191,13 @@ def vxlan():
#
# 'bgp' group for quagga ###
#
@cli.group(cls=DefaultGroup, default_if_no_args=True)
#@cli.group()
def bgp():
"""debug bgp on """
pass

@bgp.command(default=True)
def default():
"""debug bgp"""
command = 'sudo vtysh -c "debug bgp"'
run_command(command)
@cli.group(invoke_without_command=True)
@click.pass_context
def bgp(ctx):
"""debug bgp on"""
if ctx.invoked_subcommand is None:
command = 'sudo vtysh -c "debug bgp"'
run_command(command)

@bgp.command()
def events():
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@
# - swsssdk
# - tabulate
install_requires=[
'click-default-group',
'click',
'natsort'
],
Expand Down
Loading

0 comments on commit f57b487

Please sign in to comment.