def init(): """ """ parser = OptionParser() parser.add_option( "-c", "--config", dest="configfile", help="Read configuration from FILE. (Overrides default config file.)", metavar="FILE") parser.add_option("-a", "--host", dest="broker_host", help="Listen on specified address for broker.", metavar="ADDR") parser.add_option("-p", "--port", dest="broker_port", help="Listen on specified port for broker.", type="int", metavar="PORT") parser.add_option("-l", "--logfile", dest="logfile", help="Log to specified file.", metavar="FILE") parser.add_option( "--debug", action="store_true", dest="debug", default=False, help="Sets logging to debug (unless logging configured in config file)." ) (options, args) = parser.parse_args() config = init_config() injector = inject.Injector() inject.register(injector) injector.bind('config', to=config) injector.bind('words', to=VMWords.getWords, scope=inject.appscope) injector.bind('stompProtocol', to=StompProtocol, scope=inject.appscope) injector.bind('stompEngine', to=VMStompEngine, scope=inject.appscope) injector.bind('subject', to=VMStompEngine, scope=inject.appscope) if options.broker_host is not None: config.set('broker', 'host', options.broker_host) if options.broker_port is not None: config.set('broker', 'port', str(options.broker_port)) level = logging.DEBUG if options.debug else logging.INFO init_logging(logfile=options.logfile, loglevel=level) debug_config(config)
def configure(): injector = inject.Injector() inject.register(injector) injector.bind('config', to=getConfig) injector.bind('stompEngine', to=FakeEngine) injector.bind('stompProtocol', to=StompProtocol) injector.bind('subject', to=FakeSubject('FakeSubject')) #injected into BaseWord. It's either Host or VM in practice injector.bind('words', to=dict( (('AWord', AWord),) ) ) #gets injected into MsgInterpreter injector.bind('hvController', to=HyperVisorController) return injector
def test_GmailEmailMsg_should_raise_SMTPAuthenticationError(): """GmailEmailMsg should raise SMTPAuthenticationError because of wrong password""" injector = inject.Injector() injector.bind( "gmailAccount", to="*****@*****.**" ) injector.bind( "gmailPassword", to="this is another test password wrong" ) inject.register( injector ) gmail = GmailEmailMsg() temp = GmailEmailTemplate() assert_raises( SMTPAuthenticationError, gmail.sendMessage, "*****@*****.**", "test message", temp.generate( "welcome_email.html", { "userName": "******", "domain": "test.bitpostage.net", "userToken": "1234567890" } ) )
def bind(self): injector = inject.Injector() inject.register(injector) injector.bind(Logger, to=Logger()) injector.bind(DatabaseController, to=DatabaseController()) injector.bind(LocalStorage, to=LocalStorage()) injector.bind(SettingsController, to=SettingsController()) injector.bind(WallpaperController, to=WallpaperController()) injector.bind(WallpaperGenerator, to=WallpaperGenerator()) injector.bind(Downloader, to=Downloader()) injector.bind(EmbededServer, to=EmbededServer()) injector.bind(InstagramFeed, to=InstagramFeed())
def test_GmailEmailMsg_should_not_raise_any_exceptions_on_sending_email(): """GmailEmailMsg should send the email fine without raising any exceptions""" injector = inject.Injector() injector.bind( "gmailAccount", to="*****@*****.**" ) injector.bind( "gmailPassword", to="this is another test password" ) inject.register( injector ) gmail = GmailEmailMsg() temp = GmailEmailTemplate() assert_raises( Exception, closure, gmail.sendMessage, "*****@*****.**", "test message", temp.generate( "welcome_email.html", { "userName": "******", "domain": "test.bitpostage.net", "userToken": "1234567890" } ) ) assert_raises( Exception, closure, gmail.sendMessage, "*****@*****.**", "completion email", temp.generate( "completion_email.html", { "userName": "******", "domain": "test.bitpostage.net", } ) )
def test_ResourcePool_raises_Empty_exception(): """ResourcePool should raise a Empty exception when nothing is left in the pool""" injector = inject.Injector() inject.register( injector ) injector.bind( "resourcePoolTimeout", to=0 ) injector.bind( "maxObjects", to=2 ) resourcePool = ResourcePool() with resourcePool as w: with resourcePool as w1: with resourcePool as w2: pass
def Inject(): """ Setup default injections """ import inject from pylons import config i = inject.Injector() # The examples show passing an instance to but it seems to want a # callable so fake one. i.bind('config', to=lambda: config) # XXX: Ideally this would come from the ini file. I think you have # to use paste to load it. config['converter.job_log_dir'] = '/tmp/test-job-logs' inject.register(i)
def configure(): injector = inject.Injector() inject.register(injector) injector.bind('config', to=getConfig) injector.bind('stompEngine', to=FakeEngine) injector.bind('stompProtocol', to=StompProtocol) injector.bind( 'subject', to=FakeSubject('FakeSubject') ) #injected into BaseWord. It's either Host or VM in practice injector.bind('words', to=dict( (('AWord', AWord), ))) #gets injected into MsgInterpreter injector.bind('hvController', to=HyperVisorController) return injector
def config(settings): """Initialize Dependency Injection :param settings: Application settings :type settings: Settings """ injector = inject.Injector() injector.bind('service_user', to=service_user, scope=inject.appscope) injector.bind('repository_user', to=repository_user, scope=inject.appscope) injector.bind('service_document', to=service_document, scope=inject.appscope) injector.bind('repository_document', to=repository_document, scope=inject.appscope) injector.bind('settings', to=settings, scope=inject.noscope) inject.register(injector)
def CreateServer(config): # Given all configuration options wire up everything required for a # server instance. injector = inject.Injector() inject.register(injector) # Normally you can just pass an instance here but # callable(pylons.config) returns True when it really shouldn't so # inject tries to create an instance instead of using as is. The # same thing can be accomplished with bind_instance() in newer # versions of inject. injector.bind('config', to=lambda: config) ConfigureDatabase(config, injector) ConfigureDatastore(config) s = Server() atexit.register(s.Stop) return s
def init(): """ """ parser = OptionParser() parser.add_option("-c", "--config", dest="configfile", help="Read configuration from FILE. (Overrides default config file.)", metavar="FILE") parser.add_option("-a", "--host", dest="broker_host", help="Listen on specified address for broker.", metavar="ADDR") parser.add_option("-p", "--port", dest="broker_port", help="Listen on specified port for broker.", type="int", metavar="PORT") parser.add_option("-l", "--logfile", dest="logfile", help="Log to specified file.", metavar="FILE") parser.add_option("--debug", action="store_true", dest="debug", default=False, help="Sets logging to debug (unless logging configured in config file).") (options, args) = parser.parse_args() config = init_config() injector = inject.Injector() inject.register(injector) injector.bind('config', to=config) injector.bind('words', to=VMWords.getWords, scope=inject.appscope) injector.bind('stompProtocol', to=StompProtocol, scope=inject.appscope) injector.bind('stompEngine', to=VMStompEngine, scope=inject.appscope) injector.bind('subject', to=VMStompEngine, scope=inject.appscope) if options.broker_host is not None: config.set('broker', 'host', options.broker_host) if options.broker_port is not None: config.set('broker', 'port', str(options.broker_port)) level = logging.DEBUG if options.debug else logging.INFO init_logging(logfile=options.logfile, loglevel=level) debug_config(config)
def init(): """ Initializes VMController Host package. First parses command line options. Then, creates config object from default cfg file. Re-initializes config object if a config file is supplied and sets logger configuration. Finally, uses dependency injection to bind objects to names. """ parser = OptionParser() parser.add_option( "-c", "--config", dest="configfile", help="Read configuration from FILE. (Overrides default config file.)", metavar="FILE") parser.add_option( "-a", "--host", dest="xmlrpc_host", help= "Listen on specified address for XMLRPC interface (default 127.0.0.1)", metavar="ADDR") parser.add_option( "-p", "--port", dest="xmlrpc_port", help="Listen on specified port for XMLRPC interface (default 50505)", type="int", metavar="PORT") parser.add_option("-l", "--logfile", dest="logfile", help="Log to specified file.", metavar="FILE") parser.add_option( "--debug", action="store_true", dest="debug", default=False, help="Sets logging to debug (unless logging configured in config file)." ) (options, args) = parser.parse_args() config = init_config() injector = inject.Injector() inject.register(injector) injector.bind('config', to=config) injector.bind('stompEngine', to=HostStompEngine, scope=inject.appscope) injector.bind('words', to=HostWords.getWords) injector.bind('stompProtocol', to=StompProtocol, scope=inject.appscope) injector.bind('subject', to=Host) injector.bind('hvController', to=HyperVisorController) init_config_file(options.configfile) if options.xmlrpc_host is not None: config.set('xmlrpc', 'host', options.xmlrpc_host) if options.xmlrpc_port is not None: config.set('xmlrpc', 'port', str(options.xmlrpc_port)) level = logging.DEBUG if options.debug else logging.INFO init_logging(logfile=options.logfile, loglevel=level)
import inject @inject.appscope class A(object): @inject.param('b') def __init__(self, b): self.b = b def __unicode__(self): return u"Soy A con b = %s" % self.b __str__ = __unicode__ class Base(object): @inject.param('a', A) def __init__(self, a): print "In Base: a = %s" % a injector = inject.Injector() inject.register(injector) injector.bind('b', to='soy b') #Base gets an instance of A that's automatically constructed. In turn, #A's instance is constructed using the bound instance of 'b' base = Base()
# from sys import argv # if len(argv) < 2: # print "Usage: %s <config-file>" % argv[0] # exit(-1) # else: # configFile = argv[1] # # config = SafeConfigParser() # config.read(configFile) application = service.Application("boinc-vm-controller") configFile = '/etc/boinc-vm-controller/VMConfig.cfg' #FIXME: HARDCODED if not os.path.isfile(configFile): configFile = os.path.basename(configFile) config = SafeConfigParser() config.read(configFile) injector = inject.Injector() inject.register(injector) injector.bind('config', to=config) injector.bind('words', to=VMWords.getWords, scope=inject.appscope) injector.bind('stompProtocol', to=StompProtocol, scope=inject.appscope) injector.bind('stompEngine', to=VMStompEngine, scope=inject.appscope) injector.bind('subject', to=VMStompEngine, scope=inject.appscope) service = start(config) service.setServiceParent(application)
def before_suite(self): inject.register(self.injector) for hook in self.before_suite_hooks: hook()
def before_suite(self): self.configure() inject.register(self.injector) for hook in self.before_suite_hooks: hook()