forked from mirrors/thatmattlove-hyperglass
migrate CLI to module
This commit is contained in:
parent
b089eb3e0a
commit
3102a7427a
12 changed files with 13 additions and 144 deletions
6
.flake8
6
.flake8
|
|
@ -9,10 +9,8 @@ per-file-ignores=
|
||||||
# Disable redefinition warning for exception handlers
|
# Disable redefinition warning for exception handlers
|
||||||
hyperglass/api.py:F811
|
hyperglass/api.py:F811
|
||||||
# Disable classmethod warning for validator decorators
|
# Disable classmethod warning for validator decorators
|
||||||
hyperglass/models/*.py:N805,E0213,R0903
|
hyperglass/api/models/*.py:N805,E0213,R0903
|
||||||
hyperglass/configuration/models/*.py:N805,E0213,R0903
|
hyperglass/configuration/models/*.py:N805,E0213,R0903,E501,C0301
|
||||||
# Disable string length warnings so one can actually read the commands
|
|
||||||
hyperglass/configuration/models/*.py:E501,C0301
|
|
||||||
hyperglass/api/models/response.py:E501,C0301
|
hyperglass/api/models/response.py:E501,C0301
|
||||||
ignore=W503,C0330,R504,D202
|
ignore=W503,C0330,R504,D202
|
||||||
select=B, BLK, C, D, E, F, I, II, N, P, PIE, S, R, W
|
select=B, BLK, C, D, E, F, I, II, N, P, PIE, S, R, W
|
||||||
|
|
|
||||||
|
|
@ -1,51 +0,0 @@
|
||||||
"""Build docs tables from schema."""
|
|
||||||
|
|
||||||
HEADERS = ["Field", "Description", "Type", "Default"]
|
|
||||||
|
|
||||||
|
|
||||||
def build_table(data, level):
|
|
||||||
table = [HEADERS]
|
|
||||||
|
|
||||||
for prop, attrs in data.items():
|
|
||||||
if attrs.get("level", 0) == 0:
|
|
||||||
table.append(
|
|
||||||
[
|
|
||||||
prop,
|
|
||||||
attrs.get("description", ""),
|
|
||||||
attrs.get("type", attrs.get("anyOf", "")),
|
|
||||||
attrs.get("default", ""),
|
|
||||||
]
|
|
||||||
)
|
|
||||||
return table
|
|
||||||
|
|
||||||
|
|
||||||
def process_object(obj):
|
|
||||||
definitions = obj.pop("definitions", {})
|
|
||||||
properties = obj.pop("properties")
|
|
||||||
level = obj.pop("level", 0)
|
|
||||||
|
|
||||||
top_properties = {}
|
|
||||||
|
|
||||||
for key, value in properties.items():
|
|
||||||
if value["title"] not in definitions:
|
|
||||||
top_properties.update({key: value})
|
|
||||||
|
|
||||||
data = build_table(data=top_properties, level=level)
|
|
||||||
return data
|
|
||||||
|
|
||||||
|
|
||||||
def schema_top_level():
|
|
||||||
from hyperglass.configuration.models.params import Params
|
|
||||||
|
|
||||||
schema = Params.schema()
|
|
||||||
definitions = schema.get("definitions")
|
|
||||||
|
|
||||||
tables = {}
|
|
||||||
top_level = process_object(schema)
|
|
||||||
tables.update({schema["title"]: top_level})
|
|
||||||
|
|
||||||
for k, v in definitions.items():
|
|
||||||
if isinstance(v, dict) and "allOf" not in v:
|
|
||||||
tables.update({k: process_object(v)})
|
|
||||||
|
|
||||||
return tables
|
|
||||||
35
develop.py
35
develop.py
|
|
@ -1,35 +0,0 @@
|
||||||
"""Devloper functions."""
|
|
||||||
|
|
||||||
# Standard Library Imports
|
|
||||||
import os
|
|
||||||
|
|
||||||
|
|
||||||
def count_lines(directory):
|
|
||||||
"""Count lines of code.
|
|
||||||
|
|
||||||
Arguments:
|
|
||||||
directory {str} -- Path to count
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
{int} -- Line count
|
|
||||||
"""
|
|
||||||
lines = 0
|
|
||||||
excluded = ("\n",)
|
|
||||||
for thing in os.listdir(directory):
|
|
||||||
thing = os.path.join(directory, thing)
|
|
||||||
if os.path.isfile(thing):
|
|
||||||
if thing.endswith(".py"):
|
|
||||||
with open(thing, "r") as f:
|
|
||||||
readlines = [
|
|
||||||
line
|
|
||||||
for line in f.readlines()
|
|
||||||
if line not in excluded and not line.startswith("#")
|
|
||||||
]
|
|
||||||
lines += len(readlines)
|
|
||||||
|
|
||||||
for thing in os.listdir(directory):
|
|
||||||
thing = os.path.join(directory, thing)
|
|
||||||
if os.path.isdir(thing):
|
|
||||||
lines += count_lines(thing)
|
|
||||||
|
|
||||||
return lines
|
|
||||||
|
|
@ -49,7 +49,7 @@ import stackprinter
|
||||||
# Project
|
# Project
|
||||||
# Project Imports
|
# Project Imports
|
||||||
# flake8: noqa: F401
|
# flake8: noqa: F401
|
||||||
from hyperglass import api, util, constants, execution, exceptions, configuration
|
from hyperglass import api, cli, util, constants, execution, exceptions, configuration
|
||||||
|
|
||||||
stackprinter.set_excepthook()
|
stackprinter.set_excepthook()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
import stackprinter
|
import stackprinter
|
||||||
|
|
||||||
# Project
|
# Project
|
||||||
from cli import echo, util, static, commands, formatting, schema # noqa: F401
|
from hyperglass.cli import commands
|
||||||
|
|
||||||
stackprinter.set_excepthook(style="darkbg2")
|
stackprinter.set_excepthook(style="darkbg2")
|
||||||
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
#!/usr/bin/env python3
|
|
||||||
"""CLI Command definitions."""
|
"""CLI Command definitions."""
|
||||||
|
|
||||||
# Standard Library
|
# Standard Library
|
||||||
|
|
@ -8,8 +7,8 @@ from pathlib import Path
|
||||||
import click
|
import click
|
||||||
|
|
||||||
# Project
|
# Project
|
||||||
from cli.echo import error, value, cmd_help
|
from hyperglass.cli.echo import error, value, cmd_help
|
||||||
from cli.util import (
|
from hyperglass.cli.util import (
|
||||||
build_ui,
|
build_ui,
|
||||||
fix_ownership,
|
fix_ownership,
|
||||||
migrate_config,
|
migrate_config,
|
||||||
|
|
@ -17,8 +16,8 @@ from cli.util import (
|
||||||
migrate_systemd,
|
migrate_systemd,
|
||||||
start_web_server,
|
start_web_server,
|
||||||
)
|
)
|
||||||
from cli.static import LABEL, CLI_HELP, E
|
from hyperglass.cli.static import LABEL, CLI_HELP, E
|
||||||
from cli.formatting import HelpColorsGroup, HelpColorsCommand, random_colors
|
from hyperglass.cli.formatting import HelpColorsGroup, HelpColorsCommand, random_colors
|
||||||
|
|
||||||
# Define working directory
|
# Define working directory
|
||||||
WORKING_DIR = Path(__file__).parent
|
WORKING_DIR = Path(__file__).parent
|
||||||
|
|
@ -29,13 +28,7 @@ WORKING_DIR = Path(__file__).parent
|
||||||
help=CLI_HELP,
|
help=CLI_HELP,
|
||||||
help_headers_color=LABEL,
|
help_headers_color=LABEL,
|
||||||
help_options_custom_colors=random_colors(
|
help_options_custom_colors=random_colors(
|
||||||
"build-ui",
|
"build-ui", "start", "migrate-examples", "systemd", "permissions", "secret"
|
||||||
"start",
|
|
||||||
"migrate-examples",
|
|
||||||
"systemd",
|
|
||||||
"permissions",
|
|
||||||
"secret",
|
|
||||||
"docs-schema",
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
def hg():
|
def hg():
|
||||||
|
|
@ -142,34 +135,3 @@ def generate_secret(length):
|
||||||
|
|
||||||
gen_secret = secrets.token_urlsafe(length)
|
gen_secret = secrets.token_urlsafe(length)
|
||||||
value("Secret", gen_secret)
|
value("Secret", gen_secret)
|
||||||
|
|
||||||
|
|
||||||
@hg.command(
|
|
||||||
"docs-schema",
|
|
||||||
help=cmd_help(E.BOOKS, "Generate docs schema"),
|
|
||||||
cls=HelpColorsCommand,
|
|
||||||
help_options_custom_colors=random_colors("-p"),
|
|
||||||
)
|
|
||||||
@click.option(
|
|
||||||
"-p", "--print", "print_table", is_flag=True, help="Print table to console"
|
|
||||||
)
|
|
||||||
def build_docs_schema(print_table):
|
|
||||||
from rich.console import Console
|
|
||||||
from rich.table import Table
|
|
||||||
from cli.schema import schema_top_level
|
|
||||||
|
|
||||||
data = schema_top_level()
|
|
||||||
|
|
||||||
if print_table:
|
|
||||||
console = Console()
|
|
||||||
|
|
||||||
for section, table_data in data.items():
|
|
||||||
table = Table(show_header=True, header_style="bold magenta")
|
|
||||||
for col in table_data[0]:
|
|
||||||
table.add_column(col)
|
|
||||||
|
|
||||||
for row in table_data[1::]:
|
|
||||||
table.add_row(*(str(i) for i in row))
|
|
||||||
|
|
||||||
click.echo(console.print(section, style="bold red"))
|
|
||||||
click.echo(console.print(table))
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
import click
|
import click
|
||||||
|
|
||||||
# Project
|
# Project
|
||||||
from cli.static import (
|
from hyperglass.cli.static import (
|
||||||
CL,
|
CL,
|
||||||
NL,
|
NL,
|
||||||
WS,
|
WS,
|
||||||
|
|
@ -6,8 +6,8 @@ from pathlib import Path
|
||||||
import click
|
import click
|
||||||
|
|
||||||
# Project
|
# Project
|
||||||
from cli.echo import info, error, status, success
|
from hyperglass.cli.echo import info, error, status, success
|
||||||
from cli.static import CL, NL, WS, VALUE, SUCCESS, E
|
from hyperglass.cli.static import CL, NL, WS, VALUE, SUCCESS, E
|
||||||
|
|
||||||
PROJECT_ROOT = Path(__file__).parent.parent
|
PROJECT_ROOT = Path(__file__).parent.parent
|
||||||
|
|
||||||
|
|
@ -73,21 +73,16 @@ class AccessList6(HyperglassModel):
|
||||||
"permit",
|
"permit",
|
||||||
title="Action",
|
title="Action",
|
||||||
description="Permit or deny any networks contained within the prefix.",
|
description="Permit or deny any networks contained within the prefix.",
|
||||||
# regex="permit|deny",
|
|
||||||
)
|
)
|
||||||
ge: conint(ge=0, le=128) = Field(
|
ge: conint(ge=0, le=128) = Field(
|
||||||
0,
|
0,
|
||||||
title="Greater Than or Equal To",
|
title="Greater Than or Equal To",
|
||||||
description="Similar to `ge` in a Cisco prefix-list, the `ge` field defines the **bottom** threshold for prefix size. For example, a value of `64` would result in a query for `2001:db8::/48` being denied, but a query for `2001:db8::1/128` would be permitted. If this field is set to a value smaller than the `network` field's prefix length, this field's value will be overwritten to the prefix length of the prefix in the `network` field.",
|
description="Similar to `ge` in a Cisco prefix-list, the `ge` field defines the **bottom** threshold for prefix size. For example, a value of `64` would result in a query for `2001:db8::/48` being denied, but a query for `2001:db8::1/128` would be permitted. If this field is set to a value smaller than the `network` field's prefix length, this field's value will be overwritten to the prefix length of the prefix in the `network` field.",
|
||||||
# ge=0,
|
|
||||||
# le=128,
|
|
||||||
)
|
)
|
||||||
le: conint(ge=0, le=128) = Field(
|
le: conint(ge=0, le=128) = Field(
|
||||||
128,
|
128,
|
||||||
title="Less Than or Equal To",
|
title="Less Than or Equal To",
|
||||||
description="Similar to `le` in a Cisco prefix-list, the `le` field defines the **top** threshold for prefix size. For example, a value of `64` would result in a query for `2001:db8::/48` being permitted, but a query for `2001:db8::1/128` would be denied.",
|
description="Similar to `le` in a Cisco prefix-list, the `le` field defines the **top** threshold for prefix size. For example, a value of `64` would result in a query for `2001:db8::/48` being permitted, but a query for `2001:db8::1/128` would be denied.",
|
||||||
# ge=0,
|
|
||||||
# le=128,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@validator("ge")
|
@validator("ge")
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
"""hyperglass CLI management tool."""
|
"""hyperglass CLI management tool."""
|
||||||
|
|
||||||
# Project
|
# Project
|
||||||
from cli import CLI
|
from hyperglass.cli import CLI
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
CLI()
|
CLI()
|
||||||
Loading…
Add table
Reference in a new issue