Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
csst-pipeline
csst_common
Commits
96d94e4e
Commit
96d94e4e
authored
Jun 26, 2023
by
BO ZHANG
🏀
Browse files
add module_wrapper
parent
7da09866
Changes
1
Hide whitespace changes
Inline
Side-by-side
csst_common/wrapper.py
View file @
96d94e4e
...
@@ -3,6 +3,8 @@ import time
...
@@ -3,6 +3,8 @@ import time
import
traceback
import
traceback
from
collections
import
namedtuple
from
collections
import
namedtuple
from
astropy
import
time
from
csst_common.data_manager
import
CsstMsDataManager
from
csst_common.data_manager
import
CsstMsDataManager
from
csst_common.file_recorder
import
FileRecorder
from
csst_common.file_recorder
import
FileRecorder
from
csst_common.status
import
CsstStatus
from
csst_common.status
import
CsstStatus
...
@@ -54,6 +56,49 @@ def l1ppl_module(func):
...
@@ -54,6 +56,49 @@ def l1ppl_module(func):
return
call_l1ppl_module
return
call_l1ppl_module
def
module_wrapper
(
func
):
@
functools
.
wraps
(
func
)
def
call_module
(
logger
,
tstamp
=
None
,
*
args
,
**
kwargs
):
logger
.
info
(
f
"====================================================="
)
t_start
=
time
.
time
()
logger
.
info
(
f
"Starting Module: **
{
func
.
__name__
}
**"
)
# logger.info(f"Additional arguments: {args} {kwargs}")
try
:
# if the module finishes
status
,
fr
,
*
output
=
func
(
*
args
,
**
kwargs
)
except
Exception
as
e
:
# if the module raises error
exc_info
=
traceback
.
format_exc
()
# traceback info
logger
.
error
(
f
"Error occurs!
\n
{
exc_info
}
"
)
status
=
CsstStatus
.
ERROR
# default status if exceptions occur
fr
=
FileRecorder
()
# default FileRecorder if exceptions occur
output
=
[
exc_info
,
]
# default output if exceptions occur
finally
:
t_stop
=
time
.
time
()
t_cost
=
t_stop
-
t_start
if
status
in
[
CsstStatus
.
PERFECT
,
CsstStatus
.
WARNING
,
CsstStatus
.
ERROR
]:
# perfect / warning / error
logger
.
info
(
f
"Module finished with status:
{
status
}
- time cost:
{
t_cost
:
.
1
f
}
sec"
)
else
:
# invalid status
logger
.
error
(
f
"Invalid status:
{
status
}
"
)
# record exception traceback info
logger
.
info
(
f
"ModuleResult:
\n
"
f
" - name:
{
func
.
__name__
}
\n
"
f
" - status:
{
status
}
\n
"
f
" - additional output:
{
output
}
\n
"
f
" - fr: [
{
fr
.
summary
}
]
\n
{
fr
}
\n
"
)
# write time stamp
if
tstamp
is
not
None
:
with
open
(
tstamp
,
"a+"
)
as
f
:
f
.
write
(
f
"
{
time
.
Time
.
now
().
isot
}
+08:00
\n
"
)
return
ModuleResult
(
func
.
__name__
,
t_cost
,
status
,
fr
,
output
)
return
call_module
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
@
l1ppl_module
@
l1ppl_module
def
call_add
(
dm
,
a
,
b
):
def
call_add
(
dm
,
a
,
b
):
...
@@ -62,6 +107,7 @@ if __name__ == "__main__":
...
@@ -62,6 +107,7 @@ if __name__ == "__main__":
else
:
else
:
return
ModuleResult
(
CsstStatus
.
ERROR
,
None
,
(
a
,
b
))
return
ModuleResult
(
CsstStatus
.
ERROR
,
None
,
(
a
,
b
))
dm
=
CsstMsDataManager
()
dm
=
CsstMsDataManager
()
print
(
call_add
(
dm
,
1.
,
2.
))
print
(
call_add
(
dm
,
1.
,
2.
))
print
(
call_add
(
dm
,
1.
,
None
))
print
(
call_add
(
dm
,
1.
,
None
))
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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