Skip to content

Commit 52d8d1f

Browse files
SW-1352 add support for pep 440 in the mr beam plugin (mrbeam#1532)
* SW-1352 Refactor the PEP version comparison class * SW-1352 Add frontend and backend methods to handle PEP440 comparisons via API calls * SW-1352 Fix the naming of the Mr Beam Plugin version constants and use the PEP440 comparison method * SW-1352 Refactor version_comparator.py and COMPARISON_OPTIONS into Util package * SW-1352 Add doc string to compare_pep440_versions method * SW-1352 Reformat line * SW-1352 Fix API command return As the simpleApiCommand is an AJAX call then it is by default asynchronous. This means that the return statements will be executed before the response is defined. The fix is thus by setting the async parameter to false. * SW-1352 Return None upon exception * SW-1352 Refactor compare_pep440_versions method * SW-1352 Add a test for compare_pep440_versions method
1 parent 73e1889 commit 52d8d1f

19 files changed

+208
-96
lines changed

octoprint_mrbeam/__init__.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import __builtin__
55
import copy
66
import json
7+
import operator
78
import os
89
import platform
910
import pprint
@@ -15,6 +16,7 @@
1516
import logging
1617
import unicodedata
1718
from subprocess import check_output
19+
import pkg_resources
1820

1921
import octoprint.plugin
2022
import requests
@@ -104,6 +106,7 @@
104106
from octoprint_mrbeam.util.uptime import get_uptime, get_uptime_human_readable
105107
from octoprint_mrbeam.util import get_thread
106108
from octoprint_mrbeam import camera
109+
from octoprint_mrbeam.util.version_comparator import compare_pep440_versions
107110

108111
# this is a easy&simple way to access the plugin and all injections everywhere within the plugin
109112
__builtin__._mrbeam_plugin_implementation = None
@@ -159,6 +162,7 @@ class MrBeamPlugin(
159162
TIME_NTP_SYNC_CHECK_INTERVAL_FAST = 10.0
160163
TIME_NTP_SYNC_CHECK_INTERVAL_SLOW = 120.0
161164

165+
162166
def __init__(self):
163167
self.mrbeam_plugin_initialized = False
164168
self._shutting_down = False
@@ -826,7 +830,7 @@ def on_ui_render(self, now, request, render_kwargs):
826830
now=now,
827831
init_ts_ms=time.time() * 1000,
828832
language=language,
829-
beamosVersionNumber=self._plugin_version,
833+
mrBeamPluginVersionNumber=self._plugin_version,
830834
beamosVersionBranch=self._branch,
831835
beamosVersionDisplayVersion=display_version_string,
832836
beamosVersionImage=self._octopi_info,
@@ -1296,6 +1300,16 @@ def generate_backlash_compenation_pattern_gcode(self, data):
12961300
res = dict(calibration_pattern=destFile, target=FileDestinations.LOCAL)
12971301
return jsonify(res)
12981302

1303+
# simpleApiCommand: compare_pep440_versions;
1304+
def handle_pep440_comparison_result(self, data):
1305+
try:
1306+
result = compare_pep440_versions(data['v1'], data['v2'], data['operator'])
1307+
return make_response(json.dumps(result), 200)
1308+
except KeyError as e:
1309+
self._logger.error("Key is missing in data: %s", e)
1310+
return make_response(json.dumps(None), 500)
1311+
1312+
12991313
# ~~ helpers
13001314

13011315
# helper method to write data to user settings
@@ -1362,7 +1376,7 @@ def calibration_wrapper(self):
13621376
locales=dict(),
13631377
supportedExtensions=[],
13641378
# beamOS version
1365-
beamosVersionNumber=self._plugin_version,
1379+
mrBeamPluginVersionNumber=self._plugin_version,
13661380
beamosVersionBranch=self._branch,
13671381
beamosVersionDisplayVersion=display_version_string,
13681382
beamosVersionImage=self._octopi_info,
@@ -1928,6 +1942,7 @@ def get_api_commands(self):
19281942
camera_stop_lens_calibration=[],
19291943
generate_calibration_markers_svg=[],
19301944
cancel_final_extraction=[],
1945+
compare_pep440_versions=[]
19311946
)
19321947

19331948
def on_api_command(self, command, data):
@@ -2055,6 +2070,8 @@ def on_api_command(self, command, data):
20552070
) # TODO move this func to other file
20562071
elif command == "cancel_final_extraction":
20572072
self.dust_manager.set_user_abort_final_extraction()
2073+
elif command == "compare_pep440_versions":
2074+
return self.handle_pep440_comparison_result(data)
20582075

20592076
return NO_CONTENT
20602077

octoprint_mrbeam/software_update_information.py

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import copy
22
import json
3-
import operator
43
import os
54
from datetime import date
65

@@ -18,6 +17,7 @@
1817
from octoprint_mrbeam.mrb_logger import mrb_logger
1918
from octoprint_mrbeam.util import dict_merge, logExceptions
2019
from octoprint_mrbeam.util.github_api import get_file_of_repo_for_tag, REPO_URL
20+
from octoprint_mrbeam.util.version_comparator import VersionComparator, COMPARISON_OPTIONS, compare_pep440_versions
2121
from util.pip_util import get_version_of_pip_module
2222

2323

@@ -478,6 +478,7 @@ def _get_current_version(input_moduleconfig, module_id, plugin):
478478
current_version = pluginInfo.version
479479
return current_version
480480

481+
481482
def _set_current_version_for_config(update_info, plugin):
482483
for module_id in update_info:
483484
module_update_info = update_info.get(module_id)
@@ -493,32 +494,6 @@ def _set_current_version_for_config(update_info, plugin):
493494
)
494495
return update_info
495496

496-
class VersionComperator:
497-
"""
498-
Version Comperator class to compare two versions with the compare method
499-
"""
500-
501-
def __init__(self, identifier, priority, compare):
502-
self.identifier = identifier
503-
self.priority = priority
504-
self.compare = compare
505-
506-
@staticmethod
507-
def get_comperator(comparision_string, comparision_options):
508-
"""
509-
returns the comperator of the given list of VersionComperator with the matching identifier
510-
511-
Args:
512-
comparision_string (str): identifier to search for
513-
comparision_options (list): list of VersionComperator objects
514-
515-
Returns:
516-
object: matching VersionComperator object
517-
"""
518-
for item in comparision_options:
519-
if item.identifier == comparision_string:
520-
return item
521-
522497

523498
def _generate_config_of_beamos(moduleconfig, beamos_version, tierversion):
524499
"""
@@ -538,23 +513,15 @@ def _generate_config_of_beamos(moduleconfig, beamos_version, tierversion):
538513

539514
config_for_beamos_versions = moduleconfig.get("beamos_version")
540515

541-
comparision_options = [
542-
VersionComperator("__eq__", 5, operator.eq),
543-
VersionComperator("__le__", 4, operator.le),
544-
VersionComperator("__lt__", 3, operator.lt),
545-
VersionComperator("__ge__", 2, operator.ge),
546-
VersionComperator("__gt__", 1, operator.gt),
547-
]
548-
549516
sorted_config_for_beamos_versions = sorted(
550517
config_for_beamos_versions.items(),
551-
key=lambda com: VersionComperator.get_comperator(
552-
com[0], comparision_options
518+
key=lambda com: VersionComparator.get_comparator(
519+
com[0], COMPARISON_OPTIONS
553520
).priority,
554521
)
555522

556523
config_for_beamos = get_config_for_version(
557-
beamos_version, sorted_config_for_beamos_versions, comparision_options
524+
beamos_version, sorted_config_for_beamos_versions
558525
)
559526

560527
if tierversion in config_for_beamos:
@@ -566,9 +533,9 @@ def _generate_config_of_beamos(moduleconfig, beamos_version, tierversion):
566533
return config_for_beamos
567534

568535

569-
def get_config_for_version(target_version, config, comparision_options):
536+
def get_config_for_version(target_version, config):
570537
config_to_be_updated = {}
571-
for comperator, version_config_items in config:
538+
for comparator, version_config_items in config:
572539
# sort the version config items by the version
573540
sorted_version_config_items = sorted(
574541
version_config_items.items(),
@@ -578,9 +545,10 @@ def get_config_for_version(target_version, config, comparision_options):
578545
)
579546

580547
for check_version, version_config in sorted_version_config_items:
581-
if VersionComperator.get_comperator(
582-
comperator, comparision_options
583-
).compare(target_version, check_version):
548+
if compare_pep440_versions(
549+
v1=target_version,
550+
v2=check_version,
551+
comparator=comparator):
584552
config_to_be_updated = dict_merge(config_to_be_updated, version_config)
585553
return config_to_be_updated
586554

octoprint_mrbeam/static/js/convert.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ $(function () {
312312
new_material.laser_model = MRBEAM_LASER_HEAD_MODEL;
313313
new_material.device_model = MRBEAM_MODEL;
314314
new_material.custom = true;
315-
new_material.v = BEAMOS_VERSION;
315+
new_material.v = MRBEAM_PLUGIN_VERSION;
316316
// Remove compatibility keys
317317
delete new_material.compatible;
318318
delete new_material.customBeforeElementContent;
@@ -330,7 +330,7 @@ $(function () {
330330
laser_model: MRBEAM_LASER_HEAD_MODEL,
331331
device_model: MRBEAM_MODEL,
332332
custom: true,
333-
v: BEAMOS_VERSION,
333+
v: MRBEAM_PLUGIN_VERSION,
334334
colors: {},
335335
};
336336
}

octoprint_mrbeam/static/js/design_store.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,10 @@ $(function () {
133133
// 0.11.78a0 --> 0.11.78 (PEP440)
134134
// 0+unknown --> 0 (No version info)
135135
let regexp = /([0-9]+(?:\.[0-9]+)*)/g;
136-
let mrbeamPluginVersion = BEAMOS_VERSION.match(regexp)[0];
137-
console.log("Design store: Mr Beam Plugin Version: " + mrbeamPluginVersion );
136+
let mrbeamPluginVersion = MRBEAM_PLUGIN_VERSION.match(regexp)[0];
137+
console.log(
138+
"Design store: Mr Beam Plugin Version: " + mrbeamPluginVersion
139+
);
138140

139141
let userData = {
140142
email: self.getEmail(),

octoprint_mrbeam/static/js/feedback_widget.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ $(function () {
6868
email: user,
6969
custom_fields: {
7070
cf_serial: MRBEAM_SERIAL,
71-
cf_software_version: BEAMOS_VERSION,
71+
cf_software_version: MRBEAM_PLUGIN_VERSION,
7272
cf_software_channel: channel,
7373
},
7474
});

octoprint_mrbeam/static/js/messages.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,19 +173,39 @@ $(function () {
173173
return false;
174174
}
175175
// version
176-
if (restrictions.version && restrictions.version.length > 0 && restrictions.version.map(function (x) {
177-
return x.trim().toUpperCase();
178-
}).includes(BEAMOS_VERSION)) {
176+
if (
177+
restrictions.version &&
178+
restrictions.version.length > 0 &&
179+
restrictions.version
180+
.map(function (x) {
181+
return x.trim().toUpperCase();
182+
})
183+
.includes(MRBEAM_PLUGIN_VERSION)
184+
) {
179185
return false;
180186
}
181187
// version_and_newer
182-
if (restrictions.version_and_newer && restrictions.version_and_newer.length > 0 &&
183-
window.compareVersions(BEAMOS_VERSION, restrictions.version_and_newer) >= 0) {
188+
if (
189+
restrictions.version_and_newer &&
190+
restrictions.version_and_newer.length > 0 &&
191+
mrbeam.comparePEP440Versions(
192+
MRBEAM_PLUGIN_VERSION,
193+
restrictions.version_and_newer,
194+
"__ge__"
195+
)
196+
) {
184197
return false;
185198
}
186199
// version_and_older
187-
if (restrictions.version_and_older && restrictions.version_and_older.length > 0 &&
188-
window.compareVersions(BEAMOS_VERSION, restrictions.version_and_older) <= 0) {
200+
if (
201+
restrictions.version_and_older &&
202+
restrictions.version_and_older.length > 0 &&
203+
mrbeam.comparePEP440Versions(
204+
MRBEAM_PLUGIN_VERSION,
205+
restrictions.version_and_older,
206+
"__le__"
207+
)
208+
) {
189209
return false;
190210
}
191211
// ts_after

octoprint_mrbeam/static/js/mother_viewmodel.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,14 +489,14 @@ $(function () {
489489
}
490490
}
491491
if (
492-
backend_version !== BEAMOS_VERSION ||
492+
backend_version !== MRBEAM_PLUGIN_VERSION ||
493493
isFirstRun !== CONFIG_FIRST_RUN ||
494494
(laserHeadModel !== undefined &&
495495
laserHeadModel !== MRBEAM_LASER_HEAD_MODEL)
496496
) {
497497
console.log(
498498
"Frontend reload check: RELOAD! (version: frontend=" +
499-
BEAMOS_VERSION +
499+
MRBEAM_PLUGIN_VERSION +
500500
", backend=" +
501501
backend_version +
502502
", isFirstRun: frontend=" +
@@ -514,7 +514,7 @@ $(function () {
514514
} else {
515515
console.log(
516516
"Frontend reload check: OK (version: " +
517-
BEAMOS_VERSION +
517+
MRBEAM_PLUGIN_VERSION +
518518
", isFirstRun: " +
519519
CONFIG_FIRST_RUN +
520520
", laserheadModel: " +

octoprint_mrbeam/static/js/mrbeam.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,28 @@ mrbeam.debugSetOfflineState = function (locale) {
126126
mrbeam.viewModels.mrbeamViewModel.debugSetOfflineState();
127127
};
128128

129+
mrbeam.comparePEP440Versions = function (v1, v2, operator) {
130+
let data = {
131+
v1: v1,
132+
v2: v2,
133+
operator: operator,
134+
};
135+
return OctoPrint.simpleApiCommand(
136+
"mrbeam",
137+
"compare_pep440_versions",
138+
data,
139+
{
140+
async: false,
141+
error: function (response) {
142+
console.error(
143+
"compare_pep440_versions call failed:",
144+
response.responseText
145+
);
146+
},
147+
}
148+
).responseJSON;
149+
};
150+
129151
mrbeam.mrb_state = undefined;
130152
mrbeam.isOnline = undefined;
131153
mrbeam.viewModels = {};

octoprint_mrbeam/static/js/ready_to_laser_viewmodel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ $(function () {
459459
"MRBEAM_ENV_SUPPORT_MODE: " + MRBEAM_ENV_SUPPORT_MODE,
460460
"BEAMOS_IMAGE: " + BEAMOS_IMAGE,
461461
"MRBEAM_LANGUAGE: " + MRBEAM_LANGUAGE,
462-
"BEAMOS_VERSION: " + BEAMOS_VERSION,
462+
"BEAMOS_VERSION: " + MRBEAM_PLUGIN_VERSION,
463463
"MRBEAM_SW_TIER: " + MRBEAM_SW_TIER,
464464
"MRBEAM_ENV: " + MRBEAM_ENV,
465465
];

octoprint_mrbeam/static/js/settings/custom_material.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ $(function () {
4545

4646
self.backupDataUrl = ko.computed(function () {
4747
var payload = {
48-
v: BEAMOS_VERSION,
48+
v: MRBEAM_PLUGIN_VERSION,
4949
ts: Date.now(),
5050
custom_materials: self.conversion.custom_materials(),
5151
};

0 commit comments

Comments
 (0)