11import os
22import yaml
33from typing import Dict , Any , Optional , List
4+ from pydantic import BaseModel , Field
45
5- class Config :
6- def __init__ (self , config_path : str = "config.yaml" ):
6+ class PulsarConfig (BaseModel ):
7+ service_url : str = "pulsar://localhost:6650"
8+ auth_plugin : str = ""
9+ auth_params : str = ""
10+ max_concurrent_requests : int = 10
11+ max_producer_cache_size : int = 100
12+
13+ class ModuleConfig (BaseModel ):
14+ active_module : Optional [str ] = None
15+ module_configs : Dict [str , Dict [str , Any ]] = Field (default_factory = dict )
16+
17+ class SourceSpec (BaseModel ):
18+ pulsar : Dict [str , Any ] = Field (default_factory = dict )
19+
20+ class SinkSpec (BaseModel ):
21+ pulsar : Dict [str , Any ] = Field (default_factory = dict )
22+
23+ class Config (BaseModel ):
24+ pulsar : PulsarConfig = Field (default_factory = PulsarConfig )
25+ module : Optional [str ] = None
26+ sources : List [SourceSpec ] = Field (default_factory = list )
27+ requestSource : Optional [SourceSpec ] = None
28+ sink : Optional [SinkSpec ] = None
29+ subscription_name : str = "fs-sdk-subscription"
30+ name : Optional [str ] = None
31+ description : Optional [str ] = None
32+ modules : ModuleConfig = Field (default_factory = ModuleConfig )
33+ config : List [Dict [str , Any ]] = Field (default_factory = list )
34+
35+ @classmethod
36+ def from_yaml (cls , config_path : str = "config.yaml" ) -> "Config" :
737 """
838 Initialize configuration from YAML file.
939
1040 Args:
1141 config_path (str): Path to the configuration file
12- """
13- self .config_path = config_path
14- self .config = self ._load_config ()
15-
16- def _load_config (self ) -> Dict [str , Any ]:
17- """
18- Load configuration from YAML file.
19-
42+
2043 Returns:
21- Dict[str, Any] : Configuration dictionary
44+ Config : Configuration instance
2245 """
23- if not os .path .exists (self . config_path ):
24- raise FileNotFoundError (f"Configuration file not found: { self . config_path } " )
46+ if not os .path .exists (config_path ):
47+ raise FileNotFoundError (f"Configuration file not found: { config_path } " )
2548
26- with open (self .config_path , 'r' ) as f :
27- return yaml .safe_load (f )
49+ with open (config_path , 'r' ) as f :
50+ config_data = yaml .safe_load (f )
51+ return cls (** config_data )
2852
2953 def get_config_value (self , config_name : str ) -> Any :
3054 """
@@ -36,93 +60,11 @@ def get_config_value(self, config_name: str) -> Any:
3660 Returns:
3761 Any: The configuration value, or None if not found
3862 """
39- config_section = self .config .get ('config' , [])
40- for item in config_section :
63+ for item in self .config :
4164 if config_name in item :
4265 return item [config_name ]
4366 return None
44-
45- @property
46- def service_url (self ) -> str :
47- """Get Pulsar service URL."""
48- return self .config .get ('pulsar' , {}).get ('service_url' , 'pulsar://localhost:6650' )
49-
50- @property
51- def auth_plugin (self ) -> str :
52- """Get Pulsar auth plugin."""
53- return self .config .get ('pulsar' , {}).get ('authPlugin' , '' )
54-
55- @property
56- def auth_params (self ) -> str :
57- """Get Pulsar auth parameters."""
58- return self .config .get ('pulsar' , {}).get ('authParams' , '' )
59-
60- @property
61- def module (self ) -> str :
62- """Get the module name."""
63- return self .config .get ('module' )
64-
65- @property
66- def sources (self ) -> List [Dict [str , Any ]]:
67- """Get the sources configuration."""
68- return self .config .get ('sources' , [])
69-
70- @property
71- def request_sources (self ) -> List [Dict [str , Any ]]:
72- """Get the request sources configuration."""
73- return self .config .get ('requestSource' , [])
74-
75- @property
76- def sinks (self ) -> List [Dict [str , Any ]]:
77- """Get the sinks configuration."""
78- return self .config .get ('sink' , [])
79-
80- def get_pulsar_source_config (self , source : Dict [str , Any ]) -> Dict [str , Any ]:
81- """Get Pulsar source configuration from a source spec."""
82- return source .get ('pulsar' , {})
83-
84- def get_pulsar_sink_config (self , sink : Dict [str , Any ]) -> Dict [str , Any ]:
85- """Get Pulsar sink configuration from a sink spec."""
86- return sink .get ('pulsar' , {})
87-
88- @property
89- def request_topic (self ) -> str :
90- """Get request topic for the active module."""
91- topic = self .config .get ('request_topic' )
92- if not topic :
93- raise ValueError ("request_topic is not set in config.yaml" )
94- return topic
95-
96- @property
97- def subscription_name (self ) -> str :
98- """Get subscription name for the active module."""
99- return self .config .get ('subscription_name' , 'fs-sdk-subscription' )
100-
101- @property
102- def name (self ) -> str :
103- """Get the function name."""
104- return self .config .get ('name' )
105-
106- @property
107- def description (self ) -> str :
108- """Get the function description."""
109- return self .config .get ('description' )
110-
111- @property
112- def max_concurrent_requests (self ) -> int :
113- """Get maximum number of concurrent requests."""
114- return self .config .get ('pulsar' , {}).get ('max_concurrent_requests' , 10 )
115-
116- @property
117- def max_producer_cache_size (self ) -> int :
118- """Get maximum number of producers to cache."""
119- return self .config .get ('pulsar' , {}).get ('max_producer_cache_size' , 100 )
120-
121- @property
122- def active_module (self ) -> Optional [str ]:
123- """Get the name of the active module."""
124- return self .config .get ('modules' , {}).get ('active_module' )
12567
12668 def get_module_config (self , module_name : str ) -> Dict [str , Any ]:
12769 """Get configuration for a specific module."""
128- return self .config . get ( ' modules' , {}) .get (module_name , {})
70+ return self .modules . module_configs .get (module_name , {})
0 commit comments