async def test_agent_stop_with_stop(): agent = Agent('test-agent') task = asyncio.create_task(agent.start()) await asyncio.sleep(0) assert agent._running is True agent.stop() await asyncio.gather(task) assert agent._running is False
async def test_agent_stop_with_shutdown_trigger(): agent = Agent('test-agent') shutdown_trigger = Event() task = asyncio.create_task(agent.start(shutdown_trigger=shutdown_trigger)) await asyncio.sleep(0) assert agent._running is True assert agent._shutdown_trigger == shutdown_trigger shutdown_trigger.set() await asyncio.gather(task) assert agent._running is False
import asyncio import random from tkinter import * from zentropi import Agent a = Agent('switch') def stop_agent(*_): a.shutdown_event.set() btn = None btn_state = False def button_toggle(): global btn_state if btn_state: btn_state = False print('switch off') btn.configure(text='off') a.spawn(a.event('switch-off')) else: btn_state = True print('switch on') btn.configure(text='on') a.spawn(a.event('switch-on'))
from zentropi import Agent a = Agent('test-agent') @a.on_event('startup') async def hello(frame): print('Hello, world!') a.stop() a.run()
import os import traceback import jmespath import tweepy as tweepy from zentropi import Agent, on_event auth = tweepy.OAuthHandler(os.getenv('ZENTROPI_TWITTER_API_KEY'), os.getenv('ZENTROPI_TWITTER_API_SECRET')) auth.set_access_token(os.getenv('ZENTROPI_TWITTER_OAUTH_TOKEN'), os.getenv('ZENTROPI_TWITTER_OAUTH_SECRET')) # Construct the API instance api = tweepy.API(auth) agent = Agent() class ZentropiStreamListener(tweepy.StreamListener): def __init__(self): super().__init__() def on_error(self, status_code): if status_code == 420: return False def on_data(self, data): try: frame = json.loads(data) screen_name = frame.get('user', {}).get('screen_name', None) text = frame.get('text', None)
from zentropi import Agent a = Agent('test-agent') @a.on_interval('test', 2) async def test(frame): await a.event('test') @a.on_event('test') async def hello(frame): space_name = frame.meta.get("space").get("name") print(f'Hello, {space_name}.') a.run( 'ws://localhost:26514/', token='test-token', )
def test_agent_init(): agent = Agent('test-agent') assert agent.name == 'test-agent'
import subprocess from zentropi import Agent a = Agent('translate-events') @a.on_event('*') async def translate_events(frame): if frame.name == 'random-value': await a.event('intel-backlight', **frame.data) a.run( 'ws://localhost:26514/', token='test-token', )
import subprocess from zentropi import Agent a = Agent('intel-backlight') @a.on_event('intel-backlight') async def backlight(frame): value = frame.data.get("value") subprocess.run(['intel_backlight', str(value)]) a.run( 'ws://localhost:26514/', token='test-token', )
import random from zentropi import Agent a = Agent('random-value') @a.on_interval('random-value', 2) async def random_value(): await a.event('random-value', value=random.choice(range(20, 80))) a.run( 'ws://localhost:26514/', token='dfa50132fcea4105a69cba3c1424429b', )