Fix loading of nested modules

This commit is contained in:
jeremystretch 2023-03-29 16:05:44 -04:00
parent f4fe7a1f9e
commit 4da655c163
5 changed files with 19 additions and 29 deletions

View File

@ -8,16 +8,9 @@ import extras.models.mixins
def create_files(cls, root_name, root_path):
path_tree = [
path for path, _, _ in os.walk(root_path)
if os.path.basename(path)[0] not in ('_', '.')
]
modules = list(pkgutil.iter_modules(path_tree))
modules = list(pkgutil.iter_modules([root_path]))
filenames = []
for importer, module_name, is_pkg in modules:
if is_pkg:
continue
for importer, module_name, ispkg in modules:
try:
module = importer.find_module(module_name).load_module(module_name)
rel_path = os.path.relpath(module.__file__, root_path)

View File

@ -1,5 +1,5 @@
import os
from pkgutil import ModuleInfo, get_importer
from importlib.machinery import SourceFileLoader
__all__ = (
'PythonModuleMixin',
@ -12,16 +12,17 @@ class PythonModuleMixin:
def path(self):
return os.path.splitext(self.file_path)[0]
def get_module_info(self):
path = os.path.dirname(self.full_path)
module_name = os.path.basename(self.path)
return ModuleInfo(
module_finder=get_importer(path),
name=module_name,
ispkg=False
)
@property
def pymodule_name(self):
path, filename = os.path.split(self.full_path)
name = os.path.splitext(filename)[0]
if name == '__init__':
# File is a package
return os.path.basename(path)
else:
return name
def get_module(self):
importer, module_name, _ = self.get_module_info()
module = importer.find_module(module_name).load_module(module_name)
loader = SourceFileLoader(self.pymodule_name, self.full_path)
module = loader.load_module()
return module

View File

@ -1,7 +1,6 @@
import inspect
from functools import cached_property
from django.contrib.contenttypes.fields import GenericRelation
from django.db import models
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
return cls.full_name.split(".", maxsplit=1)[1]
try:
module = self.get_module()
except ImportError:
return {}
scripts = {}
ordered = getattr(module, 'script_order', [])

View File

@ -34,7 +34,7 @@
</a>
</div>
{% endif %}
<i class="mdi mdi-file-document-outline"></i> {{ module.name|bettertitle }}
<i class="mdi mdi-file-document-outline"></i> {{ module.name }}
</h5>
<div class="card-body">
{% include 'inc/sync_warning.html' with object=module %}
@ -54,7 +54,7 @@
{% with last_job=jobs|get_key:report.name %}
<tr>
<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>{{ report.description|markdown|placeholder }}</td>
{% if last_job %}

View File

@ -33,7 +33,7 @@
</a>
</div>
{% endif %}
<i class="mdi mdi-file-document-outline"></i> {{ module.name|bettertitle }}
<i class="mdi mdi-file-document-outline"></i> {{ module.name }}
</h5>
<div class="card-body">
{% include 'inc/sync_warning.html' with object=module %}
@ -51,7 +51,7 @@
{% for script_name, script_class in module.scripts.items %}
<tr>
<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>
{{ script_class.Meta.description|markdown|placeholder }}