Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Benoit Rousselle
bliss
Commits
7cd91525
Commit
7cd91525
authored
May 10, 2019
by
Matias Guijarro
Browse files
common/session.py: make 'load_script' a method of the Session object
parent
838267fb
Changes
1
Hide whitespace changes
Inline
Side-by-side
bliss/common/session.py
View file @
7cd91525
...
...
@@ -80,56 +80,6 @@ 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
:
"""
Bliss session.
...
...
@@ -422,6 +372,53 @@ class Session:
def
env_dict
(
self
):
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
None
:
import
__main__
as
main
...
...
@@ -455,7 +452,7 @@ class Session:
env_dict
[
"config"
]
=
self
.
config
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
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment