Skip to content
Permalink
Browse files

Merge branch 'release/3.1.4'

* release/3.1.4:
  Bump version: 3.1.3 → 3.1.4
  Updating history for release.
  Updated documentation with quit_spade.
  Fixed dependencies. Use factories for tests.
  Fixed tests ending.
  - Fixed bug comparing JIDs in trace _agent_in_msg function. - Imports optimized. - Added new example.
  - Fixed little bug in documentation example. - Fixed aiohttp version.
  • Loading branch information
javipalanca committed Nov 4, 2019
2 parents a23ba50 + 1aa3920 commit 229836c97c7ac76651558f57e72e0374f3df5c10
@@ -2,6 +2,15 @@
History
=======

3.1.4 (2019-11-04)
------------------

* Fixed issue with third party versions.
* Use factories in tests.
* Updated documentation and examples.
* Minor bug fixing.


3.1.3 (2019-07-18)
------------------

@@ -157,7 +157,7 @@ An example of how to kill a behaviour::
async def run(self):
print("Counter: {}".format(self.counter))
self.counter += 1
if self.counter >= 3:
if self.counter > 3:
self.kill(exit_code=10)
return
await asyncio.sleep(1)
@@ -172,7 +172,8 @@ An example of how to kill a behaviour::

if __name__ == "__main__":
dummy = DummyAgent("your_jid@your_xmpp_server", "your_password")
dummy.start()
future = dummy.start()
future.result() # Wait until the start method is finished

# wait until user interrupts with ctrl+C
while not dummy.my_behav.is_killed():
@@ -206,6 +207,32 @@ And the output of this example would be::
automatically killed and the exception will be stored as its ``exit_code``.


Finishing SPADE
---------------

There is a helper to quickly finish all the agents and behaviors running in your process. This helper function is
``quit_spade``::

from spade import quit_spade

from spade import agent

class DummyAgent(agent.Agent):
async def setup(self):
print("Hello World! I'm agent {}".format(str(self.jid)))

dummy = DummyAgent("your_jid@your_xmpp_server", "your_password")
dummy.start()

dummy.stop()

quit_spade()



.. hint::
The ``quit_spade`` helper is not mandatory, but it helps to terminate all agents of the active container along with
their behaviors, as well as free all pending resources (threads, etc...).

Creating an agent from within another agent
-------------------------------------------
@@ -0,0 +1,45 @@
import asyncio
import getpass
import time

from spade.agent import Agent
from spade.behaviour import CyclicBehaviour


class DummyAgent(Agent):
class MyBehav(CyclicBehaviour):
async def on_start(self):
print("Starting behaviour . . .")
self.counter = 0

async def run(self):
print("Counter: {}".format(self.counter))
self.counter += 1
if self.counter > 3:
self.kill(exit_code=10)
return
await asyncio.sleep(1)

async def on_end(self):
print("Behaviour finished with exit code {}.".format(self.exit_code))

async def setup(self):
print("Agent starting . . .")
self.my_behav = self.MyBehav()
self.add_behaviour(self.my_behav)


if __name__ == "__main__":
jid1 = input("Agent JID> ")
passwd1 = getpass.getpass()
dummy = DummyAgent(jid1, passwd1)
future = dummy.start()
future.result() # Wait until the start method is finished

# wait until user interrupts with ctrl+C
while not dummy.my_behav.is_killed():
try:
time.sleep(1)
except KeyboardInterrupt:
break
dummy.stop()
@@ -1,9 +1,9 @@
import asyncio
import getpass

from spade import quit_spade
from spade.agent import Agent
from spade.behaviour import OneShotBehaviour
from spade import quit_spade


class DummyAgent(Agent):
@@ -2,10 +2,10 @@
import time
from sys import getsizeof

from spade import quit_spade
from spade.agent import Agent
from spade.behaviour import OneShotBehaviour
from spade.message import Message
from spade import quit_spade


class RecvBehav(OneShotBehaviour):
@@ -1,9 +1,10 @@
import time
import getpass
import time

from spade import quit_spade
from spade.agent import Agent
from spade.behaviour import OneShotBehaviour
from spade import quit_spade


class Agent1(Agent):
async def setup(self):
@@ -1,10 +1,11 @@
import time
import getpass
import time

from spade import quit_spade
from spade.agent import Agent
from spade.behaviour import OneShotBehaviour
from spade.message import Message
from spade.template import Template
from spade import quit_spade


class SenderAgent(Agent):
@@ -2,17 +2,16 @@
import asyncio
import datetime
import random
import click
import time

import aioxmpp
import click
from aioxmpp import PresenceType, Presence, JID, PresenceShow, MessageType
from aioxmpp.roster.xso import Item
import time
from spade.message import Message

from spade.behaviour import State

from spade import agent, behaviour
from spade.behaviour import State
from spade.message import Message
from spade.template import Template


@@ -0,0 +1,2 @@
[pytest]
testpaths = tests/
@@ -1,6 +1,8 @@
multidict==4.4.0
multidict==4.5.2
pyasn1-modules==0.2.7
pyasn1==0.4.7
aioxmpp>=0.10
aiohttp>=3.4.4
aiohttp==3.5.4
aiohttp_jinja2>=0.14.0
jinja2>=2.9.6
jinja2-time==0.2.0
@@ -7,7 +7,7 @@ pyflakes==1.6.0
tox>2.3.1
coverage==4.0.3
Sphinx==1.4.8
cryptography==2.2.2
#cryptography==2.2.2
pyyaml>=4.2b1
pytest==3.6.1
pytest-runner==2.11.1
@@ -23,3 +23,4 @@ sphinx_rtd_theme==0.4.0
testfixtures==6.3.0
pytest-aiohttp==0.3.0
docutils==0.12
factory-boy==2.12.0
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 3.1.3
current_version = 3.1.4
commit = True
tag = False

@@ -27,7 +27,7 @@ def parse_requirements(filename):

setup(
name='spade',
version='3.1.3',
version='3.1.4',
description="Smart Python Agent Development Environment",
long_description=readme + '\n\n' + history,
author="Javi Palanca",
@@ -8,6 +8,6 @@

__author__ = """Javi Palanca"""
__email__ = 'jpalanca@gmail.com'
__version__ = '3.1.3'
__version__ = '3.1.4'

__all__ = ["agent", "behaviour", "message", "template"]
@@ -1,11 +1,10 @@
import asyncio
import logging

import aiosasl
import sys
import asyncio
from hashlib import md5
from threading import Event

import aiosasl
import aioxmpp
import aioxmpp.ibr as ibr
from aioxmpp.dispatcher import SimpleMessageDispatcher
@@ -1,13 +1,12 @@
import asyncio
import collections
import logging
import time
import traceback
from abc import ABCMeta, abstractmethod
from asyncio import CancelledError
from threading import Event
from datetime import timedelta, datetime
import time

import asyncio
from threading import Event
from typing import Any, Union

from .message import Message
@@ -6,7 +6,9 @@


def _agent_in_msg(agent, msg):
return msg.to == agent or msg.sender == agent
to = msg.to.bare() if msg.to else None
sender = msg.sender.bare() if msg.sender else None
return to == agent or sender == agent


class TraceStore(object):
@@ -2,11 +2,12 @@
import logging
import socket

import timeago
from aiohttp import web as aioweb
import aiohttp_jinja2
import jinja2
import timeago
from aiohttp import web as aioweb
from aioxmpp import PresenceType, JID

from spade.message import Message

logger = logging.getLogger("spade.Web")
@@ -0,0 +1,58 @@
import time

import pytest
from aioxmpp import JID

from spade import quit_spade
from spade.container import Container
from spade.message import Message


@pytest.fixture
def jid():
return JID.fromstr("friend@localhost/home")


@pytest.fixture
def message():
return Message(
to="to@localhost",
sender="sender@localhost",
body="message body",
thread="thread-id",
metadata={
"metadata1": "value1",
"metadata2": "value2"
}
)


@pytest.fixture
def message2():
return Message(
to="to2@localhost",
sender="sender2@localhost",
body="message body",
thread="thread-id",
)


@pytest.fixture(autouse=True)
def run_around_tests():
# Code that will run before your test, for example:
# A test function will be run at this point
container = Container()
if not container.is_running:
container.__init__()
yield
# Code that will run after your test, for example:
quit_spade()


def wait_for_behaviour_is_killed(behaviour, tries=500, sleep=0.01):
counter = 0
while not behaviour.is_killed() and counter < tries:
time.sleep(sleep)
counter += 1
if not behaviour.is_killed():
raise Exception("Behaviour not finished")

0 comments on commit 229836c

Please sign in to comment.
You can’t perform that action at this time.