Updated name mangling

This commit is contained in:
Brikwerk 2020-08-26 22:27:22 -07:00
commit a73c79438f

View file

@ -78,7 +78,7 @@ class Nxbt():
Upon creation, a multiprocessing Process is spun off to act at the Upon creation, a multiprocessing Process is spun off to act at the
manager for all emulated Nintendo Switch controllers. Messages manager for all emulated Nintendo Switch controllers. Messages
are passed into a queue which is consumed and acted upon by the are passed into a queue which is consumed and acted upon by the
__command_manager__. _command_manager.
All function calls that interact or control the emulated controllers All function calls that interact or control the emulated controllers
are simply message constructors that submit to the central task_queue. are simply message constructors that submit to the central task_queue.
@ -107,7 +107,7 @@ class Nxbt():
self.task_queue = Queue() self.task_queue = Queue()
# Sychronizes bluetooth actions # Sychronizes bluetooth actions
self.__bluetooth_lock__ = Lock() self._bluetooth_lock = Lock()
# Creates/manages shared resources # Creates/manages shared resources
self.resource_manager = Manager() self.resource_manager = Manager()
@ -119,10 +119,10 @@ class Nxbt():
# Shared, controller management properties. # Shared, controller management properties.
# The controller lock is used to sychronize use. # The controller lock is used to sychronize use.
self.__controller_lock__ = Lock() self._controller_lock = Lock()
self.__controller_counter__ = 0 self._controller_counter = 0
self.__adapters_in_use__ = {} self._adapters_in_use = {}
self.__controller_adapter_lookup__ = {} self._controller_adapter_lookup = {}
# Disable the BlueZ input plugin so we can use the # Disable the BlueZ input plugin so we can use the
# HID control/interrupt Bluetooth ports # HID control/interrupt Bluetooth ports
@ -133,7 +133,7 @@ class Nxbt():
# Starting the nxbt worker process # Starting the nxbt worker process
self.controllers = Process( self.controllers = Process(
target=self.__command_manager__, target=self._command_manager,
args=((self.task_queue), (self.manager_state))) args=((self.task_queue), (self.manager_state)))
# Disabling daemonization since we need to spawn # Disabling daemonization since we need to spawn
# other controller processes, however, this means # other controller processes, however, this means
@ -158,7 +158,7 @@ class Nxbt():
# Re-enable the BlueZ input plugin # Re-enable the BlueZ input plugin
toggle_input_plugin(True) toggle_input_plugin(True)
def __command_manager__(self, task_queue, state): def _command_manager(self, task_queue, state):
"""Used as the main multiprocessing Process that is launched """Used as the main multiprocessing Process that is launched
on startup to handle the message passing and instantiation of on startup to handle the message passing and instantiation of
the controllers. Messages are pulled out of a Queue and passed the controllers. Messages are pulled out of a Queue and passed
@ -172,7 +172,7 @@ class Nxbt():
:type state: multiprocessing.Manager().dict :type state: multiprocessing.Manager().dict
""" """
cm = __ControllerManager__(state, self.__bluetooth_lock__) cm = _ControllerManager(state, self._bluetooth_lock)
# Ensure a SystemExit exception is raised on SIGTERM # Ensure a SystemExit exception is raised on SIGTERM
# so that we can gracefully shutdown. # so that we can gracefully shutdown.
signal.signal(signal.SIGTERM, lambda sigterm_handler: sys.exit(0)) signal.signal(signal.SIGTERM, lambda sigterm_handler: sys.exit(0))
@ -449,12 +449,12 @@ class Nxbt():
if adapter_path not in self.get_available_adapters(): if adapter_path not in self.get_available_adapters():
raise ValueError("Specified adapter is unavailable") raise ValueError("Specified adapter is unavailable")
if adapter_path in self.__adapters_in_use__.keys(): if adapter_path in self._adapters_in_use.keys():
raise ValueError("Specified adapter in use") raise ValueError("Specified adapter in use")
else: else:
# Get all adapters we can use # Get all adapters we can use
usable_adapters = list( usable_adapters = list(
set(self.get_available_adapters()) - set(self.__adapters_in_use__)) set(self.get_available_adapters()) - set(self._adapters_in_use))
if len(usable_adapters) > 0: if len(usable_adapters) > 0:
# Use the first available adapter # Use the first available adapter
adapter_path = usable_adapters[0] adapter_path = usable_adapters[0]
@ -463,11 +463,11 @@ class Nxbt():
controller_index = None controller_index = None
try: try:
self.__controller_lock__.acquire() self._controller_lock.acquire()
self.task_queue.put({ self.task_queue.put({
"command": NxbtCommands.CREATE_CONTROLLER, "command": NxbtCommands.CREATE_CONTROLLER,
"arguments": { "arguments": {
"controller_index": self.__controller_counter__, "controller_index": self._controller_counter,
"controller_type": controller_type, "controller_type": controller_type,
"adapter_path": adapter_path, "adapter_path": adapter_path,
"colour_body": colour_body, "colour_body": colour_body,
@ -475,10 +475,10 @@ class Nxbt():
"reconnect_address": reconnect_address, "reconnect_address": reconnect_address,
} }
}) })
controller_index = self.__controller_counter__ controller_index = self._controller_counter
self.__controller_counter__ += 1 self._controller_counter += 1
self.__adapters_in_use__[adapter_path] = controller_index self._adapters_in_use[adapter_path] = controller_index
self.__controller_adapter_lookup__[controller_index] = adapter_path self._controller_adapter_lookup[controller_index] = adapter_path
# Block until the controller is ready # Block until the controller is ready
# This needs to be done to prevent race conditions # This needs to be done to prevent race conditions
@ -494,7 +494,7 @@ class Nxbt():
time.sleep(1/30) time.sleep(1/30)
finally: finally:
self.__controller_lock__.release() self._controller_lock.release()
return controller_index return controller_index
@ -509,12 +509,12 @@ class Nxbt():
if controller_index not in self.manager_state.keys(): if controller_index not in self.manager_state.keys():
raise ValueError("Specified controller does not exist") raise ValueError("Specified controller does not exist")
self.__controller_lock__.acquire() self._controller_lock.acquire()
try: try:
adapter_path = self.__controller_adapter_lookup__.pop(controller_index, None) adapter_path = self._controller_adapter_lookup.pop(controller_index, None)
self.__adapters_in_use__.pop(adapter_path, None) self._adapters_in_use.pop(adapter_path, None)
finally: finally:
self.__controller_lock__.release() self._controller_lock.release()
self.task_queue.put({ self.task_queue.put({
"command": NxbtCommands.REMOVE_CONTROLLER, "command": NxbtCommands.REMOVE_CONTROLLER,
@ -588,7 +588,7 @@ class Nxbt():
return self.manager_state return self.manager_state
class __ControllerManager__(): class _ControllerManager():
"""Used as the manager for all controllers. Each controller is """Used as the manager for all controllers. Each controller is
a daemon multiprocessing Process that the ControllerManager a daemon multiprocessing Process that the ControllerManager
object creates and manages. object creates and manages.
@ -603,8 +603,8 @@ class __ControllerManager__():
self.state = state self.state = state
self.lock = lock self.lock = lock
self.controller_resources = Manager() self.controller_resources = Manager()
self.__controller_queues__ = {} self._controller_queues = {}
self.__children__ = {} self._children = {}
def create_controller(self, index, controller_type, adapter_path, def create_controller(self, index, controller_type, adapter_path,
colour_body=None, colour_buttons=None, colour_body=None, colour_buttons=None,
@ -639,7 +639,7 @@ class __ControllerManager__():
controller_state["finished_macros"] = [] controller_state["finished_macros"] = []
controller_state["errors"] = False controller_state["errors"] = False
self.__controller_queues__[index] = controller_queue self._controller_queues[index] = controller_queue
self.state[index] = controller_state self.state[index] = controller_state
@ -652,12 +652,12 @@ class __ControllerManager__():
colour_buttons=colour_buttons) colour_buttons=colour_buttons)
controller = Process(target=server.run, args=(reconnect_address,)) controller = Process(target=server.run, args=(reconnect_address,))
controller.daemon = True controller.daemon = True
self.__children__[index] = controller self._children[index] = controller
controller.start() controller.start()
def input_macro(self, index, macro, macro_id): def input_macro(self, index, macro, macro_id):
self.__controller_queues__[index].put({ self._controller_queues[index].put({
"type": "macro", "type": "macro",
"macro": macro, "macro": macro,
"macro_id": macro_id "macro_id": macro_id
@ -665,26 +665,26 @@ class __ControllerManager__():
def stop_macro(self, index, macro_id): def stop_macro(self, index, macro_id):
self.__controller_queues__[index].put({ self._controller_queues[index].put({
"type": "stop", "type": "stop",
"macro_id": macro_id, "macro_id": macro_id,
}) })
def clear_macros(self, index): def clear_macros(self, index):
self.__controller_queues__[index].put({ self._controller_queues[index].put({
"type": "clear", "type": "clear",
}) })
def remove_controller(self, index): def remove_controller(self, index):
self.__children__[index].kill() self._children[index].kill()
def shutdown(self): def shutdown(self):
# Loop over children and kill all # Loop over children and kill all
for index in self.__children__.keys(): for index in self._children.keys():
child = self.__children__[index] child = self._children[index]
child.terminate() child.terminate()
self.controller_resources.shutdown() self.controller_resources.shutdown()