Example #1
0
    def _save(self, force=False):
        if not self._dirty and not force:
            return

        data = {}
        for name in self._users.keys():
            user = self._users[name]
            data[name] = {
                "password": user._passwordHash,
                "active": user._active,
                "roles": user._roles,
                "apikey": user._apikey,
                "settings": user._settings
            }

        with atomic_write(self._userfile,
                          "wb",
                          permissions=0o600,
                          max_permissions=0o666) as f:
            yaml.safe_dump(data,
                           f,
                           default_flow_style=False,
                           indent="    ",
                           allow_unicode=True)
            self._dirty = False
        self._load()
Example #2
0
    def _save_to_path(self, path, profile, allow_overwrite=False):
        validated_profile = self._ensure_valid_profile(profile)
        if not validated_profile:
            raise InvalidProfileError()

        if os.path.exists(path) and not allow_overwrite:
            raise SaveError(
                "Profile %s already exists and not allowed to overwrite" %
                profile["id"])

        import yaml

        from octoprint.util import atomic_write

        try:
            with atomic_write(path, mode="wt", max_permissions=0o666) as f:
                yaml.safe_dump(profile,
                               f,
                               default_flow_style=False,
                               indent=2,
                               allow_unicode=True)
        except Exception as e:
            self._logger.exception("Error while trying to save profile %s" %
                                   profile["id"])
            raise SaveError("Cannot save profile %s: %s" %
                            (profile["id"], str(e)))
Example #3
0
    def _save(self, force=False):
        if self._groupfile is None or not self._dirty and not force:
            return

        groups = {}
        for key in self._groups.keys():
            group = self._groups[key]
            groups[key] = {
                "permissions": self._from_permissions(*group._permissions),
                "subgroups": self._from_groups(*group._subgroups),
                "default": group._default,
            }
            if key not in self._default_groups:
                groups[key]["name"] = group.get_name()
                groups[key]["description"] = group.get_description()

        data = {
            "_version": self.FILE_VERSION,
            "groups": groups,
            "tracked": [x.key for x in Permissions.all()],
        }

        with atomic_write(self._groupfile,
                          mode="wt",
                          permissions=0o600,
                          max_permissions=0o666) as f:
            yaml.save_to_file(data, file=f, pretty=True)
            self._dirty = False
        self._load()
Example #4
0
    def _save(self, force=False):
        if self._groupfile is None or not self._dirty and not force:
            return

        groups = dict()
        for key in self._groups.keys():
            group = self._groups[key]
            groups[key] = dict(
                name=group._name,
                description=group._description,
                permissions=self._from_permissions(*group._permissions),
                subgroups=self._from_groups(*group._subgroups),
                default=group._default)

        data = dict(groups=groups, tracked=[x.key for x in Permissions.all()])

        with atomic_write(self._groupfile,
                          mode='wt',
                          permissions=0o600,
                          max_permissions=0o666) as f:
            import yaml
            yaml.safe_dump(data,
                           f,
                           default_flow_style=False,
                           indent=4,
                           allow_unicode=True)
            self._dirty = False
        self._load()
Example #5
0
    def _save(self, force=False):
        if not self._dirty and not force:
            return

        data = {}
        for name, user in self._users.items():
            if not user or not isinstance(user, User):
                continue

            data[name] = {
                "password": user._passwordHash,
                "active": user._active,
                "groups": self._from_groups(*user._groups),
                "permissions": self._from_permissions(*user._permissions),
                "apikey": user._apikey,
                "settings": user._settings,

                # TODO: deprecated, remove in 1.5.0
                "roles": user._roles
            }

        with atomic_write(self._userfile,
                          mode='wt',
                          permissions=0o600,
                          max_permissions=0o666) as f:
            yaml.safe_dump(data,
                           f,
                           default_flow_style=False,
                           indent=4,
                           allow_unicode=True)
            self._dirty = False
        self._load()
Example #6
0
    def save(self, path):
        """
		Will dump the contents of all streams provided during construction into the target file, in the order they were
		provided.
		"""
        import shutil

        with atomic_write(path, "wb") as dest:
            with self.stream() as source:
                shutil.copyfileobj(source, dest)
Example #7
0
	def save(self, path):
		"""
		Will dump the contents of all streams provided during construction into the target file, in the order they were
		provided.
		"""
		import shutil

		with atomic_write(path, "wb") as dest:
			with self.stream() as source:
				shutil.copyfileobj(source, dest)
Example #8
0
	def set_all_data(self, data):
		from octoprint.util import atomic_write
		import yaml

		with self._lock:
			try:
				with atomic_write(self.cachefile, "wb", max_permissions=0o666) as handle:
					yaml.safe_dump(data, handle,default_flow_style=False, indent="    ", allow_unicode=True)
			except:
				self._logger.exception("Error while writing {}".format(self.cachefile))
Example #9
0
 def _writeConfigurationFile(self, config):
     try:
         import yaml
         from octoprint.util import atomic_write
         with atomic_write(self._print_queue_file_path) as f:
             yaml.safe_dump(config, stream=f, default_flow_style=False, indent="  ", allow_unicode=True)
     except:
         self._logger.info("PQ: error writing configuration file")
     else:
         self._configuration_dict = config
Example #10
0
	def set_all_data(self, data):
		from octoprint.util import atomic_write
		import yaml

		with self._lock:
			try:
				with atomic_write(self.cachefile, "wb", max_permissions=0o666) as handle:
					yaml.safe_dump(data, handle,default_flow_style=False, indent="    ", allow_unicode=True)
			except:
				self._logger.exception("Error while writing {}".format(self.cachefile))
Example #11
0
    def _save(self, force=False):
        if not self._dirty and not force:
            return

        data = {}
        for name, user in self._users.items():
            if not user or not isinstance(user, User):
                self.logger.debug('Not saving %s' % name)
                continue

            if isinstance(user, LDAPUser):
                self.logger.debug('Saving %s as %s' %
                                  (name, LDAPUser.__name__))
                data[name] = {
                    "type": LDAPUser.USER_TYPE,
                    DISTINGUISHED_NAME: user.distinguished_name,

                    # password field has to exist because of how FilebasedUserManager processes
                    # data, but an empty password hash cannot match any entered password (as
                    # whatever the user enters will be hashed... even an empty password.
                    "password": None,
                    "active": user._active,
                    "groups": self._from_groups(*user._groups),
                    "permissions": self._from_permissions(*user._permissions),
                    "apikey": user._apikey,
                    "settings": user._settings,

                    # TODO: deprecated, remove in 1.5.0
                    "roles": user._roles
                }
            else:
                self.logger.debug('Saving %s as %s...' % (name, User.__name__))
                data[name] = {
                    "password": user._passwordHash,
                    "active": user._active,
                    "groups": self._from_groups(*user._groups),
                    "permissions": self._from_permissions(*user._permissions),
                    "apikey": user._apikey,
                    "settings": user._settings,

                    # TODO: deprecated, remove in 1.5.0
                    "roles": user._roles
                }

        with atomic_write(self._userfile,
                          mode='wt',
                          permissions=0o600,
                          max_permissions=0o666) as f:
            yaml.safe_dump(data,
                           f,
                           default_flow_style=False,
                           indent=4,
                           allow_unicode=True)
            self._dirty = False
        self._load()
Example #12
0
	def _save_keys(self):
		with self._keys_lock:
			to_persist = dict()
			for user_id, keys in self._keys.items():
				to_persist[user_id] = [x.internal() for x in keys]

			try:
				with atomic_write(self._key_path) as f:
					yaml.safe_dump(to_persist, f)
			except:
				self._logger.exception("Could not write application keys to {}".format(self._key_path))
Example #13
0
	def _save_metadata(self, path, metadata):
		with self._get_metadata_lock(path):
			metadata_path = os.path.join(path, ".metadata.yaml")
			try:
				import yaml
				with atomic_write(metadata_path) as f:
					yaml.safe_dump(metadata, stream=f, default_flow_style=False, indent="  ", allow_unicode=True)
			except:
				self._logger.exception("Error while writing .metadata.yaml to {path}".format(**locals()))
			else:
				self._metadata_cache[path] = deepcopy(metadata)
Example #14
0
	def _save_metadata(self, path, metadata):
		with self._get_metadata_lock(path):
			metadata_path = os.path.join(path, ".metadata.yaml")
			try:
				import yaml
				with atomic_write(metadata_path) as f:
					yaml.safe_dump(metadata, stream=f, default_flow_style=False, indent="  ", allow_unicode=True)
			except:
				self._logger.exception("Error while writing .metadata.yaml to {path}".format(**locals()))
			else:
				self._metadata_cache[path] = deepcopy(metadata)
Example #15
0
	def saveScript(self, script_type, name, script):
		script_folder = self.getBaseFolder("scripts")
		filename = os.path.realpath(os.path.join(script_folder, script_type, name))
		if not filename.startswith(os.path.realpath(script_folder)):
			# oops, jail break, that shouldn't happen
			raise ValueError("Invalid script path to save to: {filename} (from {script_type}:{name})".format(**locals()))

		path, _ = os.path.split(filename)
		if not os.path.exists(path):
			os.makedirs(path)
		with atomic_write(filename, "wb", max_permissions=0o666) as f:
			f.write(script)
Example #16
0
	def saveScript(self, script_type, name, script):
		script_folder = self.getBaseFolder("scripts")
		filename = os.path.realpath(os.path.join(script_folder, script_type, name))
		if not filename.startswith(script_folder):
			# oops, jail break, that shouldn't happen
			raise ValueError("Invalid script path to save to: {filename} (from {script_type}:{name})".format(**locals()))

		path, _ = os.path.split(filename)
		if not os.path.exists(path):
			os.makedirs(path)
		with atomic_write(filename, "wb") as f:
			f.write(script)
Example #17
0
    def _save_keys(self):
        with self._keys_lock:
            to_persist = {}
            for user_id, keys in self._keys.items():
                to_persist[user_id] = [x.internal() for x in keys]

            try:
                with atomic_write(self._key_path, mode="wt") as f:
                    yaml.safe_dump(to_persist, f, allow_unicode=True)
            except Exception:
                self._logger.exception(
                    "Could not write application keys to {}".format(self._key_path)
                )
Example #18
0
	def _save_version_cache(self):
		import yaml
		from octoprint.util import atomic_write
		from octoprint._version import get_versions

		octoprint_version = get_versions()["version"]
		self._version_cache["__version"] = octoprint_version

		with atomic_write(self._version_cache_path) as file_obj:
			yaml.safe_dump(self._version_cache, stream=file_obj, default_flow_style=False, indent="  ", allow_unicode=True)

		self._version_cache_dirty = False
		self._logger.info("Saved version cache to disk")
Example #19
0
	def _save_version_cache(self):
		import yaml
		from octoprint.util import atomic_write
		from octoprint._version import get_versions

		octoprint_version = get_versions()["version"]
		self._version_cache["__version"] = octoprint_version

		with atomic_write(self._version_cache_path) as file_obj:
			yaml.safe_dump(self._version_cache, stream=file_obj, default_flow_style=False, indent="  ", allow_unicode=True)

		self._version_cache_dirty = False
		self._logger.info("Saved version cache to disk")
Example #20
0
    def _save_keys(self):
        with self._keys_lock:
            to_persist = dict()
            for user_id, keys in self._keys.items():
                to_persist[user_id] = [x.internal() for x in keys]

            try:
                with atomic_write(self._key_path) as f:
                    yaml.safe_dump(to_persist, f)
            except:
                self._logger.exception(
                    "Could not write application keys to {}".format(
                        self._key_path))
Example #21
0
    def save(self, path, permissions=None):
        """
        Will dump the contents of all streams provided during construction into the target file, in the order they were
        provided.
        """
        import shutil

        with atomic_write(path, mode="wb") as dest:
            for source in self.streams:
                shutil.copyfileobj(source, dest)
        if permissions is None:
            permissions = self.DEFAULT_PERMISSIONS & ~UMASK
        os.chmod(path, permissions)
Example #22
0
	def save_recovery_data(self, origin, path, pos):
		import time
		import yaml
		from octoprint.util import atomic_write

		data = dict(origin=origin,
		            path=self.path_in_storage(origin, path),
		            pos=pos,
		            date=time.time())
		try:
			with atomic_write(self._recovery_file, max_permissions=0o666) as f:
				yaml.safe_dump(data, stream=f, default_flow_style=False, indent="  ", allow_unicode=True)
		except:
			self._logger.exception("Could not write recovery data to file {}".format(self._recovery_file))
Example #23
0
	def save(self, force=False):
		if not self._dirty and not force:
			return False

		from octoprint.util import atomic_write
		try:
			with atomic_write(self._configfile, "wb", prefix="octoprint-config-", suffix=".yaml", permissions=0o600, max_permissions=0o666) as configFile:
				yaml.safe_dump(self._config, configFile, default_flow_style=False, indent="    ", allow_unicode=True)
				self._dirty = False
		except:
			self._logger.exception("Error while saving config.yaml!")
			raise
		else:
			self.load()
			return True
Example #24
0
	def save(self, force=False):
		if not self._dirty and not force:
			return False

		from octoprint.util import atomic_write
		try:
			with atomic_write(self._configfile, "wb", prefix="octoprint-config-", suffix=".yaml") as configFile:
				yaml.safe_dump(self._config, configFile, default_flow_style=False, indent="    ", allow_unicode=True)
				self._dirty = False
		except:
			self._logger.exception("Error while saving config.yaml!")
			raise
		else:
			self.load()
			return True
Example #25
0
	def _save_to_path(self, path, profile, allow_overwrite=False):
		validated_profile = self._ensure_valid_profile(profile)
		if not validated_profile:
			raise InvalidProfileError()

		if os.path.exists(path) and not allow_overwrite:
			raise SaveError("Profile %s already exists and not allowed to overwrite" % profile["id"])

		from octoprint.util import atomic_write
		import yaml
		try:
			with atomic_write(path, "wb", max_permissions=0o666) as f:
				yaml.safe_dump(profile, f, default_flow_style=False, indent="  ", allow_unicode=True)
		except Exception as e:
			self._logger.exception("Error while trying to save profile %s" % profile["id"])
			raise SaveError("Cannot save profile %s: %s" % (profile["id"], str(e)))
Example #26
0
    def _save(self, force=False):
        if self._groupfile is None or not self._dirty and not force:
            return

        groups = dict()
        for key, group in self._groups.items():
            if not group or not isinstance(group, Group):
                self.logger.debug('Not saving %s' % key)
                continue

            if isinstance(group, LDAPGroup):
                self.logger.debug("Saving group %s as %s" %
                                  (group.get_name(), LDAPGroup.__name__))
                groups[key] = dict(
                    type=LDAPGroup.GROUP_TYPE,
                    dn=group.distinguished_name,
                    name=group.get_name(),
                    description=group.get_description(),
                    permissions=self._from_permissions(*group.permissions),
                    subgroups=self._from_groups(*group.subgroups),
                    default=group.is_default())
            else:
                self.logger.debug("Saving group %s as %s" %
                                  (group.get_name(), Group.__name__))
                groups[key] = dict(
                    name=group._name,
                    description=group._description,
                    permissions=self._from_permissions(*group._permissions),
                    subgroups=self._from_groups(*group._subgroups),
                    default=group._default)

        data = dict(groups=groups, tracked=[x.key for x in Permissions.all()])

        with atomic_write(self._groupfile,
                          mode='wt',
                          permissions=0o600,
                          max_permissions=0o666) as f:
            import yaml
            yaml.safe_dump(data,
                           f,
                           default_flow_style=False,
                           indent=4,
                           allow_unicode=True)
            self._dirty = False
        self._load()
Example #27
0
    def save_recovery_data(self, origin, path, pos):
        import time

        from octoprint.util import atomic_write

        data = {
            "origin": origin,
            "path": self.path_in_storage(origin, path),
            "pos": pos,
            "date": time.time(),
        }
        try:
            with atomic_write(self._recovery_file,
                              mode="wt",
                              max_permissions=0o666) as f:
                yaml.save_to_file(data, file=f, pretty=True)
        except Exception:
            self._logger.exception(
                f"Could not write recovery data to file {self._recovery_file}")
Example #28
0
	def _save(self, force=False):
		if not self._dirty and not force:
			return

		data = {}
		for name in self._users.keys():
			user = self._users[name]
			data[name] = {
				"password": user._passwordHash,
				"active": user._active,
				"roles": user._roles,
				"apikey": user._apikey,
				"settings": user._settings
			}

		with atomic_write(self._userfile, "wb", permissions=0o600, max_permissions=0o666) as f:
			yaml.safe_dump(data, f, default_flow_style=False, indent="    ", allow_unicode=True)
			self._dirty = False
		self._load()
Example #29
0
    def _save_to_path(self, path, profile, allow_overwrite=False):
        validated_profile = self._ensure_valid_profile(profile)
        if not validated_profile:
            raise InvalidProfileError()

        if os.path.exists(path) and not allow_overwrite:
            raise SaveError(
                "Profile %s already exists and not allowed to overwrite" %
                profile["id"])

        from octoprint.util import atomic_write

        try:
            with atomic_write(path, mode="wt", max_permissions=0o666) as f:
                yaml.save_to_file(profile, file=f, pretty=True)
        except Exception as e:
            self._logger.exception("Error while trying to save profile %s" %
                                   profile["id"])
            raise SaveError("Cannot save profile {}: {}".format(
                profile["id"], str(e)))
Example #30
0
    def save_recovery_data(self, origin, path, pos):
        import time

        import yaml

        from octoprint.util import atomic_write

        data = {
            "origin": origin,
            "path": self.path_in_storage(origin, path),
            "pos": pos,
            "date": time.time(),
        }
        try:
            with atomic_write(self._recovery_file, mode="wt", max_permissions=0o666) as f:
                yaml.safe_dump(
                    data, stream=f, default_flow_style=False, indent=2, allow_unicode=True
                )
        except Exception:
            self._logger.exception(
                "Could not write recovery data to file {}".format(self._recovery_file)
            )
Example #31
0
    def _save(self, force=False):
        if not self._dirty and not force:
            return

        data = {}
        for name in self._users.keys():
            user = self._users[name]
            if isinstance(user, LDAPUser):
                # noinspection PyProtectedMember
                data[name] = {
                    "type": LDAPUser.USER_TYPE,
                    "password": None,  # password field has to exist because of how FilebasedUserManager processes
                    # data, but an empty password hash cannot match any entered password (as
                    # whatever the user enters will be hashed... even an empty password.
                    "dn": user.get_distinguished_name(),
                    "groups": user.get_groups(),
                    "active": user.is_active(),
                    "roles": user.roles,
                    "apikey": user._apikey,
                    "settings": user.get_all_settings()
                }
            else:
                data[name] = {
                    "password": user._passwordHash,
                    "active": user._active,
                    "groups": self._from_groups(*user._groups),
                    "permissions": self._from_permissions(*user._permissions),
                    "apikey": user._apikey,
                    "settings": user._settings,

                    # TODO: deprecated, remove in 1.5.0
                    "roles": user._roles
                }

        with atomic_write(self._userfile, mode='wt', permissions=0o600, max_permissions=0o666) as f:
            yaml.safe_dump(data, f, default_flow_style=False, indent=4, allow_unicode=True)
            self._dirty = False
        self._load()
def eventHandler(self, event, payload):
    import octoprint.events
    import time
    from operator import itemgetter

    supported_event = None

    # support for print done & cancelled events
    if event == octoprint.events.Events.PRINT_DONE:
        supported_event = event

    elif event == octoprint.events.Events.PRINT_FAILED:
        supported_event = event

    # unsupported event
    if supported_event is None:
        return

    self._console_logger.info("Handled event: %s" % supported_event)
    try:
        fileData = self._file_manager.get_metadata(payload["origin"], payload["file"])
        fileName = payload["file"] if supported_event == octoprint.events.Events.PRINT_FAILED else payload["filename"]
    except:
        fileData = None

    if fileData is not None:
        timestamp = 0
        success = None

        self._console_logger.info("Metadata for: %s" % fileName)
        currentFile = {
            "fileName": fileName,
            "note": ""
        }

        # analysis - looking for info about filament usage
        if "analysis" in fileData:
            if "filament" in fileData["analysis"]:
                if "tool0" in fileData["analysis"]["filament"]:
                    filamentVolume = fileData["analysis"]["filament"]["tool0"]["volume"]
                    filamentLength = fileData["analysis"]["filament"]["tool0"]['length']

                    currentFile["filamentVolume"] = filamentVolume
                    currentFile["filamentLength"] = filamentLength
                    self._console_logger.info("Filament volume: %s, Length: %s" % (filamentVolume, filamentLength))

                if "tool1" in fileData["analysis"]["filament"]:
                    filamentVolume = fileData["analysis"]["filament"]["tool1"]["volume"]
                    filamentLength = fileData["analysis"]["filament"]["tool1"]['length']

                    currentFile["filamentVolume2"] = filamentVolume
                    currentFile["filamentLength2"] = filamentLength

                    self._console_logger.info("Tool 2 - Filament volume: %s, Length: %s" % (filamentVolume, filamentLength))

                # Temporarily disabled
                # if "tool0" in fileData["analysis"]["filament"] and "tool1" in fileData["analysis"]["filament"]:
                #     currentFile["note"] = "Dual extrusion"

        # how long print took
        if "statistics" in fileData:
            printer_profile = self._printer_profile_manager.get_current_or_default()["id"]
            if "lastPrintTime" in fileData["statistics"] and printer_profile in fileData["statistics"]["lastPrintTime"]:
                printTime = fileData["statistics"]["lastPrintTime"][printer_profile]

                currentFile["printTime"] = printTime
                self._console_logger.info("PrintTime: %s" % printTime)

        # when print happened and what was result
        if "history" in fileData:
            history = fileData["history"]

            newlist = sorted(history, key=itemgetter('timestamp'), reverse=True)

            if newlist:
                last = newlist[0]

                success = last["success"]
                timestamp = last["timestamp"]

        if not success:
            success = False if event == octoprint.events.Events.PRINT_FAILED else True

        if timestamp == 0:
            timestamp = time.time()

        history_dict = self._getHistoryDict()
        rounded_timestamp = int(timestamp * 1000);

        if history_dict.has_key(rounded_timestamp):
            self._console_logger.info("Missing history data - probably not saved to metadata.yaml yet")
            success = False if event == octoprint.events.Events.PRINT_CANCELLED else True
            timestamp = time.time()
            rounded_timestamp = int(timestamp * 1000);

        currentFile["success"] = success
        currentFile["timestamp"] = timestamp

        self._console_logger.info("Success: %s, Timestamp: %s" % (success, timestamp))

        history_dict[rounded_timestamp] = currentFile

            if "userdata" in fileData:
                if "studentid" in fileData["userdata"]:
                    studentid = fileData["userdata"]["studentid"]
                    currentFile["studentid"] = studentid
                    self._logger.info("studentid: %s" % studentid)

        try:
            import yaml
            from octoprint.util import atomic_write
            with atomic_write(self._history_file_path) as f:
                yaml.safe_dump(history_dict, stream=f, default_flow_style=False, indent="  ", allow_unicode=True)
        except:
            self._console_logger.exception("Error while writing history.yaml to {path}".format(**locals()))