Skip to content

Commit

Permalink
Upgrade restrictedpython to 4.0b2
Browse files Browse the repository at this point in the history
  • Loading branch information
fabaff committed Oct 27, 2017
1 parent 248d974 commit defd8ab
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
46 changes: 27 additions & 19 deletions homeassistant/components/python_script.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
"""Component to allow running Python scripts."""
"""
Component to allow running Python scripts.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/python_script/
"""
import datetime
import glob
import logging
Expand All @@ -7,16 +12,19 @@

import voluptuous as vol

import homeassistant.util.dt as dt_util
from homeassistant.const import SERVICE_RELOAD
from homeassistant.exceptions import HomeAssistantError
from homeassistant.loader import bind_hass
from homeassistant.util import sanitize_filename
import homeassistant.util.dt as dt_util

REQUIREMENTS = ['restrictedpython==4.0b2']

_LOGGER = logging.getLogger(__name__)

DOMAIN = 'python_script'
REQUIREMENTS = ['restrictedpython==4.0a3']

FOLDER = 'python_scripts'
_LOGGER = logging.getLogger(__name__)

CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema(dict)
Expand All @@ -43,11 +51,11 @@ class ScriptError(HomeAssistantError):


def setup(hass, config):
"""Initialize the python_script component."""
"""Initialize the Python script component."""
path = hass.config.path(FOLDER)

if not os.path.isdir(path):
_LOGGER.warning('Folder %s not found in config folder', FOLDER)
_LOGGER.warning("Folder %s not found in configuration folder", FOLDER)
return False

discover_scripts(hass)
Expand All @@ -65,7 +73,7 @@ def discover_scripts(hass):
path = hass.config.path(FOLDER)

if not os.path.isdir(path):
_LOGGER.warning('Folder %s not found in config folder', FOLDER)
_LOGGER.warning("Folder %s not found in configuration folder", FOLDER)
return False

def python_script_service_handler(call):
Expand Down Expand Up @@ -104,27 +112,27 @@ def execute(hass, filename, source, data=None):
compiled = compile_restricted_exec(source, filename=filename)

if compiled.errors:
_LOGGER.error('Error loading script %s: %s', filename,
', '.join(compiled.errors))
_LOGGER.error("Error loading script %s: %s", filename,
", ".join(compiled.errors))
return

if compiled.warnings:
_LOGGER.warning('Warning loading script %s: %s', filename,
', '.join(compiled.warnings))
_LOGGER.warning("Warning loading script %s: %s", filename,
", ".join(compiled.warnings))

def protected_getattr(obj, name, default=None):
"""Restricted method to get attributes."""
# pylint: disable=too-many-boolean-expressions
if name.startswith('async_'):
raise ScriptError('Not allowed to access async methods')
raise ScriptError("Not allowed to access async methods")
elif (obj is hass and name not in ALLOWED_HASS or
obj is hass.bus and name not in ALLOWED_EVENTBUS or
obj is hass.states and name not in ALLOWED_STATEMACHINE or
obj is hass.services and name not in ALLOWED_SERVICEREGISTRY or
obj is dt_util and name not in ALLOWED_DT_UTIL or
obj is datetime and name not in ALLOWED_DATETIME or
isinstance(obj, TimeWrapper) and name not in ALLOWED_TIME):
raise ScriptError('Not allowed to access {}.{}'.format(
raise ScriptError("Not allowed to access {}.{}".format(
obj.__class__.__name__, name))

return getattr(obj, name, default)
Expand Down Expand Up @@ -152,13 +160,13 @@ def protected_getattr(obj, name, default=None):
}

try:
_LOGGER.info('Executing %s: %s', filename, data)
_LOGGER.info("Executing %s: %s", filename, data)
# pylint: disable=exec-used
exec(compiled.code, restricted_globals, local)
except ScriptError as err:
logger.error('Error executing script: %s', err)
logger.error("Error executing script: %s", err)
except Exception as err: # pylint: disable=broad-except
logger.exception('Error executing script: %s', err)
logger.exception("Error executing script: %s", err)


class StubPrinter:
Expand All @@ -172,7 +180,7 @@ def _call_print(self, *objects, **kwargs):
"""Print text."""
# pylint: disable=no-self-use
_LOGGER.warning(
"Don't use print() inside scripts. Use logger.info() instead.")
"Don't use print() inside scripts. Use logger.info() instead")


class TimeWrapper:
Expand All @@ -186,8 +194,8 @@ def sleep(self, *args, **kwargs):
"""Sleep method that warns once."""
if not TimeWrapper.warned:
TimeWrapper.warned = True
_LOGGER.warning('Using time.sleep can reduce the performance of '
'Home Assistant')
_LOGGER.warning("Using time.sleep can reduce the performance of "
"Home Assistant")

time.sleep(*args, **kwargs)

Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ raincloudy==0.0.3
regenmaschine==0.4.1

# homeassistant.components.python_script
restrictedpython==4.0a3
restrictedpython==4.0b2

# homeassistant.components.rflink
rflink==0.0.34
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pyunifi==2.13
pywebpush==1.1.0

# homeassistant.components.python_script
restrictedpython==4.0a3
restrictedpython==4.0b2

# homeassistant.components.rflink
rflink==0.0.34
Expand Down

0 comments on commit defd8ab

Please sign in to comment.