WebDAV client library providing easy access to cloud storage services like Yandex.Drive, Dropbox, Google Drive, Box, and 4shared.
Object-oriented interface for WebDAV resources providing intuitive methods for file and directory operations, metadata access, and content manipulation. The Resource class wraps individual WebDAV resources with convenient methods for common operations.
Creates Resource objects representing specific remote paths for object-oriented operations.
class Resource:
def __init__(self, client: Client, urn: str) -> None:
"""
Initialize Resource object for specific remote path.
Parameters:
- client: Client, WebDAV client instance
- urn: str, remote path or URN for the resource
"""Methods for getting information about resources and their properties.
def __str__(self) -> str:
"""
String representation of resource path.
Returns:
- str: resource path
"""
def is_dir(self) -> bool:
"""
Check if resource is a directory.
Returns:
- bool: True if resource is directory, False if file
"""
def info(self, params=None) -> dict:
"""
Get metadata information about resource.
Parameters:
- params: dict, optional parameters for info request
Returns:
- dict: metadata including size, modification time, content type
Raises:
- RemoteResourceNotFound: if resource doesn't exist
- NotConnection: if connection to server fails
"""
def check(self) -> bool:
"""
Check if resource exists on server.
Returns:
- bool: True if resource exists, False otherwise
Raises:
- NotConnection: if connection to server fails
"""Methods for manipulating and organizing resources on the server.
def rename(self, new_name: str) -> None:
"""
Rename resource to new name in same directory.
Parameters:
- new_name: str, new filename or directory name
Raises:
- RemoteResourceNotFound: if resource doesn't exist
- RemoteParentNotFound: if parent directory doesn't exist
- NotConnection: if connection to server fails
"""
def move(self, remote_path: str) -> None:
"""
Move resource to new remote path.
Parameters:
- remote_path: str, destination path for resource
Raises:
- RemoteResourceNotFound: if resource doesn't exist
- RemoteParentNotFound: if destination parent doesn't exist
- NotConnection: if connection to server fails
"""
def copy(self, remote_path: str) -> Resource:
"""
Copy resource to new remote path.
Parameters:
- remote_path: str, destination path for copy
Returns:
- Resource: new resource object for the copied resource
Raises:
- RemoteResourceNotFound: if resource doesn't exist
- RemoteParentNotFound: if destination parent doesn't exist
- NotConnection: if connection to server fails
- NotEnoughSpace: if insufficient space on server
"""
def clean(self) -> None:
"""
Delete resource from server.
Raises:
- RemoteResourceNotFound: if resource doesn't exist
- NotConnection: if connection to server fails
"""Methods for reading and writing resource content.
def read_from(self, buff) -> None:
"""
Read resource content into buffer.
Parameters:
- buff: buffer object to write content to
Raises:
- RemoteResourceNotFound: if resource doesn't exist
- NotConnection: if connection to server fails
"""
def read(self, local_path: str) -> None:
"""
Download resource content to local file.
Parameters:
- local_path: str, local file path for download
Raises:
- RemoteResourceNotFound: if resource doesn't exist
- LocalResourceNotFound: if local parent directory doesn't exist
- NotConnection: if connection to server fails
"""
def read_async(self, local_path: str, callback=None) -> None:
"""
Asynchronously download resource content.
Parameters:
- local_path: str, local file path for download
- callback: callable, completion callback function
Note: Operation runs in background thread
"""
def write_to(self, buff) -> None:
"""
Write buffer content to resource.
Parameters:
- buff: buffer object containing data to upload
Raises:
- RemoteParentNotFound: if parent directory doesn't exist
- NotConnection: if connection to server fails
- NotEnoughSpace: if insufficient space on server
"""
def write(self, local_path: str) -> None:
"""
Upload local file content to resource.
Parameters:
- local_path: str, path to local file to upload
Raises:
- LocalResourceNotFound: if local file doesn't exist
- RemoteParentNotFound: if parent directory doesn't exist
- NotConnection: if connection to server fails
- NotEnoughSpace: if insufficient space on server
"""
def write_async(self, local_path: str, callback=None) -> None:
"""
Asynchronously upload local file content.
Parameters:
- local_path: str, path to local file to upload
- callback: callable, completion callback function
Note: Operation runs in background thread
"""Methods for controlling public access to resources.
def publish(self) -> str:
"""
Publish resource and get public URL.
Returns:
- str: public URL for accessing the resource
Raises:
- RemoteResourceNotFound: if resource doesn't exist
- MethodNotSupported: if server doesn't support publishing
- NotConnection: if connection to server fails
"""
def unpublish(self) -> None:
"""
Remove public access from resource.
Raises:
- RemoteResourceNotFound: if resource doesn't exist
- MethodNotSupported: if server doesn't support unpublishing
- NotConnection: if connection to server fails
"""Methods for getting and setting WebDAV properties on resources.
@property
def property(self, option: dict):
"""
Get WebDAV property from resource.
Parameters:
- option: dict, property specification
Returns:
- Property value based on option specification
Raises:
- RemoteResourceNotFound: if resource doesn't exist
- NotConnection: if connection to server fails
"""
@property.setter
def property(self, option: dict, value) -> None:
"""
Set WebDAV property on resource.
Parameters:
- option: dict, property specification
- value: property value to set
Raises:
- RemoteResourceNotFound: if resource doesn't exist
- NotConnection: if connection to server fails
"""import webdav.client as wc
client = wc.Client({
'webdav_hostname': "https://webdav.server.com",
'webdav_login': "username",
'webdav_password': "password"
})
# Get resource object
document = client.resource("documents/report.pdf")
# Check if resource exists and get info
if document.check():
info = document.info()
print(f"File size: {info['size']} bytes")
print(f"Is directory: {document.is_dir()}")
# Rename file
document.rename("annual_report.pdf")# Create resource for text file
config_file = client.resource("config/settings.json")
# Download file content
config_file.read("~/Downloads/settings.json")
# Upload new content
config_file.write("~/Documents/new_settings.json")
# Work with buffers
from io import BytesIO
# Read to buffer
buffer = BytesIO()
config_file.read_from(buffer)
buffer.seek(0)
content = buffer.read().decode('utf-8')
print(f"Config content: {content}")
# Write from buffer
new_data = '{"theme": "dark", "language": "en"}'
upload_buffer = BytesIO(new_data.encode('utf-8'))
config_file.write_to(upload_buffer)# Create resource objects
source_doc = client.resource("drafts/proposal.docx")
backup_doc = client.resource("backup/proposal_backup.docx")
final_doc = client.resource("final/proposal.docx")
# Copy document to backup
source_doc.copy("backup/proposal_backup.docx")
# Move to final location
source_doc.move("final/proposal.docx")
# Publish for sharing
public_url = final_doc.publish()
print(f"Document available at: {public_url}")
# Later, remove public access
final_doc.unpublish()def upload_callback(success, error=None):
if success:
print("Upload completed successfully")
else:
print(f"Upload failed: {error}")
def download_callback(success, error=None):
if success:
print("Download completed successfully")
else:
print(f"Download failed: {error}")
# Async upload and download
video_resource = client.resource("media/presentation.mp4")
video_resource.write_async("~/Videos/presentation.mp4", callback=upload_callback)
image_resource = client.resource("images/chart.png")
image_resource.read_async("~/Downloads/chart.png", callback=download_callback)# Get resource with properties
document = client.resource("documents/metadata_example.pdf")
# Get custom property
metadata_option = {
'name': 'custom:author',
'namespace': 'http://example.com/metadata'
}
author = document.property[metadata_option]
print(f"Author: {author}")
# Set custom property
document.property[metadata_option] = "John Doe"
# Get standard WebDAV properties
size_option = {'name': 'getcontentlength'}
file_size = document.property[size_option]
print(f"File size: {file_size}")# Work with directory resources
project_dir = client.resource("projects/webapp/")
# Check if it's a directory
if project_dir.is_dir():
# Get directory info
dir_info = project_dir.info()
print(f"Directory created: {dir_info.get('creation_date')}")
# Create backup copy
project_dir.copy("backup/webapp_backup/")
# Rename directory
project_dir.rename("webapp_v2")Install with Tessl CLI
npx tessl i tessl/pypi-webdavclient