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 SettingTest(unittest.TestCase): def setUp(self): # Called before the first testfunction is executed self._setting = Setting("TestSetting", 10, "int") self._machine_settings = MachineSettings() category = SettingsCategory("TestCategory") #category.addSetting(self._setting) self._machine_settings.addSettingsCategory(category) self._machine_settings.loadSettingsFromFile( "tests/Settings/SettingData.json") def tearDown(self): # Called after the last testfunction was executed pass def test_getSettingByKey(self): self.assertEqual( self._machine_settings.getSettingByKey("TestSetting").getType(), self._setting.getType()) self.assertEqual( self._machine_settings.getSettingByKey( "TestSetting").getDefaultValue(), self._setting.getDefaultValue()) pass def test_category(self): pass def test_validatorTest(self): pass
def test_parseValue(self, setting_type, data, result): setting = Setting(self._machine_manager, "test", self._catalog, type=setting_type) assert setting.parseValue(data) == result
def setUp(self): # Called before the first testfunction is executed self._setting = Setting("TestSetting", 10, "int") self._machine_settings = MachineSettings() category = SettingsCategory("TestCategory") #category.addSetting(self._setting) self._machine_settings.addSettingsCategory(category) self._machine_settings.loadSettingsFromFile( "tests/Settings/SettingData.json")
def test_fillByDict(self, data): setting = Setting(self._machine_manager, "test", self._catalog) setting.fillByDict(data) assert setting.getType() == data["type"] assert setting.getDefaultValue() == data["default"] assert setting.getLabel() == data["label"] assert setting.getDescription() == data["description"] assert setting.isVisible()
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 fillByDict(self, data): if "label" in data: self._label = self._i18n_catalog.i18nc( "{0} label".format(self._key), data["label"]) if "visible" in data: self._visible = data["visible"] if "icon" in data: self._icon = data["icon"] if "settings" in data: for key, value in data["settings"].items(): setting = self.getSetting(key) if not setting: if "default" not in value and "type" not in value: Logger.log( "w", "Invalid setting definition for setting %s", key) continue setting = Setting(self._machine_manager, key, self._i18n_catalog, type=value["type"]) setting.setCategory(self) setting.setParent(self) self._settings.append(setting) setting.fillByDict(value) setting.visibleChanged.connect(self._onSettingVisibleChanged) setting.defaultValueChanged.connect(self.defaultValueChanged) self._onSettingVisibleChanged(None)
def setUp(self): # Called before the first testfunction is executed self._setting = Setting("TestSetting",10,"int") self._machine_settings = MachineSettings() category = SettingsCategory("TestCategory") #category.addSetting(self._setting) self._machine_settings.addSettingsCategory(category) self._machine_settings.loadSettingsFromFile("tests/Settings/SettingData.json")
def fillByDict(self, data): if "label" in data: self._label = self._i18n_catalog.i18nc("{0} label".format(self._key), data["label"]) if "visible" in data: self._visible = data["visible"] if "icon" in data: self._icon = data["icon"] if "settings" in data: for key, value in data["settings"].items(): setting = self.getSetting(key) if not setting: if "default" not in value and "type" not in value: Logger.log("w", "Invalid setting definition for setting %s", key) continue setting = Setting(self._machine_manager, key, self._i18n_catalog, type = value["type"]) setting.setCategory(self) setting.setParent(self) self._settings.append(setting) setting.fillByDict(value) setting.visibleChanged.connect(self._onSettingVisibleChanged) setting.defaultValueChanged.connect(self.defaultValueChanged) self._onSettingVisibleChanged(None)
def test_fillByDict(self): setting = Setting(self._machine_manager, "test", self._catalog) data = { "type": "int", "default": 4, "label": "Test Setting", "description": "A Test Setting", "unit": "furlongs per fortnight", "visible": True } setting.fillByDict(data) self.assertEqual("int", setting.getType()) self.assertEqual(4, setting.getDefaultValue()) self.assertEqual("Test Setting", setting.getLabel()) self.assertEqual("A Test Setting", setting.getDescription()) self.assertEqual("furlongs per fortnight", setting.getUnit()) self.assertTrue(setting.isVisible())
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 test_construct(self): # Most basic construction, only required arguments. setting = Setting(self._machine_manager, "test", self._catalog) # This check is mostly to see if the object was constructed properly assert isinstance(setting, Setting) assert setting.getKey() == "test" # Construct with keyword arguments setting = Setting( self._machine_manager, "test", self._catalog, label="Test Setting", type="string", ) assert isinstance(setting, Setting) assert setting.getKey() == "test" assert setting.getLabel() == "Test Setting" assert setting.getType() == "string"
def fillByDict(self, data): if "label" in data: self._label = self._i18n_catalog.i18nc("{0} label".format(self._key), data["label"]) if "visible" in data: self._visible = data["visible"] if "icon" in data: self._icon = data["icon"] if "settings" in data: for key, value in data["settings"].items(): if "default" in value and "type" in value: temp_setting = Setting(key, self._i18n_catalog, default = value["default"], type = value["type"]) temp_setting.setCategory(self) temp_setting.setParent(self) temp_setting.fillByDict(value) temp_setting.visibleChanged.connect(self._onSettingVisibleChanged) self._settings.append(temp_setting) self._onSettingVisibleChanged(None)
def test_construct(self): # Most basic construction, only required arguments. setting = Setting(self._machine_manager, "test", self._catalog) # This check is mostly to see if the object was constructed properly assert isinstance(setting, Setting) assert setting.getKey() == "test" # Construct with keyword arguments setting = Setting( self._machine_manager, "test", self._catalog, label = "Test Setting", type = "string", ) assert isinstance(setting, Setting) assert setting.getKey() == "test" assert setting.getLabel() == "Test Setting" assert setting.getType() == "string"
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
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 test_fillByDictWithChildren(self): setting = Setting(self._machine_manager, "test", self._catalog) data = { "type": "int", "default": 4, "label": "Test Setting", "description": "A Test Setting", "unit": "furlongs per fortnight", "visible": True, # Since python's dict is unordered but we want an ordered dict we # have to go through a bit of magic to make sure we define it as an # ordered dict. "children": collections.OrderedDict( sorted({ "test_child1": { "type": "int", "default": 9, "label": "Test Child 1", "description": "Test Setting Child 1", }, "test_child2": { "type": "int", "default": 5, "label": "Test Child 2", "description": "Test Setting Child 2", "inherit": False }, "test_child3": { "type": "int", "default": 99, "label": "Test Child 3", "description": "Test Setting Child 3", "inherit_function": "parent_value * 10" } }.items(), key=lambda k: k[0])) } setting.fillByDict(data) profile = Profile(setting) assert len(setting.getChildren()) == 3 child1 = setting.getChildren()[0] child2 = setting.getChildren()[1] child3 = setting.getChildren()[2] # Children should keep the order in which they were defined assert child1.getKey() == "test_child1" assert child2.getKey() == "test_child2" assert child3.getKey() == "test_child3" # All children should have "setting" as their parent. assert child1.getParent() == setting assert child2.getParent() == setting assert child3.getParent() == setting # Child visibility should not affect parent visibility. assert setting.isVisible() # Child 1 uses default inheritance so should inherit its parent value assert child1.getDefaultValue(profile) == 4 # Child 2 does not inherit so should return its default value assert child2.getDefaultValue(profile) == 5 # Child 3 uses an inherit function and should return parent's value * 10. assert child3.getDefaultValue(profile) == 40
def test_parseValue(self): setting = Setting(self._machine_manager, "test", self._catalog, type="int") self.assertEqual(setting.parseValue("1"), 1) self.assertEqual(setting.parseValue(1), 1) self.assertEqual(setting.parseValue(True), 1) self.assertEqual(setting.parseValue(False), 0) self.assertEqual(setting.parseValue(1.1), 1) # An error value should return a default value valid for the setting type. self.assertEqual(setting.parseValue("something"), 0) setting = Setting(self._machine_manager, "test", self._catalog, type="boolean") self.assertEqual(setting.parseValue("True"), True) self.assertEqual(setting.parseValue(1), True) self.assertEqual(setting.parseValue(1.0), True) self.assertEqual(setting.parseValue("False"), False) self.assertEqual(setting.parseValue(0), False) self.assertEqual(setting.parseValue(-1), True) # Value should be a valid python literal expression self.assertEqual(setting.parseValue("true"), False) setting = Setting(self._machine_manager, "test", self._catalog, type="float") self.assertEqual(setting.parseValue("1.0"), 1.0) self.assertEqual(setting.parseValue(1), 1.0) self.assertEqual(setting.parseValue(True), 1.0) # Floats should also be able to be specified using , as decimal separator. self.assertEqual(setting.parseValue("1,0"), 1.0) self.assertEqual(setting.parseValue("something"), 0.0)
def test_parseValue(self, setting_type, data, result): setting = Setting(self._machine_manager, "test", self._catalog, type = setting_type) assert setting.parseValue(data) == result
class SettingTest(unittest.TestCase): def setUp(self): # Called before the first testfunction is executed self._setting = Setting("TestSetting",10,"int") self._machine_settings = MachineSettings() category = SettingsCategory("TestCategory") #category.addSetting(self._setting) self._machine_settings.addSettingsCategory(category) self._machine_settings.loadSettingsFromFile("tests/Settings/SettingData.json") def tearDown(self): # Called after the last testfunction was executed pass def test_getSettingByKey(self): self.assertEqual(self._machine_settings.getSettingByKey("TestSetting").getType(), self._setting.getType()) self.assertEqual(self._machine_settings.getSettingByKey("TestSetting").getDefaultValue(), self._setting.getDefaultValue()) pass def test_category(self): pass def test_validatorTest(self): pass
def test_fillByDictWithChildren(self): setting = Setting(self._machine_manager, "test", self._catalog) data = { "type": "int", "default": 4, "label": "Test Setting", "description": "A Test Setting", "unit": "furlongs per fortnight", "visible": True, # Since python's dict is unordered but we want an ordered dict we # have to go through a bit of magic to make sure we define it as an # ordered dict. "children": collections.OrderedDict(sorted({ "test_child1": { "type": "int", "default": 9, "label": "Test Child 1", "description": "Test Setting Child 1", }, "test_child2": { "type": "int", "default": 5, "label": "Test Child 2", "description": "Test Setting Child 2", "inherit": False }, "test_child3": { "type": "int", "default": 99, "label": "Test Child 3", "description": "Test Setting Child 3", "inherit_function": "parent_value * 10" } }.items(), key = lambda k: k[0])) } setting.fillByDict(data) profile = Profile(setting) assert len(setting.getChildren()) == 3 child1 = setting.getChildren()[0] child2 = setting.getChildren()[1] child3 = setting.getChildren()[2] # Children should keep the order in which they were defined assert child1.getKey() == "test_child1" assert child2.getKey() == "test_child2" assert child3.getKey() == "test_child3" # All children should have "setting" as their parent. assert child1.getParent() == setting assert child2.getParent() == setting assert child3.getParent() == setting # Child visibility should not affect parent visibility. assert setting.isVisible() # Child 1 uses default inheritance so should inherit its parent value assert child1.getDefaultValue(profile) == 4 # Child 2 does not inherit so should return its default value assert child2.getDefaultValue(profile) == 5 # Child 3 uses an inherit function and should return parent's value * 10. assert child3.getDefaultValue(profile) == 40
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