def __init__(self, configApp, database, name, quantity, listFollowedComponents=None, listInfoProduct=None): """ Minimal constructor """ super(Foodstuff, self).__init__(configApp, database, _("Foodstuff")) self.dictComponents = dict() self.setData("name", name) self.setData("quantity", quantity) if listFollowedComponents is not None: self.update(self.database.getInfoFood(name)) for codeComponent in listFollowedComponents: component = Component.Component(self.configApp, self.database, self.getData("code"), codeComponent, quantity) self.dictComponents[codeComponent] = component self.logger.debug(_("Created in model from database") + str(self)) elif listInfoProduct is not None: self.setData("code", listInfoProduct[2]) self.setData("familyName", listInfoProduct[3]) self.setData("source", listInfoProduct[4]) self.setData("dateSource", listInfoProduct[5]) self.setData("urlSource", listInfoProduct[6]) isGroup = self.getData("code") < \ int(self.configApp.get('Limits', 'startGroupProductCodes')) self.setData("isGroup", isGroup) self.logger.debug(_("Created in model from list") + str(self)) else: raise CalcalExceptions.CalcalValueError(self.configApp, "Bad usage for Foodstuff constructor")
def addComponentFromList(self, listInfoComponent): """ Create a new component without to access to database, from info in listInfoComponent """ codeComponent = listInfoComponent[0] component = Component.Component(self.configApp, self.database, self.getData("code"), codeComponent, self.getData("quantity"), listInfoComponent) self.dictComponents[codeComponent] = component
def addMissingComponents(self, listFollowedComponents): """ Add missing components according listFollowedComponents in this foodstuff """ missingCompCodes = set(listFollowedComponents) - self.dictComponents.keys() for codeComp in missingCompCodes: component = Component.Component(self.configApp, self.database, self.getData("code"), codeComp, self.getData("quantity")) self.dictComponents[codeComp] = component self.logger.debug("addMissingComponents : add component " + str(codeComp) + " in " + self.getData("name"))
def string_to_taxonomy(taxonomy_string: str) -> List[Component]: """Converts string into taxonomy (list of components). :param taxonomy_string: taxonomy represented as string :return: List of components representing taxonomy. """ taxonomy_string = taxonomy_string.replace( ' ' * NUMBER_OF_SPACES_EQUAL_TO_TAB, CHILD_SYMBOL) # make sure N spaces are converted to '\t' taxonomy = [] last_on_level = [] cmp_names = [] for line in taxonomy_string.split('\n'): if not line or line.isspace( ): # If entire line contains only spaces, ignore it continue level, component_name = __extract_tabs(line) if component_name in cmp_names: # If names are not unique, raise error raise BGError(f'Taxonomy contains more than one component ' f'named "{component_name}".') else: cmp_names.append(component_name) if len(last_on_level) < level: # If there are too many intendations, raise BGError( f'Cannot create a child of non-existing component. ' f'Check number of tabs in component: "{component_name} " ' f'and its ancestor.') component = Component(component_name, level) if len(last_on_level) > level: last_on_level[ level] = component.id_ # there already exists a level else: last_on_level.append(component.id_) # add level above if level != 0: component.parent_id = last_on_level[level - 1] taxonomy.append(component) return taxonomy
def updateFollowedComponents(self, componentCodes2delete, componentCodes2Add): """ Update this model according components asked by user """ # Delete components removed by user for k in componentCodes2delete: self.dictComponents.pop(k, None) # Add new components asked by user for codeComponent in componentCodes2Add: self.dictComponents[codeComponent] = Component.Component(self.configApp, self.database, self.getData("code"), codeComponent, self.getData("quantity")) self.logger.debug("Add components : " + str(list(componentCodes2Add)))
def test_updateQuantity(): """ Test updateQuantity """ # Call init fixture configApp, databaseManager = initEnv() database = databaseManager.getDatabase() codeProduct = 2004 codeComponent = 400 quantity = 200. component = Component.Component(configApp, database, codeProduct, codeComponent, quantity) assert component.getData("quantity") == approx(2.0 * 88.9) component.updateQuantity(100.0) assert component.getData("quantity") == approx(88.9) # Close demo database databaseManager.closeDatabase()
def test_getValueFormated(qualifier, value, strOK): """ Test getValueFormated """ # Call init fixture configApp, databaseManager = initEnv() database = databaseManager.getDatabase() codeProduct = 2004 codeComponent = 400 quantity = 200. component = Component.Component(configApp, database, codeProduct, codeComponent, quantity) # Start test component.setData("qualifValue", qualifier) component.setData("quantity", value) assert component.getValueFormated() == _(strOK) # Close demo database databaseManager.closeDatabase()
def __add(self, cmp_name: str, level: int, parent_id: Optional[int]) -> None: """Executed after creating of a new component. :param cmp_name: Name of a new component :param level: Level of the new component. :param parent_id: Id of the parent of the new component. """ cmp = Component(cmp_name, level, parent_id=parent_id, is_leaf=True, symmetry_breaking=True) self.__state.model.add_component(cmp) self.__taxonomy_tree.add_item(cmp) self.__selected_component = cmp self.__cmp_name_var.set(trim_string(cmp.name, length=22)) change_controls_state(tk.NORMAL, self.__remove_button, self.__remove_recursively_button, self.__rename_button) pub.sendMessage(actions.TAXONOMY_EDITED)
def test_init(): """ Test constructor """ # Call init fixture configApp, databaseManager = initEnv() database = databaseManager.getDatabase() codeProduct = 2004 codeComponent = 400 quantity = 200. component = Component.Component(configApp, database, codeProduct, codeComponent, quantity) assert component.getData("productCode") == 2004 assert component.getData("constituantCode") == 400 assert component.getData("value") == approx(88.9) assert component.getData("quantity") == approx(2.0 * 88.9) assert component.getData("qualifValue") == 'N' assert component.getData("name") == "Eau" # Close demo database databaseManager.closeDatabase()
def test_getValueFormatedError(): """ Test getValueFormated """ # Call init fixture configApp, databaseManager = initEnv() database = databaseManager.getDatabase() codeProduct = 2004 codeComponent = 400 quantity = 200. component = Component.Component(configApp, database, codeProduct, codeComponent, quantity) # Start test badQualifier = "XX" component.setData("qualifValue", badQualifier) component.setData("quantity", 45.0) with pytest.raises(CalcalExceptions.CalcalInternalError) as e: component.getValueFormated() print(e) assert "getValueFormated : unknown value qualifier : " in str(e.value) assert badQualifier in str(e.value) # Close demo database databaseManager.closeDatabase()
def new_component(): pjson = json.loads(request.data) app = Component(pjson.get('name')) app.save() return jsonify({'result': "Ok"})