Skip to content

Commit

Permalink
optionally run patterns unrestricted
Browse files Browse the repository at this point in the history
  • Loading branch information
nils committed Oct 13, 2021
1 parent 330ab8e commit bb4acd0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
7 changes: 6 additions & 1 deletion ledcontrol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def main():
help='Enable sACN / E1.31 support. Default: False')
parser.add_argument('--no_timer_reset', action='store_true',
help='Do not reset the animation timer when patterns are changed. Default: False')
parser.add_argument('--run-unsafe', dest='run_restricted', action='store_false', help='Do NOT run the pattern Python code in RestrictedPython.')
parser.set_defaults(run_restricted=True)

args = parser.parse_args()

pixel_mapping = None
Expand All @@ -58,7 +61,9 @@ def main():
args.led_brightness_limit,
args.save_interval,
args.sacn,
args.no_timer_reset)
args.no_timer_reset,
args.run_restricted)

run_simple(args.host, args.port, app,
use_reloader=False,
use_debugger=True,
Expand Down
15 changes: 13 additions & 2 deletions ledcontrol/animationcontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ def __init__(self,
mapping_func,
led_color_correction,
enable_sacn,
no_timer_reset):
no_timer_reset,
run_restricted):
self.led_controller = led_controller
self.refresh_rate = refresh_rate
self.led_count = led_count
self.mapping_func = mapping_func
self._enable_sacn = enable_sacn
self._no_timer_reset = no_timer_reset
self.run_restricted = run_restricted

# Initialize prev state arrays
self.reset_prev_states()
Expand Down Expand Up @@ -152,7 +154,16 @@ def getiter(obj):
restricted_locals = {}
arg_names = ['t', 'dt', 'x', 'y', 'z', 'prev_state']

results = RestrictedPython.compile_restricted_exec(source)
results = None
if self.run_restricted:
results = RestrictedPython.compile_restricted_exec(source)
code = results.code
else:
code = compile(source, 'pattern', 'exec')
print('compiled unsafe')
results = type('CompileResult', (object,),{'code': code, 'errors': [], 'warnings': [], 'used_names': []})
restricted_globals.update(globals())

warnings = list(results.warnings)
for name in results.used_names:
if name not in restricted_globals and name not in arg_names:
Expand Down
6 changes: 4 additions & 2 deletions ledcontrol/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def create_app(led_count,
led_v_limit,
save_interval,
enable_sacn,
no_timer_reset):
no_timer_reset,
run_restricted):
app = Flask(__name__)

# Create pixel mapping function
Expand All @@ -64,7 +65,8 @@ def create_app(led_count,
mapping_func,
led_color_correction,
enable_sacn,
no_timer_reset)
no_timer_reset,
run_restricted)

patterns = dict(animpatterns.default)

Expand Down

0 comments on commit bb4acd0

Please sign in to comment.