def _apply_sub_frames(cls, documents, subs):
        """Convert embedded documents to sub-frames for one or more documents"""

        # Dereference each reference
        for path, projection in subs.items():

            # Get the SubFrame class we'll use to wrap the embedded document
            sub = None
            expect_map = False
            if '$sub' in projection:
                sub = projection.pop('$sub')
            elif '$sub.' in projection:
                sub = projection.pop('$sub.')
                expect_map = True
            else:
                continue

            # Add sub-frames to the documents
            raw_subs = []
            for document in documents:
                value = cls._path_to_value(path, document)
                if value is None:
                    continue

                if isinstance(value, dict):
                    if expect_map:
                        # Dictionary of embedded documents
                        raw_subs += value.values()
                        for k, v in value.items():
                            if isinstance(v ,list):
                                value[k] = [
                                    sub(u) for u in v if isinstance(u, dict)]
                            else:
                                value[k] = sub(v)

                    # Single embedded document
                    else:
                        raw_subs.append(value)
                        value = sub(value)

                elif isinstance(value, list):
                    # List of embedded documents
                    raw_subs += value
                    value = [sub(v) for v in value if isinstance(v, dict)]

                else:
                    raise TypeError('Not a supported sub-frame type')

                child_document = document
                keys = cls._path_to_keys(path)
                for key in keys[:-1]:
                    child_document = child_document[key]
                child_document[keys[-1]] = value

            # Apply the projection to the list of sub frames
            if projection:
                sub._apply_projection(raw_subs, projection)
def _apply_sub_frames ( cls , documents , subs ) : for path , projection in subs . items ( ) : sub = None expect_map = False if '$sub' in projection : sub = projection . pop ( '$sub' ) elif '$sub.' in projection : sub = projection . pop ( '$sub.' ) expect_map = True else : continue raw_subs = [ ] for document in documents : value = cls . _path_to_value ( path , document ) if value is None : continue if isinstance ( value , dict ) : if expect_map : raw_subs += value . values ( ) for k , v in value . items ( ) : if isinstance ( v , list ) : value [ k ] = [ sub ( u ) for u in v if isinstance ( u , dict ) ] else : value [ k ] = sub ( v ) else : raw_subs . append ( value ) value = sub ( value ) elif isinstance ( value , list ) : raw_subs += value value = [ sub ( v ) for v in value if isinstance ( v , dict ) ] else : raise TypeError ( 'Not a supported sub-frame type' ) child_document = document keys = cls . _path_to_keys ( path ) for key in keys [ : - 1 ] : child_document = child_document [ key ] child_document [ keys [ - 1 ] ] = value if projection : sub . _apply_projection ( raw_subs , projection )
Convert embedded documents to sub-frames for one or more documents