mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-26 17:26:10 -06:00
Fix loading of nested modules
This commit is contained in:
parent
f4fe7a1f9e
commit
4da655c163
@ -8,16 +8,9 @@ import extras.models.mixins
|
|||||||
|
|
||||||
def create_files(cls, root_name, root_path):
|
def create_files(cls, root_name, root_path):
|
||||||
|
|
||||||
path_tree = [
|
modules = list(pkgutil.iter_modules([root_path]))
|
||||||
path for path, _, _ in os.walk(root_path)
|
|
||||||
if os.path.basename(path)[0] not in ('_', '.')
|
|
||||||
]
|
|
||||||
|
|
||||||
modules = list(pkgutil.iter_modules(path_tree))
|
|
||||||
filenames = []
|
filenames = []
|
||||||
for importer, module_name, is_pkg in modules:
|
for importer, module_name, ispkg in modules:
|
||||||
if is_pkg:
|
|
||||||
continue
|
|
||||||
try:
|
try:
|
||||||
module = importer.find_module(module_name).load_module(module_name)
|
module = importer.find_module(module_name).load_module(module_name)
|
||||||
rel_path = os.path.relpath(module.__file__, root_path)
|
rel_path = os.path.relpath(module.__file__, root_path)
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
from pkgutil import ModuleInfo, get_importer
|
from importlib.machinery import SourceFileLoader
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'PythonModuleMixin',
|
'PythonModuleMixin',
|
||||||
@ -12,16 +12,17 @@ class PythonModuleMixin:
|
|||||||
def path(self):
|
def path(self):
|
||||||
return os.path.splitext(self.file_path)[0]
|
return os.path.splitext(self.file_path)[0]
|
||||||
|
|
||||||
def get_module_info(self):
|
@property
|
||||||
path = os.path.dirname(self.full_path)
|
def pymodule_name(self):
|
||||||
module_name = os.path.basename(self.path)
|
path, filename = os.path.split(self.full_path)
|
||||||
return ModuleInfo(
|
name = os.path.splitext(filename)[0]
|
||||||
module_finder=get_importer(path),
|
if name == '__init__':
|
||||||
name=module_name,
|
# File is a package
|
||||||
ispkg=False
|
return os.path.basename(path)
|
||||||
)
|
else:
|
||||||
|
return name
|
||||||
|
|
||||||
def get_module(self):
|
def get_module(self):
|
||||||
importer, module_name, _ = self.get_module_info()
|
loader = SourceFileLoader(self.pymodule_name, self.full_path)
|
||||||
module = importer.find_module(module_name).load_module(module_name)
|
module = loader.load_module()
|
||||||
return module
|
return module
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import inspect
|
import inspect
|
||||||
from functools import cached_property
|
from functools import cached_property
|
||||||
|
|
||||||
from django.contrib.contenttypes.fields import GenericRelation
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
@ -51,10 +50,7 @@ class ScriptModule(PythonModuleMixin, JobsMixin, ManagedFile):
|
|||||||
# For child objects in submodules use the full import path w/o the root module as the name
|
# For child objects in submodules use the full import path w/o the root module as the name
|
||||||
return cls.full_name.split(".", maxsplit=1)[1]
|
return cls.full_name.split(".", maxsplit=1)[1]
|
||||||
|
|
||||||
try:
|
module = self.get_module()
|
||||||
module = self.get_module()
|
|
||||||
except ImportError:
|
|
||||||
return {}
|
|
||||||
scripts = {}
|
scripts = {}
|
||||||
ordered = getattr(module, 'script_order', [])
|
ordered = getattr(module, 'script_order', [])
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@
|
|||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<i class="mdi mdi-file-document-outline"></i> {{ module.name|bettertitle }}
|
<i class="mdi mdi-file-document-outline"></i> {{ module.name }}
|
||||||
</h5>
|
</h5>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
{% include 'inc/sync_warning.html' with object=module %}
|
{% include 'inc/sync_warning.html' with object=module %}
|
||||||
@ -54,7 +54,7 @@
|
|||||||
{% with last_job=jobs|get_key:report.name %}
|
{% with last_job=jobs|get_key:report.name %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<a href="{% url 'extras:report' module=module.path name=report.class_name %}" id="{{ report.module }}.{{ report.class_name }}">{{ report.name }}</a>
|
<a href="{% url 'extras:report' module=module.pymodule_name name=report.class_name %}" id="{{ report.module }}.{{ report.class_name }}">{{ report.name }}</a>
|
||||||
</td>
|
</td>
|
||||||
<td>{{ report.description|markdown|placeholder }}</td>
|
<td>{{ report.description|markdown|placeholder }}</td>
|
||||||
{% if last_job %}
|
{% if last_job %}
|
||||||
|
@ -33,7 +33,7 @@
|
|||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<i class="mdi mdi-file-document-outline"></i> {{ module.name|bettertitle }}
|
<i class="mdi mdi-file-document-outline"></i> {{ module.name }}
|
||||||
</h5>
|
</h5>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
{% include 'inc/sync_warning.html' with object=module %}
|
{% include 'inc/sync_warning.html' with object=module %}
|
||||||
@ -51,7 +51,7 @@
|
|||||||
{% for script_name, script_class in module.scripts.items %}
|
{% for script_name, script_class in module.scripts.items %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<a href="{% url 'extras:script' module=module.path name=script_name %}" name="script.{{ script_name }}">{{ script_class.name }}</a>
|
<a href="{% url 'extras:script' module=module.pymodule_name name=script_name %}" name="script.{{ script_name }}">{{ script_class.name }}</a>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
{{ script_class.Meta.description|markdown|placeholder }}
|
{{ script_class.Meta.description|markdown|placeholder }}
|
||||||
|
Loading…
Reference in New Issue
Block a user