def set_defaults_from_obj(self, obj): """ Override current recipe defaults and crafters which will be used. :param obj: See the defaults schema in the readme. """ recipes = {} crafters = {} if 'recipes' in obj: recipes = obj['recipes'] if 'crafters' in obj: crafters = obj['crafters'] elif 'crafters' in obj: crafters = obj['crafters'] else: recipes = obj for resource, recipe in recipes.items(): if type(recipe) in [type(None), str]: self.set_default_recipe(resource, recipe) else: raise ParseError( "Invalid type for default recipe; resource: " + resource) for recipe, crafter in crafters.items(): if type(crafter) == str: self.set_default_crafter(recipe, crafter) else: raise ParseError( "Invalid type for default crafter; resource: " + recipe)
def set_default_recipe(self, resource: str, recipe: Optional[str]): if not self.is_resource(resource): raise ParseError("Resource '{}' not defined.".format(resource)) if recipe is None: self._defaults[resource] = None elif not self.is_recipe(recipe): raise ParseError("Recipe '{}' not defined.".format(recipe)) else: self._defaults[resource] = self[recipe]
def set_default_crafter(self, recipe: str, crafter: str): if not self.is_recipe(recipe): raise ParseError("Recipe '{}' is not defined.".format(recipe)) if not self.is_crafter(crafter): raise ParseError("Crafter '{}' is not defined.".format(crafter)) crafter = self._crafters[crafter] rcrafters = self._recipes[recipe].crafters # the recipe's crafter list try: index = rcrafters.index(crafter) # swap the one we want with the front of the list rcrafters[0], rcrafters[index] = rcrafters[index], rcrafters[0] except ValueError: raise ParseError("Crafter '{}' if not able to craft '{}'.".format(crafter.name, recipe))
def from_obj(name, obj: Dict, aval_crafters: Dict[str, Crafter]): """ Parse an object representation of a recipe. Mostly to read from YAML or JSON. :param name: Name of the new recipe. :param obj: See recipe schema in readme. :param aval_crafters: Available crafters which can be specified. :return: A new recipe object. """ inputs = {} outputs = {} duration = 1.0 crafters = [] if 'inputs' in obj: for resource, count in obj['inputs'].items(): inputs[resource] = float(count) if 'outputs' in obj: for resource, count in obj['outputs'].items(): outputs[resource] = float(count) if len(inputs) and len(outputs) == 0: raise ParseError( "Recipe {} does not have inputs or outputs!".format(name)) if len(set(inputs.keys()) & set(outputs.keys())) > 0: raise ParseError( "Recipe {} has an output which is also an input!".format(name)) if 'duration' in obj: duration = float(obj['duration']) if 'crafters' in obj: if type(obj['crafters']) == str: obj['crafters'] = [obj['crafters']] for c in obj['crafters']: if c not in aval_crafters: raise ParseError('Crafter {} not defined.'.format(c)) crafters.append(aval_crafters[c]) return Recipe(name, inputs, outputs, duration, crafters)