Skip to content

Commit

Permalink
Merge branch 'pre-release'
Browse files Browse the repository at this point in the history
  • Loading branch information
cp2004 committed Jun 7, 2021
2 parents ec8a473 + 673bc61 commit 55ad381
Show file tree
Hide file tree
Showing 39 changed files with 1,227 additions and 1,462 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ indent_size = 4

[**.js]
indent_style = space
indent_size = 4
indent_size = 2
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ repos:
rev: master
hooks:
- id: prettier
args: ["--tab-width", "2"]
51 changes: 43 additions & 8 deletions octoprint_eeprom_marlin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@

import flask
import octoprint.plugin
from octoprint.access import ADMIN_GROUP, READONLY_GROUP, USER_GROUP
from octoprint.access.permissions import Permissions

from octoprint_eeprom_marlin import (
_version,
api,
backup,
data,
events,
parser,
settings,
sponsors_contributors,
Expand All @@ -36,7 +37,6 @@ class EEPROMMarlinPlugin(
octoprint.plugin.WizardPlugin,
octoprint.plugin.SettingsPlugin,
octoprint.plugin.SimpleApiPlugin,
octoprint.plugin.EventHandlerPlugin,
octoprint.plugin.BlueprintPlugin,
):
# Data models
Expand Down Expand Up @@ -67,7 +67,6 @@ def initialize(self):
self._backup_handler = backup.BackupHandler(self)
self._parser = parser.Parser(self._logger)
self._api = api.API(self)
self._event_reactor = events.EventHandler(self)

self._logger.info("All EEPROM editor modules loaded")

Expand Down Expand Up @@ -103,6 +102,7 @@ def get_template_configs(self):
def get_template_vars(self):
return {
"version": self._plugin_version,
"DATA_STRUCTURE": data.ALL_DATA_STRUCTURE,
"SPONSORS": sponsors_contributors.export_sponsors(),
"CONTRIBUTORS": sponsors_contributors.export_contributors(),
}
Expand All @@ -123,6 +123,7 @@ def on_api_get(self, request):

# BluePrint handling
@octoprint.plugin.BlueprintPlugin.route("/download/<name>")
@Permissions.PLUGIN_EEPROM_MARLIN_READ.require(403)
def download_backup(self, name):
try:
backup_data = self._backup_handler.read_backup(name)
Expand All @@ -138,10 +139,6 @@ def download_backup(self, name):
},
)

# Event handling
def on_event(self, event, payload):
self._event_reactor.on_event(event, payload)

# Websocket communication
def send_message(self, type, data):
payload = {"type": type, "data": data}
Expand Down Expand Up @@ -178,7 +175,6 @@ def comm_protocol_gcode_sending(
**kwargs
):
# https://docs.octoprint.org/en/master/plugins/hooks.html#protocol_gcodephase_hook
self._logger.debug(cmd)
if cmd == "M501" or cmd == "M503":
self._logger.info("{} detected, collecting data".format(cmd))
self.collecting_eeprom = True
Expand All @@ -204,6 +200,42 @@ def comm_protocol_gcode_received(self, comm, line, *args, **kwargs):

return line

def comm_protocol_atcommand_sending(
self, comm, phase, cmd, params, tags=None, *args, **kwargs
):
if cmd.upper() == "EEPROM_DEBUG":
# Trigger data collection manually using @EEPROM_DEBUG, for sending test files at the parser
# Useful for virtual printer testing, where the responses are not implemented.
self.collecting_eeprom = True

def get_additional_permissions(self, *args, **kwargs):
return [
{
"key": "READ",
"name": "Read EEPROM",
"description": "Can read EEPROM data",
"roles": ["read"],
"dangerous": False,
"default_groups": [ADMIN_GROUP, USER_GROUP, READONLY_GROUP],
},
{
"key": "EDIT",
"name": "Edit EEPROM",
"description": "Can edit EEPROM data and save it to the printer",
"roles": ["edit"],
"dangerous": False,
"default_groups": [ADMIN_GROUP, USER_GROUP],
},
{
"key": "RESET",
"name": "Reset EEPROM",
"description": "Can reset the firmware to factory defaults",
"roles": ["reset"],
"dangerous": True,
"default_groups": [ADMIN_GROUP],
},
]

# Update hook
def get_update_information(self):
# https://docs.octoprint.org/en/master/bundledplugins/softwareupdate.html#sec-bundledplugins-softwareupdate-hooks-check-config
Expand Down Expand Up @@ -241,6 +273,7 @@ def get_update_information(self):
"""
__plugin_author__ = "Charlie Powell"
__plugin_license__ = "AGPLv3"
__plugin_url__ = "https://github.com/cp2004/OctoPrint-EEPROM-Marlin."
__plugin_pythoncompat__ = ">=2.7,<4"
__plugin_version__ = __version__

Expand All @@ -257,4 +290,6 @@ def __plugin_load__():
"octoprint.comm.protocol.firmware.capabilities": plugin.comm_protocol_firmware_cap,
"octoprint.comm.protocol.gcode.received": plugin.comm_protocol_gcode_received,
"octoprint.comm.protocol.gcode.sending": plugin.comm_protocol_gcode_sending,
"octoprint.comm.protocol.atcommand.sending": plugin.comm_protocol_atcommand_sending,
"octoprint.access.permissions": plugin.get_additional_permissions,
}
46 changes: 39 additions & 7 deletions octoprint_eeprom_marlin/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import flask
import octoprint.util
from octoprint.access.permissions import Permissions

from octoprint_eeprom_marlin import util
from octoprint_eeprom_marlin.backup import BackupMissingError, BackupNameTakenError
Expand All @@ -24,12 +25,12 @@

class API:
def __init__(self, plugin):
self._settings = plugin._settings
self._logger = plugin._logger
self._printer = plugin._printer
self._firmware_info = plugin._firmware_info
self._eeprom_data = plugin._eeprom_data
self._backup_handler = plugin._backup_handler
self._settings = plugin._settings # noqa
self._logger = plugin._logger # noqa
self._printer = plugin._printer # noqa
self._firmware_info = plugin._firmware_info # noqa
self._eeprom_data = plugin._eeprom_data # noqa
self._backup_handler = plugin._backup_handler # noqa
self._plugin = plugin

@staticmethod
Expand All @@ -46,32 +47,63 @@ def get_api_commands():

def on_api_command(self, command, data):
if command == CMD_LOAD:
if not Permissions.PLUGIN_EEPROM_MARLIN_READ.can():
# Insufficient rights
flask.abort(403)

# Get data from printer
if self._printer.is_ready():
self._printer.commands(
"M503" if self._settings.get(["use_m503"]) else "M501"
)
elif command == CMD_SAVE:
if not Permissions.PLUGIN_EEPROM_MARLIN_EDIT.can():
# Insufficient rights
flask.abort(403)

# Send changed data to printer
self.save_eeprom_data(data.get("eeprom_data"))

elif command == CMD_BACKUP:
if not Permissions.PLUGIN_EEPROM_MARLIN_EDIT.can():
# Insufficient rights
flask.abort(403)

# Execute a backup
return self.create_backup(data.get("name"))
elif command == CMD_RESTORE:
if not Permissions.PLUGIN_EEPROM_MARLIN_EDIT.can():
# Insufficient rights
flask.abort(403)

# Restore the backup
return self.restore_backup(data.get("name"))
elif command == CMD_DELETE:
if not Permissions.PLUGIN_EEPROM_MARLIN_EDIT.can():
# Insufficient rights
flask.abort(403)

# Delete the backup
return self.delete_backup(data.get("name"))
elif command == CMD_UPLOAD_RESTORE:
if not Permissions.PLUGIN_EEPROM_MARLIN_EDIT.can():
# Insufficient rights
flask.abort(403)

# Restore backup from upload
return self.upload_restore(data.get("data"))
elif command == CMD_RESET:
if not Permissions.PLUGIN_EEPROM_MARLIN_RESET.can():
# Insufficient rights
flask.abort(403)

# Reset (M502)
return self.reset_eeprom()

def on_api_get(self, request):
if not Permissions.PLUGIN_EEPROM_MARLIN_READ.can():
flask.abort(403)

return flask.jsonify(
{
"info": self._firmware_info.to_dict(),
Expand All @@ -90,7 +122,7 @@ def save_eeprom_data(self, new_data):
new_data = new_eeprom[name]
diff = octoprint.util.dict_minimal_mergediff(data, new_data)
if diff:
commands.append(util.construct_command(new_data))
commands.append(util.construct_command(new_data, name))

if commands:
self._logger.info("Saving EEPROM data")
Expand Down
Loading

0 comments on commit 55ad381

Please sign in to comment.