Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fileURLToPath } from 'node:url';
import { fileURLToPath, URL } from 'node:url';

import { fixupConfigRules, fixupPluginRules, includeIgnoreFile } from '@eslint/compat';
import { FlatCompat } from '@eslint/eslintrc';
Expand Down Expand Up @@ -93,7 +93,21 @@ export default [
},
},
{
files: ['**/*.js'],
files: ['octoprint_octodash/**/*.js'],
languageOptions: {
globals: {
...globals.browser,
ko: 'readonly',
$: 'readonly',
OCTOPRINT_VIEWMODELS: 'readonly',
},
},
rules: {
'@typescript-eslint/no-this-alias': 'off',
},
},
{
files: ['src/**/*.js'],

languageOptions: {
globals: {
Expand Down
68 changes: 66 additions & 2 deletions octoprint_octodash/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@

import shutil

from flask import redirect, send_file, make_response, Response
from flask import make_response, send_file, request
from flask import redirect, Response
import os.path

import json

import octoprint.plugin
from octoprint.access.permissions import Permissions
from octoprint.filemanager import FileDestinations
from octoprint.util.paths import normalize
from octoprint.events import Events
Expand All @@ -26,6 +29,8 @@ class OctodashPlugin(
octoprint.plugin.SettingsPlugin,
octoprint.plugin.EventHandlerPlugin,
octoprint.plugin.BlueprintPlugin,
octoprint.plugin.WizardPlugin,
octoprint.plugin.TemplatePlugin,
):


Expand Down Expand Up @@ -173,6 +178,17 @@ def get_extension_tree(self, *args, **kwargs):
)
)

##~~ AssetPlugin mixin
def get_assets(self):
# Define your plugin's asset files to automatically include in the
# core UI here.
return {
"js": ['js/octodash.js'],
"css": [],
"less": [],
}


# ~~ BlueprintPlugin

@octoprint.plugin.BlueprintPlugin.route("/custom-styles.css")
Expand All @@ -192,14 +208,32 @@ def is_blueprint_protected(self):
def get_ui_root(self, path):
return send_file(self._get_index_path())

@octoprint.plugin.BlueprintPlugin.route("/api/migrate", methods=["POST"])
@Permissions.ADMIN.require(403)
def migrate_legacy_config(self):
#TODO: Don't blindly use the path from the request
data = request.json
if not data or "path" not in data:
return make_response(json.dumps({"error": "Path not provided"}), 400)

path = data["path"]
if not os.path.exists(path):
return make_response(json.dumps({"error": "File does not exist"}), 404)

try:
legacy_config = self._migrate_legacy_config(path)
return make_response(json.dumps({"success": True, "config": legacy_config}), 200)
except Exception as e:
return make_response(json.dumps({"error": str(e)}), 500)

def is_blueprint_csrf_protected(self):
return False

def is_blueprint_protected(self):
return False

def get_blueprint_api_prefixes(self):
return []
return ['api']

def _get_index_path(self):
"""Return the path on the filesystem to the index.html file to be used for
Expand Down Expand Up @@ -228,6 +262,14 @@ def on_ui_render(self, now, request, render_kwargs):
def get_ui_permissions(self):
return []

def is_wizard_required(self):
return True

def get_wizard_details(self):
details = {"legacyConfigs": self._find_legacy_config()}
self._logger.info(f"Returning wizard details: {details}")
return details

##~~ Softwareupdate hook

def get_update_information(self):
Expand All @@ -248,6 +290,28 @@ def get_update_information(self):
}
}

def _find_legacy_config(self):
paths = [
'~/.config/octodash/config.json',
'/home/pi/.config/octodash/config.json',
'~/Library/Application Support/octodash/config.json'
]
expanded = [os.path.expanduser(p) for p in paths]

return [{"path": p, "exists": os.path.exists(p)} for p in expanded]



def _migrate_legacy_config(self, path):
with open(path, 'r') as f:
conf = json.load(f)["config"]
# merge with the existing settings
settings = self._settings
for key in ['printer', 'filament', 'plugins', 'octodash']:
settings.set([key], conf[key])
settings.save()



__plugin_name__ = "Octodash Plugin"

Expand Down
39 changes: 39 additions & 0 deletions octoprint_octodash/static/js/octodash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
$(function () {
function OctoDashViewModel() {
var self = this;

self.configPaths = ko.observableArray([]);

self.migrated = ko.observable(false);

self.onWizardDetails = function (data) {
console.log(data);
const paths = data.octodash.details.legacyConfigs.filter(path => path.exists).map(path => path.path);
self.configPaths(paths);
};

self.migrate = path => {
$.ajax({
url: '/plugin/octodash/api/migrate',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({ path: path }),
success: function () {
self.migrated(true);
},
error: function (xhr, status, error) {
// TODO: Better handle this
console.error('Migration failed:', error);
alert('Migration failed: ' + error);
},
});
};
}

// Register the view model with OctoPrint
OCTOPRINT_VIEWMODELS.push({
construct: OctoDashViewModel,
dependencies: [], // Add dependencies here if needed
elements: ['#wizard_plugin_octodash'], // Bind to the DOM element
});
});
9 changes: 9 additions & 0 deletions octoprint_octodash/templates/octodash_wizard.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h3>OctoDash Setup</h3>

<p>Welcome to OctoDash v3! Some things are new here</p>

<p>Found config at</p>
<ul data-bind="foreach: configPaths">
<li><span data-bind="text: $data" ></span><button data-bind="click: $parent.migrate">Migrate</button></li>
</ul>
<p data-bind="if: migrated">Config migrated!</p>
Loading