"""
SparklessContext implementation for Sparkless.
This module provides a mock implementation of PySpark's SparkContext
that behaves identically to the real SparkContext for testing and development.
It includes context management, JVM simulation, and logging without requiring
a JVM or actual Spark installation.
Key Features:
- Complete PySpark SparkContext API compatibility
- JVM context simulation
- Log level management
- Application name management
- Context lifecycle management
Example:
>>> from sparkless.session import SparkContext
>>> sc = SparkContext("MyApp")
>>> sc.setLogLevel("WARN")
>>> print(sc.appName)
MyApp
"""
from typing import Any
import getpass
[docs]
class MockJVMFunctions:
"""Mock JVM functions for testing without actual JVM."""
[docs]
def __init__(self) -> None:
"""Initialize mock JVM functions."""
pass
[docs]
class JVMContext:
"""Mock JVM context for testing without actual JVM."""
[docs]
def __init__(self) -> None:
"""Initialize mock JVM context."""
self._jvm_available = True
self.functions = MockJVMFunctions()
@property
def available(self) -> bool:
"""Check if JVM is available."""
return self._jvm_available
[docs]
def get(self, key: str) -> Any:
"""Get JVM property."""
return None # Mock implementation
[docs]
def set(self, key: str, value: Any) -> None:
"""Set JVM property."""
pass # Mock implementation
[docs]
class SparkContext:
"""SparklessContext for testing without PySpark.
Provides a comprehensive mock implementation of PySpark's SparkContext
that supports all major operations including context management, logging,
and JVM simulation without requiring actual Spark installation.
Attributes:
app_name: Application name for the Spark context.
_jvm: JVM context for JVM operations.
Example:
>>> sc = SparkContext("MyApp")
>>> sc.setLogLevel("WARN")
>>> print(sc.appName)
MyApp
"""
[docs]
def __init__(self, app_name: str = "SparklessApp"):
"""Initialize SparkContext.
Args:
app_name: Name of the Spark application.
"""
self.app_name = app_name
self._jvm = JVMContext()
[docs]
def setLogLevel(self, level: str) -> None:
"""Set log level.
Args:
level: Log level (DEBUG, INFO, WARN, ERROR, FATAL).
"""
# Mock implementation - in real Spark this would configure logging
pass
@property
def appName(self) -> str:
"""Get application name.
Returns:
Application name string.
"""
return self.app_name
@property
def jvm(self) -> JVMContext:
"""Get JVM context.
Returns:
JVM context instance.
"""
return self._jvm
[docs]
def stop(self) -> None:
"""Stop the Spark context.
In a real Spark context, this would stop the Spark application.
This is a mock implementation.
"""
# Mock implementation - in real Spark this would stop the context
pass
[docs]
def sparkUser(self) -> str:
"""Return the logical Spark user associated with this context."""
return getpass.getuser()
[docs]
def __enter__(self) -> "SparkContext":
"""Context manager entry."""
return self
[docs]
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
"""Context manager exit."""
self.stop()