components/policy/tools/template_writers/writers/jamf_writer.py
def WriteRefItems(self, root, obj, path_to_obj_parent, known_ids,
                    ids_in_ancestry):
    '''Replaces all the '$ref' items by their actual value. Nested properties
      are limited to a depth of MAX_RECURSIVE_FIELDS_DEPTH, after which the
      recursive field is removed.

    Args:
      root: The root of the object tree to scan for refs.
      obj: The current object being checked for ids.
      path_to_obj_parent: A array of all the keys leading to the parent of |obj|
                          starting at |root|.
      known_ids: The dictionary of all the known ids.
      ids_in_ancestry: A list of ids found in the tree starting at root. Use to
                       keep nested fields in check.
    '''
    if type(obj) is not dict:
      return
    if 'id' in obj:
      ids_in_ancestry.add(obj['id'])
    # Make a copy of items since we are going to change |obj|.
    for key, value in list(obj.items()):
      if type(value) is not dict:
        continue
      if '$ref' in value:
        # If the id is an ancestor, we have a nested field.
        if value['$ref'] in ids_in_ancestry:
          id = value['$ref']

          last_obj = None
          parent = root
          grandparent = root

          # Find the parent and grandparent of obj to create the |last_obj|
          # which is the field where the nesting stops.
          for i in range(0, len(path_to_obj_parent)):
            if i + 1 < len(path_to_obj_parent):
              grandparent = grandparent[path_to_obj_parent[i]]
            else:
              parent = grandparent[path_to_obj_parent[i]]
              # Remove the link between grand parent and parent so we can have a
              # copy of the object without nesting.
              grandparent[path_to_obj_parent[i]] = None
              del grandparent[path_to_obj_parent[i]]
              # last_obj is a copy of the reference object without nesting.
              last_obj = copy.deepcopy(known_ids[id])
              # Re-establish the link between grand parent and parent.
              grandparent[path_to_obj_parent[i]] = parent

          del obj[key]
          obj[key] = last_obj
          # Create nested '$ref' objects with |last_obj| as the last object.
          for count in range(1, self.MAX_RECURSIVE_FIELDS_DEPTH):
            obj[key] = copy.deepcopy(known_ids[id])
          obj_grandparent_ref = path_to_obj_parent[len(path_to_obj_parent) - 1]
        else:
          # If no nested field, simply assign the '$ref'.
          obj[key] = dict(known_ids[value['$ref']])
          self.WriteRefItems(root, obj[key], path_to_obj_parent + [key],
                             known_ids, ids_in_ancestry)
      else:
        self.WriteRefItems(root, value, path_to_obj_parent + [key], known_ids,
                           ids_in_ancestry)
Replaces all the '$ref' items by their actual value. Nested properties
      are limited to a depth of MAX_RECURSIVE_FIELDS_DEPTH, after which the
      recursive field is removed.

    Args:
      root: The root of the object tree to scan for refs.
      obj: The current object being checked for ids.
      path_to_obj_parent: A array of all the keys leading to the parent of |obj|
                          starting at |root|.
      known_ids: The dictionary of all the known ids.
      ids_in_ancestry: A list of ids found in the tree starting at root. Use to
                       keep nested fields in check.
    
Replaces all the '$ref' items by their actual value. Nested properties
are limited to a depth of MAX_RECURSIVE_FIELDS_DEPTH, after which the
recursive field is removed.
Replaces all the ' $ref ' items by their actual value . Nested properties are limited to a depth of MAX_RECURSIVE_FIELDS_DEPTH after which the recursive field is removed .
def WriteRefItems(self, root, obj, path_to_obj_parent, known_ids,
                    ids_in_ancestry):
    if type(obj) is not dict:
      return
    if 'id' in obj:
      ids_in_ancestry.add(obj['id'])
    for key, value in list(obj.items()):
      if type(value) is not dict:
        continue
      if '$ref' in value:
        if value['$ref'] in ids_in_ancestry:
          id = value['$ref']
          last_obj = None
          parent = root
          grandparent = root
          for i in range(0, len(path_to_obj_parent)):
            if i + 1 < len(path_to_obj_parent):
              grandparent = grandparent[path_to_obj_parent[i]]
            else:
              parent = grandparent[path_to_obj_parent[i]]
              grandparent[path_to_obj_parent[i]] = None
              del grandparent[path_to_obj_parent[i]]
              last_obj = copy.deepcopy(known_ids[id])
              grandparent[path_to_obj_parent[i]] = parent
          del obj[key]
          obj[key] = last_obj
          for count in range(1, self.MAX_RECURSIVE_FIELDS_DEPTH):
            obj[key] = copy.deepcopy(known_ids[id])
          obj_grandparent_ref = path_to_obj_parent[len(path_to_obj_parent) - 1]
        else:
          obj[key] = dict(known_ids[value['$ref']])
          self.WriteRefItems(root, obj[key], path_to_obj_parent + [key],
                             known_ids, ids_in_ancestry)
      else:
        self.WriteRefItems(root, value, path_to_obj_parent + [key], known_ids,
                           ids_in_ancestry)
def WriteRefItems ( self , root , obj , path_to_obj_parent , known_ids , ids_in_ancestry ) : if type ( obj ) is not dict : return if 'id' in obj : ids_in_ancestry . add ( obj [ 'id' ] ) for key , value in list ( obj . items ( ) ) : if type ( value ) is not dict : continue if '$ref' in value : if value [ '$ref' ] in ids_in_ancestry : id = value [ '$ref' ] last_obj = None parent = root grandparent = root for i in range ( 0 , len ( path_to_obj_parent ) ) : if i + 1 < len ( path_to_obj_parent ) : grandparent = grandparent [ path_to_obj_parent [ i ] ] else : parent = grandparent [ path_to_obj_parent [ i ] ] grandparent [ path_to_obj_parent [ i ] ] = None del grandparent [ path_to_obj_parent [ i ] ] last_obj = copy . deepcopy ( known_ids [ id ] ) grandparent [ path_to_obj_parent [ i ] ] = parent del obj [ key ] obj [ key ] = last_obj for count in range ( 1 , self . MAX_RECURSIVE_FIELDS_DEPTH ) : obj [ key ] = copy . deepcopy ( known_ids [ id ] ) obj_grandparent_ref = path_to_obj_parent [ len ( path_to_obj_parent ) - 1 ] else : obj [ key ] = dict ( known_ids [ value [ '$ref' ] ] ) self . WriteRefItems ( root , obj [ key ] , path_to_obj_parent + [ key ] , known_ids , ids_in_ancestry ) else : self . WriteRefItems ( root , value , path_to_obj_parent + [ key ] , known_ids , ids_in_ancestry )
Replaces all the '$ref' items by their actual value.
Replaces all the ' $ref ' items by their actual value .
'''Replaces all the '$ref' items by their actual value. Nested properties
      are limited to a depth of MAX_RECURSIVE_FIELDS_DEPTH, after which the
      recursive field is removed.

    Args:
      root: The root of the object tree to scan for refs.
      obj: The current object being checked for ids.
      path_to_obj_parent: A array of all the keys leading to the parent of |obj|
                          starting at |root|.
      known_ids: The dictionary of all the known ids.
      ids_in_ancestry: A list of ids found in the tree starting at root. Use to
                       keep nested fields in check.
    ''' # Make a copy of items since we are going to change |obj|. # If the id is an ancestor, we have a nested field. # Find the parent and grandparent of obj to create the |last_obj| # which is the field where the nesting stops. # Remove the link between grand parent and parent so we can have a # copy of the object without nesting. # last_obj is a copy of the reference object without nesting. # Re-establish the link between grand parent and parent. # Create nested '$ref' objects with |last_obj| as the last object. # If no nested field, simply assign the '$ref'.
self
root
The root of the object tree to scan for refs.
The root of the object tree to scan for refs .
obj
The current object being checked for ids.
The current object being checked for ids .
path_to_obj_parent
A array of all the keys leading to the parent of |obj|
starting at |root|.
A array of all the keys leading to the parent of |obj| starting at |root| .
known_ids
The dictionary of all the known ids.
The dictionary of all the known ids .
ids_in_ancestry
A list of ids found in the tree starting at root. Use to
keep nested fields in check.
A list of ids found in the tree starting at root . Use to keep nested fields in check .