Merge branch 'maintenance' into devel
# Conflicts: # src/octoprint/server/api/system.py
This commit is contained in:
commit
38eff3fe6f
5 files changed
+123
-29
No files matched your search
@@ -384,10 +384,10 @@ octoprint.cli.commands
|
||||
OctoPrint's CLI.
|
||||
:rtype: list
|
||||
|
||||
.. _sec-plugins-hook-comm-firmware-info:
|
||||
.. _sec-plugins-hook-comm-protocol-firmware-info:
|
||||
|
||||
octoprint.comm.firmware.info
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
octoprint.comm.protocol.firmware.info
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. py:function:: firmware_info_hook(comm_instance, firmware_name, firmware_data, *args, **kwargs)
|
||||
|
||||
@@ -417,10 +417,10 @@ octoprint.comm.firmware.info
|
||||
:param str firmware_name: The name of the parsed capability
|
||||
:param dict firmware_data: All data contained in the ``M115`` report
|
||||
|
||||
.. _sec-plugins-hook-comm-firmware-capabilities:
|
||||
.. _sec-plugins-hook-comm-protocol-firmware-capabilities:
|
||||
|
||||
octoprint.comm.firmware.capabilities
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
octoprint.comm.firmware.protocol.capabilities
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. py:function:: firmware_capability_hook(comm_instance, capability, enabled, already_defined, *args, **kwargs)
|
||||
|
||||
|
||||
@@ -12,6 +12,8 @@ import threading
|
||||
from flask import request, make_response, jsonify, url_for
|
||||
from flask_babel import gettext
|
||||
|
||||
import psutil
|
||||
|
||||
from octoprint.settings import settings as s
|
||||
|
||||
from octoprint.server import NO_CONTENT
|
||||
@@ -20,6 +22,21 @@ from octoprint.server.util.flask import no_firstrun_access, get_remote_address
|
||||
from octoprint.access.permissions import Permissions
|
||||
from octoprint.logging import prefix_multilines
|
||||
|
||||
@api.route("/system/usage", methods=["GET"])
|
||||
@restricted_access
|
||||
@Permissions.SYSTEM.require(403)
|
||||
def readUsageForFolders():
|
||||
return jsonify(usage=_usageForFolders())
|
||||
|
||||
def _usageForFolders():
|
||||
data = {}
|
||||
for folder_name in s().get(['folder']).keys():
|
||||
path = s().getBaseFolder(folder_name, check_writable=False)
|
||||
if path is not None:
|
||||
usage = psutil.disk_usage(path)
|
||||
data[folder_name] = { 'free': usage.free, 'total': usage.total }
|
||||
return data
|
||||
|
||||
@api.route("/system", methods=["POST"])
|
||||
@no_firstrun_access
|
||||
@Permissions.SYSTEM.require(403)
|
||||
@@ -134,7 +151,6 @@ def executeSystemCommand(source, command):
|
||||
|
||||
return NO_CONTENT
|
||||
|
||||
|
||||
def _to_client_specs(specs):
|
||||
result = list()
|
||||
for spec in specs.values():
|
||||
|
||||
@@ -877,11 +877,15 @@ class PreemptiveCache(object):
|
||||
if cache_data is None:
|
||||
cache_data = dict()
|
||||
|
||||
if not self._validate_data(cache_data):
|
||||
self._logger.warn("Preemptive cache data was invalid, ignoring it")
|
||||
cache_data = dict()
|
||||
|
||||
return cache_data
|
||||
|
||||
def get_data(self, root):
|
||||
cache_data = self.get_all_data()
|
||||
return cache_data.get(root, dict())
|
||||
return cache_data.get(root, list())
|
||||
|
||||
def set_all_data(self, data):
|
||||
from octoprint.util import atomic_write
|
||||
@@ -924,7 +928,7 @@ class PreemptiveCache(object):
|
||||
def get_newest(entries):
|
||||
result = None
|
||||
for entry in entries:
|
||||
if "_timestamp" in entry and (result is None or ("_timestamp" in entry and result["_timestamp"] < entry["_timestamp"])):
|
||||
if "_timestamp" in entry and (result is None or ("_timestamp" in result and result["_timestamp"] < entry["_timestamp"])):
|
||||
result = entry
|
||||
return result
|
||||
|
||||
@@ -950,12 +954,32 @@ class PreemptiveCache(object):
|
||||
|
||||
return set(strip_ignored(a).items()) == set(strip_ignored(b).items())
|
||||
|
||||
def _validate_data(self, data):
|
||||
if not isinstance(data, dict):
|
||||
return False
|
||||
|
||||
for root, entries in data.items():
|
||||
if not isinstance(entries, list):
|
||||
return False
|
||||
|
||||
for entry in entries:
|
||||
if not self._validate_entry(entry):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def _validate_entry(self, entry):
|
||||
return isinstance(entry, dict) and "_timestamp" in entry and "_count" in entry
|
||||
|
||||
|
||||
def preemptively_cached(cache, data, unless=None):
|
||||
def decorator(f):
|
||||
@functools.wraps(f)
|
||||
def decorated_function(*args, **kwargs):
|
||||
cache.record(data, unless=unless)
|
||||
try:
|
||||
cache.record(data, unless=unless)
|
||||
except:
|
||||
logging.getLogger(__name__).exception(u"Error while recording preemptive cache entry: {!r}".format(data))
|
||||
return f(*args, **kwargs)
|
||||
return decorated_function
|
||||
return decorator
|
||||
@@ -967,11 +991,14 @@ def etagged(etag):
|
||||
def decorated_function(*args, **kwargs):
|
||||
rv = f(*args, **kwargs)
|
||||
if isinstance(rv, flask.Response):
|
||||
result = etag
|
||||
if callable(result):
|
||||
result = result(rv)
|
||||
if result:
|
||||
rv.set_etag(result)
|
||||
try:
|
||||
result = etag
|
||||
if callable(result):
|
||||
result = result(rv)
|
||||
if result:
|
||||
rv.set_etag(result)
|
||||
except:
|
||||
logging.getLogger(__name__).exception(u"Error while calculating the etag value for response {!r}".format(rv))
|
||||
return rv
|
||||
return decorated_function
|
||||
return decorator
|
||||
@@ -983,16 +1010,19 @@ def lastmodified(date):
|
||||
def decorated_function(*args, **kwargs):
|
||||
rv = f(*args, **kwargs)
|
||||
if not "Last-Modified" in rv.headers:
|
||||
result = date
|
||||
if callable(result):
|
||||
result = result(rv)
|
||||
try:
|
||||
result = date
|
||||
if callable(result):
|
||||
result = result(rv)
|
||||
|
||||
if not isinstance(result, basestring):
|
||||
from werkzeug.http import http_date
|
||||
result = http_date(result)
|
||||
if not isinstance(result, basestring):
|
||||
from werkzeug.http import http_date
|
||||
result = http_date(result)
|
||||
|
||||
if result:
|
||||
rv.headers["Last-Modified"] = result
|
||||
if result:
|
||||
rv.headers["Last-Modified"] = result
|
||||
except:
|
||||
logging.getLogger(__name__).exception(u"Error while calculating the lastmodified value for response {!r}".format(rv))
|
||||
return rv
|
||||
return decorated_function
|
||||
return decorator
|
||||
@@ -1002,12 +1032,15 @@ def conditional(condition, met):
|
||||
def decorator(f):
|
||||
@functools.wraps(f)
|
||||
def decorated_function(*args, **kwargs):
|
||||
if callable(condition) and condition():
|
||||
# condition has been met, return met-response
|
||||
rv = met
|
||||
if callable(met):
|
||||
rv = met()
|
||||
return rv
|
||||
try:
|
||||
if callable(condition) and condition():
|
||||
# condition has been met, return met-response
|
||||
rv = met
|
||||
if callable(met):
|
||||
rv = met()
|
||||
return rv
|
||||
except:
|
||||
logging.getLogger(__name__).exception(u"Error while evaluating conditional {!r} or met {!r}".format(condition, met))
|
||||
|
||||
# condition hasn't been met, call decorated function
|
||||
return f(*args, **kwargs)
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
# coding=utf-8
|
||||
"""
|
||||
Unit tests for ``octoprint.server.api``.
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
__author__ = "Gina Häußge <osd@foosel.net>"
|
||||
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
|
||||
__copyright__ = "Copyright (C) 2016 The OctoPrint Project - Released under terms of the AGPLv3 License"
|
||||
@@ -0,0 +1,35 @@
|
||||
# coding=utf-8
|
||||
"""
|
||||
Unit tests for ``octoprint.server.api`` system.
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
__author__ = "Gina Häußge <osd@foosel.net>"
|
||||
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
|
||||
__copyright__ = "Copyright (C) 2016 The OctoPrint Project - Released under terms of the AGPLv3 License"
|
||||
|
||||
|
||||
import mock
|
||||
import unittest
|
||||
|
||||
class GetFolderUsageTest(unittest.TestCase):
|
||||
|
||||
def test_readUsageForFolder(self):
|
||||
from octoprint.server.api.system import _usageForFolders
|
||||
|
||||
with mock.patch("psutil.disk_usage") as disk_usage_mock:
|
||||
disk_usage = mock.MagicMock()
|
||||
disk_usage.free = 50
|
||||
disk_usage.total = 512
|
||||
disk_usage_mock.return_value = disk_usage
|
||||
|
||||
with mock.patch("octoprint.server.api.system.s") as settings_mock:
|
||||
settings = mock.MagicMock()
|
||||
settings.get.return_value = dict(uploads="mocked")
|
||||
settings.getBaseFolder.return_value = "mocked"
|
||||
settings_mock.return_value = settings
|
||||
|
||||
data = _usageForFolders()
|
||||
self.assertEquals(data['uploads']['free'], 50)
|
||||
self.assertEquals(data['uploads']['total'], 512)
|
||||
Reference in new issue
Block a user