From b39bb4c961558896e91c4314667be9432c639d08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 27 Jun 2016 12:19:28 +0200 Subject: [PATCH] Move get_connection_options as classmethod into PrinterInterface --- src/octoprint/printer/__init__.py | 57 +++++++++++++++----------- src/octoprint/server/__init__.py | 3 +- src/octoprint/server/api/connection.py | 8 ++-- src/octoprint/server/api/settings.py | 5 +-- 4 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/octoprint/printer/__init__.py b/src/octoprint/printer/__init__.py index 857e9b3e..e46227c1 100644 --- a/src/octoprint/printer/__init__.py +++ b/src/octoprint/printer/__init__.py @@ -23,31 +23,15 @@ __copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms import re -import octoprint.util.comm as comm from octoprint.settings import settings +from octoprint.util import deprecated + +@deprecated(message="get_connection_options has been replaced by PrinterInterface.get_connection_options", + includedoc="Replaced by :func:`PrinterInterface.get_connection_options`", + since="1.3.0") def get_connection_options(): - """ - Retrieves the available ports, baudrates, preferred port and baudrate for connecting to the printer. - - Returned ``dict`` has the following structure:: - - ports: - baudrates: - portPreference: - baudratePreference: - autoconnect: - - Returns: - (dict): A dictionary holding the connection options in the structure specified above - """ - return { - "ports": comm.serialList(), - "baudrates": comm.baudrateList(), - "portPreference": settings().get(["serial", "port"]), - "baudratePreference": settings().getInt(["serial", "baudrate"]), - "autoconnect": settings().getBoolean(["serial", "autoconnect"]) - } + return PrinterInterface.get_connection_options() class PrinterInterface(object): @@ -65,6 +49,31 @@ class PrinterInterface(object): valid_heater_regex = re.compile("^(tool\d+|bed)$") """Regex for valid heater identifiers.""" + @classmethod + def get_connection_options(cls): + """ + Retrieves the available ports, baudrates, preferred port and baudrate for connecting to the printer. + + Returned ``dict`` has the following structure:: + + ports: + baudrates: + portPreference: + baudratePreference: + autoconnect: + + Returns: + (dict): A dictionary holding the connection options in the structure specified above + """ + import octoprint.util.comm as comm + return { + "ports": comm.serialList(), + "baudrates": comm.baudrateList(), + "portPreference": settings().get(["serial", "port"]), + "baudratePreference": settings().getInt(["serial", "baudrate"]), + "autoconnect": settings().getBoolean(["serial", "autoconnect"]) + } + def connect(self, port=None, baudrate=None, profile=None): """ Connects to the printer, using the specified serial ``port``, ``baudrate`` and printer ``profile``. If a @@ -200,7 +209,7 @@ class PrinterInterface(object): Arguments: factor (int, float): The factor for the feed rate to send to the firmware. Percentage expressed as either an - int between 0 and 100 or a float between 0 and 1. + int between 0 and 100 or a float between 0 and 1. """ raise NotImplementedError() @@ -210,7 +219,7 @@ class PrinterInterface(object): Arguments: factor (int, float): The factor for the flow rate to send to the firmware. Percentage expressed as either an - int between 0 and 100 or a float between 0 and 1. + int between 0 and 100 or a float between 0 and 1. """ raise NotImplementedError() diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index e313d88e..733b68c0 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -53,7 +53,6 @@ user_permission = Permission(RoleNeed("user")) # only import the octoprint stuff down here, as it might depend on things defined above to be initialized already from octoprint import __version__, __branch__, __display_version__, __revision__ -from octoprint.printer import get_connection_options from octoprint.printer.profile import PrinterProfileManager from octoprint.printer.standard import Printer from octoprint.settings import settings @@ -469,7 +468,7 @@ class Server(object): if self._settings.getBoolean(["serial", "autoconnect"]): (port, baudrate) = self._settings.get(["serial", "port"]), self._settings.getInt(["serial", "baudrate"]) printer_profile = printerProfileManager.get_default() - connectionOptions = get_connection_options() + connectionOptions = printer.__class__.get_connection_options() if port in connectionOptions["ports"]: printer.connect(port=port, baudrate=baudrate, profile=printer_profile["id"] if "id" in printer_profile else "_default") diff --git a/src/octoprint/server/api/connection.py b/src/octoprint/server/api/connection.py index 40c7259f..d7c75b9f 100644 --- a/src/octoprint/server/api/connection.py +++ b/src/octoprint/server/api/connection.py @@ -8,11 +8,9 @@ __copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms from flask import request, jsonify, make_response from octoprint.settings import settings -from octoprint.printer import get_connection_options from octoprint.server import printer, printerProfileManager, NO_CONTENT from octoprint.server.api import api from octoprint.server.util.flask import restricted_access, get_json_command_from_request -import octoprint.util as util @api.route("/connection", methods=["GET"]) @@ -42,7 +40,7 @@ def connectionCommand(): return response if command == "connect": - connection_options = get_connection_options() + connection_options = printer.__class__.get_connection_options() port = None baudrate = None @@ -75,7 +73,7 @@ def connectionCommand(): return NO_CONTENT def _get_options(): - connection_options = get_connection_options() + connection_options = printer.__class__.get_connection_options() profile_options = printerProfileManager.get_all() default_profile = printerProfileManager.get_default() @@ -88,4 +86,4 @@ def _get_options(): printerProfilePreference=default_profile["id"] if "id" in default_profile else None ) - return options \ No newline at end of file + return options diff --git a/src/octoprint/server/api/settings.py b/src/octoprint/server/api/settings.py index d0f56874..a67dc2a4 100644 --- a/src/octoprint/server/api/settings.py +++ b/src/octoprint/server/api/settings.py @@ -12,9 +12,8 @@ from werkzeug.exceptions import BadRequest from octoprint.events import eventManager, Events from octoprint.settings import settings -from octoprint.printer import get_connection_options -from octoprint.server import admin_permission +from octoprint.server import admin_permission, printer from octoprint.server.api import api from octoprint.server.util.flask import restricted_access @@ -30,7 +29,7 @@ def getSettings(): s = settings() - connectionOptions = get_connection_options() + connectionOptions = printer.__class__.get_connection_options() data = { "api": {