Improve Python script (#9417)
* add datetime and support for unpacksequence add datetime to builtin and support for unpacksequence a,b = (1,2) for a,b in [(1,2),(3,4)] * add test for python_script * fix test * restore previous test restore previous tests, removed by mistake sorry... * fix test * Update test_python_script.py * fix travis * fix test * Update test_python_script.py * Add files via upload * fix travis...
This commit is contained in:
parent
371d1cc872
commit
20f3e3dcf9
2 changed files with 30 additions and 2 deletions
|
@ -2,6 +2,7 @@
|
|||
import glob
|
||||
import os
|
||||
import logging
|
||||
import datetime
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
|
@ -63,7 +64,8 @@ def execute_script(hass, name, data=None):
|
|||
def execute(hass, filename, source, data=None):
|
||||
"""Execute Python source."""
|
||||
from RestrictedPython import compile_restricted_exec
|
||||
from RestrictedPython.Guards import safe_builtins, full_write_guard
|
||||
from RestrictedPython.Guards import safe_builtins, full_write_guard, \
|
||||
guarded_iter_unpack_sequence, guarded_unpack_sequence
|
||||
from RestrictedPython.Utilities import utility_builtins
|
||||
from RestrictedPython.Eval import default_guarded_getitem
|
||||
|
||||
|
@ -94,13 +96,16 @@ def execute(hass, filename, source, data=None):
|
|||
|
||||
builtins = safe_builtins.copy()
|
||||
builtins.update(utility_builtins)
|
||||
builtins['datetime'] = datetime
|
||||
restricted_globals = {
|
||||
'__builtins__': builtins,
|
||||
'_print_': StubPrinter,
|
||||
'_getattr_': protected_getattr,
|
||||
'_write_': full_write_guard,
|
||||
'_getiter_': iter,
|
||||
'_getitem_': default_guarded_getitem
|
||||
'_getitem_': default_guarded_getitem,
|
||||
'_iter_unpack_sequence_': guarded_iter_unpack_sequence,
|
||||
'_unpack_sequence_': guarded_unpack_sequence,
|
||||
}
|
||||
logger = logging.getLogger('{}.{}'.format(__name__, filename))
|
||||
local = {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue