1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
#
import asyncio
import socket
import typing
from termcolor import cprint
from nubia import argument, command, context
@command(aliases=["lookup"])
@argument("hosts", description="Hostnames to resolve", aliases=["i"])
@argument("bad_name", name="nice", description="testing")
def lookup_hosts(hosts: typing.List[str], bad_name: int):
"""
This will lookup the hostnames and print the corresponding IP addresses
"""
ctx = context.get_context()
cprint("Input: {}".format(hosts), "yellow")
cprint("Verbose? {}".format(ctx.verbose), "yellow")
for host in hosts:
cprint("{} is {}".format(host, socket.gethostbyname(host)))
# optional, by default it's 0
return 0
@command("good-name")
def bad_name():
"""
This command has a bad function name, but we ask Nubia to register a nicer
name instead
"""
cprint("Good Name!", "green")
@command("async-good-name")
async def async_bad_name():
"""
This command has a bad function name, but we ask Nubia to register a nicer
name instead
"""
cprint("This is async!", "green")
@command
@argument("number", type=int)
async def triple(number):
"Calculates the triple of the input value"
cprint("Input is {}".format(number))
cprint("Type of input is {}".format(type(number)))
cprint("{} * 3 = {}".format(number, number * 3))
await asyncio.sleep(2)
@command
@argument("number", type=int, positional=True)
def double(number):
"Calculates the triple of the input value"
cprint("Input is {}".format(number))
cprint("Type of input is {}".format(type(number)))
cprint("{} * 2 = {}".format(number, number * 2))
@command
@argument("number", type=int, positional=True)
@argument("text", type=str)
def pos_and_kv(number: int, text: str = ''):
"Accepts both positional and keyval args"
cprint("Input is {}".format(number))
cprint(f"The text is {text}")
@command
@argument("number", type=int, positional=True)
@argument("text", type=str, positional=True)
def multipos(number: int, text: str):
"Muliple positional args"
cprint(f"number is {number}")
cprint(f"The text is {text}")
@command
@argument("number", type=int, positional=True)
@argument("text", type=str, positional=True)
@argument("mylist", type=list)
@argument("mydict", type=dict)
def multipos_and_kv(number: int, text: str, mylist=None, mydict=None):
"Muliple positional args"
cprint(f"number is {number}")
cprint(f"The text is {text}")
cprint(f'mylist is {mylist}')
cprint(f'mydict is {mydict}')
@command("be-blocked")
def be_blocked():
"""
This command is an example of command that blocked in configerator.
"""
cprint("If you see me, something is wrong, Bzzz", "red")
@command
@argument("style", description="Pick a style", choices=["test", "toast", "toad"])
@argument("stuff", description="more colors", choices=["red", "green", "blue"])
@argument("code", description="Color code", choices=[12, 13, 14])
def pick(style: str, stuff: typing.List[str], code: int):
"""
A style picking tool
"""
cprint("Style is '{}' code is {}".format(style, code), "yellow")
# instead of replacing _ we rely on camelcase to - super-command
@command
class SuperCommand:
"This is a super command"
def __init__(self, shared: int = 0) -> None:
self._shared = shared
@property
def shared(self) -> int:
return self._shared
"""This is the super command help"""
@command
@argument("firstname", positional=True)
def print_name(self, firstname: str):
"""
print a name
"""
cprint("My name is: {}".format(firstname))
@command(aliases=["do"])
def do_stuff(self, stuff: int):
"""
doing stuff
"""
cprint("stuff={}, shared={}".format(stuff, self.shared))
|