Python 在buildbot master.cfg文件的上下文中,如何使用本机模块基于字符串实例化类
在stock python中,我可以执行以下操作,基于包含其名称的字符串实例化一个类:Python 在buildbot master.cfg文件的上下文中,如何使用本机模块基于字符串实例化类,python,twisted,introspection,buildbot,Python,Twisted,Introspection,Buildbot,在stock python中,我可以执行以下操作,基于包含其名称的字符串实例化一个类: #!/usr/bin/env python2.7 import sys class Foo(object): pass cls = getattr(sys.modules[__name__], 'Foo') instance = cls() print repr(instance) 其输出如下: ahammond@af6119›~⁑ ./test.py <__main__.Foo object at
#!/usr/bin/env python2.7
import sys
class Foo(object): pass
cls = getattr(sys.modules[__name__], 'Foo')
instance = cls()
print repr(instance)
其输出如下:
ahammond@af6119›~⁑ ./test.py
<__main__.Foo object at 0x1095b0a10>
产生
2015-03-11 18:39:24-0700 [-] error while parsing config file:
Traceback (most recent call last):
File "/opt/buildbot_virtualenv/lib/python2.7/site-packages/twisted/internet/defer.py", line 577, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/opt/buildbot_virtualenv/lib/python2.7/site- packages/twisted/internet/defer.py", line 1155, in gotResult
_inlineCallbacks(r, g, deferred)
File "/opt/buildbot_virtualenv/lib/python2.7/site-packages/twisted/internet/defer.py", line 1099, in _inlineCallbacks
result = g.send(result)
File "/opt/buildbot_git/master/buildbot/master.py", line 189, in startService
self.configFileName)
--- <exception caught here> ---
File "/opt/buildbot_git/master/buildbot/config.py", line 156, in loadConfig
exec f in localDict
File "/home/buildbot/master.cfg", line 208, in <module>
cls = getattr(sys.modules[__name__], build_options['build_type'])
exceptions.AttributeError: 'module' object has no attribute 'BuildStashToSrpmsToRpmsToDepot'
2015-03-11 18:39:24-0700 [-] Configuration Errors:
2015-03-11 18:39:24-0700 [-] error while parsing config file: 'module' object has no attribute 'BuildStashToSrpmsToRpmsToDepot' (traceback in logfile)
2015-03-11 18:39:24-0700[-]解析配置文件时出错:
回溯(最近一次呼叫最后一次):
文件“/opt/buildbot\u virtualenv/lib/python2.7/site packages/twisted/internet/defer.py”,第577行,在运行回调中
current.result=回调(current.result,*args,**kw)
文件“/opt/buildbot_virtualenv/lib/python2.7/site-packages/twisted/internet/defer.py”,第1155行,在gotResult中
_内联回调(r、g、延迟)
文件“/opt/buildbot\u virtualenv/lib/python2.7/site packages/twisted/internet/defer.py”,第1099行,在内联回调中
结果=g.send(结果)
文件“/opt/buildbot\u git/master/buildbot/master.py”,第189行,在startService中
self.configFileName)
--- ---
loadConfig中的文件“/opt/buildbot\u git/master/buildbot/config.py”,第156行
localDict中的execf
文件“/home/buildbot/master.cfg”,第208行,在
cls=getattr(系统模块[\uuuuu名称]、构建选项['build\u类型]]
exceptions.AttributeError:“模块”对象没有属性“BuildStathToSrpMstorpMstoDepot”
2015-03-11 18:39:24-0700[-]配置错误:
2015-03-11 18:39:24-0700[-]分析配置文件时出错:“模块”对象没有属性“BuildStathToSrpMstorpMstoDepot”(在日志文件中回溯)
换一种说法,我想我真正想问的是加载新的master.cfg
时使用的临时模块是什么,我如何引用它
我目前正在使用{‘类名’:class_object}的字典映射,但我更喜欢一些本地的东西。您的问题是:当运行buildbot时,您的dunder名称(uuuu name,不带空格…)当buildbot执行时,您的配置是buildbot.config,这就是为什么“模块对象没有属性…” 我想你可以做你想做的事情,让build_选项字典的值成为类本身,而不是带有类名的字符串。像这样:
class BaseBuild(object): pass
class BuildStashToSrpmsToRpmsToDepot(BaseBuild):
def init(name, build_type):
self.name = name
def setup():
pass # actually does stuff here, but...
# here the dict goes with key/class not key/class name
build_options = {'build_type': BuildStashToSrpmsToRpmsToDepot}
for build_name, build_options in config['builds'].iteritems():
cls = build_options['build_type']
build = cls(name=build_name, **build_options)
build.setup()
以防万一,以下是buildbot exec master.cfg(module buildbot.config)的用法:
好的,问题在这里:
cls = getattr(sys.modules[__name__], build_options['build_type'])
这不起作用,因为exec
使\uuuuuu name\uuuuuuu
具有值“\uuuuuu内置”
。但是,您可以使用globals()
获取当前全局:
cls = globals()[build_options['build_type']]
例如,如果我将以下代码添加到全新的master.cfg
文件中(由buildbot create master
自动创建的文件,从master.cfg.sample
重命名):
我在与master.cfg
相同的目录中创建了以下config.json
文件:
{
"builds": {
"one": {
"build_type": "Something"
},
"two": {
"build_type": "SomethingElse"
}
}
}
然后,当我运行buildbot start master
时,我将在日志中获得以下行:
2015-03-13 12:11:05-0400 [-] two something else setup called
2015-03-13 12:11:05-0400 [-] one something setup called
# Load the configuration from somewhere.
import json
config = json.load(open("./config.json"))
class BaseBuild(object):
pass
class Something(BaseBuild):
def __init__(self, name, build_type):
self.name = name
def setup(self):
print self.name, "something setup called"
class SomethingElse(BaseBuild):
def __init__(self, name, build_type):
self.name = name
def setup(self):
print self.name, "something else setup called"
for build_name, build_options in config['builds'].iteritems():
cls = globals()[build_options['build_type']]
build = cls(name=build_name, **build_options)
build.setup()
{
"builds": {
"one": {
"build_type": "Something"
},
"two": {
"build_type": "SomethingElse"
}
}
}
2015-03-13 12:11:05-0400 [-] two something else setup called
2015-03-13 12:11:05-0400 [-] one something setup called