def check_for_update(self): if "check" in flask.request.values: check_targets = map(lambda x: x.strip(), flask.request.values["check"].split(",")) else: check_targets = None force = flask.request.values.get("force", "false") in octoprint.settings.valid_boolean_trues def view(): self._environment_ready.wait(timeout=30.0) try: information, update_available, update_possible = self.get_current_versions(check_targets=check_targets, force=force) return flask.jsonify(dict(status="inProgress" if self._update_in_progress else "updatePossible" if update_available and update_possible and self._environment_supported else "updateAvailable" if update_available else "current", information=information, timestamp=self._version_cache_timestamp, environment=dict(supported=self._environment_supported, versions=[dict(name=gettext("Python"), current=self._environment_versions.get("python", "unknown"), minimum=MINIMUM_PYTHON), dict(name=gettext("pip"), current=self._environment_versions.get("pip", "unknown"), minimum=MINIMUM_PIP), dict(name=gettext("setuptools"), current=self._environment_versions.get("setuptools", "unknown"), minimum=MINIMUM_SETUPTOOLS)]))) except exceptions.ConfigurationInvalid as e: return flask.make_response("Update not properly configured, can't proceed: %s" % e.message, 500) def etag(): checks = self._get_configured_checks() targets = check_targets if targets is None: targets = checks.keys() import hashlib hash = hashlib.sha1() targets = sorted(targets) for target in targets: current_hash = self._get_check_hash(checks.get(target, dict())) if target in self._version_cache and not force: data = self._version_cache[target] hash.update(current_hash) hash.update(str(data["timestamp"] + self._version_cache_ttl >= time.time() > data["timestamp"])) hash.update(repr(data["information"])) hash.update(str(data["available"])) hash.update(str(data["possible"])) hash.update(str(data.get("online", None))) hash.update(",".join(targets)) hash.update(str(self._environment_supported)) hash.update(str(self._version_cache_timestamp)) hash.update(str(self._connectivity_checker.online)) hash.update(str(self._update_in_progress)) hash.update(self.DATA_FORMAT_VERSION) return hash.hexdigest() def condition(): return check_etag(etag()) return with_revalidation_checking(etag_factory=lambda *args, **kwargs: etag(), condition=lambda *args, **kwargs: condition(), unless=lambda: force)(view)()
def get_channel_data(self): from octoprint.settings import valid_boolean_trues result = dict() force = flask.request.values.get("force", "false") in valid_boolean_trues enabled = self._settings.get(["enabled_channels"]) forced = self._settings.get(["forced_channels"]) channel_configs = self._get_channel_configs(force=force) def view(): channel_data = self._fetch_all_channels(force=force) for key, data in list(channel_configs.items()): read_until = channel_configs[key].get("read_until", None) entries = sorted(self._to_internal_feed(channel_data.get( key, []), read_until=read_until), key=lambda e: e["published"], reverse=True) unread = len([e for e in entries if not e["read"]]) if read_until is None and entries: last = entries[0]["published"] self._mark_read_until(key, last) result[key] = dict(channel=data["name"], url=data["url"], priority=data.get("priority", 2), enabled=key in enabled or key in forced, forced=key in forced, data=entries, unread=unread) return flask.jsonify(result) def etag(): import hashlib hash = hashlib.sha1() hash.update(repr(sorted(enabled))) hash.update(repr(sorted(forced))) for channel in sorted(channel_configs.keys()): hash.update(repr(channel_configs[channel])) channel_data = self._get_channel_data_from_cache( channel, channel_configs[channel]) hash.update(repr(channel_data)) return hash.hexdigest() def condition(): return check_etag(etag()) return with_revalidation_checking( etag_factory=lambda *args, **kwargs: etag(), condition=lambda *args, **kwargs: condition(), unless=lambda: force)(view)()
def getQueue(self): from octoprint.settings import valid_boolean_trues force = request.values.get("force", "false") in valid_boolean_trues if force: self._queue_dict = None def view(): queue_dict = self._getQueueDict() if queue_dict is not None: return jsonify(queue=queue_dict) else: return jsonify({}) def etag(): # The etag here is used to check the modifications table, which tracks changes to the # queue connection = sqlite3.connect(self._queue_db_path) cursor = connection.cursor() cursor.execute( "SELECT changed_at FROM modifications ORDER BY changed_at DESC LIMIT 1" ) last_modified = cursor.fetchone() connection.close() import hashlib hash = hashlib.sha1() hash.update(str(last_modified).encode("UTF-8")) return hash.hexdigest() def condition(): return check_etag(etag()) return with_revalidation_checking( etag_factory=lambda *args, **kwargs: etag(), condition=lambda *args, **kwargs: condition(), unless=lambda: force)(view)()
def get_channel_data(self): from octoprint.settings import valid_boolean_trues result = [] force = flask.request.values.get("force", "false") in valid_boolean_trues enabled = self._settings.get(["enabled_channels"]) forced = self._settings.get(["forced_channels"]) channel_configs = self._get_channel_configs(force=force) def view(): channel_data = self._fetch_all_channels(force=force) for key, data in channel_configs.items(): read_until = channel_configs[key].get("read_until", None) entries = sorted(self._to_internal_feed(channel_data.get(key, []), read_until=read_until), key=lambda e: e["published"], reverse=True) unread = count(filter(lambda e: not e["read"], entries)) if read_until is None and entries: last = entries[0]["published"] self._mark_read_until(key, last) result.append(dict(key=key, channel=data["name"], url=data["url"], description=data.get("description", ""), priority=data.get("priority", 2), enabled=key in enabled or key in forced, forced=key in forced, data=entries, unread=unread)) return flask.jsonify(channels=result) def etag(): import hashlib hash = hashlib.sha1() def hash_update(value): hash.update(value.encode("utf-8")) hash_update(repr(sorted(enabled))) hash_update(repr(sorted(forced))) hash_update(OCTOPRINT_VERSION) for channel in sorted(channel_configs.keys()): hash_update(repr(channel_configs[channel])) channel_data = self._get_channel_data_from_cache(channel, channel_configs[channel]) hash_update(repr(channel_data)) return hash.hexdigest() # noinspection PyShadowingNames def condition(lm, etag): return check_etag(etag) return with_revalidation_checking(etag_factory=lambda *args, **kwargs: etag(), condition=lambda lm, etag: condition(lm, etag), unless=lambda: force)(view)()
def get_channel_data(self): from octoprint.settings import valid_boolean_trues result = [] force = flask.request.values.get("force", "false") in valid_boolean_trues enabled = self._settings.get(["enabled_channels"]) forced = self._settings.get(["forced_channels"]) channel_configs = self._get_channel_configs(force=force) def view(): channel_data = self._fetch_all_channels(force=force) for key, data in channel_configs.items(): read_until = channel_configs[key].get("read_until", None) entries = sorted(self._to_internal_feed(channel_data.get(key, []), read_until=read_until), key=lambda e: e["published"], reverse=True) unread = len(filter(lambda e: not e["read"], entries)) if read_until is None and entries: last = entries[0]["published"] self._mark_read_until(key, last) result.append(dict(key=key, channel=data["name"], url=data["url"], description=data.get("description", ""), priority=data.get("priority", 2), enabled=key in enabled or key in forced, forced=key in forced, data=entries, unread=unread)) return flask.jsonify(channels=result) def etag(): import hashlib hash = hashlib.sha1() hash.update(repr(sorted(enabled))) hash.update(repr(sorted(forced))) hash.update(OCTOPRINT_VERSION) for channel in sorted(channel_configs.keys()): hash.update(repr(channel_configs[channel])) channel_data = self._get_channel_data_from_cache(channel, channel_configs[channel]) hash.update(repr(channel_data)) return hash.hexdigest() # noinspection PyShadowingNames def condition(lm, etag): return check_etag(etag) return with_revalidation_checking(etag_factory=lambda *args, **kwargs: etag(), condition=lambda lm, etag: condition(lm, etag), unless=lambda: force)(view)()
def on_api_get(self, request): if not Permissions.PLUGIN_PLUGINMANAGER_MANAGE.can(): return make_response("Insufficient rights", 403) from octoprint.server import safe_mode refresh_repository = request.values.get("refresh_repository", "false") in valid_boolean_trues if refresh_repository: self._repository_available = self._refresh_repository() refresh_notices = request.values.get("refresh_notices", "false") in valid_boolean_trues if refresh_notices: self._notices_available = self._refresh_notices() def view(): return jsonify(plugins=self._get_plugins(), repository=dict( available=self._repository_available, plugins=self._repository_plugins ), os=get_os(), octoprint=get_octoprint_version_string(), pip=dict( available=self._pip_caller.available, version=self._pip_caller.version_string, install_dir=self._pip_caller.install_dir, use_user=self._pip_caller.use_user, virtual_env=self._pip_caller.virtual_env, additional_args=self._settings.get(["pip_args"]), python=sys.executable ), safe_mode=safe_mode, online=self._connectivity_checker.online) def etag(): import hashlib hash = hashlib.sha1() def hash_update(value): value = value.encode('utf-8') hash.update(value) hash_update(repr(self._get_plugins())) hash_update(str(self._repository_available)) hash_update(repr(self._repository_plugins)) hash_update(str(self._notices_available)) hash_update(repr(self._notices)) hash_update(repr(safe_mode)) hash_update(repr(self._connectivity_checker.online)) hash_update(repr(_DATA_FORMAT_VERSION)) return hash.hexdigest() def condition(): return check_etag(etag()) return with_revalidation_checking(etag_factory=lambda *args, **kwargs: etag(), condition=lambda *args, **kwargs: condition(), unless=lambda: refresh_repository or refresh_notices)(view)()
def check_for_update(self): if "check" in flask.request.values: check_targets = map(lambda x: x.strip(), flask.request.values["check"].split(",")) else: check_targets = None force = flask.request.values.get( "force", "false") in octoprint.settings.valid_boolean_trues def view(): try: information, update_available, update_possible = self.get_current_versions( check_targets=check_targets, force=force) return flask.jsonify( dict(status="updatePossible" if update_available and update_possible else "updateAvailable" if update_available else "current", information=information)) except exceptions.ConfigurationInvalid as e: return flask.make_response( "Update not properly configured, can't proceed: %s" % e.message, 500) def etag(): checks = self._get_configured_checks() targets = check_targets if targets is None: targets = checks.keys() import hashlib hash = hashlib.sha1() targets = sorted(targets) for target in targets: current_hash = self._get_check_hash(checks.get(target, dict())) if target in self._version_cache and not force: data = self._version_cache[target] hash.update(current_hash) hash.update( str(data["timestamp"] + self._version_cache_ttl >= time.time() > data["timestamp"])) hash.update(repr(data["information"])) hash.update(str(data["available"])) hash.update(str(data["possible"])) hash.update(",".join(targets)) return hash.hexdigest() def condition(): return check_etag(etag()) return with_revalidation_checking( etag_factory=lambda *args, **kwargs: etag(), condition=lambda *args, **kwargs: condition(), unless=lambda: force)(view)()
def check_for_update(self): if "check" in flask.request.values: check_targets = map(lambda x: x.strip(), flask.request.values["check"].split(",")) else: check_targets = None force = flask.request.values.get("force", "false") in octoprint.settings.valid_boolean_trues def view(): try: information, update_available, update_possible = self.get_current_versions(check_targets=check_targets, force=force) # we don't want to transfer python_checker or python_updater values through json - replace with True for key, data in information.items(): if "check" in data: if "python_checker" in data["check"]: data["check"]["python_checker"] = True if "python_updater" in data["check"]: data["check"]["python_updater"] = True return flask.jsonify(dict(status="updatePossible" if update_available and update_possible else "updateAvailable" if update_available else "current", information=information, timestamp=self._version_cache_timestamp)) except exceptions.ConfigurationInvalid as e: return flask.make_response("Update not properly configured, can't proceed: %s" % e.message, 500) def etag(): checks = self._get_configured_checks() targets = check_targets if targets is None: targets = checks.keys() import hashlib hash = hashlib.sha1() targets = sorted(targets) for target in targets: current_hash = self._get_check_hash(checks.get(target, dict())) if target in self._version_cache and not force: data = self._version_cache[target] hash.update(current_hash) hash.update(str(data["timestamp"] + self._version_cache_ttl >= time.time() > data["timestamp"])) hash.update(repr(data["information"])) hash.update(str(data["available"])) hash.update(str(data["possible"])) hash.update(",".join(targets)) hash.update(str(self._version_cache_timestamp)) return hash.hexdigest() def condition(): return check_etag(etag()) return with_revalidation_checking(etag_factory=lambda *args, **kwargs: etag(), condition=lambda *args, **kwargs: condition(), unless=lambda: force)(view)()
def on_api_get(self, request): if not admin_permission.can(): return make_response("Insufficient rights", 403) from octoprint.server import safe_mode refresh_repository = request.values.get("refresh_repository", "false") in valid_boolean_trues if refresh_repository: self._repository_available = self._refresh_repository() refresh_notices = request.values.get("refresh_notices", "false") in valid_boolean_trues if refresh_notices: self._notices_available = self._refresh_notices() def view(): return jsonify(plugins=self._get_plugins(), repository=dict( available=self._repository_available, plugins=self._repository_plugins ), os=get_os(), octoprint=get_octoprint_version_string(), pip=dict( available=self._pip_caller.available, version=self._pip_caller.version_string, install_dir=self._pip_caller.install_dir, use_user=self._pip_caller.use_user, virtual_env=self._pip_caller.virtual_env, additional_args=self._settings.get(["pip_args"]), python=sys.executable ), safe_mode=safe_mode, online=self._connectivity_checker.online) def etag(): import hashlib hash = hashlib.sha1() hash.update(repr(self._get_plugins())) hash.update(str(self._repository_available)) hash.update(repr(self._repository_plugins)) hash.update(str(self._notices_available)) hash.update(repr(self._notices)) hash.update(repr(safe_mode)) hash.update(repr(self._connectivity_checker.online)) hash.update(repr(_DATA_FORMAT_VERSION)) return hash.hexdigest() def condition(): return check_etag(etag()) return with_revalidation_checking(etag_factory=lambda *args, **kwargs: etag(), condition=lambda *args, **kwargs: condition(), unless=lambda: refresh_repository or refresh_notices)(view)()
def on_api_get(self, request): if not admin_permission.can(): return make_response("Insufficient rights", 403) from octoprint.server import safe_mode refresh_repository = request.values.get("refresh_repository", "false") in valid_boolean_trues if refresh_repository: self._repository_available = self._refresh_repository() def view(): return jsonify(plugins=self._get_plugins(), repository=dict( available=self._repository_available, plugins=self._repository_plugins ), os=self._get_os(), octoprint=self._get_octoprint_version_string(), pip=dict( available=self._pip_caller.available, version=self._pip_caller.version_string, install_dir=self._pip_caller.install_dir, use_user=self._pip_caller.use_user, virtual_env=self._pip_caller.virtual_env, additional_args=self._settings.get(["pip_args"]), python=sys.executable ), safe_mode=safe_mode) def etag(): import hashlib hash = hashlib.sha1() hash.update(repr(self._get_plugins())) hash.update(str(self._repository_available)) hash.update(repr(self._repository_plugins)) hash.update(repr(safe_mode)) return hash.hexdigest() def condition(): return check_etag(etag()) return with_revalidation_checking(etag_factory=lambda *args, **kwargs: etag(), condition=lambda *args, **kwargs: condition(), unless=lambda: refresh_repository)(view)()
def getHistoryData(self): from octoprint.settings import valid_boolean_trues force = request.values.get("force", "false") in valid_boolean_trues if force: self._history_dict = None def view(): history_dict = self._getHistoryDict() if history_dict is not None: result = jsonify(history=history_dict) else: result = jsonify({}) return result def etag(): conn = sqlite3.connect(self._history_db_path) cur = conn.cursor() cur.execute( "SELECT changed_at FROM modifications ORDER BY changed_at DESC LIMIT 1" ) lm = cur.fetchone() conn.close() import hashlib hash = hashlib.sha1() hash.update(str(lm)) hexdigest = hash.hexdigest() return hexdigest def condition(): check = check_etag(etag()) return check return with_revalidation_checking( etag_factory=lambda *args, **kwargs: etag(), condition=lambda *args, **kwargs: condition(), unless=lambda: force)(view)()