本文整理匯總了Python中twisted.protocols.amp.AMP屬性的典型用法代碼示例。如果您正苦於以下問題:Python amp.AMP屬性的具體用法?Python amp.AMP怎麽用?Python amp.AMP使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類twisted.protocols.amp
的用法示例。
在下文中一共展示了amp.AMP屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: connectionMade
# 需要導入模塊: from twisted.protocols import amp [as 別名]
# 或者: from twisted.protocols.amp import AMP [as 別名]
def connectionMade(self):
"""
When connection is made, create the AMP protocol instance.
"""
self._ampProtocol.makeConnection(LocalWorkerTransport(self.transport))
if not os.path.exists(self._logDirectory):
os.makedirs(self._logDirectory)
self._outLog = open(os.path.join(self._logDirectory, 'out.log'), 'w')
self._errLog = open(os.path.join(self._logDirectory, 'err.log'), 'w')
testLog = open(os.path.join(self._logDirectory, self._logFile), 'w')
self._ampProtocol.setTestStream(testLog)
logDirectory = self._logDirectory
if isinstance(logDirectory, unicode):
logDirectory = logDirectory.encode("utf-8")
d = self._ampProtocol.callRemote(workercommands.Start,
directory=logDirectory)
# Ignore the potential errors, the test suite will fail properly and it
# would just print garbage.
d.addErrback(lambda x: None)
示例2: setUp
# 需要導入模塊: from twisted.protocols import amp [as 別名]
# 或者: from twisted.protocols.amp import AMP [as 別名]
def setUp(self):
self.managerTransport = StringTransport()
self.managerAMP = LocalWorkerAMP()
self.managerAMP.makeConnection(self.managerTransport)
self.result = TestResult()
self.workerTransport = StringTransport()
self.worker = AMP()
self.worker.makeConnection(self.workerTransport)
config = trial.Options()
self.testName = b"twisted.doesnexist"
config['tests'].append(self.testName)
self.testCase = trial._getSuite(config)._tests.pop()
self.managerAMP.run(self.testCase, self.result)
self.managerTransport.clear()
示例3: test_processEnded
# 需要導入模塊: from twisted.protocols import amp [as 別名]
# 或者: from twisted.protocols.amp import AMP [as 別名]
def test_processEnded(self):
"""
L{LocalWorker.processEnded} calls C{connectionLost} on itself and on
the L{AMP} protocol.
"""
class FakeStream(object):
callNumber = 0
def close(self):
self.callNumber += 1
transport = FakeTransport()
protocol = FakeAMProtocol()
localWorker = LocalWorker(protocol, '.', 'test.log')
localWorker.makeConnection(transport)
localWorker._outLog = FakeStream()
localWorker.processEnded(Failure(CONNECTION_DONE))
self.assertEqual(localWorker._outLog.callNumber, 1)
self.assertIdentical(None, protocol.transport)
return self.assertFailure(localWorker.endDeferred, ConnectionDone)
示例4: test_lookupFunctionDeprecatedOverride
# 需要導入模塊: from twisted.protocols import amp [as 別名]
# 或者: from twisted.protocols.amp import AMP [as 別名]
def test_lookupFunctionDeprecatedOverride(self):
"""
Subclasses which override locateResponder under its old name,
lookupFunction, should have the override invoked instead. (This tests
an AMP subclass, because in the version of the code that could invoke
this deprecated code path, there was no L{CommandLocator}.)
"""
locator = OverrideLocatorAMP()
customResponderObject = self.assertWarns(
PendingDeprecationWarning,
"Override locateResponder, not lookupFunction.",
__file__, lambda : locator.locateResponder(b"custom"))
self.assertEqual(locator.customResponder, customResponderObject)
# Make sure upcalling works too
normalResponderObject = self.assertWarns(
PendingDeprecationWarning,
"Override locateResponder, not lookupFunction.",
__file__, lambda : locator.locateResponder(b"simple"))
result = normalResponderObject(amp.Box(greeting=b"ni hao", cookie=b"5"))
def done(values):
self.assertEqual(values, amp.AmpBox(cookieplus=b'8'))
return result.addCallback(done)
示例5: test_protocolSwitchFail
# 需要導入模塊: from twisted.protocols import amp [as 別名]
# 或者: from twisted.protocols.amp import AMP [as 別名]
def test_protocolSwitchFail(self, switcher=SimpleSymmetricCommandProtocol):
"""
Verify that if we try to switch protocols and it fails, the connection
stays up and we can go back to speaking AMP.
"""
self.testSucceeded = False
serverDeferred = defer.Deferred()
serverProto = switcher(serverDeferred)
clientDeferred = defer.Deferred()
clientProto = switcher(clientDeferred)
c, s, p = connectedServerAndClient(ServerClass=lambda: serverProto,
ClientClass=lambda: clientProto)
L = []
c.switchToTestProtocol(fail=True).addErrback(L.append)
p.flush()
L.pop().trap(UnknownProtocol)
self.assertFalse(self.testSucceeded)
# It's a known error, so let's send a "hello" on the same connection;
# it should work.
c.sendHello(b'world').addCallback(L.append)
p.flush()
self.assertEqual(L.pop()['hello'], b'world')
示例6: test_toStringProto
# 需要導入模塊: from twisted.protocols import amp [as 別名]
# 或者: from twisted.protocols.amp import AMP [as 別名]
def test_toStringProto(self):
"""
To send a file descriptor, L{Descriptor.toStringProto} uses the
L{IUNIXTransport.sendFileDescriptor} implementation of the transport of
the protocol passed to it to copy the file descriptor. Each subsequent
descriptor sent over a particular AMP connection is assigned the next
integer value, starting from 0. The base ten string representation of
this value is the byte encoding of the argument.
This is a whitebox test which involves direct L{_DescriptorExchanger}
state inspection and mutation.
"""
argument = amp.Descriptor()
self.assertEqual(b"0", argument.toStringProto(2, self.protocol))
self.assertEqual(
("fileDescriptorReceived", 2 + self.fuzz), self.transport._queue.pop(0))
self.assertEqual(b"1", argument.toStringProto(4, self.protocol))
self.assertEqual(
("fileDescriptorReceived", 4 + self.fuzz), self.transport._queue.pop(0))
self.assertEqual(b"2", argument.toStringProto(6, self.protocol))
self.assertEqual(
("fileDescriptorReceived", 6 + self.fuzz), self.transport._queue.pop(0))
self.assertEqual({}, self.protocol._descriptors)
示例7: test_send_era_on_connect
# 需要導入模塊: from twisted.protocols import amp [as 別名]
# 或者: from twisted.protocols.amp import AMP [as 別名]
def test_send_era_on_connect(self):
"""
Upon connecting a ``SetNodeEraCommand`` is sent with the current
node's era and UUID.
"""
client = AgentAMP(self.reactor, self.service)
# The object that processes incoming AMP commands:
server_locator = UpdateNodeEraLocator()
server = AMP(locator=server_locator)
pump = connectedServerAndClient(lambda: client, lambda: server)[2]
pump.flush()
self.assertEqual(
# Actual result of handling AMP commands, if any:
dict(era=server_locator.era, uuid=server_locator.uuid),
# Expected result:
dict(era=unicode(self.service.era),
uuid=unicode(self.deployer.node_uuid)))
示例8: test_sendTableWithName
# 需要導入模塊: from twisted.protocols import amp [as 別名]
# 或者: from twisted.protocols.amp import AMP [as 別名]
def test_sendTableWithName(self):
"""
You can send a reference to a table through a L{SchemaAMP} via
L{TableSyntaxByName}.
"""
client = AMP()
class SampleCommand(Command):
arguments = [("id", Integer())]
class Receiver(AMP):
@SampleCommand.responder
def gotIt(self, id):
self.it = id
return {}
server = Receiver()
clientT = StringTransport()
serverT = StringTransport()
client.makeConnection(clientT)
server.makeConnection(serverT)
client.callRemote(SampleCommand, id=123)
server.dataReceived(clientT.io.getvalue())
self.assertEqual(server.it, 123)
示例9: failsafeResponder
# 需要導入模塊: from twisted.protocols import amp [as 別名]
# 或者: from twisted.protocols.amp import AMP [as 別名]
def failsafeResponder(command):
"""
Wrap an AMP command responder in some fail-safe logic, to make it so that
unknown errors won't drop the connection, as AMP's default behavior would.
"""
def wrap(inner):
@inlineCallbacks
def innerinner(*a, **k):
try:
val = yield inner(*a, **k)
except:
f = Failure()
if f.type in command.errors:
returnValue(f)
else:
log.failure("shared database connection pool error", failure=f)
raise FailsafeException()
else:
returnValue(val)
return command.responder(innerinner)
return wrap
示例10: connectionMade
# 需要導入模塊: from twisted.protocols import amp [as 別名]
# 或者: from twisted.protocols.amp import AMP [as 別名]
def connectionMade(self):
"""
When connection is made, create the AMP protocol instance.
"""
self._ampProtocol.makeConnection(LocalWorkerTransport(self.transport))
if not os.path.exists(self._logDirectory):
os.makedirs(self._logDirectory)
self._outLog = open(os.path.join(self._logDirectory, 'out.log'), 'wb')
self._errLog = open(os.path.join(self._logDirectory, 'err.log'), 'wb')
self._testLog = open(
os.path.join(self._logDirectory, self._logFile), 'w')
self._ampProtocol.setTestStream(self._testLog)
logDirectory = self._logDirectory
d = self._ampProtocol.callRemote(workercommands.Start,
directory=logDirectory)
# Ignore the potential errors, the test suite will fail properly and it
# would just print garbage.
d.addErrback(lambda x: None)
示例11: setUp
# 需要導入模塊: from twisted.protocols import amp [as 別名]
# 或者: from twisted.protocols.amp import AMP [as 別名]
def setUp(self):
self.managerTransport = StringTransport()
self.managerAMP = LocalWorkerAMP()
self.managerAMP.makeConnection(self.managerTransport)
self.result = TestResult()
self.workerTransport = StringTransport()
self.worker = AMP()
self.worker.makeConnection(self.workerTransport)
config = trial.Options()
self.testName = "twisted.doesnexist"
config['tests'].append(self.testName)
self.testCase = trial._getSuite(config)._tests.pop()
self.managerAMP.run(self.testCase, self.result)
self.managerTransport.clear()
示例12: test_processEnded
# 需要導入模塊: from twisted.protocols import amp [as 別名]
# 或者: from twisted.protocols.amp import AMP [as 別名]
def test_processEnded(self):
"""
L{LocalWorker.processEnded} calls C{connectionLost} on itself and on
the L{AMP} protocol.
"""
transport = FakeTransport()
protocol = FakeAMProtocol()
localWorker = LocalWorker(protocol, '.', 'test.log')
localWorker.makeConnection(transport)
localWorker.processEnded(Failure(CONNECTION_DONE))
self.assertTrue(localWorker._outLog.closed)
self.assertTrue(localWorker._errLog.closed)
self.assertTrue(localWorker._testLog.closed)
self.assertIdentical(None, protocol.transport)
return self.assertFailure(localWorker.endDeferred, ConnectionDone)
示例13: __init__
# 需要導入模塊: from twisted.protocols import amp [as 別名]
# 或者: from twisted.protocols.amp import AMP [as 別名]
def __init__(self, directory):
"""
"""
amp.AMP.__init__(self)
self._directory = directory
# How to large we let an AMP response get before breaking it up
self._maxSize = 55000
# The cache of results we have not fully responded with. A dictionary
# whose keys are "continuation tokens" and whose values are tuples of
# (timestamp, list-of-records). When a response does not fit within
# AMP size limits, the remaining records are stored in this dictionary
# keyed by an opaque token we generate to return to the client so that
# it can ask for the remaining results later.
self._continuations = {}
示例14: recordToDict
# 需要導入模塊: from twisted.protocols import amp [as 別名]
# 或者: from twisted.protocols.amp import AMP [as 別名]
def recordToDict(self, record):
"""
Turn a record in a dictionary of fields which can be reconstituted
within the client
"""
fields = {}
if record is not None:
for field, value in record.fields.iteritems():
# FIXME: need to sort out dealing with enormous groups; we
# can ignore these when sending AMP responses because the
# client will always fetch members via a members( ) AMP
# command.
if field.name in (u"memberDNs", u"memberUIDs"):
continue
valueType = record.service.fieldName.valueType(field)
if valueType in (unicode, bool):
fields[field.name] = value
elif valueType is uuid.UUID:
fields[field.name] = str(value)
elif issubclass(valueType, (Names, NamedConstant)):
fields[field.name] = value.name if value else None
return fields
示例15: _sendCommand
# 需要導入模塊: from twisted.protocols import amp [as 別名]
# 或者: from twisted.protocols.amp import AMP [as 別名]
def _sendCommand(self, command, **kwds):
"""
Execute a remote AMP command, first making the connection to the peer.
Any kwds are passed on to the AMP command.
@param command: the AMP command to call
@type command: L{twisted.protocols.amp.Command}
"""
ampProto = (yield self._getConnection())
try:
results = (yield ampProto.callRemote(command, **kwds))
except Exception, e:
log.error("Failed AMP command", error=e)
# FIXME: is there a way to hook into ConnectionLost?
self._connection = None
raise