Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed import error using submodules #65

Merged
merged 1 commit into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/outliner/modules/trace.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sys
import collections
import os


from pathlib import Path
from ..views import RunningObject
Expand Down Expand Up @@ -27,6 +29,7 @@ def __init__(self):
self.functions_flow = collections.OrderedDict()

def _trace_function(self, frame, event, arg):

self.detailed_data[frame.f_code.co_name][str(event)] += 1
self.functions_flow[frame.f_code.co_name] = None
self.detailed_data[frame.f_code.co_name]["file"] = Path(
Expand All @@ -35,22 +38,22 @@ def _trace_function(self, frame, event, arg):
self.detailed_data[frame.f_code.co_name][
"start_line"
] = frame.f_code.co_firstlineno
if str(event) == "exception":
raise ExceptionWhileTracing()

return self._trace_function

def _running_trace(self, obj) -> None:
try:
instance = obj.instance()
sys.stdout = open(os.devnull, "w")
sys.settrace(self._trace_function)
instance()
except ExceptionWhileTracing as error:
return
except Exception:
raise Exception("Erro durante execução")
sys.settrace(None)
except Exception as error:
raise (f"Erro durante execução: {str(error)}")
finally:
sys.settrace(None)
sys.stdout = sys.__stdout__
return

def trace_obj(self, running_obj: RunningObject):
if not callable(running_obj):
Expand Down
13 changes: 13 additions & 0 deletions src/outliner/views/module_obj.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
import ast
import importlib
import os

from importlib import util
from pathlib import Path
Expand Down Expand Up @@ -29,10 +30,22 @@ def __init__(
try:
importlib.import_module(submodule)
except ModuleNotFoundError:
self._try_find_folder_of_submodule(submodule)
raise ModuleNotFoundError(
f"Could not add the submodule '{submodule}' to the namespace.\n check if the module is installed."
)

def _try_find_folder_of_submodule(self, name: str):
base_path = self.module_path
submodule_as_path = name.replace(".", "/")
for dad_parent in base_path.parents:
pat = Path(str(dad_parent) + "/" + submodule_as_path + ".py")
if pat.is_file():
sys.path.append(pat)
importlib.import_module(name)

return True

def _find_imported_modules(self) -> iter:
"""
A generator with all imports the ast object contains in its namespace
Expand Down
Loading