Skip to content

Commit

Permalink
Export only marked functions to context
Browse files Browse the repository at this point in the history
  • Loading branch information
MrNaif2018 committed Jul 7, 2021
1 parent 1864ce4 commit 1bb9c97
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
5 changes: 3 additions & 2 deletions bitccl/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
from bitccl.exceptions import CompilationRestrictedError
from bitccl.utils import no_imports_importer

# TODO: currently it exports more than needed
functions = {name: func for (name, func) in inspect.getmembers(functions_module, inspect.isfunction)}
functions = {
name: func for (name, func) in inspect.getmembers(functions_module, inspect.isfunction) if hasattr(func, "bitccl")
}
events = {
name: event
for (name, event) in inspect.getmembers(events_module, inspect.isclass)
Expand Down
8 changes: 7 additions & 1 deletion bitccl/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,27 @@

from bitccl.state import config as config_ctx
from bitccl.state import event_listeners
from bitccl.utils import call_universal, prepare_event, silent_debug, time_limit
from bitccl.utils import call_universal, function, prepare_event, silent_debug, time_limit

PASSWORD_ALPHABET = string.ascii_uppercase + string.ascii_lowercase + string.digits + "-_"
SECURE_PASSWORD_LENGTH = 43


@function
def add_event_listener(event, func):
event = prepare_event(event)
event_listeners[event].append(func)


@function
def on(event):
def wrapper(func):
add_event_listener(event, func)

return wrapper


@function
def dispatch_event(event, *args, **kwargs):
event = prepare_event(event)
for listener in event_listeners[event]:
Expand All @@ -36,6 +39,7 @@ def dispatch_event(event, *args, **kwargs):
pass


@function
def template(name, data={}):
try:
with open(f"templates/{name}.j2") as f:
Expand All @@ -46,6 +50,7 @@ def template(name, data={}):
return ""


@function
def send_email(to, subject, text): # pragma: no cover
try:
config = config_ctx.get()
Expand All @@ -66,5 +71,6 @@ def send_email(to, subject, text): # pragma: no cover
return False


@function
def password(length=SECURE_PASSWORD_LENGTH):
return "".join(secrets.choice(PASSWORD_ALPHABET) for _ in range(length))
6 changes: 6 additions & 0 deletions bitccl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,9 @@ def call_universal(func, *args, **kwargs):
if inspect.isawaitable(result):
result = asyncio.get_event_loop().run_until_complete(result)
return result


def function(f):
"""Use function decorator to mark functions to be exported to BitCCL execution context"""
f.bitccl = True
return f
1 change: 1 addition & 0 deletions tests/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def test_disable_imports():

@pytest.mark.parametrize("func", functions.keys())
def test_disallowed_imports(func):
assert hasattr(functions[func], "bitccl")
assert "Imports disabled" in run(f"from functions import {func}")


Expand Down

0 comments on commit 1bb9c97

Please sign in to comment.