Commit aa2fe36e authored by Matias Guijarro's avatar Matias Guijarro
Browse files

common/session.py: make 'load_script' a method of the Session object

parent fe40cbe4
......@@ -93,57 +93,7 @@ class _StringImporter(object):
return get_text_file(filename) if filename else ""
def load_script(env_dict, script_module_name, session=None):
"""
load a script name script_module_name and export all public
(not starting with _) object and function in env_dict.
just print exception but not throwing it.
Args:
env_dict (python dictionnary) where object will be exported
script_module_name the python file you want to load
session from which session name you want to load your script,
default (None) is the current.
"""
if session is None:
session = get_current()
elif isinstance(session, str):
session = static.get_config().get(session)
if session._scripts_module_path:
importer = _StringImporter(
session._scripts_module_path, session.name, in_load_script=True
)
try:
sys.meta_path.insert(0, importer)
module_name = "%s.%s.%s" % (
_StringImporter.BASE_MODULE_NAMESPACE,
session.name,
os.path.splitext(script_module_name)[0],
)
filename = importer._modules.get(module_name)
if not filename:
raise RuntimeError("Cannot find module %s" % module_name)
s_code = get_text_file(filename)
c_code = compile(s_code, filename, "exec")
globals_dict = env_dict.copy()
try:
exec(c_code, globals_dict)
except Exception:
sys.excepthook(*sys.exc_info())
finally:
sys.meta_path.remove(importer)
for k in globals_dict.keys():
if k.startswith("_"):
continue
env_dict[k] = globals_dict[k]
class Session(object):
class Session:
"""
Bliss session.
......@@ -444,6 +394,53 @@ class Session(object):
# running as a library
return self.__env_dict
def load_script(self, script_module_name, session=None):
"""
load a script name script_module_name and export all public
(not starting with _) object and function in env_dict.
just print exception but not throwing it.
Args:
env_dict (python dictionnary) where object will be exported
script_module_name the python file you want to load
"""
if session is None:
session = self
elif isinstance(session, str):
session = self.config.get(session)
if session._scripts_module_path:
importer = _StringImporter(
session._scripts_module_path, session.name, in_load_script=True
)
try:
sys.meta_path.insert(0, importer)
module_name = "%s.%s.%s" % (
_StringImporter.BASE_MODULE_NAMESPACE,
session.name,
os.path.splitext(script_module_name)[0],
)
filename = importer._modules.get(module_name)
if not filename:
raise RuntimeError("Cannot find module %s" % module_name)
s_code = get_text_file(filename)
c_code = compile(s_code, filename, "exec")
globals_dict = self.env_dict.copy()
try:
exec(c_code, globals_dict)
except Exception:
sys.excepthook(*sys.exc_info())
for k in globals_dict.keys():
if k.startswith("_"):
continue
self.env_dict[k] = globals_dict[k]
finally:
sys.meta_path.remove(importer)
def setup(self, env_dict=None, verbose=False):
if env_dict is not None:
# set a new env dict
......@@ -476,7 +473,7 @@ class Session(object):
_SESSION_IMPORTERS.add(self.name)
if not "load_script" in env_dict:
env_dict["load_script"] = functools.partial(load_script, env_dict)
env_dict["load_script"] = self.load_script
exec("from bliss.common.standard import *", env_dict)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment