Skip to content

Commit fe89fd7

Browse files
committed
load plugins from env variable
1 parent 9ffa3cb commit fe89fd7

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

reflex/environment.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import concurrent.futures
66
import dataclasses
77
import enum
8+
import importlib
89
import inspect
910
import multiprocessing
1011
import os
@@ -24,6 +25,7 @@
2425
)
2526

2627
from reflex import constants
28+
from reflex.plugins import Plugin
2729
from reflex.utils.exceptions import EnvironmentVarValueError
2830
from reflex.utils.types import GenericType, is_union, value_inside_optional
2931

@@ -126,6 +128,48 @@ def interpret_path_env(value: str, field_name: str) -> Path:
126128
return Path(value)
127129

128130

131+
def interpret_plugin_env(value: str, field_name: str) -> Plugin:
132+
"""Interpret a plugin environment variable value.
133+
134+
Args:
135+
value: The environment variable value.
136+
field_name: The field name.
137+
138+
Returns:
139+
The interpreted value.
140+
141+
Raises:
142+
EnvironmentVarValueError: If the value is invalid.
143+
"""
144+
if "." not in value:
145+
msg = f"Invalid plugin value: {value!r} for {field_name}. Plugin name must be in the format 'package.module.PluginName'."
146+
raise EnvironmentVarValueError(msg)
147+
148+
import_path, plugin_name = value.rsplit(".", 1)
149+
150+
try:
151+
module = importlib.import_module(import_path)
152+
except ImportError as e:
153+
msg = f"Failed to import module {import_path!r} for {field_name}: {e}"
154+
raise EnvironmentVarValueError(msg) from e
155+
156+
try:
157+
plugin_class = getattr(module, plugin_name, None)
158+
except Exception as e:
159+
msg = f"Failed to get plugin class {plugin_name!r} from module {import_path!r} for {field_name}: {e}"
160+
raise EnvironmentVarValueError(msg) from e
161+
162+
if not inspect.isclass(plugin_class) or not issubclass(plugin_class, Plugin):
163+
msg = f"Invalid plugin class: {plugin_name!r} for {field_name}. Must be a subclass of Plugin."
164+
raise EnvironmentVarValueError(msg)
165+
166+
try:
167+
return plugin_class()
168+
except Exception as e:
169+
msg = f"Failed to instantiate plugin {plugin_name!r} for {field_name}: {e}"
170+
raise EnvironmentVarValueError(msg) from e
171+
172+
129173
def interpret_enum_env(value: str, field_type: GenericType, field_name: str) -> Any:
130174
"""Interpret an enum environment variable value.
131175
@@ -181,6 +225,8 @@ def interpret_env_var_value(
181225
return interpret_path_env(value, field_name)
182226
if field_type is ExistingPath:
183227
return interpret_existing_path_env(value, field_name)
228+
if field_type is Plugin:
229+
return interpret_plugin_env(value, field_name)
184230
if get_origin(field_type) is list:
185231
return [
186232
interpret_env_var_value(

0 commit comments

Comments
 (0)