1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Move script attributes under a Meta class

This commit is contained in:
Jeremy Stretch
2019-08-12 13:16:18 -04:00
parent ab504439fb
commit 19eb4c510c
4 changed files with 22 additions and 17 deletions

View File

@ -39,20 +39,22 @@ Returning output from your script is optional. Any raw output generated by the s
## Script Attributes ## Script Attributes
### script_name Script attributes are defined under a class named `Meta` within the script. These are optional, but encouraged.
### `name`
This is the human-friendly names of your script. If omitted, the class name will be used. This is the human-friendly names of your script. If omitted, the class name will be used.
### script_description ### `description`
A human-friendly description of what your script does (optional). A human-friendly description of what your script does.
### script_fields ### `fields`
The order in which the variable fields should appear. This is optional, however on Python 3.5 and earlier the fields will appear in random order. (Declarative ordering is preserved on Python 3.6 and above.) For example: The order in which the variable fields should appear. This is optional, however on Python 3.5 and earlier the fields will appear in random order. (Declarative ordering is preserved on Python 3.6 and above.) For example:
``` ```
script_fields = ['var1', 'var2', 'var3'] fields = ['var1', 'var2', 'var3']
``` ```
## Logging ## Logging
@ -124,9 +126,11 @@ from extras.scripts import *
class NewBranchScript(Script): class NewBranchScript(Script):
script_name = "New Branch"
script_description = "Provision a new branch site" class Meta:
script_fields = ['site_name', 'switch_count', 'switch_model'] name = "New Branch"
description = "Provision a new branch site"
fields = ['site_name', 'switch_count', 'switch_model']
site_name = StringVar( site_name = StringVar(
description="Name of the new site" description="Name of the new site"

View File

@ -118,6 +118,9 @@ class Script:
""" """
Custom scripts inherit this object. Custom scripts inherit this object.
""" """
class Meta:
pass
def __init__(self): def __init__(self):
# Initiate the log # Initiate the log
@ -128,16 +131,14 @@ class Script:
self.source = inspect.getsource(self.__class__) self.source = inspect.getsource(self.__class__)
def __str__(self): def __str__(self):
if hasattr(self, 'script_name'): return getattr(self.Meta, 'name', self.__class__.__name__)
return self.script_name
return self.__class__.__name__
def _get_vars(self): def _get_vars(self):
vars = OrderedDict() vars = OrderedDict()
# Infer order from script_fields (Python 3.5 and lower) # Infer order from Meta.fields (Python 3.5 and lower)
if hasattr(self, 'script_fields'): fields = getattr(self.Meta, 'fields')
for name in self.script_fields: for name in fields:
vars[name] = getattr(self, name) vars[name] = getattr(self, name)
# Default to order of declaration on class # Default to order of declaration on class

View File

@ -16,7 +16,7 @@
</div> </div>
</div> </div>
<h1>{{ script }}</h1> <h1>{{ script }}</h1>
<p>{{ script.script_description }}</p> <p>{{ script.Meta.description }}</p>
<ul class="nav nav-tabs" role="tablist"> <ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active"> <li role="presentation" class="active">
<a href="#run" role="tab" data-toggle="tab" class="active">Run</a> <a href="#run" role="tab" data-toggle="tab" class="active">Run</a>

View File

@ -22,7 +22,7 @@
<td> <td>
<a href="{% url 'extras:script' module=module name=class_name %}" name="script.{{ class_name }}"><strong>{{ script }}</strong></a> <a href="{% url 'extras:script' module=module name=class_name %}" name="script.{{ class_name }}"><strong>{{ script }}</strong></a>
</td> </td>
<td>{{ script.script_description }}</td> <td>{{ script.Meta.description }}</td>
<td></td> <td></td>
</tr> </tr>
{% endfor %} {% endfor %}