ソースを参照

Allow user to override module name

Jeremy Stretch 6 年 前
コミット
3d6a583ce4
2 ファイル変更8 行追加0 行削除
  1. 6 0
      docs/additional-features/custom-scripts.md
  2. 2 0
      netbox/extras/scripts.py

+ 6 - 0
docs/additional-features/custom-scripts.md

@@ -37,6 +37,12 @@ Defining variables is optional: You may create a script with only a `run()` meth
 
 
 Returning output from your script is optional. Any raw output generated by the script will be displayed under the "output" tab in the UI.
 Returning output from your script is optional. Any raw output generated by the script will be displayed under the "output" tab in the UI.
 
 
+## Module Attributes
+
+### `name`
+
+You can define `name` within a script module (the Python file which contains one or more scripts) to set the module name. If `name` is not defined, the filename will be used.
+
 ## Script Attributes
 ## Script Attributes
 
 
 Script attributes are defined under a class named `Meta` within the script. These are optional, but encouraged.
 Script attributes are defined under a class named `Meta` within the script. These are optional, but encouraged.

+ 2 - 0
netbox/extras/scripts.py

@@ -233,6 +233,8 @@ def get_scripts():
     # defined.
     # defined.
     for importer, module_name, _ in pkgutil.iter_modules([settings.SCRIPTS_ROOT]):
     for importer, module_name, _ in pkgutil.iter_modules([settings.SCRIPTS_ROOT]):
         module = importer.find_module(module_name).load_module(module_name)
         module = importer.find_module(module_name).load_module(module_name)
+        if hasattr(module, 'name'):
+            module_name = module.name
         module_scripts = OrderedDict()
         module_scripts = OrderedDict()
         for name, cls in inspect.getmembers(module, is_script):
         for name, cls in inspect.getmembers(module, is_script):
             module_scripts[name] = cls
             module_scripts[name] = cls