Skip to content

add configuration option "allow_all_imports" #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion custom_components/pyscript/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import logging
import os

import voluptuous as vol

from homeassistant.const import (
EVENT_HOMEASSISTANT_STARTED,
EVENT_HOMEASSISTANT_STOP,
EVENT_STATE_CHANGED,
SERVICE_RELOAD,
)
import homeassistant.helpers.config_validation as cv
from homeassistant.loader import bind_hass

from .const import DOMAIN, FOLDER, LOGGER_PATH, SERVICE_JUPYTER_KERNEL_START
Expand All @@ -24,10 +27,20 @@

_LOGGER = logging.getLogger(LOGGER_PATH)

CONF_ALLOW_ALL_IMPORTS = "allow_all_imports"

CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{vol.Optional(CONF_ALLOW_ALL_IMPORTS, default=False): cv.boolean}
),
},
extra=vol.ALLOW_EXTRA,
)


async def async_setup(hass, config):
"""Initialize the pyscript component."""

handler_func = Handler(hass)
event_func = Event(hass)
trig_time_func = TrigTime(hass, handler_func)
Expand All @@ -44,6 +57,9 @@ def check_isdir(path):
_LOGGER.error("Folder %s not found in configuration folder", FOLDER)
return False

hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN]["allow_all_imports"] = config[DOMAIN].get(CONF_ALLOW_ALL_IMPORTS)

await compile_scripts( # pylint: disable=unused-variable
hass,
event_func=event_func,
Expand Down
11 changes: 8 additions & 3 deletions custom_components/pyscript/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import logging
import sys

from .const import ALLOWED_IMPORTS, LOGGER_PATH
from .const import ALLOWED_IMPORTS, DOMAIN, LOGGER_PATH

_LOGGER = logging.getLogger(LOGGER_PATH + ".eval")

Expand Down Expand Up @@ -361,6 +361,11 @@ def __init__(
self.logger_handlers = set()
self.logger = None
self.set_logger_name(logger_name if logger_name is not None else self.name)
self.allow_all_imports = (
global_ctx.hass.data[DOMAIN]["allow_all_imports"]
if global_ctx.hass is not None
else False
)

async def ast_not_implemented(self, arg, *args):
"""Raise NotImplementedError exception for unimplemented AST types."""
Expand Down Expand Up @@ -399,7 +404,7 @@ async def ast_module(self, arg):
async def ast_import(self, arg):
"""Execute import."""
for imp in arg.names:
if imp.name not in ALLOWED_IMPORTS:
if not self.allow_all_imports and imp.name not in ALLOWED_IMPORTS:
raise ModuleNotFoundError(f"import of {imp.name} not allowed")
if imp.name not in sys.modules:
mod = importlib.import_module(imp.name)
Expand All @@ -409,7 +414,7 @@ async def ast_import(self, arg):

async def ast_importfrom(self, arg):
"""Execute from X import Y."""
if arg.module not in ALLOWED_IMPORTS:
if not self.allow_all_imports and arg.module not in ALLOWED_IMPORTS:
raise ModuleNotFoundError(f"import from {arg.module} not allowed")
if arg.module not in sys.modules:
mod = importlib.import_module(arg.module)
Expand Down