From 452ee8e15626dd48ef65c5a327a2ff893951a998 Mon Sep 17 00:00:00 2001 From: Peter Backx Date: Wed, 4 Oct 2017 21:17:16 +0200 Subject: [PATCH 1/8] Adding documentation for the virtual printer plugin. --- docs/development/environment.rst | 177 ++++++++++++++++++++++++++ docs/development/index.rst | 183 +-------------------------- docs/development/virtual_printer.rst | 116 +++++++++++++++++ 3 files changed, 297 insertions(+), 179 deletions(-) create mode 100644 docs/development/environment.rst create mode 100644 docs/development/virtual_printer.rst diff --git a/docs/development/environment.rst b/docs/development/environment.rst new file mode 100644 index 000000000..785e90fda --- /dev/null +++ b/docs/development/environment.rst @@ -0,0 +1,177 @@ +.. _sec-development-environment: + +Setting up a Development environment +==================================== + +.. _sec-development-environment-source: + +Obtaining, building and running the source +------------------------------------------ + +This describes the general steps in obtaining, building and running. OS specific instructions can be found +below. + + * Prerequisites: + + * `Python 2.7 `_ including ``pip``, ``setuptools`` and ``virtualenv`` + * `Git `_ + + * Checkout the OctoPrint sources from their Git repository: ``git clone https://github.com/foosel/OctoPrint.git`` + * Create a virtual environment in the checkout folder to use for installing and running OctoPrint and its + dependencies (this avoids potential versioning issues for the dependencies with system wide installed + instances): ``virtualenv venv`` + * Activate the virtual environment: ``source venv/bin/activate`` (might differ per your platform/OS) + * Update ``pip`` in the virtual environment: ``pip install --upgrade pip`` + * Install OctoPrint in `"editable" mode `_, + including its regular *and* development dependencies: ``pip install -e .[develop]`` + +.. _sec-development-environment-source-linux: + +Linux +..... + +This assumes you'll host your OctoPrint development checkout at ``~/devel/OctoPrint``. If you want to use a different +location, please substitute accordingly. + +First make sure you have python including its header files, pip, setuptools, virtualenv, git and some build requirements +installed: + + * On apt based distributions (e.g. Debian, Ubuntu, ...): + + .. code-block:: none + + sudo apt-get install python python-pip python-dev python-setuptools python-virtualenv git libyaml-dev build-essential + +.. todo:: + + Using a Linux distribution that doesn't use ``apt``? Please send a + `Pull Request `_ to get the necessary + steps into this guide! + +Then: + +.. code-block:: none + + cd ~/devel + git clone https://github.com/foosel/OctoPrint.git + cd OctoPrint + virtualenv venv + source ./venv/bin/activate + pip install --upgrade pip + pip install -e .[develop] + +You can then start OctoPrint via ``~/devel/OctoPrint/venv/bin/octoprint`` or just ``octoprint`` if you activated the virtual +environment. + +.. _sec-development-environment-windows: + +Windows +....... + +This assumes you'll host your OctoPrint development checkout at ``C:\Devel\OctoPrint``. If you want to use a different +location, please substitute accordingly. + +First download & install: + + * `Python 2.7.12 Windows x86 MSI installer `_ + + * make sure to have the installer add Python to the ``PATH`` and have it install ``pip`` too + + * `Microsoft Visual C++ Compiler for Python 2.7 `_ + * `Git for Windows `_ + +Open the Git Bash you just installed and in that: + +.. code-block:: none + + pip install virtualenv + cd /c/Devel + git clone https://github.com/foosel/OctoPrint.git + cd OctoPrint + virtualenv venv + source ./venv/bin/activate + pip install --upgrade pip + pip install -e .[develop] + +You can then start OctoPrint via ``/c/Devel/OctoPrint/venv/bin/octoprint`` or just ``octoprint`` if you activated the virtual +environment. + +.. _sec-development-environment-mac: + +Mac OS X +........ + +.. note:: + + This guide is based on the `Setup Guide for Mac OS X on OctoPrint's wiki `_. + Please report back if it works for you, due to lack of access to a Mac I cannot test it myself. Thanks. + +This assumes you'll host your OctoPrint development checkout at ``~/devel/OctoPrint``. If you want to use a different +location, please substitute accordingly. + +You'll need a user account with administrator privileges. + + * Install the latest version of Xcode suitable for your OS. For example, OS X 10.11 (El Capitan) requires Xcode 7. + * Install Xcode's command line tools: + + * ``xcode-select --install`` + * ``sudo xcodebuild`` (ensure the license was accepted) + * If you have more than one Xcode installed: ``sudo xcode-select -s /Applications/Xcode.app/Contents/Developer`` + + * Install Homebrew and use that to install Python: + + * ``ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"`` + * ``brew install python`` + + * Install `virtualenv `_ + + * ``pip install virtualenv`` + + * Install OctoPrint + + .. code-block:: none + + cd ~/devel + git clone https://github.com/foosel/OctoPrint.git + cd OctoPrint + virtualenv venv + source venv/bin/activate + pip install -e .[develop] + +You can then start OctoPrint via ``~/devel/OctoPrint/venv/bin/octoprint`` or just ``octoprint`` if you activated the virtual +environment. + +.. _sec-development-environment-ides: + +IDE Setup +--------- + +.. todo:: + + Using another IDE than the ones below? Please send a + `Pull Request `_ to get the necessary + steps into this guide! + +.. _sec-development-environment-ides-pycharm: + +PyCharm +....... + + - "File" > "Open ...", select OctoPrint checkout folder (e.g. ``~/devel/OctoPrint`` or ``C:\Devel\OctoPrint``) + - "File" > "Settings ..." > "Project: OctoPrint" > "Project Interpreter" > "Add local ...", select OctoPrint venv + folder (e.g. ``~/devel/OctoPrint/venv`` or ``C:\Devel\OctoPrint\venv``) + - Right click "src" in project tree, mark as source folder + - Add Run/Debug Configuration, select "Python": + + * Name: OctoPrint server + * Script: path to ``run`` in the OctoPrint checkout folder (e.g. ``~/devel/OctoPrint/run`` or ``C:\Devel\OctoPrint\run``) + * Script parameters: ``--debug`` + * Project: ``OctoPrint`` + * Python interpreter: the ``venv`` local virtual environment + * Working directory: the OctoPrint checkout folder (e.g. ``~/devel/OctoPrint`` or ``C:\Devel\OctoPrint``) + +.. note:: + + Make sure you are running a PyCharm version of 2016.1 or later, or manually fix + `a debugger bug contained in earlier versions `_ or plugin management + will not work in your developer install when running OctoPrint from PyCharm in debug mode. diff --git a/docs/development/index.rst b/docs/development/index.rst index 7d24a843e..8b3038592 100644 --- a/docs/development/index.rst +++ b/docs/development/index.rst @@ -4,183 +4,8 @@ Development ########### -.. contents:: - :local: +.. toctree:: + :maxdepth: 3 -.. _sec-development-environment: - -Setting up a Development environment -==================================== - -.. _sec-development-environment-source: - -Obtaining, building and running the source ------------------------------------------- - -This describes the general steps in obtaining, building and running. OS specific instructions can be found -below. - - * Prerequisites: - - * `Python 2.7 `_ including ``pip``, ``setuptools`` and ``virtualenv`` - * `Git `_ - - * Checkout the OctoPrint sources from their Git repository: ``git clone https://github.com/foosel/OctoPrint.git`` - * Create a virtual environment in the checkout folder to use for installing and running OctoPrint and its - dependencies (this avoids potential versioning issues for the dependencies with system wide installed - instances): ``virtualenv venv`` - * Activate the virtual environment: ``source venv/bin/activate`` (might differ per your platform/OS) - * Update ``pip`` in the virtual environment: ``pip install --upgrade pip`` - * Install OctoPrint in `"editable" mode `_, - including its regular *and* development dependencies: ``pip install -e .[develop]`` - -.. _sec-development-environment-source-linux: - -Linux -..... - -This assumes you'll host your OctoPrint development checkout at ``~/devel/OctoPrint``. If you want to use a different -location, please substitute accordingly. - -First make sure you have python including its header files, pip, setuptools, virtualenv, git and some build requirements -installed: - - * On apt based distributions (e.g. Debian, Ubuntu, ...): - - .. code-block:: none - - sudo apt-get install python python-pip python-dev python-setuptools python-virtualenv git libyaml-dev build-essential - -.. todo:: - - Using a Linux distribution that doesn't use ``apt``? Please send a - `Pull Request `_ to get the necessary - steps into this guide! - -Then: - -.. code-block:: none - - cd ~/devel - git clone https://github.com/foosel/OctoPrint.git - cd OctoPrint - virtualenv venv - source ./venv/bin/activate - pip install --upgrade pip - pip install -e .[develop] - -You can then start OctoPrint via ``~/devel/OctoPrint/venv/bin/octoprint`` or just ``octoprint`` if you activated the virtual -environment. - -.. _sec-development-environment-windows: - -Windows -....... - -This assumes you'll host your OctoPrint development checkout at ``C:\Devel\OctoPrint``. If you want to use a different -location, please substitute accordingly. - -First download & install: - - * `Python 2.7.12 Windows x86 MSI installer `_ - - * make sure to have the installer add Python to the ``PATH`` and have it install ``pip`` too - - * `Microsoft Visual C++ Compiler for Python 2.7 `_ - * `Git for Windows `_ - -Open the Git Bash you just installed and in that: - -.. code-block:: none - - pip install virtualenv - cd /c/Devel - git clone https://github.com/foosel/OctoPrint.git - cd OctoPrint - virtualenv venv - source ./venv/bin/activate - pip install --upgrade pip - pip install -e .[develop] - -You can then start OctoPrint via ``/c/Devel/OctoPrint/venv/bin/octoprint`` or just ``octoprint`` if you activated the virtual -environment. - -.. _sec-development-environment-mac: - -Mac OS X -........ - -.. note:: - - This guide is based on the `Setup Guide for Mac OS X on OctoPrint's wiki `_. - Please report back if it works for you, due to lack of access to a Mac I cannot test it myself. Thanks. - -This assumes you'll host your OctoPrint development checkout at ``~/devel/OctoPrint``. If you want to use a different -location, please substitute accordingly. - -You'll need a user account with administrator privileges. - - * Install the latest version of Xcode suitable for your OS. For example, OS X 10.11 (El Capitan) requires Xcode 7. - * Install Xcode's command line tools: - - * ``xcode-select --install`` - * ``sudo xcodebuild`` (ensure the license was accepted) - * If you have more than one Xcode installed: ``sudo xcode-select -s /Applications/Xcode.app/Contents/Developer`` - - * Install Homebrew and use that to install Python: - - * ``ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"`` - * ``brew install python`` - - * Install `virtualenv `_ - - * ``pip install virtualenv`` - - * Install OctoPrint - - .. code-block:: none - - cd ~/devel - git clone https://github.com/foosel/OctoPrint.git - cd OctoPrint - virtualenv venv - source venv/bin/activate - pip install -e .[develop] - -You can then start OctoPrint via ``~/devel/OctoPrint/venv/bin/octoprint`` or just ``octoprint`` if you activated the virtual -environment. - -.. _sec-development-environment-ides: - -IDE Setup ---------- - -.. todo:: - - Using another IDE than the ones below? Please send a - `Pull Request `_ to get the necessary - steps into this guide! - -.. _sec-development-environment-ides-pycharm: - -PyCharm -....... - - - "File" > "Open ...", select OctoPrint checkout folder (e.g. ``~/devel/OctoPrint`` or ``C:\Devel\OctoPrint``) - - "File" > "Settings ..." > "Project: OctoPrint" > "Project Interpreter" > "Add local ...", select OctoPrint venv - folder (e.g. ``~/devel/OctoPrint/venv`` or ``C:\Devel\OctoPrint\venv``) - - Right click "src" in project tree, mark as source folder - - Add Run/Debug Configuration, select "Python": - - * Name: OctoPrint server - * Script: path to ``run`` in the OctoPrint checkout folder (e.g. ``~/devel/OctoPrint/run`` or ``C:\Devel\OctoPrint\run``) - * Script parameters: ``--debug`` - * Project: ``OctoPrint`` - * Python interpreter: the ``venv`` local virtual environment - * Working directory: the OctoPrint checkout folder (e.g. ``~/devel/OctoPrint`` or ``C:\Devel\OctoPrint``) - -.. note:: - - Make sure you are running a PyCharm version of 2016.1 or later, or manually fix - `a debugger bug contained in earlier versions `_ or plugin management - will not work in your developer install when running OctoPrint from PyCharm in debug mode. + environment.rst + virtual_printer.rst diff --git a/docs/development/virtual_printer.rst b/docs/development/virtual_printer.rst new file mode 100644 index 000000000..479cdc198 --- /dev/null +++ b/docs/development/virtual_printer.rst @@ -0,0 +1,116 @@ +.. _sec-development-virtual-printer: + +Setting up the virtual printer for debugging +============================================ + +OctoPrint includes, by default, a virtual printer plugin. This plugin allows you to debug OctoPrint's serial +communication without connecting to an actual printer. Furthermore, it is possible to create certain edge conditions +that may be hard to reproduce with a real printer. + +.. _sec-development-virtual-printer-enable: + +Enabling the virtual printer +---------------------------- + +The virtual printer is enabled by editing OctoPrint's config.yaml file. Details on the configuration file can +be found in the full :ref:`config.yaml documentation `. + +The steps to take are as follows: + +* Find config.yaml in the OctoPrint settings folder. Usually in ``~/.octoprint`` on Linux, in ``%APPDATA%/OctoPrint`` on Windows and in ``~/Library/Application Support/OctoPrint`` on MacOS. +* Add or extend the ``devel`` section with: + +.. code-block:: yaml + + devel: + virtualPrinter: + enabled: true + +* Restart OctoPrint. +* In the connection panel, a new option will appear in the Serial Port dropdown labeled ``VIRTUAL``. +* Select this option and click ``connect``. +* The virtual printer is now active. + +.. _sec-development-virtual-printer-config: + +Virtual printer configuration options +------------------------------------- + +The config.yaml file has many configuration options for the virtual printer that allow you to fine-tune its behavior. + +Please see the relevant :ref:`config.yaml section ` for the full details. + +.. _sec-development-virtual-printer-log: + +Log file +-------- + +Once activated, the virtual printer will log all serial communication in the ``plugin_virtual_printer_serial.log`` file +that can be found in the OctoPrint settings folder. + +.. _sec-development-virtual-printer-debug: + +Debug commands +-------------- + +You can simulate certain conditions and communications through the terminal tab in OctoPrint's interface. + +All commands start with ``!!DEBUG:`` and are followed by the command you want to execute. For instance, sending +``!!DEBUG:action_disconnect`` will disconnect the printer. Sending ``!!DEBUG`` without command will show a help +message with all the available commands. + +Action Triggers +............... + +``action_pause`` +Sends a "// action:pause" action trigger to the host. + +``action_resume`` +Sends a "// action:resume" action trigger to the host. + +``action_disconnect`` +Sends a "// action:disconnect" action trigger to the host. + +``action_custom [ ]`` +Sends a custom "// action: " action trigger to the host. + +Communication Errors +.................... + +``dont_answer`` +Will not acknowledge the next command. + +``go_awol`` +Will completely stop replying. + +``trigger_resend_lineno`` +Triggers a resend error with a line number mismatch + +``trigger_resend_checksum`` +Triggers a resend error with a checksum mismatch + +``drop_connection`` +Drops the serial connection + +``prepare_ok `` +Will cause to be enqueued for use, will be used instead of actual "ok" + +Reply Timing / Sleeping +....................... +``sleep `` +Sleep s + +``sleep_after `` +Sleeps s after each execution of + +``sleep_after_next `` +Sleeps s after execution of next + +Misc +.... + +``help`` +Show the available commands. + +``send `` +Sends back From 178131945ea379327bd2785926b341060451ca60 Mon Sep 17 00:00:00 2001 From: ntoff Date: Fri, 6 Oct 2017 09:32:49 +1000 Subject: [PATCH 2/8] Add possible print distruption warning to shutdown dialog --- docs/api/system.rst | 12 ++++++------ .../plugins/pluginmanager/static/js/pluginmanager.js | 2 +- src/octoprint/server/api/system.py | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/api/system.rst b/docs/api/system.rst index 78ea128ff..439373a2b 100644 --- a/docs/api/system.rst +++ b/docs/api/system.rst @@ -39,7 +39,7 @@ List all registered system commands "action": "shutdown", "name": "Shutdown", "command": "sudo shutdown -h now", - "confirm": "You are about to shutdown the system.", + "confirm": "You are about to shutdown the system.

This action may disrupt any ongoing print jobs (depending on your printer's controller and general setup that might also apply to prints run directly from your printer's internal storage).", "async": true, "ignore": true, "source": "core", @@ -49,7 +49,7 @@ List all registered system commands "action": "reboot", "name": "Reboot", "command": "sudo reboot", - "confirm": "You are about to reboot the system.", + "confirm": "You are about to reboot the system.

This action may disrupt any ongoing print jobs (depending on your printer's controller and general setup that might also apply to prints run directly from your printer's internal storage).", "async": true, "ignore": true, "source": "core", @@ -59,7 +59,7 @@ List all registered system commands "action": "restart", "name": "Restart OctoPrint", "command": "sudo service octoprint restart", - "confirm": "You are about to restart the OctoPrint server.", + "confirm": "You are about to restart the OctoPrint server.

This action may disrupt any ongoing print jobs (depending on your printer's controller and general setup that might also apply to prints run directly from your printer's internal storage).", "async": true, "ignore": true, "source": "core", @@ -100,7 +100,7 @@ List all registered system commands for a source "action": "shutdown", "name": "Shutdown", "command": "sudo shutdown -h now", - "confirm": "You are about to shutdown the system.", + "confirm": "You are about to shutdown the system.

This action may disrupt any ongoing print jobs (depending on your printer's controller and general setup that might also apply to prints run directly from your printer's internal storage).", "async": true, "ignore": true, "source": "core", @@ -110,7 +110,7 @@ List all registered system commands for a source "action": "reboot", "name": "Reboot", "command": "sudo reboot", - "confirm": "You are about to reboot the system.", + "confirm": "You are about to reboot the system.

This action may disrupt any ongoing print jobs (depending on your printer's controller and general setup that might also apply to prints run directly from your printer's internal storage).", "async": true, "ignore": true, "source": "core", @@ -120,7 +120,7 @@ List all registered system commands for a source "action": "restart", "name": "Restart OctoPrint", "command": "sudo service octoprint restart", - "confirm": "You are about to restart the OctoPrint server.", + "confirm": "You are about to restart the OctoPrint server.

This action may disrupt any ongoing print jobs (depending on your printer's controller and general setup that might also apply to prints run directly from your printer's internal storage).", "async": true, "ignore": true, "source": "core", diff --git a/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js b/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js index 9663cc619..6984644fe 100644 --- a/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js +++ b/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js @@ -731,7 +731,7 @@ $(function() { if (restartClicked) return; restartClicked = true; showConfirmationDialog({ - message: gettext("This will restart your OctoPrint server."), + message: gettext("This will restart your OctoPrint server.

This action may disrupt any ongoing print jobs (depending on your printer's controller and general setup that might also apply to prints run directly from your printer's internal storage)."), onproceed: function() { OctoPrint.system.executeCommand("core", "restart") .done(function() { diff --git a/src/octoprint/server/api/system.py b/src/octoprint/server/api/system.py index e17bdb781..8be2fec25 100644 --- a/src/octoprint/server/api/system.py +++ b/src/octoprint/server/api/system.py @@ -167,19 +167,19 @@ def _get_core_command_specs(): shutdown=dict( command=s().get(["server", "commands", "systemShutdownCommand"]), name=gettext("Shutdown system"), - confirm=gettext("You are about to shutdown the system.")), + confirm=gettext("You are about to shutdown the system.

This action may disrupt any ongoing print jobs (depending on your printer's controller and general setup that might also apply to prints run directly from your printer's internal storage).")), reboot=dict( command=s().get(["server", "commands", "systemRestartCommand"]), name=gettext("Reboot system"), - confirm=gettext("You are about to reboot the system.")), + confirm=gettext("You are about to reboot the system.

This action may disrupt any ongoing print jobs (depending on your printer's controller and general setup that might also apply to prints run directly from your printer's internal storage).")), restart=dict( command=s().get(["server", "commands", "serverRestartCommand"]), name=gettext("Restart OctoPrint"), - confirm=gettext("You are about to restart the OctoPrint server.")), + confirm=gettext("You are about to restart the OctoPrint server.

This action may disrupt any ongoing print jobs (depending on your printer's controller and general setup that might also apply to prints run directly from your printer's internal storage).")), restart_safe=dict( command=s().get(["server", "commands", "serverRestartCommand"]), name=gettext("Restart OctoPrint in safe mode"), - confirm=gettext("You are about to restart the OctoPrint server in safe mode."), + confirm=gettext("You are about to restart the OctoPrint server in safe mode.

This action may disrupt any ongoing print jobs (depending on your printer's controller and general setup that might also apply to prints run directly from your printer's internal storage)."), before=enable_safe_mode) ) From b89b6970c6d545b096b28042cbc10a6b715564b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 9 Oct 2017 15:48:31 +0200 Subject: [PATCH 3/8] Fix temperature plot init if it's not the 1st tab If the temperature graph was not on the first tab, the plot would be initialized without being visible, causing some sizing issues and alignment issues with the Y axis ticks. Forcing the plot to only get initialized once visible solves these problems. Fixes #2147 --- .../static/js/app/viewmodels/temperature.js | 189 ++++++++++-------- 1 file changed, 103 insertions(+), 86 deletions(-) diff --git a/src/octoprint/static/js/app/viewmodels/temperature.js b/src/octoprint/static/js/app/viewmodels/temperature.js index 9c4b6ef64..6e70610bc 100644 --- a/src/octoprint/static/js/app/viewmodels/temperature.js +++ b/src/octoprint/static/js/app/viewmodels/temperature.js @@ -142,7 +142,7 @@ $(function() { if (!self._printerProfileInitialized) { self._triggerBacklog(); } - self.updatePlot(false); + self.updatePlot(); }; self.settingsViewModel.printerProfiles.currentProfileData.subscribe(function() { self._printerProfileUpdated(); @@ -221,12 +221,12 @@ $(function() { if (!CONFIG_TEMPERATURE_GRAPH) return; self.temperatures = self._processTemperatureData(serverTime, data, self.temperatures); - self.updatePlot(false); + self.updatePlot(); }; self._processTemperatureHistoryData = function(serverTime, data) { self.temperatures = self._processTemperatureData(serverTime, data); - self.updatePlot(false); + self.updatePlot(); }; self._processOffsetData = function(data) { @@ -285,100 +285,112 @@ $(function() { return result; }; - self.updatePlot = function(force) { - force = force == undefined ? true : force; - + self.updatePlot = function() { var graph = $("#temperature-graph"); - if (graph.length) { - var data = []; - var heaterOptions = self.heaterOptions(); - if (!heaterOptions) return; + if (!graph.length) return; // no graph + if (!self.plot) return; // plot not yet initialized - var maxTemps = [310/1.1]; + var plotInfo = self._getPlotInfo(); + if (plotInfo === undefined) return; - var showFahrenheit = self._shallShowFahrenheit(); + // update the data + self.plot.setData(plotInfo.data); + self.plot.getAxes().yaxis.max = Math.max(Math.max.apply(null, plotInfo.max) * 1.1, 310); + self.updateLegend(self._replaceLegendLabel); + self.plot.draw(); + }; - _.each(_.keys(heaterOptions), function(type) { - if (type == "bed" && !self.hasBed()) { - return; + self._initializePlot = function(force) { + var graph = $("#temperature-graph"); + if (!graph.length) return; // no graph + if (self.plot && !force) return; // already initialized + + var plotInfo = self._getPlotInfo(); + if (plotInfo === undefined) return; + + // we don't have a plot yet, we need to set stuff up + var options = { + yaxis: { + min: 0, + max: Math.max(Math.max.apply(null, plotInfo.max) * 1.1, 310), + ticks: 10 + }, + xaxis: { + mode: "time", + minTickSize: [2, "minute"], + tickFormatter: function(val, axis) { + if (val === undefined || val === 0) + return ""; // we don't want to display the minutes since the epoch if not connected yet ;) + + // current time in milliseconds in UTC + var timestampUtc = Date.now(); + + // calculate difference in milliseconds + var diff = timestampUtc - val; + + // convert to minutes + var diffInMins = Math.round(diff / (60 * 1000)); + if (diffInMins === 0) + return gettext("just now"); + else + return "- " + diffInMins + " " + gettext("min"); } + }, + legend: { + position: "sw", + noColumns: 2, + backgroundOpacity: 0 + } + }; - var actuals = []; - var targets = []; + if (!OctoPrint.coreui.browser.mobile) { + options["crosshair"] = { mode: "x" }; + options["grid"] = { hoverable: true, autoHighlight: false }; + } - if (self.temperatures[type]) { - actuals = self.temperatures[type].actual; - targets = self.temperatures[type].target; - } + self.plot = $.plot(graph, plotInfo.data, options); + }; - var actualTemp = actuals && actuals.length ? formatTemperature(actuals[actuals.length - 1][1], showFahrenheit) : "-"; - var targetTemp = targets && targets.length ? formatTemperature(targets[targets.length - 1][1], showFahrenheit) : "-"; + self._getPlotInfo = function() { + var data = []; + var heaterOptions = self.heaterOptions(); + if (!heaterOptions) return undefined; - data.push({ - label: gettext("Actual") + " " + heaterOptions[type].name + ": " + actualTemp, - color: heaterOptions[type].color, - data: actuals - }); - data.push({ - label: gettext("Target") + " " + heaterOptions[type].name + ": " + targetTemp, - color: pusher.color(heaterOptions[type].color).tint(0.5).html(), - data: targets - }); + var maxTemps = [310/1.1]; - maxTemps.push(self.getMaxTemp(actuals, targets)); + var showFahrenheit = self._shallShowFahrenheit(); + + _.each(_.keys(heaterOptions), function(type) { + if (type === "bed" && !self.hasBed()) { + return; + } + + var actuals = []; + var targets = []; + + if (self.temperatures[type]) { + actuals = self.temperatures[type].actual; + targets = self.temperatures[type].target; + } + + var actualTemp = actuals && actuals.length ? formatTemperature(actuals[actuals.length - 1][1], showFahrenheit) : "-"; + var targetTemp = targets && targets.length ? formatTemperature(targets[targets.length - 1][1], showFahrenheit) : "-"; + + data.push({ + label: gettext("Actual") + " " + heaterOptions[type].name + ": " + actualTemp, + color: heaterOptions[type].color, + data: actuals + }); + data.push({ + label: gettext("Target") + " " + heaterOptions[type].name + ": " + targetTemp, + color: pusher.color(heaterOptions[type].color).tint(0.5).html(), + data: targets }); - if (!self.plot || force) { - // we don't have a plot yet, we need to set stuff up - var options = { - yaxis: { - min: 0, - max: Math.max(Math.max.apply(null, maxTemps) * 1.1, 310), - ticks: 10 - }, - xaxis: { - mode: "time", - minTickSize: [2, "minute"], - tickFormatter: function(val, axis) { - if (val == undefined || val == 0) - return ""; // we don't want to display the minutes since the epoch if not connected yet ;) + maxTemps.push(self.getMaxTemp(actuals, targets)); + }); - // current time in milliseconds in UTC - var timestampUtc = Date.now(); - - // calculate difference in milliseconds - var diff = timestampUtc - val; - - // convert to minutes - var diffInMins = Math.round(diff / (60 * 1000)); - if (diffInMins == 0) - return gettext("just now"); - else - return "- " + diffInMins + " " + gettext("min"); - } - }, - legend: { - position: "sw", - noColumns: 2, - backgroundOpacity: 0 - } - }; - - if (!OctoPrint.coreui.browser.mobile) { - options["crosshair"] = { mode: "x" }; - options["grid"] = { hoverable: true, autoHighlight: false }; - } - - self.plot = $.plot(graph, data, options); - - } else { - // graph already active, let's just update the data - self.plot.setData(data); - self.plot.getAxes().yaxis.max = Math.max(Math.max.apply(null, maxTemps) * 1.1, 310); - self.updateLegend(self._replaceLegendLabel); - self.plot.draw(); - } - } + return {max: maxTemps, data: data}; }; self.updateLegend = function(replaceLegendLabel) { @@ -704,10 +716,15 @@ $(function() { }; self.onAfterTabChange = function(current, previous) { - if (current != "#temp") { + if (current !== "#temp") { return; } - self.updatePlot(false); + + if (!self.plot) { + self._initializePlot(); + } else { + self.updatePlot(); + } }; self.onStartup = function() { From abfcc6e5aa0c252741b261163826e6209aa286bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 10 Oct 2017 12:41:47 +0200 Subject: [PATCH 4/8] Fix deletion of unrendered timelapses If the filename contained a [] pair, the file would not match the glob pattern used for selecting for deletion. Backporting the glob.escape function from Python 3.4 and using it here should fix that. We only use our own ported version if glob.escape doesn't exist - that should reduce redundant code once we become Python 3 compatible. --- src/octoprint/timelapse.py | 4 +++- src/octoprint/util/__init__.py | 23 ++++++++++++++++++ tests/util/test_file_helpers.py | 42 +++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/octoprint/timelapse.py b/src/octoprint/timelapse.py index 58de381f9..a3cff03ee 100644 --- a/src/octoprint/timelapse.py +++ b/src/octoprint/timelapse.py @@ -139,12 +139,14 @@ def get_unrendered_timelapses(): def delete_unrendered_timelapse(name): global _cleanup_lock + + pattern = "{}*.jpg".format(util.glob_escape(name)) basedir = settings().getBaseFolder("timelapse_tmp") with _cleanup_lock: for entry in scandir(basedir): try: - if fnmatch.fnmatch(entry.name, "{}*.jpg".format(name)): + if fnmatch.fnmatch(entry.name, pattern): os.remove(entry.path) except: if logging.getLogger(__name__).isEnabledFor(logging.DEBUG): diff --git a/src/octoprint/util/__init__.py b/src/octoprint/util/__init__.py index 862347c70..cc8a4a91e 100644 --- a/src/octoprint/util/__init__.py +++ b/src/octoprint/util/__init__.py @@ -866,6 +866,29 @@ def is_hidden_path(path): return False +try: + from glob import escape + glob_escape = escape +except ImportError: + # no glob.escape - we need to implement our own + _glob_escape_check = re.compile("([*?[])") + _glob_escape_check_bytes = re.compile(b"([*?[])") + + def glob_escape(pathname): + """ + Ported from Python 3.4 + + See https://github.com/python/cpython/commit/fd32fffa5ada8b8be8a65bd51b001d989f99a3d3 + """ + + drive, pathname = os.path.splitdrive(pathname) + if isinstance(pathname, bytes): + pathname = _glob_escape_check_bytes.sub(br"[\1]", pathname) + else: + pathname = _glob_escape_check.sub(r"[\1]", pathname) + return drive + pathname + + class RepeatedTimer(threading.Thread): """ This class represents an action that should be run repeatedly in an interval. It is similar to python's diff --git a/tests/util/test_file_helpers.py b/tests/util/test_file_helpers.py index 2f0add32c..36bf525da 100644 --- a/tests/util/test_file_helpers.py +++ b/tests/util/test_file_helpers.py @@ -358,3 +358,45 @@ class IsHiddenPathTest(unittest.TestCase): def test_is_hidden_path(self, path_id, expected): path = getattr(self, path_id) if path_id is not None else None self.assertEqual(octoprint.util.is_hidden_path(path), expected) + + +try: + from glob import escape + +except ImportError: + # no glob.escape - tests for our ported implementation + + @ddt.ddt + class GlobEscapeTest(unittest.TestCase): + """ + Ported from Python 3.4 + + See https://github.com/python/cpython/commit/fd32fffa5ada8b8be8a65bd51b001d989f99a3d3 + """ + + @ddt.data( + ("abc", "abc"), + ("[", "[[]"), + ("?", "[?]"), + ("*", "[*]"), + ("[[_/*?*/_]]", "[[][[]_/[*][?][*]/_]]"), + ("/[[_/*?*/_]]/", "/[[][[]_/[*][?][*]/_]]/") + ) + @ddt.unpack + def test_glob_escape(self, text, expected): + actual = octoprint.util.glob_escape(text) + self.assertEqual(actual, expected) + + @ddt.data( + ("?:?", "?:[?]"), + ("*:*", "*:[*]"), + (r"\\?\c:\?", r"\\?\c:\[?]"), + (r"\\*\*\*", r"\\*\*\[*]"), + ("//?/c:/?", "//?/c:/[?]"), + ("//*/*/*", "//*/*/[*]") + ) + @ddt.unpack + @unittest.skipUnless(sys.platform == "win32", "Win32 specific test") + def test_glob_escape_windows(self, text, expected): + actual = octoprint.util.glob_escape(text) + self.assertEqual(actual, expected) From d1fdbd5080a19fe0bc9d273466bd5e65532644a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 10 Oct 2017 12:51:43 +0200 Subject: [PATCH 5/8] More lenient check for unrendered timelapses Only delete automatically if modification AND creation date are older than the calculated cutoff. --- src/octoprint/timelapse.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/octoprint/timelapse.py b/src/octoprint/timelapse.py index a3cff03ee..7d203c4c5 100644 --- a/src/octoprint/timelapse.py +++ b/src/octoprint/timelapse.py @@ -194,7 +194,8 @@ def delete_old_unrendered_timelapses(): if prefix in prefixes_to_clean: continue - if entry.stat().st_mtime < cutoff: + # delete if both creation and modification time are older than the cutoff + if max(entry.stat().st_ctime, entry.stat().st_mtime) < cutoff: prefixes_to_clean.append(prefix) except: if logging.getLogger(__name__).isEnabledFor(logging.DEBUG): From 4093995ba09024882acd2d0ebf05d5068f044fbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 11 Oct 2017 16:19:37 +0200 Subject: [PATCH 6/8] STK500v2: Retries for reading from serial This would normally not fly at all but we give it a pass here since we only use that module for auto detection anyhow (and probably not for much longer since that approach is simply too error prone). Adapted from https://github.com/foosel/OctoPrint/issues/2138#issuecomment-334117910 by @dtynan. Solves #2138 until I come up with something better --- src/octoprint/util/avr_isp/stk500v2.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/octoprint/util/avr_isp/stk500v2.py b/src/octoprint/util/avr_isp/stk500v2.py index 0fe59fe0b..41561c4ab 100644 --- a/src/octoprint/util/avr_isp/stk500v2.py +++ b/src/octoprint/util/avr_isp/stk500v2.py @@ -111,8 +111,11 @@ class Stk500v2(ispBase.IspBase): state = 'Start' checksum = 0 while True: - s = self.serial.read() - if len(s) < 1: + for _ in range(0, 5): + s = self.serial.read() + if len(s) > 0: + break + else: raise ispBase.IspError("Timeout") b = struct.unpack(">B", s)[0] checksum ^= b From 054bbd8a9b9598e148a3600b6fe1323d9c74c56a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 11 Oct 2017 16:20:37 +0200 Subject: [PATCH 7/8] comm: no need to fetch potential ports again --- src/octoprint/util/comm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 23c34a1f0..f1ee6be04 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -1867,7 +1867,7 @@ class MachineCom(object): elif len(potentials) > 1: programmer = stk500v2.Stk500v2() - for p in serialList(): + for p in potentials: serial_obj = None try: From 42018ae29a27d73ac006025f2bc53aaf780ebf68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 11 Oct 2017 16:27:51 +0200 Subject: [PATCH 8/8] starts_with => startswith Now that was embarrassing. Fixes #2151 --- src/octoprint/plugins/pluginmanager/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/octoprint/plugins/pluginmanager/__init__.py b/src/octoprint/plugins/pluginmanager/__init__.py index 35f38447a..a6806ac7f 100644 --- a/src/octoprint/plugins/pluginmanager/__init__.py +++ b/src/octoprint/plugins/pluginmanager/__init__.py @@ -306,7 +306,7 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin, def command_install(self, url=None, path=None, force=False, reinstall=None, dependency_links=False): if url is not None: - if not any(map(lambda scheme: url.starts_with(scheme + "://"), self.URL_SCHEMES)): + if not any(map(lambda scheme: url.startswith(scheme + "://"), self.URL_SCHEMES)): raise ValueError("Invalid URL to pip install from") source = url