Moving core functions to internal get/set_data

This commit is contained in:
James Barnsley
2020-08-18 21:04:35 +12:00
parent 8db8b0fcdb
commit c2e55cefae

View File

@ -38,6 +38,10 @@ class IrisCore(pykka.ThreadingActor):
"seed_tracks": [], "seed_tracks": [],
"results": [], "results": [],
} }
data = {
"commands": [],
"pinned": [],
}
ioloop = None ioloop = None
@classmethod @classmethod
@ -56,7 +60,7 @@ class IrisCore(pykka.ThreadingActor):
logger.info("Starting Iris " + Extension.version) logger.info("Starting Iris " + Extension.version)
# Load our commands from file # Load our commands from file
self.commands = self.load_from_file("commands") self.data['commands'] = self.load_from_file("commands")
## ##
# Mopidy is shutting down # Mopidy is shutting down
@ -822,49 +826,70 @@ class IrisCore(pykka.ThreadingActor):
self.queue_metadata = cleaned_queue_metadata self.queue_metadata = cleaned_queue_metadata
## ##
# Commands # Server-side data assets
# #
# These are stored locally for all users to access # These functions are used internally to store data packets locally for all users to access
## ##
def get_commands(self, *args, **kwargs): def get_data(self, name, *args, **kwargs):
callback = kwargs.get("callback", False) callback = kwargs.get("callback", False)
response = {"commands": self.commands} response = {name: self.data[name]}
if callback: if callback:
callback(response) callback(response)
else: else:
return response return response
def set_commands(self, *args, **kwargs): def set_data(self, name, *args, **kwargs):
callback = kwargs.get("callback", False) callback = kwargs.get("callback", False)
data = kwargs.get("data", {}) data = kwargs.get("data", {})
# Update our temporary variable # Update our temporary variable
self.commands = data["commands"] self.data[name] = data[name]
# Save the new commands to file storage # Save the new commands to file storage
self.save_to_file(self.commands, "commands") self.save_to_file(self.data[name], name)
self.broadcast( self.broadcast(
data={ data={
"method": "commands_changed", "method": f"{name}_changed",
"params": {"commands": self.commands}, "params": {name: self.data[name]},
} }
) )
response = {"message": "Commands saved"} response = {"message": f"Saved {name}"}
if callback: if callback:
callback(response) callback(response)
else: else:
return response return response
##
# Pinned assets
##
def get_pinned(self, *args, **kwargs):
return self.get_data('pinned', *args, **kwargs)
def set_pinned(self, *args, **kwargs):
return self.set_data('pinned', *args, **kwargs)
##
# Commands
##
def get_commands(self, *args, **kwargs):
return self.get_data('commands', *args, **kwargs)
def set_commands(self, *args, **kwargs):
return self.set_data('commands', *args, **kwargs)
async def run_command(self, *args, **kwargs): async def run_command(self, *args, **kwargs):
callback = kwargs.get("callback", False) callback = kwargs.get("callback", False)
data = kwargs.get("data", {}) data = kwargs.get("data", {})
error = False error = False
if str(data["id"]) not in self.commands: if str(data["id"]) not in self.data['commands']:
error = { error = {
"message": "Command failed", "message": "Command failed",
"description": "Could not find command by ID " "description": "Could not find command by ID "
@ -873,7 +898,7 @@ class IrisCore(pykka.ThreadingActor):
+ '"', + '"',
} }
else: else:
command = self.commands[str(data["id"])] command = self.data['commands'][str(data["id"])]
if "method" not in command: if "method" not in command:
error = { error = {
"message": "Command failed", "message": "Command failed",