Commit 6e5d093d authored by Tristan Gingold's avatar Tristan Gingold

configparser: minor refactoring.

parent 4e94a10e
...@@ -34,14 +34,12 @@ import contextlib ...@@ -34,14 +34,12 @@ import contextlib
@contextlib.contextmanager @contextlib.contextmanager
def stdout_io(stdout=None): def capture_stdout():
"""This is a function used to grab the stdout from """This is a function used to grab the stdout from
the executed Manifest.py files""" the executed Manifest.py files"""
old = sys.stdout old = sys.stdout
if stdout is None: sys.stdout = StringIO()
stdout = StringIO() yield sys.stdout
sys.stdout = stdout
yield stdout
sys.stdout = old sys.stdout = old
...@@ -251,7 +249,7 @@ types:[<type 'int'>] ...@@ -251,7 +249,7 @@ types:[<type 'int'>]
"""method that acts as an 'exec' wraper to run the Python code""" """method that acts as an 'exec' wraper to run the Python code"""
options = {} options = {}
try: try:
with stdout_io() as stdout_aux: with capture_stdout() as stdout_aux:
root_path = os.getcwd() root_path = os.getcwd()
exec_path = os.path.dirname(self.config_file) exec_path = os.path.dirname(self.config_file)
os.chdir(exec_path) os.chdir(exec_path)
...@@ -260,22 +258,19 @@ types:[<type 'int'>] ...@@ -260,22 +258,19 @@ types:[<type 'int'>]
printed = stdout_aux.getvalue() printed = stdout_aux.getvalue()
if len(printed) > 0: if len(printed) > 0:
logging.info( logging.info(
"The manifest inside " + "The manifest inside {} tried to print something:".format(
self.config_file + self.config_file))
" tried to print something:")
for line in printed.split('\n'): for line in printed.split('\n'):
print("> " + line) print("> " + line)
except SyntaxError as error_syntax: except SyntaxError as error_syntax:
raise Exception("Invalid syntax in the manifest file " + raise Exception("Invalid syntax in the manifest file {}:\n {}{}".format(
self.config_file + ":\n" + str(error_syntax) + self.config_file, str(error_syntax), content))
content)
except SystemExit as error_exit: except SystemExit as error_exit:
raise Exception("Exit requested by the manifest file " + raise Exception("Exit requested by the manifest file {}:\n{}{}".format(
self.config_file + ":\n" + str(error_exit) + self.config_file, str(error_exit), content))
content)
except: except:
logging.error("Encountered unexpected error while parsing " + logging.error("Encountered unexpected error while parsing {}".format(
self.config_file) self.config_file))
logging.error(content) logging.error(content)
print(str(sys.exc_info()[0]) + ':' + str(sys.exc_info()[1])) print(str(sys.exc_info()[0]) + ':' + str(sys.exc_info()[1]))
raise raise
......
Markdown is supported
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