🔍 Code Extractor

function create_folder

Maturity: 36

Creates a nested folder structure on a FileCloud server by traversing a path and creating missing directories.

File:
/tf/active/vicechatdev/filecloud_wuxi_sync.py
Lines:
12 - 44
Complexity:
moderate

Purpose

This function recursively creates folders on a FileCloud server (vicebio.com) by walking through a given path. It checks if each directory level exists using the FileCloud API, and creates the entire remaining path structure when it encounters a missing directory. The function is designed to ensure a complete folder hierarchy exists before file operations.

Source Code

def create_folder(start_path,s):
    path_elements=start_path[1:].split('/')
    walking=True
    for i,p in enumerate(path_elements[3:]):
        if walking:
            ServerURL='https://filecloud.vicebio.com/'   #To be defined
            infoEndPoint = 'core/getfilelist'
            ApiParams = {'path': '/'+'/'.join(path_elements[:i+3])}
            #print(ApiParams)
            InfoCall=s.post(ServerURL+infoEndPoint, params=ApiParams, cookies = s.cookies)
            doc = xmltodict.parse(InfoCall.text)
            found=False
            try:
                for e in doc['entries']['entry']:
                    if e['name']==p:
                        found=True
            except:
                pass
            if found:
                ## We found this dir entry so moving one down
                pass
            else:
                ## We need to create this directory all the way down
                createEndPoint = 'core/createfolder'
                ## Upload API params.
                ApiParams = {'path': '/'+'/'.join(path_elements[:i+3]),'subpath': "/".join(path_elements[i+3:])}
                print("ready to create ",ApiParams)
                createCall=s.post(ServerURL+createEndPoint, params=ApiParams, cookies = s.cookies)
                if createCall.text == 'OK':
                    print('folder creation successfull.')
                else:
                    print('folder creation failed.')
                walking=False

Parameters

Name Type Default Kind
start_path - - positional_or_keyword
s - - positional_or_keyword

Parameter Details

start_path: A string representing the full path where folders should be created. Expected format is '/root/folder1/folder2/...' with leading slash. The path is split and processed starting from the 4th element (index 3) after splitting by '/'.

s: A requests.Session object that maintains authentication cookies and connection state for making API calls to the FileCloud server. Must be pre-authenticated with valid session cookies.

Return Value

This function does not return any value (implicitly returns None). It performs side effects by creating folders on the remote server and printing status messages to stdout indicating success or failure of folder creation operations.

Dependencies

  • requests
  • xmltodict

Required Imports

import requests
import xmltodict

Usage Example

import requests
import xmltodict

# Create and authenticate session
s = requests.Session()
# Authenticate with FileCloud (authentication code not shown)
# s.post('https://filecloud.vicebio.com/core/login', data={'username': 'user', 'password': 'pass'})

# Create folder structure
path = '/root/projects/2024/data/analysis'
create_folder(path, s)

# Output will print:
# ready to create {'path': '/root/projects/2024', 'subpath': 'data/analysis'}
# folder creation successfull.

Best Practices

  • Ensure the session object 's' is properly authenticated before calling this function
  • The start_path should follow the format '/root/level1/level2/...' with a leading slash
  • Handle network errors externally as this function does not implement error handling for connection issues
  • The function assumes path_elements[3:] contains the relevant path parts, so ensure your path structure matches this expectation
  • Consider wrapping calls in try-except blocks to handle API failures gracefully
  • The hardcoded ServerURL should ideally be parameterized for better reusability
  • The function prints to stdout; consider logging instead for production use
  • The bare except clause may hide important errors; consider more specific exception handling

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function create_folder_hierarchy 72.6% similar

    Creates a hierarchical structure of Subfolder nodes in a Neo4j graph database based on a file system path, connecting each folder level with PATH relationships.

    From: /tf/active/vicechatdev/offline_docstore_multi_vice.py
  • function create_folder_hierarchy_v2 71.8% similar

    Creates a hierarchical structure of Subfolder nodes in a Neo4j graph database based on a file path, connecting each folder level with PATH relationships.

    From: /tf/active/vicechatdev/offline_docstore_multi.py
  • function test_filecloud_operations 69.0% similar

    Tests FileCloud basic operations by creating a test folder to verify connectivity and authentication with a FileCloud server.

    From: /tf/active/vicechatdev/SPFCsync/test_connections.py
  • function create_folder_hierarchy_v1 68.9% similar

    Creates a hierarchical structure of Subfolder nodes in a Neo4j graph database based on a file path, establishing parent-child relationships between folders.

    From: /tf/active/vicechatdev/offline_parser_docstore.py
  • function check_filecloud_structure 60.7% similar

    Diagnostic function that checks the FileCloud server structure and verifies accessibility of various paths including root, SHARED, and configured base paths.

    From: /tf/active/vicechatdev/SPFCsync/check_filecloud_structure.py
← Back to Browse