Skip to content

Folders

create_folders_from_path_string(workspace, path)

Create recursively folders and subfolders from a path string.

Parameters:

Name Type Description Default
workspace str

The name or ID of the workspace.

required
path str

The name or ID of the folder.

required

Returns:

Name Type Description
str str

The ID of the final folder.

Source code in src/pyfabricops/helpers/folders.py
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
def create_folders_from_path_string(workspace: str, path: str) -> str:
    """
    Create recursively folders and subfolders from a path string.

    Args:
        workspace (str): The name or ID of the workspace.
        path (str): The name or ID of the folder.

    Returns:
        str: The ID of the final folder.
    """
    workspace_id = resolve_workspace(workspace)

    if path is None or '/' not in path:
        return None

    folders_tree = path.split('/')

    parent_folder_id = None

    for folder in folders_tree:

        # Get folder_id if folder exists
        folder_id = resolve_folder(workspace_id, folder)
        if folder_id is not None:
            logger.info(
                f'Folder `{folder}` already exists with ID `{folder_id}`.'
            )

        # If not, creates it.
        else:
            folder_id = create_folder(
                workspace_id,
                folder,
                parent_folder=parent_folder_id,
                df=False,
            ).get('id')
            logger.success(
                f'Folder `{folder}` created with ID `{folder_id}` successfully.'
            )

        parent_folder_id = folder_id

    return folder_id

deploy_folders(workspace, path)

Creates folders in Fabric workspace based on local folder structure

Parameters:

Name Type Description Default
workspace str

The name or ID of the workspace.

required
path str

The path to the project directory.

required
Source code in src/pyfabricops/helpers/folders.py
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
def deploy_folders(
    workspace: str,
    path: Union[str, Path],
):
    """
    Creates folders in Fabric workspace based on local folder structure

    Args:
        workspace (str): The name or ID of the workspace.
        path (str): The path to the project directory.
    """
    if not os.path.exists(path):
        logger.error(f'Path {path} does not exist.')
        return None

    # Resolve workspace ID
    workspace_id = resolve_workspace(workspace)
    if not workspace_id:
        return None

    # Get all local folders that contain Fabric artifacts
    fabric_artifacts = [
        '.SemanticModel',
        '.Report',
        '.Dataflow',
        '.Lakehouse',
        '.Warehouse',
        '.Notebook',
        '.DataPipeline',
    ]

    def _has_fabric_artifacts(path):
        """Check if folder or any subfolder contains Fabric artifacts"""
        for root, dirs, files in os.walk(path):
            for dir_name in dirs:
                if any(
                    dir_name.endswith(artifact)
                    for artifact in fabric_artifacts
                ):
                    return True
        return False

    # First pass: identify folders with artifacts and their parent folders
    folders_with_artifacts = set()

    for root, dirs, files in os.walk(path):
        for dir_name in dirs:
            full_path = os.path.join(root, dir_name)

            # Check if this folder has Fabric artifacts
            if _has_fabric_artifacts(full_path):
                relative_path = os.path.relpath(full_path, path).replace(
                    '\\', '/'
                )
                folders_with_artifacts.add(relative_path)

                # Also mark all parent folders as needed
                parent_path = os.path.dirname(relative_path).replace('\\', '/')
                while (
                    parent_path != path
                    and parent_path != '.'
                    and parent_path != ''
                ):
                    folders_with_artifacts.add(parent_path)
                    parent_path = os.path.dirname(parent_path).replace(
                        '\\', '/'
                    )

    # Second pass: build folder list only for folders with artifacts
    local_folders = []
    for root, dirs, files in os.walk(path):
        for dir_name in dirs:
            full_path = os.path.join(root, dir_name)
            relative_path = os.path.relpath(full_path, path).replace('\\', '/')

            # Only include folders that contain artifacts or are parents of folders with artifacts
            if relative_path in folders_with_artifacts:
                # Calculate depth for proper ordering (parents before children)
                depth = relative_path.count('/')

                # Get parent folder name (not full path)
                parent_relative_path = os.path.dirname(relative_path).replace(
                    '\\', '/'
                )
                parent_folder_name = None
                if (
                    parent_relative_path
                    and parent_relative_path != '.'
                    and parent_relative_path != ''
                ):
                    parent_folder_name = os.path.basename(parent_relative_path)

                local_folders.append(
                    {
                        'path': relative_path,
                        'name': dir_name,
                        'full_path': full_path,
                        'depth': depth,
                        'parent_path': parent_relative_path,
                        'parent_name': parent_folder_name,
                    }
                )

    # Sort by depth to ensure parent folders are created first
    local_folders.sort(key=lambda x: x['depth'])

    logger.info(
        f'Found {len(local_folders)} folders containing Fabric artifacts'
    )

    # Keep track of created folders by path -> folder_id
    created_folders = {}

    for folder_info in local_folders:
        folder_name = folder_info['name']
        parent_path = folder_info['parent_path']
        parent_name = folder_info['parent_name']

        # Determine parent folder ID from previously created folders
        parent_folder_id = None
        if parent_path and parent_path in created_folders:
            parent_folder_id = created_folders[parent_path]

        # Create folder in Fabric
        if parent_folder_id:
            create_folder(
                workspace, folder_name, parent_folder=parent_folder_id
            )
        elif parent_name:
            create_folder(workspace, folder_name, parente_folder=parent_name)
        else:
            create_folder(workspace, folder_name)

    logger.success(f'Created all folders in the workspace {workspace}.')

export_folders(workspace, path)

Export all folders from a workspace to a specified path

Source code in src/pyfabricops/helpers/folders.py
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
def export_folders(workspace: str, path: Union[str, Path]) -> None:
    """
    Export all folders from a workspace to a specified path
    """
    folders = get_folders_paths(workspace)
    folders_list = folders.to_dict(orient='records')
    for folder in folders_list:
        folder_path_ = Path(path) / folder['folder_path']
        os.makedirs(folder_path_, exist_ok=True)
        # Create a dummy README.md in each created folder
        with open(
            Path(folder_path_) / 'README.md', 'w', encoding='utf-8'
        ) as f:
            f.write(
                f'# {folder["folder_path"]}\n\nThis folder corresponds to the Fabric workspace folder: **{folder["folder_path"]}**\n'
            )
    logger.success(
        f'All folders from workspace {workspace} were exported to {path} successfully.'
    )

generate_folders_paths(folders_df)

Returns the full path for the folder folder_id recursively concatenating the names of its parents.

Parameters:

Name Type Description Default
folders_df DataFrame

The DataFrame containing folder information.

required

Returns:

Name Type Description
DataFrame DataFrame

The full folder paths.

Source code in src/pyfabricops/helpers/folders.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def generate_folders_paths(
    folders_df: DataFrame,
) -> DataFrame:
    """
    Returns the full path for the folder `folder_id` recursively concatenating the names of its parents.

    Args:
        folders_df (DataFrame): The DataFrame containing folder information.

    Returns:
        DataFrame: The full folder paths.
    """

    df = folders_df

    # Create a dict to lookup: id → {displayName, parentFolderId}
    folder_map = df.set_index('id')[['displayName', 'parentFolderId']].to_dict(
        'index'
    )

    # Recursive function with cache to build the full path
    @lru_cache(maxsize=None)
    def _build_full_path(folder_id: str) -> str:
        """
        Returns the full path for the folder `folder_id`,
        recursively concatenating the names of its parents.
        """
        node = folder_map.get(folder_id)
        if node is None:
            return ''  # id not found
        name = node['displayName']
        parent = node['parentFolderId']
        # If without parent, is root
        if pandas.isna(parent) or parent == '':
            return name
        # Otherwise, joins the parent path with self name
        return _build_full_path(parent) + '/' + name

    # Apply the function by each dataframe row
    df['folder_path'] = df['id'].apply(lambda x: _build_full_path(x))

    df = df.rename(columns={'id': 'folder_id'})
    return df[['folder_id', 'folder_path']]

get_folders_config(workspace)

Get the folder configuration for a specific workspace.

Parameters:

Name Type Description Default
workspace str

The workspace name or ID.

required

Returns:

Type Description
Union[Dict[str, Any], None]

The folder configuration or None if not found.

Source code in src/pyfabricops/helpers/folders.py
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def get_folders_config(workspace: str) -> Union[Dict[str, Any], None]:
    """
    Get the folder configuration for a specific workspace.

    Args:
        workspace (str): The workspace name or ID.

    Returns:
        (Union[Dict[str, Any], None]): The folder configuration or None if not found.
    """
    folders = get_folders_paths(workspace)
    if folders is None:
        return None

    return folders.to_dict(orient='records')

get_folders_paths(workspace)

Get the full folder paths for all folders in the workspace.

Parameters:

Name Type Description Default
workspace str

The workspace name.

required

Returns:

Name Type Description
DataFrame DataFrame

A DataFrame with folder IDs and their full paths.

Source code in src/pyfabricops/helpers/folders.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def get_folders_paths(workspace: str) -> DataFrame:
    """
    Get the full folder paths for all folders in the workspace.

    Args:
        workspace (str): The workspace name.

    Returns:
        DataFrame: A DataFrame with folder IDs and their full paths.
    """
    folders_df = list_folders(workspace)

    if folders_df is None or folders_df.empty:
        logger.debug(f'No folders found in workspace {workspace}.')
        return None

    if 'parentFolderId' not in folders_df.columns:
        folders_df['parentFolderId'] = ''

    return generate_folders_paths(folders_df)

resolve_folder_from_id_to_path(workspace, folder_id)

Return the folder path to the folder_id given for a specified worspace.

Source code in src/pyfabricops/helpers/folders.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
def resolve_folder_from_id_to_path(
    workspace: str, folder_id: str
) -> Union[str, None]:
    """
    Return the folder path to the folder_id given for a specified worspace.
    """
    folders = get_folders_paths(workspace)
    if folders is None:
        return None

    folder_path = folders[folders['folder_id'] == folder_id][
        'folder_path'
    ].iloc[0]

    if folder_path is None:
        logger.info(f'{folder_id} not found in the workspace {workspace}')
        return None

    return folder_path