<commit_before># coding=utf-8
__author__ = "AstroPrint Product Team <<EMAIL>>"
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
__copyright__ = "Copyright (C) 2017 3DaGoGo, Inc - Released under terms of the AGPLv3 License"

# singleton
_instance = None

def printerProfileManager():
	global _instance
	if _instance is None:
		_instance = PrinterProfileManager()
	return _instance

import os
import yaml
import logging
import shutil
import uuid

from octoprint.settings import settings

from astroprint.plugin import pluginManager
from astroprint.util import merge_dict
from astroprint.manufacturerpkg import manufacturerPkgManager

class PrinterProfileManager(object):
	def __init__(self):
		self._settings = settings()

		configDir = self._settings.getConfigFolder()

		self._infoFile = "%s/printer-profile.yaml" % configDir
		self._logger = logging.getLogger(__name__)
		self.data = {
			'driver': "marlin",
			'plugin': None,
			'extruder_count': 1,
			'max_nozzle_temp': 280,
			'max_bed_temp': 140,
			'heated_bed': True,
			'cancel_gcode': ['G28 X0 Y0'],
			'invert_z': False,
			'invert_x': False,
			'invert_y': False,
			'printer_model': {
				'id': None,
				'name': None
			},
			'temp_presets' : [
					{ 'id' : "3e0fc9b398234f2f871310c1998aa000",
					'name' : "PLA",
					'nozzle_temp' : 220,
					'bed_temp' : 40},
				 	{'id' : "2cc9df599f3e4292b379913f4940c000s",
					'name': "ABS",
					'nozzle_temp': 230,
					'bed_temp' : 80}
			],
			'last_presets_used' : [
			]
		}

		#overlay manufactuer definition of printer profile
		config = {}

		mfDefinition = manufacturerPkgManager().printerProfile

		for k in mfDefinition.keys():
			v = mfDefinition[k]
			if v is not None:
				config[k] = v
				if k == "temp_presets":
					for preset in v:
						preset['id'] = uuid.uuid4().hex
		if config:
			merge_dict(self.data, config)
			manufacturerPkgManager().removeKeyFromFile("printer_profile") #consume editable key once is used
			self.save()

		if not os.path.isfile(self._infoFile):
			factoryFile = "%s/printer-profile.factory" % configDir

			if os.path.isfile(factoryFile):
				with open(factoryFile, "r") as f:
					config = yaml.safe_load(f)

		else:
			with open(self._infoFile, "r") as f:
				config = yaml.safe_load(f)

			if config:
				merge_dict(self.data, config)

	def save(self):
		with open(self._infoFile, "wb") as infoFile:
			yaml.safe_dump(self.data, infoFile, default_flow_style=False, indent="    ", allow_unicode=True)

	def set(self, changes):
		for k in changes:
			if k in self.data:
				if self.data[k] != changes[k]:
					if k == 'driver':
						#change printer object
						from astroprint.printer.manager import printerManager
<commit_msg>Always keep user custom temperature presets<commit_after># coding=utf-8
__author__ = "AstroPrint Product Team <<EMAIL>>"
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
__copyright__ = "Copyright (C) 2017 3DaGoGo, Inc - Released under terms of the AGPLv3 License"

# singleton
_instance = None

def printerProfileManager():
	global _instance
	if _instance is None:
		_instance = PrinterProfileManager()
	return _instance

import os
import yaml
import logging
import shutil
import uuid

from octoprint.settings import settings

from astroprint.plugin import pluginManager
from astroprint.util import merge_dict
from astroprint.manufacturerpkg import manufacturerPkgManager

class PrinterProfileManager(object):
	def __init__(self):
		self._settings = settings()

		configDir = self._settings.getConfigFolder()

		self._infoFile = "%s/printer-profile.yaml" % configDir
		self._logger = logging.getLogger(__name__)
		self.data = {
			'driver': "marlin",
			'plugin': None,
			'extruder_count': 1,
			'max_nozzle_temp': 280,
			'max_bed_temp': 140,
			'heated_bed': True,
			'cancel_gcode': ['G28 X0 Y0'],
			'invert_z': False,
			'invert_x': False,
			'invert_y': False,
			'printer_model': {
				'id': None,
				'name': None
			},
			'temp_presets' : [
					{ 'id' : "3e0fc9b398234f2f871310c1998aa000",
					'name' : "PLA",
					'nozzle_temp' : 220,
					'bed_temp' : 40},
				 	{'id' : "2cc9df599f3e4292b379913f4940c000s",
					'name': "ABS",
					'nozzle_temp': 230,
					'bed_temp' : 80}
			],
			'last_presets_used' : [
			]
		}

		#overlay manufactuer definition of printer profile
		config = {}

		mfDefinition = manufacturerPkgManager().printerProfile

		for k in mfDefinition.keys():
			v = mfDefinition[k]
			if v is not None:
				config[k] = v
				if k == "temp_presets":
					for preset in v:
						preset['id'] = uuid.uuid4().hex
						preset['manufacturer'] = True

		# If manufacturer pkg contains temp presets, keep custom user
		if "temp_presets" in config:
			if os.path.isfile(self._infoFile):
				with open(self._infoFile, "r") as infoF:
					userconfig = yaml.safe_load(infoF)
					if userconfig["temp_presets"]:
						for k in userconfig["temp_presets"]:
							if "manufacturer" not in k:
								config['temp_presets'].append(k)

			merge_dict(self.data, config)
			manufacturerPkgManager().removeKeyFromFile("printer_profile") #consume editable key once is used
		self.save()

		if not os.path.isfile(self._infoFile):
			factoryFile = "%s/printer-profile.factory" % configDir

			if os.path.isfile(factoryFile):
				with open(factoryFile, "r") as f:
					config = yaml.safe_load(f)

		else:
			with open(self._infoFile, "r") as f:
				config = yaml.safe_load(f)

			if config:
				merge_dict(self.data, config)

	def save(self):
		with open(self._infoFile, "wb") as infoFile:
			yaml.safe_dump(self.data, infoFile, default_flow_style=False, indent="    ", allow_unicode=True)

	def set(self, changes):
		for k in changes:
			if k in self.data:
				if self.data[k] != changes[k]:
					if k == 'driver':
						#change printer object
						from astroprint.printer.manager import printerManager

{'fraction_non_alphanumeric': 0.12076883823779554, 'fraction_numerical': 0.025514543289675114, 'mean_word_length': 3.749596122778675, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3388098', 'n_tokens_mistral': 2517, 'n_tokens_neox': 2027, 'n_words': 505}