Skip to content

Commit

Permalink
Improvement: Use Keyring instead plaintext token save (#74)
Browse files Browse the repository at this point in the history
* Improvement: Use Keyring instead plaintext token save
* Improvemnet: Open SM 2.0 single extruder discovery
  • Loading branch information
parachvte authored May 12, 2023
1 parent ca7b266 commit 7c684ba
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 41 deletions.
23 changes: 22 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
SNAPMAKER_2_A150 = dict(
name="Snapmaker A150",
model="Snapmaker 2 Model A150",
header_version=0, # default is 1
)

SNAPMAKER_2_A250 = dict(
name="Snapmaker A250",
model="Snapmaker 2 Model A250",
header_version=0,
)

SNAPMAKER_2_A350 = dict(
name="Snapmaker A350",
model="Snapmaker 2 Model A350",
header_version=0,
)

SNAPMAKER_2_A150_DUAL_EXTRUDER = dict(
name="Snapmaker 2.0 A150 Dual Extruder",
model="Snapmaker 2 Model A150",
header_version=0, # default is 1
header_version=0,
)

SNAPMAKER_2_A250_DUAL_EXTRUDER = dict(
Expand Down Expand Up @@ -31,6 +49,9 @@
SNAPMAKER_DISCOVER_MACHINES = [
SNAPMAKER_J1,
SNAPMAKER_ARTISAN,
SNAPMAKER_2_A150,
SNAPMAKER_2_A250,
SNAPMAKER_2_A350,
SNAPMAKER_2_A150_DUAL_EXTRUDER,
SNAPMAKER_2_A250_DUAL_EXTRUDER,
SNAPMAKER_2_A350_DUAL_EXTRUDER,
Expand Down
55 changes: 20 additions & 35 deletions network_plugin/HTTPTokenManager.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import json
from typing import Union

from UM.Application import Application
from UM.Logger import Logger
from cura.OAuth2.Models import BaseModel
from cura.OAuth2.KeyringAttribute import KeyringAttribute


class HTTPTokenManager:
class HTTPTokenManager(BaseModel):
"""Manager for HTTP tokens."""

PREFERENCE_KEY_TOKEN = "SnapmakerPlugin/tokens"
Expand All @@ -21,42 +21,27 @@ def getInstance(cls) -> "HTTPTokenManager":
return cls.instance

def __init__(self) -> None:
self._tokens = {} # type: Dict[str, str]

self._dirty = False
self._attributes = {}

def loadTokens(self) -> None:
preferences = Application.getInstance().getPreferences()
preferences.addPreference(self.PREFERENCE_KEY_TOKEN, "{}")

try:
self._tokens = json.loads(
preferences.getValue(self.PREFERENCE_KEY_TOKEN)
)
except ValueError:
# failed to parse JSON
self._tokens = {}
self._dirty = True

if not isinstance(self._tokens, dict):
self._tokens = {}
self._dirty = True

def saveTokens(self) -> None:
if self._dirty:
try:
preferences = Application.getInstance().getPreferences()
preferences.setValue(self.PREFERENCE_KEY_TOKEN, json.dumps(self._tokens))
except ValueError:
pass

self._dirty = False
# Remove the preference we used on earlier version (<= 0.9.0)
preferences.removePreference(self.PREFERENCE_KEY_TOKEN)

def getToken(self, key: str) -> Union[str, None]:
return self._tokens.get(key, None)
attribute = getattr(self, key, None) # type: Union[KeyringAttribute, None]
return attribute.__get__(self, type(self)) if attribute else None

def setToken(self, key: str, token: str) -> None:
saved_token = self._tokens.get(key, "")
if not saved_token or token != saved_token:
self._tokens[key] = token
self._dirty = True
attribute = getattr(self, key, None) # type: Union[KeyringAttribute, None]
if not attribute:
# Note that we use dynamic descriptors instead static
attribute = KeyringAttribute()
attribute.__set_name__(self, key)
setattr(self, key, attribute)

# It's a bit tricky to call __get__ and __set__ directly, cuz we
# can not call descriptor by acccess instance.attribute.
saved_token = attribute.__get__(self, type(self))
if saved_token != token:
attribute.__set__(self, token)
11 changes: 7 additions & 4 deletions network_plugin/SnapmakerOutputDevicePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
is_machine_discover_supported,
SNAPMAKER_J1,
SNAPMAKER_ARTISAN,
SNAPMAKER_2_A150,
SNAPMAKER_2_A250,
SNAPMAKER_2_A350,
SNAPMAKER_2_A150_DUAL_EXTRUDER,
SNAPMAKER_2_A250_DUAL_EXTRUDER,
SNAPMAKER_2_A350_DUAL_EXTRUDER,
Expand Down Expand Up @@ -74,9 +77,6 @@ def __discover(self) -> None:
"Discovering networked printer... (interface: %s)", sock.address.toString())
sock.discover(b"discover")

# save tokens
self._http_token_manager.saveTokens()

# TODO: remove output devices that not reply message for a period of time

def __onData(self, msg: str) -> None:
Expand Down Expand Up @@ -120,7 +120,10 @@ def __onData(self, msg: str) -> None:
# Artisan
device = SnapmakerArtisanOutputDevice(device_id, address, properties)
self.getOutputDeviceManager().addOutputDevice(device)
elif model in [SNAPMAKER_2_A150_DUAL_EXTRUDER['model'],
elif model in [SNAPMAKER_2_A150['model'],
SNAPMAKER_2_A250['model'],
SNAPMAKER_2_A350['model'],
SNAPMAKER_2_A150_DUAL_EXTRUDER['model'],
SNAPMAKER_2_A250_DUAL_EXTRUDER['model'],
SNAPMAKER_2_A350_DUAL_EXTRUDER['model'], ]:
# Snapmaker 2.0 Dual Extruder
Expand Down
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Snapmaker Plugin",
"author": "Snapmaker",
"version": "0.9.1",
"version": "0.9.2",
"description": "Provides support for Snapmaker J1 & Snapmaker Artisan.",
"api": 8,
"supported_sdk_versions": ["8.0.0", "8.1.0", "8.2.0", "8.3.0"],
Expand Down

0 comments on commit 7c684ba

Please sign in to comment.