本文整理汇总了Python中twisted.internet.defer.maybeDeferred函数的典型用法代码示例。如果您正苦于以下问题:Python maybeDeferred函数的具体用法?Python maybeDeferred怎么用?Python maybeDeferred使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了maybeDeferred函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_badCredentials
def test_badCredentials(self):
"""
Test the behavior of locateChild when SACLS are enabled, and
incorrect credentials are given.
should return a 401 UnauthorizedResponse
"""
self.root.resource.useSacls = True
request = SimpleRequest(
self.site,
"GET",
"/principals/",
headers=http_headers.Headers({
"Authorization": ["basic", "%s" % (
"dreid:dreid".encode("base64"),)]}))
resrc, segments = (yield maybeDeferred(
self.root.locateChild, request, ["principals"]
))
try:
resrc, segments = (yield maybeDeferred(
resrc.locateChild, request, ["principals"]
))
except HTTPError, e:
self.assertEquals(e.response.code, 401)
开发者ID:azbarcea,项目名称:calendarserver,代码行数:27,代码来源:test_root.py
示例2: children
def children(self, request, tag):
"""
Renderer which yields all child object tags as table rows.
"""
whenChildren = (
maybeDeferred(self.resource.listChildren)
.addCallback(sorted)
.addCallback(
lambda names: gatherResults(
[maybeDeferred(self.resource.getChild, x) for x in names]
)
.addCallback(lambda children: zip(children, names))
)
)
@whenChildren.addCallback
def gotChildren(children):
for even, [child, name] in zip(cycle(["odd", "even"]), children):
[url, name, size, lastModified, contentType] = map(
str, self.resource.getChildDirectoryEntry(
child, name, request)
)
yield tag.clone().fillSlots(
url=url, name=name, size=str(size),
lastModified=lastModified, even=even, type=contentType,
)
return whenChildren
开发者ID:anemitz,项目名称:calendarserver,代码行数:26,代码来源:extensions.py
示例3: render
def render(self, request):
"""This is a modified version of render() from XMLRPC that will
pass keyword arguments with extra state information if it can.
Currently this just consists of the current request.
"""
request.content.seek(0, 0)
args, functionPath = xmlrpclib.loads(request.content.read())
try:
function = self._getFunction(functionPath)
except xmlrpc.NoSuchFunction:
self._cbRender(
xmlrpc.Fault(self.NOT_FOUND, "no such function %s" % functionPath),
request
)
else:
request.setHeader("content-type", "text/xml")
# Pass as many keyword args to the function as we can.
# Note that this currently doesn't work on protected functions.
kwargs = {}
try:
if 'request' in function.func_code.co_varnames:
kwargs['request'] = request
except AttributeError:
pass
defer.maybeDeferred(function, *args, **kwargs).addErrback(
self._ebRender
).addCallback(
self._cbRender, request
)
return server.NOT_DONE_YET
开发者ID:SkyFire,项目名称:cia-vc,代码行数:32,代码来源:RpcServer.py
示例4: render
def render(self, request):
"""Handle a SOAP command."""
data = request.content.read()
p, header, body, attrs = SOAPpy.parseSOAPRPC(data, 1, 1, 1)
methodName, args, kwargs, ns = p._name, p._aslist, p._asdict, p._ns
# deal with changes in SOAPpy 0.11
if callable(args):
args = args()
if callable(kwargs):
kwargs = kwargs()
function = self.lookupFunction(methodName)
if not function:
self._methodNotFound(request, methodName)
return server.NOT_DONE_YET
else:
if hasattr(function, "useKeywords"):
keywords = {}
for k, v in kwargs.items():
keywords[str(k)] = v
d = defer.maybeDeferred(function, **keywords)
else:
d = defer.maybeDeferred(function, *args)
d.addCallback(self._gotResult, request, methodName)
d.addErrback(self._gotError, request, methodName)
return server.NOT_DONE_YET
开发者ID:0004c,项目名称:VTK,代码行数:31,代码来源:soap.py
示例5: dataPhase
def dataPhase(self):
while len(self.buf) >= NBDRequestHeader.size:
header = NBDRequestHeader(self.buf[:NBDRequestHeader.size])
log.msg("Received %r" % header)
if header.requestType == NBD_CMD_WRITE:
if len(self.buf) < header.size + header.length:
log.msg("Incomplete write request received")
return
data = self.buf[header.size:header.size+header.length]
self.buf = self.buf[header.size+header.length:]
d = defer.maybeDeferred(self.bdev.write, header.offset, data)
d.addCallback(self.reply, header.handle)
d.addErrback(self.errorReply, header.handle)
elif header.requestType == NBD_CMD_READ:
self.buf = self.buf[header.size:]
d = defer.maybeDeferred(self.bdev.read, header.offset, header.length)
d.addCallback(self.reply, header.handle)
d.addErrback(self.errorReply, header.handle)
elif header.requestType == NBD_CMD_DISC:
self.transport.loseConnection()
return
else:
self.buf = self.buf[header.size:]
log.msg("Unsupported request type %d" % header.requestType)
self.transport.write(
struct.pack(">II8s",
NBD_REPLY_MAGIC,
1, # Operation not permitted
header.handle
)
)
开发者ID:therealmik,项目名称:avctproxy,代码行数:31,代码来源:nbdserver.py
示例6: privmsg
def privmsg(self, user, channel, message):
nick, _, host = user.partition('!')
message = message.strip()
if not message.startswith('!'): # not a trigger command
return # do nothing
command, sep, rest = message.lstrip('!').partition(' ')
# Get the function corresponding to the command given.
func = getattr(self, 'command_' + command, None)
# Or, if there was no function, ignore the message.
if func is None:
return
# maybeDeferred will always return a Deferred. It calls func(rest), and
# if that returned a Deferred, return that. Otherwise, return the return
# value of the function wrapped in twisted.internet.defer.succeed. If
# an exception was raised, wrap the traceback in
# twisted.internet.defer.fail and return that.
if command == 'items':
rest = (channel, nick, rest)
d = defer.maybeDeferred(func, rest)
else:
d = defer.maybeDeferred(func, rest)
# Add callbacks to deal with whatever the command results are.
# If the command gives error, the _show_error callback will turn the
# error into a terse message first:
d.addErrback(self._show_error)
# Whatever is returned is sent back as a reply:
if channel == self.nickname:
# When channel == self.nickname, the message was sent to the bot
# directly and not to a channel. So we will answer directly too:
d.addCallback(self._send_message, nick)
else:
# Otherwise, send the answer to the channel, and use the nick
# as addressing in the message itself:
d.addCallback(self._send_message, channel, nick)
开发者ID:techalchemy,项目名称:python-random-ircbot-prelims,代码行数:34,代码来源:bot.py
示例7: getPackagesLoop
def getPackagesLoop(self, result = None):
if result:
if not isinstance(result, failure.Failure):
self.tmppackages.append(result)
else:
logging.getLogger().error("Error: %s", str(result))
if not self.p_apis and self.tmppackages:
# Merge temporary results
lp = self.tmppackages[0]
map(lambda p: _merge_list(lp, p), self.tmppackages)
self.packages.extend(map(lambda m: [m, self.index, self.p_api_first], lp))
self.tmppackages = []
if self.plists and not self.p_apis:
# Fill self.p_apis if empty
self.p_apis = self.plists.pop()
self.p_api_first = self.p_apis[0][0]
self.index = self.index + 1
if self.p_apis:
p_api = self.p_apis.pop()
if 'pending' in self.filt:
d = defer.maybeDeferred(PackageGetA(p_api[0]).getAllPendingPackages, p_api[1])
else:
d = defer.maybeDeferred(PackageGetA(p_api[0]).getAllPackages, p_api[1])
d.addCallbacks(self.getPackagesLoop)
else:
# No more remote call to do, we are done
self.sendResult(self.packages)
开发者ID:pruebagit,项目名称:mmc,代码行数:27,代码来源:package_api.py
示例8: poll
def poll(self):
if self.dq.pending:
return
c = yield maybeDeferred(self.q.count)
if c:
msg = yield maybeDeferred(self.q.pop)
returnValue(self.dq.put(self._message(msg)))
开发者ID:LightKool,项目名称:scraper,代码行数:7,代码来源:poller.py
示例9: twisted_collect
def twisted_collect(self, S3Connection, TwistedS3Connection):
key = Mock()
key.size = 3
bucket = Mock()
bucket.get_all_keys.return_value = maybeDeferred(lambda: [key] * 2)
bucket.name = sentinel.component
connection = Mock()
connection.get_all_buckets.return_value = maybeDeferred(lambda: [bucket])
TwistedS3Connection.adapt.return_value = connection
S3Connection.return_value = None
config = Mock()
config.datasources = [MagicMock()]
config.datasources[0].component = sentinel.component
config.datasources[0].ec2accesskey = "ACCESSKEY"
config.datasources[0].ec2secretkey = "SECRETKEY"
from ZenPacks.zenoss.AWS.dsplugins import S3BucketPlugin
plugin = S3BucketPlugin()
data = yield plugin.collect(config)
self.assertIn(sentinel.component, data["values"])
d = data["values"][sentinel.component]
self.assertEquals(d["keys_count"][0], 2)
self.assertEquals(d["total_size"][0], 6)
开发者ID:zenoss,项目名称:ZenPacks.zenoss.AWS,代码行数:30,代码来源:test_dsplugins.py
示例10: _callMethod
def _callMethod(self, request_dict):
"""
Here we actually find and call the method.
@type request_dict: dict
@param request_dict: Dict with details about the method
@rtype: Deferred
@return: Deferred, that will eventually fire with the method's result.
@raise JSONRPCError: When method not found.
"""
function = getattr(self, 'jsonrpc_%s' % request_dict['method'], None)
if callable(function):
if 'params' in request_dict:
if isinstance(request_dict['params'], dict):
d = maybeDeferred(function, **request_dict['params'])
else:
d = maybeDeferred(function, *request_dict['params'])
else:
d = maybeDeferred(function)
return d
else:
msg = 'Method %s not found' % request_dict['method']
raise jsonrpc.JSONRPCError(msg, jsonrpc.METHOD_NOT_FOUND,
id_=request_dict['id'],
version=request_dict['jsonrpc'])
开发者ID:auxten,项目名称:fastjsonrpc,代码行数:31,代码来源:server.py
示例11: doProvisioning
def doProvisioning(self, authtoken):
"""
Loops on all the provisioners to perform the provisioning.
@param authtoken: AuthenticationToken containing user informations
@type authtoken: AuthenticationToken
@return; Deferred resulting to authtoken
"""
def _cbError(failure):
self.logger.exception(failure.getTraceback())
raise ProvisioningError
d = None
if authtoken.isAuthenticated():
login = authtoken.getLogin()
for name, klass in self.components:
self.logger.debug("Provisioning user with %s / %s" % (name, str(klass)))
instance = klass()
if login.lower() in instance.config.exclude:
self.logger.debug("User %s is in the exclude list of this provisioner, so skipping it" % login)
continue
if not d:
d = defer.maybeDeferred(instance.doProvisioning, authtoken)
else:
d.addCallback(lambda x: defer.maybeDeferred(instance.doProvisioning, authtoken))
if d:
ret = d.addCallback(lambda x:authtoken).addErrback(_cbError)
else:
ret = defer.succeed(authtoken)
return ret
开发者ID:AnatomicJC,项目名称:mmc,代码行数:31,代码来源:provisioning.py
示例12: privmsg
def privmsg(self, user, channel, message, tasks=None):
try:
message = message.decode('utf-8')
except UnicodeDecodeError:
try:
message = message.decode('iso-8859-1')
except UnicodeDecodeError:
message = message.decode('cp1252')
message = cleanblanks(message)
nick, user = self.log(message, user, channel)
d = None
if channel == "#gazouilleur" and not message.startswith("%schans" % config.COMMAND_CHARACTER):
return
if not message.startswith(config.COMMAND_CHARACTER):
if self.nickname.lower() in message.lower():
d = defer.maybeDeferred(self.command_test)
else:
return
message = message.encode('utf-8')
if config.DEBUG:
loggvar("COMMAND: %s: %s" % (user, message), channel)
command, _, rest = message.lstrip(config.COMMAND_CHARACTER).partition(' ')
func = self._find_command_function(command)
if func is None and d is None:
d = defer.maybeDeferred(self.command_help, command, channel, nick)
target = nick if channel == self.nickname else channel
if d is None:
if self._can_user_do(nick, channel, func):
d = defer.maybeDeferred(func, rest, channel, nick)
else:
return self._send_message("Sorry, you don't have the rights to use this command in this channel.", target, nick)
d.addCallback(self._send_message, target, nick, tasks=tasks)
d.addErrback(self._show_error, target, nick)
开发者ID:kerneis,项目名称:gazouilleur,代码行数:33,代码来源:bot.py
示例13: dispatch_message
def dispatch_message(payload, msg, dispatchIn, conv=None):
"""
Dispatches a message by operation in a given class.
Expected message content:
body = {
"op": "operation name here",
"content": # Any valid type here (str, list, dict)
}
"""
try:
log_message(msg)
if "op" in payload:
op = payload['op']
content = payload.get('content','')
opname = 'op_' + str(op)
# dynamically invoke the operation in the given class
if hasattr(dispatchIn, opname):
opf = getattr(dispatchIn, opname)
return defer.maybeDeferred(opf, content, payload, msg)
elif hasattr(dispatchIn,'op_none'):
return defer.maybeDeferred(dispatchIn.op_none, content, payload, msg)
else:
logging.error("Receive() failed. Cannot dispatch to catch")
else:
logging.error("Invalid message. No 'op' in header", payload)
except StandardError, e:
log_exception('Exception while dispatching: ',e)
开发者ID:clemesha-ooi,项目名称:lcaarch,代码行数:29,代码来源:procutils.py
示例14: checkid_setup
def checkid_setup(registry, requestData, user=None):
"""
This method will validate and redirect a successful request to its
return_to param. If the user isn't logged in, or doesn't have an account,
we'll redirect to an internal page.
@param registry: the current OpenID registry
@type registry: L{OpenIDRegistry}
@param requestData: the current request data
@type requestData: L{OpenIDRequest}
@param user: the current user
@type user: L{txopenid.user.User}
@return: association response
@rtype: L{nevow.url.URL}
"""
if(user is not None):
def _identity_state():
return user.hasIdentity(requestData['openid.identity'])
def _trust_state():
return user.trustsRoot(requestData['openid.trust_root'])
if not(yield maybeDeferred(_identity_state)):
return_to = util.appendQuery(OPENID_IDENTITY_URL, requestData)
elif not(yield maybeDeferred(_trust_state)):
return_to = util.appendQuery(OPENID_TRUST_URL, requestData)
else:
return_to = get_login_response(registry, requestData)
else:
return_to = util.appendQuery(OPENID_LOGIN_URL, requestData)
returnValue(URL.fromString(return_to))
开发者ID:iffy,项目名称:txOpenID,代码行数:35,代码来源:protocol.py
示例15: render_POST
def render_POST(self, request):
request.content.seek(0, 0)
request.setHeader(b"content-type", b"text/xml; charset=utf-8")
try:
args, functionPath = xmlrpclib.loads(request.content.read(), use_datetime=self.useDateTime)
except Exception as e:
f = Fault(self.FAILURE, "Can't deserialize input: %s" % (e,))
self._cbRender(f, request)
else:
try:
function = self.lookupProcedure(functionPath)
except Fault as f:
self._cbRender(f, request)
else:
# Use this list to track whether the response has failed or not.
# This will be used later on to decide if the result of the
# Deferred should be written out and Request.finish called.
responseFailed = []
request.notifyFinish().addErrback(responseFailed.append)
if getattr(function, "withRequest", False):
d = defer.maybeDeferred(function, request, *args)
else:
d = defer.maybeDeferred(function, *args)
d.addErrback(self._ebRender)
d.addCallback(self._cbRender, request, responseFailed)
return server.NOT_DONE_YET
开发者ID:ssilverek,项目名称:kodb,代码行数:26,代码来源:xmlrpc.py
示例16: check_password
def check_password(self, username, password):
if username == "":
log.info("Rejected worker for blank username")
defer.returnValue(False)
allowed_chars = Set('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-.')
if Set(username).issubset(allowed_chars) != True:
log.info("Username contains bad arguments")
defer.returnValue(False)
if username.count('.') > 1:
log.info("Username contains multiple . ")
defer.returnValue(False)
# Force username and password to be strings
username = str(username)
password = str(password)
if not settings.USERS_CHECK_PASSWORD and (yield self.user_exists(username)):
defer.returnValue(True)
elif self.cache.get(username) == password:
defer.returnValue(True)
elif (yield defer.maybeDeferred(self.dbi.check_password, username, password)):
self.cache.set(username, password)
defer.returnValue(True)
elif settings.USERS_AUTOADD == True:
uid = yield defer.maybeDeferred(self.dbi.get_uid, username)
if uid != False:
self.dbi.insert_worker(uid, username, password)
self.cache.set(username, password)
defer.returnValue(True)
log.info("Authentication for %s failed" % username)
defer.returnValue(False)
开发者ID:Crypto-Expert,项目名称:stratum-mining,代码行数:31,代码来源:DBInterface.py
示例17: stopService
def stopService(self):
# This will stop plugin Processors
D2 = defer.maybeDeferred(service.MultiService.stopService, self)
U = defer.maybeDeferred(self.udp.stopListening)
T = defer.maybeDeferred(self.tcp.stopListening)
return defer.DeferredList([U,T,D2], consumeErrors=True)
开发者ID:ChannelFinder,项目名称:recsync,代码行数:7,代码来源:application.py
示例18: is_user_admin
def is_user_admin(self, user):
"""Check if an user is admin - returns a deferred!"""
user = user.lower()
d = defer.Deferred()
def _cb_userinfo(userinfo):
if not userinfo:
d.callback(False)
else:
if userinfo[2] in self.get_adminlist():
d.callback(True)
else:
d.callback(False)
def _cb_auth(authinfo):
if not authinfo:
d.callback(False)
else:
if authinfo[1] in self.get_adminlist():
d.callback(True)
else:
d.callback(False)
if self.config["Connection"].get("adminbyhost", False):
maybe_def = defer.maybeDeferred(self.user_info, user)
maybe_def.addCallback(_cb_userinfo)
else:
maybe_def = defer.maybeDeferred(self.get_auth, user)
maybe_def.addCallback(_cb_auth)
return d
开发者ID:DefaultUser,项目名称:PyTIBot,代码行数:31,代码来源:pytibot.py
示例19: _component_start
def _component_start(self):
def on_start(result):
self._component_state = "Started"
self._component_starting_deferred = None
self._component_start_timer()
return True
def on_start_fail(result):
self._component_state = "Stopped"
self._component_starting_deferred = None
log.error(result)
return result
if self._component_state == "Stopped":
if hasattr(self, "start"):
self._component_state = "Starting"
d = maybeDeferred(self.start)
d.addCallback(on_start)
d.addErrback(on_start_fail)
self._component_starting_deferred = d
else:
d = maybeDeferred(on_start, None)
elif self._component_state == "Starting":
return self._component_starting_deferred
elif self._component_state == "Started":
d = succeed(True)
else:
d = fail("Cannot start a component not in a Stopped state!")
return d
开发者ID:Ashod,项目名称:Deluge,代码行数:30,代码来源:component.py
示例20: start
def start(self):
yield defer.maybeDeferred(self.configure)
for spider, batches in self._scheduled.iteritems():
yield self._schedule(spider, batches)
yield defer.maybeDeferred(self.engine.start)
开发者ID:MayankAgarwal,项目名称:scrapy,代码行数:7,代码来源:crawler.py
注:本文中的twisted.internet.defer.maybeDeferred函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论