mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-24 16:26:09 -06:00
Tweak command flags
This commit is contained in:
parent
71983ad84e
commit
ebca49f8e3
@ -1,9 +1,10 @@
|
|||||||
|
import os
|
||||||
from json import dumps as json_dumps
|
from json import dumps as json_dumps
|
||||||
from json import loads as json_loads
|
from json import loads as json_loads
|
||||||
from jinja2 import FileSystemLoader, Environment
|
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
|
from jinja2 import FileSystemLoader, Environment
|
||||||
|
|
||||||
from dcim.choices import (
|
from dcim.choices import (
|
||||||
DeviceAirflowChoices, SubdeviceRoleChoices, ConsolePortTypeChoices, PowerPortTypeChoices,
|
DeviceAirflowChoices, SubdeviceRoleChoices, ConsolePortTypeChoices, PowerPortTypeChoices,
|
||||||
@ -11,20 +12,18 @@ from dcim.choices import (
|
|||||||
InterfacePoETypeChoices, PortTypeChoices, WeightUnitChoices
|
InterfacePoETypeChoices, PortTypeChoices, WeightUnitChoices
|
||||||
)
|
)
|
||||||
|
|
||||||
|
TEMPLATE_FILENAME = 'generated_schema.json'
|
||||||
|
OUTPUT_FILENAME = 'contrib/generated_schema.json'
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
help = "Generate the NetBox validation schemas."
|
help = "Generate the NetBox validation schemas."
|
||||||
|
|
||||||
def add_arguments(self, parser):
|
def add_arguments(self, parser):
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--console',
|
'--write',
|
||||||
action='store_true',
|
action='store_true',
|
||||||
help="Print the generated schema to stdout"
|
help="Write the generated schema to file"
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'--file',
|
|
||||||
action='store_true',
|
|
||||||
help="Print the generated schema to the generated file"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def handle(self, *args, **kwargs):
|
def handle(self, *args, **kwargs):
|
||||||
@ -43,15 +42,16 @@ class Command(BaseCommand):
|
|||||||
|
|
||||||
template_loader = FileSystemLoader(searchpath=f'{settings.TEMPLATES_DIR}/extras/')
|
template_loader = FileSystemLoader(searchpath=f'{settings.TEMPLATES_DIR}/extras/')
|
||||||
template_env = Environment(loader=template_loader)
|
template_env = Environment(loader=template_loader)
|
||||||
TEMPLATE_FILE = 'generated_schema.json'
|
template = template_env.get_template(TEMPLATE_FILENAME)
|
||||||
template = template_env.get_template(TEMPLATE_FILE)
|
|
||||||
outputText = template.render(schemas=schemas)
|
outputText = template.render(schemas=schemas)
|
||||||
|
|
||||||
if kwargs['console']:
|
if kwargs['write']:
|
||||||
print(json_dumps(json_loads(outputText), indent=4))
|
# $root/contrib/generated_schema.json
|
||||||
|
filename = os.path.join(os.path.split(settings.BASE_DIR)[0], OUTPUT_FILENAME)
|
||||||
if kwargs['file']:
|
with open(filename, mode='w', encoding='UTF-8') as f:
|
||||||
with open(f'{settings.BASE_DIR}/../contrib/generated_schema.json', mode='w', encoding='UTF-8') as generated_json_file:
|
f.write(json_dumps(json_loads(outputText), indent=4))
|
||||||
generated_json_file.write(json_dumps(json_loads(outputText), indent=4))
|
f.write('\n')
|
||||||
generated_json_file.write('\n')
|
f.close()
|
||||||
generated_json_file.close()
|
self.stdout.write(self.style.SUCCESS(f"Schema written to {filename}."))
|
||||||
|
else:
|
||||||
|
self.stdout.write(outputText)
|
||||||
|
Loading…
Reference in New Issue
Block a user