diff --git a/script/epic.py b/script/epic.py index 3cd2f0c..b8cbf32 100644 --- a/script/epic.py +++ b/script/epic.py @@ -670,6 +670,7 @@ def register_listener_callback(fd, dispatch_function, cleanup_function): return True +# Internal use functions @on('exit') def cleanup_listener_callbacks(args): """Called by our exit hook to close up our servers. @@ -678,3 +679,28 @@ def cleanup_listener_callbacks(args): for fd, funcs in _listening_sockets.items(): cancel_callback(fd) funcs[1]() # Run the cleanup function + + +@alias('update_python_path') +def update_python_path(args): + split_args = args.split() + old_load_path = split_args[0].split(':') + new_load_path = split_args[1].split(':') + sys_path = sys.path.copy() + + if len(split_args) > 2: + xecho("Warning: Extra arguments passed to update_load_path() are ignored!", banner=True) + + # Remove paths that don't appear in new_load_path + if old_load_path: + for path in old_load_path: + if path not in new_load_path: + sys_path.remove(path) + + # Add paths that are not in sys_path yet + for path in new_load_path: + if path not in sys_path: + sys_path.append(path) + + # Replace sys.path with our new version + sys.path[:] = sys_path diff --git a/script/python b/script/python index 53155cf..b73e63e 100644 --- a/script/python +++ b/script/python @@ -1,13 +1,15 @@ # Setup the IRC client for python # Usage: /load python -if (python(1) == 1) -{ - # Make sure we can import local modules +if (python(1) == 1) { + # Make sure we can import modules in the epic script directory python import sys - python if '.' not in sys.path: sys.path.append('.') - @ dir = before(-1 / $word(1 $loadinfo())) - python if '$dir' not in sys.path: sys.path.append\('$dir'\) + + @ script_dir = before(-1 / $word(1 $loadinfo())) + python if '$script_dir' not in sys.path: sys.path.append\('$script_dir'\) + + # Update python's sys.path with epic's $load_path + update_python_path( $load_path) # Setup some helpful aliases alias pyecho {echo $python($*)}