[PR #10290] [MERGED] Fixes #10258 - Allow running scripts nested in modules/packages #13605

Closed
opened 2025-12-29 23:19:49 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbox-community/netbox/pull/10290
Author: @kkthxbye-code
Created: 9/7/2022
Status: Merged
Merged: 9/15/2022
Merged by: @jeremystretch

Base: developHead: 10258-nested-module-scripts


📝 Commits (3)

  • f489ffa Allow running scripts nested in modules/packages
  • 356ff45 Allow reports to be nested in submodules
  • c335b76 PEP8: Fix whitespace on blank line

📊 Changes

7 files changed (+38 additions, -24 deletions)

View changed files

📝 netbox/extras/api/views.py (+4 -4)
📝 netbox/extras/management/commands/runreport.py (+2 -2)
📝 netbox/extras/reports.py (+17 -10)
📝 netbox/extras/scripts.py (+7 -1)
📝 netbox/extras/urls.py (+3 -3)
📝 netbox/extras/views.py (+4 -3)
📝 netbox/templates/extras/script_list.html (+1 -1)

📄 Description

Fixes: #10258

The purpose of this FR is to allow a structure like this:

scripts
├── __init__.py
├── script0.py
└── testscriptfolder
    ├── __init__.py
    ├── script1.py
    ├── script2.py
    └── testnestedfolder
        ├── __init__.py
        └── script3.py

All scripts contain (with the numbers replaced):

from extras.scripts import *

class Script0(Script):
    def run(self, data, commit):
        self.log_success("Script0")

The __init__.py file in the testscriptfolder contains:

from .script1 import Script1
from .script2 import Script2
from .testnestedfolder.script3 import Script3

This setup results in the following script list with the PR:

image

This is achieved by splitting import paths so that the module_name contains the base module only, while the script name contains the rest of the path. For Script3 the import path would look like:

testscriptfolder.testnestedfolder.script3 which would be split to:

module_name = "testscriptfolder"
script_name = "testnestedfolder.script3"

The advantage is that scripts become much easier to manage as before the only way to have scripts grouped under the same module was to have them all in the same file. With long scripts this instantly becomes unwieldy.

I tested every combination I could come up with, and as far as I can tell defining custom names for the scripts still work. Defining custom names for the module still works (done in the __init__.py file). Calling scripts from the API still works. Script ordering still works.

I'm not sure if this is the right approach. As far as I can tell it doesn't break anything, but it's hard to guarantee that people haven't found crazy ways around the limitations of script loading. I would really like some feedback and help testing here. Maybe there's a better way to achieve better script organization that I'm missing.

Another limitation is that I haven't touched reports here.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbox-community/netbox/pull/10290 **Author:** [@kkthxbye-code](https://github.com/kkthxbye-code) **Created:** 9/7/2022 **Status:** ✅ Merged **Merged:** 9/15/2022 **Merged by:** [@jeremystretch](https://github.com/jeremystretch) **Base:** `develop` ← **Head:** `10258-nested-module-scripts` --- ### 📝 Commits (3) - [`f489ffa`](https://github.com/netbox-community/netbox/commit/f489ffa043c8859cce39df69a8397b11825a720b) Allow running scripts nested in modules/packages - [`356ff45`](https://github.com/netbox-community/netbox/commit/356ff457be08d5527920c617eb598f24a6edbc3d) Allow reports to be nested in submodules - [`c335b76`](https://github.com/netbox-community/netbox/commit/c335b76ec69515ea2055a93a5b8cd0f735139dd6) PEP8: Fix whitespace on blank line ### 📊 Changes **7 files changed** (+38 additions, -24 deletions) <details> <summary>View changed files</summary> 📝 `netbox/extras/api/views.py` (+4 -4) 📝 `netbox/extras/management/commands/runreport.py` (+2 -2) 📝 `netbox/extras/reports.py` (+17 -10) 📝 `netbox/extras/scripts.py` (+7 -1) 📝 `netbox/extras/urls.py` (+3 -3) 📝 `netbox/extras/views.py` (+4 -3) 📝 `netbox/templates/extras/script_list.html` (+1 -1) </details> ### 📄 Description ### Fixes: #10258 The purpose of this FR is to allow a structure like this: ``` scripts ├── __init__.py ├── script0.py └── testscriptfolder ├── __init__.py ├── script1.py ├── script2.py └── testnestedfolder ├── __init__.py └── script3.py ``` All scripts contain (with the numbers replaced): ``` from extras.scripts import * class Script0(Script): def run(self, data, commit): self.log_success("Script0") ``` The `__init__.py` file in the `testscriptfolder` contains: ``` from .script1 import Script1 from .script2 import Script2 from .testnestedfolder.script3 import Script3 ``` This setup results in the following script list with the PR: ![image](https://user-images.githubusercontent.com/400797/188974371-0eeda253-a327-4b03-b345-540f3968061a.png) This is achieved by splitting import paths so that the module_name contains the base module only, while the script name contains the rest of the path. For Script3 the import path would look like: `testscriptfolder.testnestedfolder.script3` which would be split to: ``` module_name = "testscriptfolder" script_name = "testnestedfolder.script3" ``` The advantage is that scripts become much easier to manage as before the only way to have scripts grouped under the same module was to have them all in the same file. With long scripts this instantly becomes unwieldy. I tested every combination I could come up with, and as far as I can tell defining custom names for the scripts still work. Defining custom names for the module still works (done in the `__init__.py` file). Calling scripts from the API still works. Script ordering still works. I'm not sure if this is the right approach. As far as I can tell it doesn't break anything, but it's hard to guarantee that people haven't found crazy ways around the limitations of script loading. I would really like some feedback and help testing here. Maybe there's a better way to achieve better script organization that I'm missing. Another limitation is that I haven't touched reports here. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-29 23:19:49 +01:00
adam closed this issue 2025-12-29 23:19:49 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#13605