Skip to content

Commit e5836b4

Browse files
besobeso
authored andcommitted
Implement Create Directory Function
Implements koii-network#12697 Implements koii-network#12607 # Implement Create Directory Function ## Task Write a function to create a new directory. ## Acceptance Criteria All tests must pass. ## Summary of Changes Added a new utility function to create directories with error handling and path validation. The function ensures that the directory can be created safely and handles potential edge cases. ## Test Cases - Verify the function creates a directory successfully - Check error handling when creating a directory that already exists - Test creating directories with different permission levels - Validate input path handling and normalization - Ensure proper error handling for invalid or unauthorized paths This PR was created automatically by a Koii Network AI Agent powered by Together.ai.
1 parent c693c97 commit e5836b4

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

utilities.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import os
2+
from typing import Optional
3+
4+
def create_directory(path: str, permission_level: Optional[int] = None) -> None:
5+
"""
6+
Creates a new directory at the specified path with optional permission level.
7+
8+
:param path: The path to create the directory.
9+
:param permission_level: The permission level to set on the directory.
10+
:return: None
11+
"""
12+
# Normalize and validate the path
13+
path = os.path.normpath(path)
14+
if not path or any(c in path for c in ('<', '>', ':', '"', '|', '?', '*')):
15+
raise ValueError("Invalid path provided.")
16+
17+
# Check if directory already exists
18+
if os.path.exists(path):
19+
return
20+
21+
# Create the directory with specified permission level
22+
try:
23+
os.mkdir(path, permission_level)
24+
except FileExistsError:
25+
return
26+
except PermissionError:
27+
raise
28+
except Exception as e:
29+
raise RuntimeError(f"Failed to create directory: {e}")

0 commit comments

Comments
 (0)