from cloudcode import Local
import os
# Set your project directory and API key
your_project_dir = "/path/to/your/project"
sdk = Local(
working_dir=your_project_dir,
api_key=os.getenv("CLOUD_CODE_API_KEY"),
)
# 1. Create a new file
file_path = "src/example.py"
file_content = """
def hello(name):
return f"Hello, {name}!"
"""
created = sdk.create_file(file_path, file_content)
if created:
print(f"Created file: {file_path}")
else:
print(f"Failed to create file: {file_path}")
# 2. Read the file back
content = sdk.read_file(file_path)
if content is not None:
print(f"\nRead from {file_path}:\n{content}")
else:
print(f"File {file_path} does not exist or could not be read.")
# 3. Search for a function definition in all Python files
search_query = "def hello"
search_results = sdk.search_files(
query=search_query,
file_patterns=["src/**/*.py"]
)
print(f"\nSearch results for '{search_query}':")
for path, lines in search_results.items():
print(f"- {path}")
for line in lines:
print(f" {line.strip()}")
# 4. (Optional) Handling binary files
import base64
binary_data = b'\x00\x01\x02\x03'
encoded = base64.b64encode(binary_data).decode('utf-8')
sdk.create_file("data/binary.dat", encoded)
read_encoded = sdk.read_file("data/binary.dat")
if read_encoded:
binary_content = base64.b64decode(read_encoded)
print(f"\nRead {len(binary_content)} bytes from binary.dat")