function create_folder
Creates a nested folder structure in FileCloud by iterating through path elements and checking/creating directories as needed.
/tf/active/vicechatdev/mailsearch/upload_non_wuxi_coded.py
23 - 60
moderate
Purpose
This function ensures that a complete folder hierarchy exists in a FileCloud server by walking through the path elements, checking if each directory exists, and creating missing directories. It uses FileCloud's REST API to query existing folders and create new ones. The function stops walking the path once it needs to create a folder and creates the remaining path in one operation.
Source Code
def create_folder(start_path, s):
"""Create folder structure in FileCloud if it doesn't exist"""
ServerURL = 'https://filecloud.vicebio.com/'
path_elements = start_path[1:].split('/')
walking = True
for i, p in enumerate(path_elements[3:]):
if walking:
infoEndPoint = 'core/getfilelist'
ApiParams = {'path': '/' + '/'.join(path_elements[:i+3])}
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:
# Directory exists, continue
pass
else:
# Create directory
createEndPoint = 'core/createfolder'
ApiParams = {
'path': '/' + '/'.join(path_elements[:i+3]),
'subpath': "/".join(path_elements[i+3:])
}
print(f" Creating folder: {ApiParams}")
createCall = s.post(ServerURL + createEndPoint, params=ApiParams, cookies=s.cookies)
if createCall.text == 'OK':
print(' ✓ Folder creation successful')
else:
print(f' ✗ Folder creation failed: {createCall.text}')
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 to create in FileCloud. Expected format is '/root/folder1/folder2/...' where the first 3 elements are skipped (likely representing server/user/root structure). Must start with '/' and use '/' as path separator.
s: A requests.Session object that maintains authentication cookies and connection state for FileCloud API calls. Must be pre-authenticated with valid FileCloud credentials before being passed to this function.
Return Value
This function does not return any value (implicitly returns None). It performs side effects by creating folders on the FileCloud server and printing status messages to stdout.
Dependencies
requestsxmltodict
Required Imports
import requests
import xmltodict
Usage Example
import requests
import xmltodict
# Create and authenticate session
session = requests.Session()
login_url = 'https://filecloud.vicebio.com/core/loginguest'
login_params = {'userid': 'username', 'password': 'password'}
session.post(login_url, params=login_params)
# Create folder structure
path_to_create = '/server/user/root/projects/2024/data'
create_folder(path_to_create, session)
# The function will create any missing folders in the hierarchy
# and print status messages about the creation process
Best Practices
- Ensure the session object is properly authenticated before calling this function
- The function assumes the first 3 path elements are system/user/root and skips them - verify this matches your FileCloud structure
- The function prints status messages directly to stdout - consider capturing or redirecting output if needed
- Error handling is minimal (bare except clause) - consider adding more robust error handling for production use
- The ServerURL is hardcoded - consider parameterizing it for reusability across different FileCloud instances
- The function uses a 'walking' flag to optimize by creating remaining path in one API call once a missing folder is found
- XML parsing errors are silently caught - ensure proper error logging if this is critical
- The function does not validate the response from createfolder beyond checking for 'OK' text
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_folder_v1 94.3% similar
-
function create_folder_hierarchy 70.0% similar
-
function create_folder_hierarchy_v1 69.8% similar
-
function test_filecloud_operations 68.9% similar
-
function ensure_document_folders_v1 66.4% similar