Package: pspring
pspringThis is a lightweight framework to enable python developers to quickly develop apps with annotations/decorators. Inspired by Spring Framework of Java, the framework provides the ability for IOC ( Inversion of control ) and Autowiring the beans.The default environment variables can always be founddefaultvars.pyfileBelow is the index the annotations/decorators supported by this module@Bean(name="")This decorator will register the class or method ( which returns an instance of bean ) to the pspring application context. You can provide an optionalnameattribute to register the bean with a qualified name. Providing no name would register the bean for its type ( and its base classes )@Autowired(name=beanname)This decorator is the heard of dependency injection and will autowire the arguments of a method ( especially used in__init__constructor ). You can provide a list of name value pairs where,nameis the argument name in the method definition for which a bean namedbeannamewould be injected with that qualifier.Configuration FrameworkConfiguring applications can get complicated with environment variables, default variables and various source of configurations ( like secrets, encrypted variables etc ).pspring brings a configuration framework which can be used exhaustively for developing cloud applications. The idea in Configuration framework is to provide a standard interface for applications to fetch properties.The two classes which forms the backbone of this framework areConfigurationandConfigurationProvider. During the application start up, the framework needs to be initialized with a list of ConfigurationProviders as shown belowfrompspsringimportConfiguration,ConfigurationProviderconfig={"firstname":"dave","lastname":"picaso"}# A simple dictionary based config providerclassDictionaryConfigurationProvider(ConfigurationProvider):defgetProperty(propertyName):returnconfig.get(propertyName)Configuration.initialize([DictionaryConfigurationProvider()])config=Configuration.getConfig(__name__)# will print 'dave'print(config.getProperty("firstname"))Within a module, the configuration provides a means for namespace to avoid property name conflicts.Example:frompspsringimportConfigurationconfig=Configuration.getConfig(__name__)print(config.getProperty("firstname"))In the above code, we create a configiration accessor instance by passing in the module name (name). This will create a namespace under which the property lives. For Eg: if the file is saved under the package as com/example/application.py, the property name will be actually referenced ascom.example.application.firstname.when getProperty method is invoked1) the property is looked up in environment variables
2) the property is searched in the given list of ConfigurationProviders during initialization
3) the property is searched in the defaults
4) Search the property one above the hierarchy in the namespace starting from step 1. ( Eg: com.example.firstname )The above search sequence is pursued until the property is found. If the property is not found,thenNoneis returned.pspring along with pspring-aws provides a wealth of ConfigurationProviders ( SecretsMgrConfigProvider, DynamodbConfigProvider, RealTimeSecretsMgrConfigProvider etc...) that can be used with aws.UsageThe context of pspring should be initialized before any dependency injection is expected. A sample of code is show belowimportsys,osfrompspringimport*config=Configuration.getConfig(__name__)@Bean(name="loggerBeanName")classLogger():def__init__():passdefsayHi(self,name):print("Hi "+str(name));classMyApp():@Autowired(mylogger="loggerBeanName")def__init__(self,mylogger:Logger):self.logger=myloggeros.environ["name"]="dave"Configuration.initialize([])ApplicationContext.initialize()app=MyApp()# prints "Hi dave"app.logger.sayHi(config.getProperty("name"))