yamole.py
def expand(self, obj, parent, parent_dir=None, depth=0):
        """Recursively expand an object, considering any potential JSON
        references it may contain.

        See https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03 for a
        brief description of what a JSON reference is.

        Args:
            obj: The object to expand, which may contain JSON references.
            parent: The top parent (i.e. root) for the object expand() has
                been called on.
            parent_dir: The directory where the file containing "parent" is
                located.
            depth: The current object nesting level.
        """
        parent_dir = parent_dir or self.data_dir

        if depth > self.max_depth:
            raise RuntimeError('The object has a depth higher than the '
                               'current limit ({}). Maybe the document has '
                               'circular references?'
                               .format(self.max_depth))

        if isinstance(obj, dict):
            for key, value in sorted(obj.items()):
                if key == '$ref':
                    # Parse the reference's format, which should look
                    # something like:
                    #     some/dir/example.json#/foo/bar
                    uri, route = self.REF_REGEX.match(obj['$ref']).groups()
                    route = route.lstrip('#').strip('/')

                    # Let's get ref_src, which is the object where the
                    # reference's path takes effect
                    if uri:
                        # Normalize the URI and set the new parent directory
                        if os.path.isabs(uri):
                            new_parent_dir = os.path.dirname(uri)
                        else:
                            uri = os.path.join(parent_dir, uri)
                            new_parent_dir = os.path.dirname(uri)

                        if uri in self.cache:
                            ref_src = self.cache[uri]
                        else:
                            with open(uri, 'r') as file:
                                ref_src = yaml.load(file)
                            self.cache[uri] = ref_src
                    else:
                        ref_src = parent
                        new_parent_dir = parent_dir

                    # Index the ref_src dict with the path
                    ref = reduce(operator.getitem, route.split('/'), ref_src)

                    # Replace the $ref key with the reference's contents
                    del obj['$ref']

                    obj.update(self.expand(ref, ref_src, new_parent_dir,
                                           depth + 1))
                elif key == 'allOf' and self.openapi_mode:
                    copy = deepcopy(obj)
                    del copy['allOf']
                    for item in obj['allOf']:
                        self.merge(copy, item)
                        copy = self.expand(copy, parent, parent_dir,
                                           depth + 1)
                    obj = copy
                else:
                    # This key isn't a reference, but it may contain one
                    obj[key] = self.expand(value, parent, parent_dir,
                                           depth + 1)
        elif isinstance(obj, list):
            # If this is a list, expand each item one by one
            copy = deepcopy(obj)
            obj = [self.expand(item, parent, parent_dir, depth + 1)
                   for item in copy]

        # Return the expanded object to end the recursion
        return deepcopy(obj)
Recursively expand an object, considering any potential JSON
        references it may contain.

        See https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03 for a
        brief description of what a JSON reference is.

        Args:
            obj: The object to expand, which may contain JSON references.
            parent: The top parent (i.e. root) for the object expand() has
                been called on.
            parent_dir: The directory where the file containing "parent" is
                located.
            depth: The current object nesting level.
        
Recursively expand an object, considering any potential JSON
references it may contain.


Recursively expand an object considering any potential JSON references it may contain .
def expand(self, obj, parent, parent_dir=None, depth=0):
        parent_dir = parent_dir or self.data_dir
        if depth > self.max_depth:
            raise RuntimeError('The object has a depth higher than the '
                               'current limit ({}). Maybe the document has '
                               'circular references?'
                               .format(self.max_depth))
        if isinstance(obj, dict):
            for key, value in sorted(obj.items()):
                if key == '$ref':
                    uri, route = self.REF_REGEX.match(obj['$ref']).groups()
                    route = route.lstrip('#').strip('/')
                    if uri:
                        if os.path.isabs(uri):
                            new_parent_dir = os.path.dirname(uri)
                        else:
                            uri = os.path.join(parent_dir, uri)
                            new_parent_dir = os.path.dirname(uri)
                        if uri in self.cache:
                            ref_src = self.cache[uri]
                        else:
                            with open(uri, 'r') as file:
                                ref_src = yaml.load(file)
                            self.cache[uri] = ref_src
                    else:
                        ref_src = parent
                        new_parent_dir = parent_dir
                    ref = reduce(operator.getitem, route.split('/'), ref_src)
                    del obj['$ref']
                    obj.update(self.expand(ref, ref_src, new_parent_dir,
                                           depth + 1))
                elif key == 'allOf' and self.openapi_mode:
                    copy = deepcopy(obj)
                    del copy['allOf']
                    for item in obj['allOf']:
                        self.merge(copy, item)
                        copy = self.expand(copy, parent, parent_dir,
                                           depth + 1)
                    obj = copy
                else:
                    obj[key] = self.expand(value, parent, parent_dir,
                                           depth + 1)
        elif isinstance(obj, list):
            copy = deepcopy(obj)
            obj = [self.expand(item, parent, parent_dir, depth + 1)
                   for item in copy]
        return deepcopy(obj)
def expand ( self , obj , parent , parent_dir = None , depth = 0 ) : parent_dir = parent_dir or self . data_dir if depth > self . max_depth : raise RuntimeError ( 'The object has a depth higher than the ' 'current limit ({}). Maybe the document has ' 'circular references?' . format ( self . max_depth ) ) if isinstance ( obj , dict ) : for key , value in sorted ( obj . items ( ) ) : if key == '$ref' : uri , route = self . REF_REGEX . match ( obj [ '$ref' ] ) . groups ( ) route = route . lstrip ( '#' ) . strip ( '/' ) if uri : if os . path . isabs ( uri ) : new_parent_dir = os . path . dirname ( uri ) else : uri = os . path . join ( parent_dir , uri ) new_parent_dir = os . path . dirname ( uri ) if uri in self . cache : ref_src = self . cache [ uri ] else : with open ( uri , 'r' ) as file : ref_src = yaml . load ( file ) self . cache [ uri ] = ref_src else : ref_src = parent new_parent_dir = parent_dir ref = reduce ( operator . getitem , route . split ( '/' ) , ref_src ) del obj [ '$ref' ] obj . update ( self . expand ( ref , ref_src , new_parent_dir , depth + 1 ) ) elif key == 'allOf' and self . openapi_mode : copy = deepcopy ( obj ) del copy [ 'allOf' ] for item in obj [ 'allOf' ] : self . merge ( copy , item ) copy = self . expand ( copy , parent , parent_dir , depth + 1 ) obj = copy else : obj [ key ] = self . expand ( value , parent , parent_dir , depth + 1 ) elif isinstance ( obj , list ) : copy = deepcopy ( obj ) obj = [ self . expand ( item , parent , parent_dir , depth + 1 ) for item in copy ] return deepcopy ( obj )
Recursively expand an object, considering any potential JSON
references it may contain.
Recursively expand an object considering any potential JSON references it may contain .
"""Recursively expand an object, considering any potential JSON
        references it may contain.

        See https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03 for a
        brief description of what a JSON reference is.

        Args:
            obj: The object to expand, which may contain JSON references.
            parent: The top parent (i.e. root) for the object expand() has
                been called on.
            parent_dir: The directory where the file containing "parent" is
                located.
            depth: The current object nesting level.
        """ # Parse the reference's format, which should look # something like: #     some/dir/example.json#/foo/bar # Let's get ref_src, which is the object where the # reference's path takes effect # Normalize the URI and set the new parent directory # Index the ref_src dict with the path # Replace the $ref key with the reference's contents # This key isn't a reference, but it may contain one # If this is a list, expand each item one by one # Return the expanded object to end the recursion
self
obj
The object to expand, which may contain JSON references.
The object to expand which may contain JSON references .
parent
The top parent  for the object expand() has
been called on.
The top parent for the object expand () has been called on .
parent_dir
The directory where the file containing "parent" is
located.
The directory where the file containing " parent " is located .
depth
The current object nesting level.
The current object nesting level .