def loadSettingsFromFile(self, file_name): with open(file_name, "rt", -1, "utf-8") as f: data = json.load(f, object_pairs_hook=collections.OrderedDict) self._i18n_catalog = i18nCatalog(os.path.basename(file_name)) self._json_file = file_name if "id" in data: self._type_id = data["id"] if "platform" in data: self._platform_mesh = data["platform"] if "platform_texture" in data: self._platform_texture = data["platform_texture"] if "name" in data: self._type_name = data["name"] if "version" in data: self._type_version = str(data["version"]) if "icon" in data: self._icon = data["icon"] if "inherits" in data: inherits_from = MachineSettings() inherits_from.loadSettingsFromFile(os.path.dirname(file_name) + "/" + data["inherits"]) self._machine_settings = inherits_from._machine_settings self._categories = inherits_from._categories if "machine_settings" in data: for key, value in data["machine_settings"].items(): setting = self.getSettingByKey(key) if not setting: setting = Setting(key, self._i18n_catalog) self.addSetting(setting) setting.fillByDict(value) if "categories" in data: for key, value in data["categories"].items(): category = self.getSettingsCategory(key) if not category: category = SettingsCategory(key, self._i18n_catalog, self) self.addSettingsCategory(category) category.fillByDict(value) if "overrides" in data: for key, value in data["overrides"].items(): setting = self.getSettingByKey(key) if not setting: continue setting.fillByDict(value) for setting in self.getAllSettings(): setting.valueChanged.connect(self.settingChanged) self.settingsLoaded.emit() #Emit signal that all settings are loaded (some setting stuff can only be done when all settings are loaded (eg; the conditional stuff)
class Script(): def __init__(self): super().__init__() self._settings = None self._settings_model = None try: setting_data = self.getSettingData() if "key" in setting_data: self._settings = SettingsCategory(setting_data["key"], i18n_catalog, self) self._settings.fillByDict(self.getSettingData()) self._settings_model = SettingsFromCategoryModel(self._settings) else: Logger.log("e", "Script has no key in meta data. Unable to use.") except NotImplementedError: pass ## Needs to return a dict that can be used to construct a settingcategory file. # See the example script for an example. # It follows the same style / guides as the Uranium settings. def getSettingData(self): raise NotImplementedError() ## Get the initialised settings def getSettings(self): return self._settings def getSettingsModel(self): return self._settings_model ## Get setting by key. (convenience function) # \param key Key to select setting by (string) # \return Setting or none if no setting was found. def getSettingByKey(self, key): return self._settings.getSettingByKey(key) ## Set the value of a setting by key. # \param key Key of setting to change. # \param value value to set. def setSettingValueByKey(self, key, value): setting = self.getSettingByKey(key) if setting is not None: setting.setValue(value) ## Get the value of setting by key. # \param key Key of the setting to get value from # \return value (or none) def getSettingValueByKey(self, key): setting = self.getSettingByKey(key) if setting is not None: return setting.getValue() return None ## This is called when the script is executed. # It gets a list of g-code strings and needs to return a (modified) list. def execute(self, data): raise NotImplementedError()
def loadAll(self): if self._loaded: return with open(self._path, "rt", -1, "utf-8") as f: self._json_data = json.load( f, object_pairs_hook=collections.OrderedDict) if not self._name: self.loadMetaData() self._i18n_catalog = i18nCatalog(os.path.basename(self._path)) self._platform_mesh = self._json_data.get("platform", "") self._platform_texture = self._json_data.get("platform_texture", "") if "inherits" in self._json_data: inherits_from = MachineDefinition( self._machine_manager, Resources.getPath(Resources.MachineDefinitions, self._json_data["inherits"])) inherits_from.loadAll() self._machine_settings = inherits_from._machine_settings self._categories = inherits_from._categories if "machine_settings" in self._json_data: for key, value in self._json_data["machine_settings"].items(): setting = self.getSetting(key) if not setting: setting = Setting(self._machine_manager, key, self._i18n_catalog) self._machine_settings.append(setting) setting.fillByDict(value) if "categories" in self._json_data: for key, value in self._json_data["categories"].items(): category = self.getSettingsCategory(key) if not category: category = SettingsCategory(self._machine_manager, key, self._i18n_catalog, self) self._categories.append(category) category.fillByDict(value) if "overrides" in self._json_data: for key, value in self._json_data["overrides"].items(): setting = self.getSetting(key) if not setting: continue setting.fillByDict(value) self.settingsLoaded.emit() #self._json_data = None self._loaded = True
def loadAll(self): if self._loaded: return with open(self._path, "rt", -1, "utf-8") as f: self._json_data = json.load(f, object_pairs_hook=collections.OrderedDict) if not self._name: self.loadMetaData() self._i18n_catalog = i18nCatalog(os.path.basename(self._path)) self._platform_mesh = self._json_data.get("platform", "") self._platform_texture = self._json_data.get("platform_texture", "") if "inherits" in self._json_data: inherits_from = MachineDefinition(self._machine_manager, Resources.getPath(Resources.MachineDefinitions, self._json_data["inherits"])) inherits_from.loadAll() self._machine_settings = inherits_from._machine_settings self._categories = inherits_from._categories if "machine_settings" in self._json_data: for key, value in self._json_data["machine_settings"].items(): setting = self.getSetting(key) if not setting: setting = Setting(self._machine_manager, key, self._i18n_catalog) self._machine_settings.append(setting) setting.fillByDict(value) if "categories" in self._json_data: for key, value in self._json_data["categories"].items(): category = self.getSettingsCategory(key) if not category: category = SettingsCategory(self._machine_manager, key, self._i18n_catalog, self) self._categories.append(category) category.fillByDict(value) if "overrides" in self._json_data: for key, value in self._json_data["overrides"].items(): setting = self.getSetting(key) if not setting: continue setting.fillByDict(value) self.settingsLoaded.emit() #self._json_data = None self._loaded = True
class Script(): def __init__(self): super().__init__() self._settings = None self._settings_model = None try: setting_data = self.getSettingData() if "key" in setting_data: self._settings = SettingsCategory(setting_data["key"], i18n_catalog, self) self._settings.fillByDict(self.getSettingData()) self._settings_model = SettingsFromCategoryModel( self._settings) else: Logger.log("e", "Script has no key in meta data. Unable to use.") except NotImplementedError: pass ## Needs to return a dict that can be used to construct a settingcategory file. # See the example script for an example. # It follows the same style / guides as the Uranium settings. def getSettingData(self): raise NotImplementedError() ## Get the initialised settings def getSettings(self): return self._settings def getSettingsModel(self): return self._settings_model def getValue(self, line, key, default=None): if not key in line or (';' in line and line.find(key) > line.find(';')): return default sub_part = line[line.find(key) + 1:] m = re.search('^[0-9]+\.?[0-9]*', sub_part) if m is None: return default try: return float(m.group(0)) except: return default ## Get setting by key. (convenience function) # \param key Key to select setting by (string) # \return Setting or none if no setting was found. def getSettingByKey(self, key): return self._settings.getSettingByKey(key) ## Set the value of a setting by key. # \param key Key of setting to change. # \param value value to set. def setSettingValueByKey(self, key, value): setting = self.getSettingByKey(key) if setting is not None: setting.setValue(value) ## Get the value of setting by key. # \param key Key of the setting to get value from # \return value (or none) def getSettingValueByKey(self, key): setting = self.getSettingByKey(key) if setting is not None: return setting.getValue() return None ## This is called when the script is executed. # It gets a list of g-code strings and needs to return a (modified) list. def execute(self, data): raise NotImplementedError()
def loadAll(self): if self._loaded: return with open(self._path, "rt", -1, "utf-8") as f: try: self._json_data = json.load( f, object_pairs_hook=collections.OrderedDict) except JSONDecodeError as e: raise SettingsError.InvalidFileError(self._path) from e if not self._name: self.loadMetaData() self._i18n_catalog = i18nCatalog(os.path.basename(self._path)) self._platform_mesh = self._json_data.get("platform", "") self._platform_texture = self._json_data.get("platform_texture", "") if "inherits" in self._json_data: try: path = Resources.getPath(Resources.MachineDefinitions, self._json_data["inherits"]) except FileNotFoundError as e: # If we cannot find the file in Resources, try and see if it can be found relative to this file. # This is primarily used by the unit tests. path = os.path.join(os.path.dirname(self._path), self._json_data["inherits"]) if not os.path.exists(path): raise FileNotFoundError( "Could not find file {0} in Resources or next to {1}". format(self._json_data["inherits"], self._path)) from e inherits_from = MachineDefinition(self._machine_manager, path) inherits_from.loadAll() self._machine_settings = inherits_from._machine_settings self._categories = inherits_from._categories self._preferences = inherits_from._preferences if "machine_settings" in self._json_data: for key, value in self._json_data["machine_settings"].items(): setting = self.getSetting(key) if not setting: setting = Setting(self._machine_manager, key, self._i18n_catalog) self._machine_settings.append(setting) setting.fillByDict(value) if "categories" in self._json_data: for key, value in self._json_data["categories"].items(): category = self.getSettingsCategory(key) if not category: category = SettingsCategory(self._machine_manager, key, self._i18n_catalog, self) self._categories.append(category) category.fillByDict(value) if "overrides" in self._json_data: for key, value in self._json_data["overrides"].items(): setting = self.getSetting(key) if not setting: continue setting.fillByDict(value) if "machine_preferences" in self._json_data: for key, value in self._json_data["machine_preferences"].items(): self._preferences[key] = value self.settingsLoaded.emit() self._loaded = True
class Script(SignalEmitter): def __init__(self): super().__init__() self._settings = None self._settings_model = None self._profile = ScriptProfile.ScriptProfile(self) try: setting_data = self.getSettingData() if "key" in setting_data: self._settings = SettingsCategory(self, setting_data["key"], i18n_catalog, self) self._settings.fillByDict(self.getSettingData()) self._settings_model = SettingsFromCategoryModel(self._settings, machine_manager = self) self._settings_model.sort(lambda t: t["key"]) self.settingsLoaded.emit() else: Logger.log("e", "Script has no key in meta data. Unable to use.") except NotImplementedError: pass settingsLoaded = Signal() activeProfileChanged = Signal() def getActiveProfile(self): return self._profile ## Needs to return a dict that can be used to construct a settingcategory file. # See the example script for an example. # It follows the same style / guides as the Uranium settings. def getSettingData(self): raise NotImplementedError() ## Get the initialised settings def getSettings(self): return self._settings def getSettingsModel(self): return self._settings_model def getValue(self, line, key, default = None): if not key in line or (';' in line and line.find(key) > line.find(';')): return default sub_part = line[line.find(key) + 1:] m = re.search('^[0-9]+\.?[0-9]*', sub_part) if m is None: return default try: return float(m.group(0)) except: return default ## Get setting by key. (convenience function) # \param key Key to select setting by (string) # \return Setting or none if no setting was found. def getSettingByKey(self, key): return self._settings.getSetting(key) ## Set the value of a setting by key. # \param key Key of setting to change. # \param value value to set. def setSettingValueByKey(self, key, value): self._profile.setSettingValue(key,value) ## Get the value of setting by key. # \param key Key of the setting to get value from # \return value (or none) def getSettingValueByKey(self, key): return self._profile.getSettingValue(key) ## This is called when the script is executed. # It gets a list of g-code strings and needs to return a (modified) list. def execute(self, data): raise NotImplementedError()
def loadSettingsFromFile(self, file_name): with open(file_name, "rt", -1, "utf-8") as f: data = json.load(f, object_pairs_hook=collections.OrderedDict) if "id" not in data or "name" not in data: raise SettingsError.InvalidFileError(file_name) if "version" not in data or data[ "version"] != self.MachineDefinitionVersion: raise SettingsError.InvalidVersionError(file_name) self._i18n_catalog = i18nCatalog(os.path.basename(file_name)) self._json_file = file_name self._type_id = data["id"] self._type_name = data["name"] if "platform" in data: self._platform_mesh = data["platform"] if "platform_texture" in data: self._platform_texture = data["platform_texture"] if "version" in data: self._type_version = str(data["version"]) if "icon" in data: self._icon = data["icon"] if "inherits" in data: inherits_from = MachineSettings() inherits_from.loadSettingsFromFile( os.path.dirname(file_name) + "/" + data["inherits"]) self._machine_settings = inherits_from._machine_settings self._categories = inherits_from._categories if "machine_settings" in data: for key, value in data["machine_settings"].items(): setting = self.getSettingByKey(key) if not setting: setting = Setting(key, self._i18n_catalog) self.addSetting(setting) setting.fillByDict(value) if "categories" in data: for key, value in data["categories"].items(): category = self.getSettingsCategory(key) if not category: category = SettingsCategory(key, self._i18n_catalog, self) self.addSettingsCategory(category) category.fillByDict(value) if "overrides" in data: for key, value in data["overrides"].items(): setting = self.getSettingByKey(key) if not setting: continue setting.fillByDict(value) for setting in self.getAllSettings(): setting.valueChanged.connect(self.settingChanged) self.settingsLoaded.emit( ) #Emit signal that all settings are loaded (some setting stuff can only be done when all settings are loaded (eg; the conditional stuff)
def loadAll(self): if self._loaded: return with open(self._path, "rt", -1, "utf-8") as f: try: self._json_data = json.load(f, object_pairs_hook=collections.OrderedDict) except JSONDecodeError as e: raise SettingsError.InvalidFileError(self._path) from e if not self._name: self.loadMetaData() self._i18n_catalog = i18nCatalog(os.path.basename(self._path)) self._platform_mesh = self._json_data.get("platform", "") self._platform_texture = self._json_data.get("platform_texture", "") if "inherits" in self._json_data: try: path = Resources.getPath(Resources.MachineDefinitions, self._json_data["inherits"]) except FileNotFoundError as e: # If we cannot find the file in Resources, try and see if it can be found relative to this file. # This is primarily used by the unit tests. path = os.path.join(os.path.dirname(self._path), self._json_data["inherits"]) if not os.path.exists(path): raise FileNotFoundError("Could not find file {0} in Resources or next to {1}".format(self._json_data["inherits"], self._path)) from e inherits_from = MachineDefinition(self._machine_manager, path) inherits_from.loadAll() self._machine_settings = inherits_from._machine_settings self._categories = inherits_from._categories if "machine_settings" in self._json_data: for key, value in self._json_data["machine_settings"].items(): setting = self.getSetting(key) if not setting: setting = Setting(self._machine_manager, key, self._i18n_catalog) self._machine_settings.append(setting) setting.fillByDict(value) if "categories" in self._json_data: for key, value in self._json_data["categories"].items(): category = self.getSettingsCategory(key) if not category: category = SettingsCategory(self._machine_manager, key, self._i18n_catalog, self) self._categories.append(category) category.fillByDict(value) if "overrides" in self._json_data: for key, value in self._json_data["overrides"].items(): setting = self.getSetting(key) if not setting: continue setting.fillByDict(value) self.settingsLoaded.emit() #self._json_data = None self._loaded = True