Esempio n. 1
0
 def get_and_replace_parent_variables(self):
     """
     Checks the particles desired state definition and checks to see if there are values that
     dependent on one of its parents.
     The format should look like $flavor:pcf_name$key_in_parent_current_state_definition.
     If there are values then this function looks for the corresponding parents and adds those
     values to this particles desired_state_definition.
     """
     var_lookup_list = pcf_util.find_nested_vars(
         self.desired_state_definition, var_list=[])
     for (nested_key, id_var) in var_lookup_list:
         if id_var[0] == "inherit":
             pcf_id = id_var[1]
             parent_var = id_var[2]
             try:
                 parent = next(p for p in self.parents
                               if p.pcf_id == pcf_id)
             except StopIteration:
                 raise pcf_exceptions.InvalidValueReplaceException(
                     "{} parent was not found".format(pcf_id))
             else:
                 var = pcf_util.find_nested_dict_value(
                     curr_dict=parent.current_state_definition,
                     list_nested_keys=parent_var.split('.'))
                 if not var:
                     raise pcf_exceptions.InvalidValueReplaceException(
                         "{} var was not found in {}".format(
                             parent_var, pcf_id))
             pcf_util.replace_value_nested_dict(
                 curr_dict=self.desired_state_definition,
                 list_nested_keys=nested_key.split('.'),
                 new_value=var)
 def id_replace(self):
     """
     Looks through the particle definition for $lookup and replaces them with specified resource with given name
     """
     var_lookup_list = pcf_util.find_nested_vars(
         self.desired_state_definition, var_list=[])
     for (nested_key, id_var) in var_lookup_list:
         if id_var[0] == "lookup":
             resource = id_var[1]
             names = id_var[2].split(':')
             var = self.lookup(resource, names).get_id()
             pcf_util.replace_value_nested_dict(
                 curr_dict=self.desired_state_definition,
                 list_nested_keys=nested_key.split('.'),
                 new_value=var)
    def _fuse_particles(self):
        """
        This functions adds all particles to a pcf object (pcf_field). If a particle contains a multiplier field that number of particles
        are created with indexes appended to the particle's unique identifier. If the quasiparticle has a parent, that parent is added to all
        particles in the quasiparticle. Finally link_particles is called on the pcf_field.
        """
        for particle in self.member_particles:
            if not particle.get("pcf_name"):
                particle["pcf_name"] = self.name
            multiplier = particle.get("multiplier", False)
            base_particle_config = particle.get("base_particle_config", False)

            if base_particle_config:
                particle = self.update_particle_definition(
                    particle,
                    self.pcf_field.get_particle_from_pcf_id(
                        particle["flavor"] + ":" + base_particle_config))

            # when there is no multiplier add quasiparticle parents to particle and add particle to pcf field
            if not multiplier:
                if self.particle_definition.get("parents"):
                    particle = self.add_parents_to_particle(particle)

                self.pcf_field.load_particle_definition(particle)

            # when there is a multiplier get the unique identifers, index them, add quasiparticle parents, and then add all particles to the pcf field
            else:
                particle_name = particle["pcf_name"]
                unique_identifier_list = pcf_util.get_particle_unique_identifiers(
                    particle['flavor'])
                unqiue_value_dict = dict([
                    (x,
                     pcf_util.find_nested_dict_value(particle, x.split('.')))
                    for x in unique_identifier_list
                ])
                for i in range(multiplier):
                    particle_multiple = deepcopy(particle)

                    particle_multiple["pcf_name"] = particle_name + "-" + str(
                        i)
                    # appends the correct index to each item in particle definition that is unique
                    particle_multiple = functools.reduce(
                        (lambda d, l: pcf_util.replace_value_nested_dict(
                            d, l.split('.'),
                            unqiue_value_dict.get(l) + '-' + str(i))),
                        unique_identifier_list, particle_multiple)
                    if self.particle_definition.get("parents"):
                        particle_multiple = self.add_parents_to_particle(
                            particle_multiple)

                    self.pcf_field.load_particle_definition(particle_multiple)

        self.pcf_field.link_particles(self.pcf_field.particles)