microagent 1.8.3__tar.gz → 1.9.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. {microagent-1.8.3 → microagent-1.9.0}/.github/workflows/python-app.yml +1 -1
  2. {microagent-1.8.3 → microagent-1.9.0}/CHANGELOG.rst +9 -0
  3. {microagent-1.8.3 → microagent-1.9.0}/PKG-INFO +10 -4
  4. {microagent-1.8.3 → microagent-1.9.0}/README.rst +9 -3
  5. {microagent-1.8.3 → microagent-1.9.0}/docs/agent.rst +9 -0
  6. {microagent-1.8.3 → microagent-1.9.0}/docs/broker.rst +1 -0
  7. microagent-1.9.0/docs/bus.rst +78 -0
  8. {microagent-1.8.3 → microagent-1.9.0}/docs/example_run.rst +12 -1
  9. {microagent-1.8.3 → microagent-1.9.0}/docs/index.rst +9 -3
  10. microagent-1.9.0/docs/launcher.rst +43 -0
  11. {microagent-1.8.3 → microagent-1.9.0}/examples/comment_agent.py +8 -6
  12. microagent-1.9.0/examples/email_agent.py +18 -0
  13. {microagent-1.8.3 → microagent-1.9.0}/examples/push_agent.py +6 -6
  14. {microagent-1.8.3 → microagent-1.9.0}/examples/settings.py +6 -1
  15. {microagent-1.8.3 → microagent-1.9.0}/examples/signals.json +1 -0
  16. {microagent-1.8.3 → microagent-1.9.0}/examples/user_agent.py +8 -7
  17. {microagent-1.8.3 → microagent-1.9.0}/microagent/__init__.py +89 -15
  18. {microagent-1.8.3 → microagent-1.9.0}/microagent/abc.py +2 -0
  19. {microagent-1.8.3 → microagent-1.9.0}/microagent/agent.py +12 -6
  20. {microagent-1.8.3 → microagent-1.9.0}/microagent/broker.py +10 -9
  21. {microagent-1.8.3 → microagent-1.9.0}/microagent/bus.py +12 -8
  22. {microagent-1.8.3 → microagent-1.9.0}/microagent/hooks.py +2 -0
  23. {microagent-1.8.3 → microagent-1.9.0}/microagent/launcher.py +27 -8
  24. {microagent-1.8.3 → microagent-1.9.0}/microagent/queue.py +26 -16
  25. {microagent-1.8.3 → microagent-1.9.0}/microagent/signal.py +27 -8
  26. {microagent-1.8.3 → microagent-1.9.0}/microagent/timer.py +37 -32
  27. {microagent-1.8.3 → microagent-1.9.0}/microagent/tools/amqp.py +9 -6
  28. {microagent-1.8.3 → microagent-1.9.0}/microagent/tools/kafka.py +3 -2
  29. {microagent-1.8.3 → microagent-1.9.0}/microagent/tools/mocks.py +3 -4
  30. {microagent-1.8.3 → microagent-1.9.0}/microagent/tools/redis.py +4 -3
  31. {microagent-1.8.3 → microagent-1.9.0}/microagent/utils.py +1 -0
  32. {microagent-1.8.3 → microagent-1.9.0}/microagent.egg-info/PKG-INFO +10 -4
  33. {microagent-1.8.3 → microagent-1.9.0}/pyproject.toml +9 -9
  34. {microagent-1.8.3 → microagent-1.9.0}/tests/conftest.py +3 -0
  35. microagent-1.9.0/tests/test_api.py +181 -0
  36. microagent-1.8.3/docs/bus.rst +0 -43
  37. microagent-1.8.3/docs/launcher.rst +0 -23
  38. microagent-1.8.3/examples/email_agent.py +0 -17
  39. microagent-1.8.3/tests/test_api.py +0 -75
  40. {microagent-1.8.3 → microagent-1.9.0}/.gitignore +0 -0
  41. {microagent-1.8.3 → microagent-1.9.0}/.readthedocs.yml +0 -0
  42. {microagent-1.8.3 → microagent-1.9.0}/.travis.yml +0 -0
  43. {microagent-1.8.3 → microagent-1.9.0}/LICENSE.md +0 -0
  44. {microagent-1.8.3 → microagent-1.9.0}/Makefile +0 -0
  45. {microagent-1.8.3 → microagent-1.9.0}/docs/Makefile +0 -0
  46. {microagent-1.8.3 → microagent-1.9.0}/docs/amqp.rst +0 -0
  47. {microagent-1.8.3 → microagent-1.9.0}/docs/conf.py +0 -0
  48. {microagent-1.8.3 → microagent-1.9.0}/docs/example_agents.rst +0 -0
  49. {microagent-1.8.3 → microagent-1.9.0}/docs/hooks.rst +0 -0
  50. {microagent-1.8.3 → microagent-1.9.0}/docs/kafka.rst +0 -0
  51. {microagent-1.8.3 → microagent-1.9.0}/docs/ma-bus.dia +0 -0
  52. {microagent-1.8.3 → microagent-1.9.0}/docs/ma-bus.svg +0 -0
  53. {microagent-1.8.3 → microagent-1.9.0}/docs/ma-logo.dia +0 -0
  54. {microagent-1.8.3 → microagent-1.9.0}/docs/ma-logo.svg +0 -0
  55. {microagent-1.8.3 → microagent-1.9.0}/docs/ma-periodic.dia +0 -0
  56. {microagent-1.8.3 → microagent-1.9.0}/docs/ma-periodic.svg +0 -0
  57. {microagent-1.8.3 → microagent-1.9.0}/docs/ma-queue.dia +0 -0
  58. {microagent-1.8.3 → microagent-1.9.0}/docs/ma-queue.svg +0 -0
  59. {microagent-1.8.3 → microagent-1.9.0}/docs/mocks.rst +0 -0
  60. {microagent-1.8.3 → microagent-1.9.0}/docs/periodic.rst +0 -0
  61. {microagent-1.8.3 → microagent-1.9.0}/docs/redis.rst +0 -0
  62. {microagent-1.8.3 → microagent-1.9.0}/docs/requirements.txt +0 -0
  63. {microagent-1.8.3 → microagent-1.9.0}/examples/__init__.py +0 -0
  64. {microagent-1.8.3 → microagent-1.9.0}/examples/redis_server.py +0 -0
  65. {microagent-1.8.3 → microagent-1.9.0}/examples/requirements.txt +0 -0
  66. {microagent-1.8.3 → microagent-1.9.0}/microagent/py.typed +0 -0
  67. {microagent-1.8.3 → microagent-1.9.0}/microagent/tools/__init__.py +0 -0
  68. {microagent-1.8.3 → microagent-1.9.0}/microagent.egg-info/SOURCES.txt +0 -0
  69. {microagent-1.8.3 → microagent-1.9.0}/microagent.egg-info/dependency_links.txt +0 -0
  70. {microagent-1.8.3 → microagent-1.9.0}/microagent.egg-info/entry_points.txt +0 -0
  71. {microagent-1.8.3 → microagent-1.9.0}/microagent.egg-info/requires.txt +0 -0
  72. {microagent-1.8.3 → microagent-1.9.0}/microagent.egg-info/top_level.txt +0 -0
  73. {microagent-1.8.3 → microagent-1.9.0}/setup.cfg +0 -0
  74. {microagent-1.8.3 → microagent-1.9.0}/tests/__init__.py +0 -0
  75. {microagent-1.8.3 → microagent-1.9.0}/tests/stuff.json +0 -0
  76. {microagent-1.8.3 → microagent-1.9.0}/tests/test_agent.py +0 -0
  77. {microagent-1.8.3 → microagent-1.9.0}/tests/test_broker.py +0 -0
  78. {microagent-1.8.3 → microagent-1.9.0}/tests/test_bus.py +0 -0
  79. {microagent-1.8.3 → microagent-1.9.0}/tests/test_hook.py +0 -0
  80. {microagent-1.8.3 → microagent-1.9.0}/tests/test_launcher.py +0 -0
  81. {microagent-1.8.3 → microagent-1.9.0}/tests/test_timer.py +0 -0
  82. {microagent-1.8.3 → microagent-1.9.0}/tests/test_tools_amqp.py +0 -0
  83. {microagent-1.8.3 → microagent-1.9.0}/tests/test_tools_kafka.py +0 -0
  84. {microagent-1.8.3 → microagent-1.9.0}/tests/test_tools_redis.py +0 -0
@@ -20,7 +20,7 @@ jobs:
20
20
  enable-cache: true
21
21
 
22
22
  - name: Lint with ruff
23
- run: uvx ruff check microagent
23
+ run: uvx ruff --version && uvx ruff check microagent
24
24
 
25
25
  - name: Lint with mypy
26
26
  run: uvx mypy microagent
@@ -3,6 +3,15 @@ Changelog
3
3
  =========
4
4
 
5
5
 
6
+ 1.9.0 (2026-07-29)
7
+ ==================
8
+
9
+ - New ``configure()`` for loading signals and queues
10
+ - ``Signal`` and ``Queue`` class attribute access
11
+ - ``DoubleLoadError`` for duplicate loading
12
+ - Docs and examples update
13
+
14
+
6
15
  1.8.3 (2026-06-18)
7
16
  ==================
8
17
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: microagent
3
- Version: 1.8.3
3
+ Version: 1.9.0
4
4
  Summary: Tool for agent ecosystem
5
5
  Author-email: Dmitriy Vlasov <scailer@yandex.ru>
6
6
  License: MIT License
@@ -119,6 +119,12 @@ See MicroAgent documentation_.
119
119
 
120
120
  .. code-block:: python
121
121
 
122
+ from microagent import (MicroAgent, Queue, Signal, configure,
123
+ consumer, cron, on, periodic, receiver)
124
+ from microagent.tools.redis import RedisBroker, RedisSignalBus
125
+
126
+ configure('file://signals.json')
127
+
122
128
  class Agent(MicroAgent):
123
129
 
124
130
  @on('pre_start')
@@ -134,13 +140,13 @@ See MicroAgent documentation_.
134
140
  pass # do something by schedule
135
141
 
136
142
  # subscribe to signals (events)
137
- @receiver(signals.user_updated, signals.user_deleted)
143
+ @receiver(Signal.user_updated, Signal.user_deleted)
138
144
  async def send_notification(self, **kwargs):
139
145
  # send signal (event) to bus
140
146
  await self.bus.check_something.send(sender='agent', **kwargs)
141
147
 
142
148
  # message consumer from queue
143
- @consumer(queues.mailer)
149
+ @consumer(Queue.mailer)
144
150
  async def send_emails(self, **kwargs):
145
151
  # send message to queue
146
152
  await self.broker.statistic_collector.send(kwargs)
@@ -152,7 +158,7 @@ See MicroAgent documentation_.
152
158
 
153
159
  # usage bus and broker separate from agent
154
160
  await bus.started.send('user_agent')
155
- await broker.mailer(data)
161
+ await broker.mailer.send(data)
156
162
 
157
163
  agent = Agent(bus=bus, broker=broker)
158
164
  await agent.start()
@@ -52,6 +52,12 @@ See MicroAgent documentation_.
52
52
 
53
53
  .. code-block:: python
54
54
 
55
+ from microagent import (MicroAgent, Queue, Signal, configure,
56
+ consumer, cron, on, periodic, receiver)
57
+ from microagent.tools.redis import RedisBroker, RedisSignalBus
58
+
59
+ configure('file://signals.json')
60
+
55
61
  class Agent(MicroAgent):
56
62
 
57
63
  @on('pre_start')
@@ -67,13 +73,13 @@ See MicroAgent documentation_.
67
73
  pass # do something by schedule
68
74
 
69
75
  # subscribe to signals (events)
70
- @receiver(signals.user_updated, signals.user_deleted)
76
+ @receiver(Signal.user_updated, Signal.user_deleted)
71
77
  async def send_notification(self, **kwargs):
72
78
  # send signal (event) to bus
73
79
  await self.bus.check_something.send(sender='agent', **kwargs)
74
80
 
75
81
  # message consumer from queue
76
- @consumer(queues.mailer)
82
+ @consumer(Queue.mailer)
77
83
  async def send_emails(self, **kwargs):
78
84
  # send message to queue
79
85
  await self.broker.statistic_collector.send(kwargs)
@@ -85,7 +91,7 @@ See MicroAgent documentation_.
85
91
 
86
92
  # usage bus and broker separate from agent
87
93
  await bus.started.send('user_agent')
88
- await broker.mailer(data)
94
+ await broker.mailer.send(data)
89
95
 
90
96
  agent = Agent(bus=bus, broker=broker)
91
97
  await agent.start()
@@ -13,6 +13,15 @@ Decorators details.
13
13
  :members: receiver, consumer, periodic, cron, on
14
14
 
15
15
 
16
+ Loading signals and queues from config-file.
17
+
18
+ .. autofunction:: microagent.configure
19
+ :noindex:
20
+
21
+
22
+ .. autofunction:: microagent.load_stuff
23
+
24
+
16
25
  .. autoclass:: MicroAgent
17
26
  :members:
18
27
  :member-order: bysource
@@ -42,3 +42,4 @@ Queue broker
42
42
 
43
43
 
44
44
  .. autoclass:: microagent.queue.SerializingError
45
+
@@ -0,0 +1,78 @@
1
+ .. _bus:
2
+
3
+ Signal bus
4
+ ======================================
5
+
6
+
7
+ Default channel prefix
8
+ ----------------------
9
+
10
+ Channel prefix is used to construct channel names for signals on the bus.
11
+ It can be set in two ways:
12
+
13
+ 1. Explicitly via ``prefix`` argument to bus constructor
14
+ 2. Globally via ``default_prefix`` field in configuration JSON file
15
+
16
+ If ``default_prefix`` is specified in the config file, it overrides the
17
+ default value (``PUBSUB``) for all signal buses created after
18
+ ``configure()`` is called. Explicit ``prefix`` argument always overrides
19
+ the global default.
20
+
21
+ .. code-block:: python
22
+
23
+ from microagent import configure
24
+ from microagent.tools.redis import RedisSignalBus
25
+
26
+ # signals.json: {"default_prefix": "MYAPP", "signals": [...]}
27
+ configure('file://signals.json')
28
+
29
+ # Bus will use 'MYAPP' as prefix (from default_prefix in json)
30
+ bus = RedisSignalBus('redis://localhost/7')
31
+
32
+ # Explicit prefix overrides the global default
33
+ bus2 = RedisSignalBus('redis://localhost/7', prefix='OTHER')
34
+
35
+
36
+ .. automodule:: microagent.bus
37
+
38
+
39
+ .. autofunction:: microagent.configure
40
+
41
+
42
+ .. autofunction:: microagent.load_signals
43
+
44
+
45
+ .. autofunction:: microagent.receiver
46
+
47
+
48
+ .. autoclass:: microagent.bus.AbstractSignalBus
49
+ :members:
50
+ :member-order: bysource
51
+
52
+
53
+ .. autoclass:: Signal
54
+ :members:
55
+ :member-order: bysource
56
+
57
+
58
+ **Internals stuff for signal bus binding**
59
+
60
+ .. autoclass:: microagent.bus.BoundSignal
61
+ :members:
62
+
63
+
64
+ .. autoclass:: microagent.bus.Receiver
65
+
66
+
67
+ **Exceptions**
68
+
69
+ .. autoclass:: microagent.signal.SignalException
70
+
71
+
72
+ .. autoclass:: microagent.signal.SignalNotFound
73
+
74
+
75
+ .. autoclass:: microagent.signal.SerializingError
76
+
77
+
78
+ .. autoclass:: microagent.DoubleLoadError
@@ -2,7 +2,18 @@ Configuration, launch and etc.
2
2
  ======================================
3
3
 
4
4
 
5
- Configuration file of deploying unit, for shipped launcher (settings.py)
5
+ Configuration file (signals.json)
6
+ ---------------------------------
7
+
8
+ Configuration JSON file supports optional ``default_prefix`` field to set
9
+ the global channel prefix for all buses and brokers.
10
+
11
+ .. include:: ../examples/signals.json
12
+ :code: json
13
+
14
+
15
+ Configuration Python file (settings.py)
16
+ ----------------------------------------
6
17
 
7
18
  .. include:: ../examples/settings.py
8
19
  :code: python
@@ -32,6 +32,12 @@ Tool provide features:
32
32
 
33
33
  .. code-block:: python
34
34
 
35
+ from microagent import (MicroAgent, Queue, Signal, configure,
36
+ consumer, cron, on, periodic, receiver)
37
+ from microagent.tools.redis import RedisBroker, RedisSignalBus
38
+
39
+ configure('file://signals.json')
40
+
35
41
  class Agent(MicroAgent):
36
42
 
37
43
  @on('pre_start')
@@ -47,13 +53,13 @@ Tool provide features:
47
53
  pass # do something by schedule
48
54
 
49
55
  # subscribe to signals (events)
50
- @receiver(signals.user_updated, signals.user_deleted)
56
+ @receiver(Signal.user_updated, Signal.user_deleted)
51
57
  async def send_notification(self, **kwargs):
52
58
  # send signal (event) to bus
53
59
  await self.bus.check_something.send(sender='agent', **kwargs)
54
60
 
55
61
  # message consumer from queue
56
- @consumer(queues.mailer)
62
+ @consumer(Queue.mailer)
57
63
  async def send_emails(self, **kwargs):
58
64
  # send message to queue
59
65
  await self.broker.statistic_collector.send(kwargs)
@@ -65,7 +71,7 @@ Tool provide features:
65
71
 
66
72
  # usage bus and broker separate from agent
67
73
  await bus.started.send('user_agent')
68
- await broker.mailer(data)
74
+ await broker.mailer.send(data)
69
75
 
70
76
  agent = Agent(bus=bus, broker=broker)
71
77
  await agent.start()
@@ -0,0 +1,43 @@
1
+ Launcher and configuration
2
+ ======================================
3
+
4
+ Configuration file
5
+ ------------------
6
+
7
+ Configuration JSON file supports optional ``default_prefix`` field to set
8
+ the global channel prefix for all buses and brokers.
9
+
10
+ .. include:: ../examples/signals.json
11
+ :code: json
12
+
13
+
14
+ Configuration Python file
15
+ -------------------------
16
+
17
+ Signals and queues can be loaded via the ``CONFIG`` variable. When present,
18
+ the launcher calls ``configure()`` automatically before processing BUS, BROKER
19
+ and AGENT dictionaries.
20
+
21
+ .. include:: ../examples/settings_config.py
22
+ :code: python
23
+
24
+ .. automodule:: microagent.launcher
25
+
26
+
27
+ .. include:: ../examples/settings.py
28
+ :code: python
29
+
30
+
31
+ .. autoclass:: microagent.launcher.ServerInterrupt
32
+
33
+
34
+ .. autofunction:: microagent.launcher.load_configuration
35
+
36
+
37
+ .. autofunction:: microagent.launcher.init_agent
38
+
39
+
40
+ .. autoclass:: microagent.launcher.AgentsManager
41
+
42
+
43
+ .. autoclass:: microagent.launcher.GroupInterrupt
@@ -1,12 +1,14 @@
1
1
  # mypy: ignore-errors
2
2
  import asyncio
3
- import os
4
3
  from collections import defaultdict
5
4
 
6
- from microagent import MicroAgent, load_stuff, on, receiver
5
+ from microagent import MicroAgent, Signal, on, receiver
7
6
 
8
- cur_dir = os.path.dirname(os.path.realpath(__file__))
9
- signals, queues = load_stuff('file://' + os.path.join(cur_dir, 'signals.json'))
7
+ # single-run
8
+ # import os
9
+ # from microagent import configure
10
+ # cur_dir = os.path.dirname(os.path.realpath(__file__))
11
+ # configure('file://' + os.path.join(cur_dir, 'signals.json'))
10
12
 
11
13
 
12
14
  class CommentAgent(MicroAgent):
@@ -16,13 +18,13 @@ class CommentAgent(MicroAgent):
16
18
  self.comments_cache = defaultdict(lambda: 0)
17
19
  self.counter = 0
18
20
 
19
- @receiver(signals.rpc_comments_count)
21
+ @receiver(Signal.rpc_comments_count)
20
22
  async def example_rpc_handler(self, user_id, **kwargs):
21
23
  self.log.info('Catch signal %s', kwargs)
22
24
  await asyncio.sleep(1)
23
25
  return self.comments_cache[user_id]
24
26
 
25
- @receiver(signals.user_comment)
27
+ @receiver(Signal.user_comment)
26
28
  async def example_signal_receiver_send_message(self, user_id, **kwargs):
27
29
  self.log.info('Catch signal %s', kwargs)
28
30
  self.comments_cache[user_id] += 1
@@ -0,0 +1,18 @@
1
+ # mypy: ignore-errors
2
+ from microagent import MicroAgent, Queue, consumer, on
3
+
4
+ # single-run
5
+ # import os
6
+ # from microagent import configure
7
+ # cur_dir = os.path.dirname(os.path.realpath(__file__))
8
+ # configure('file://' + os.path.join(cur_dir, 'signals.json'))
9
+
10
+
11
+ class EmailAgent(MicroAgent):
12
+ @on('pre_start')
13
+ async def setup(self):
14
+ self.log.info('Run ...\n %s', self.info())
15
+
16
+ @consumer(Queue.mailer)
17
+ async def example_read_queue(self, **kwargs):
18
+ self.log.info('Catch emailer %s', kwargs)
@@ -4,7 +4,7 @@ import logging
4
4
  import os
5
5
  import sys
6
6
 
7
- from microagent import MicroAgent, consumer, load_stuff, on, periodic
7
+ from microagent import MicroAgent, Queue, configure, consumer, on, periodic
8
8
  from microagent.tools import amqp, redis
9
9
 
10
10
  logging.basicConfig(format=(
@@ -13,7 +13,7 @@ logging.basicConfig(format=(
13
13
  ), stream=sys.stdout, level=logging.INFO)
14
14
 
15
15
  cur_dir = os.path.dirname(os.path.realpath(__file__))
16
- signals, queues = load_stuff('file://' + os.path.join(cur_dir, 'signals.json'))
16
+ configure('file://' + os.path.join(cur_dir, 'signals.json'))
17
17
 
18
18
 
19
19
  class UserAgent(MicroAgent):
@@ -27,19 +27,19 @@ class UserAgent(MicroAgent):
27
27
  await self.broker.push3.send({'text': 'informer text'}, topic='msg.ios')
28
28
  await self.broker.push.send({'text': 'informer 1'})
29
29
 
30
- @consumer(queues.android)
30
+ @consumer(Queue.android)
31
31
  async def example_read_queue_android(self, **kwargs):
32
32
  self.log.info('Catch android %s', kwargs)
33
33
 
34
- @consumer(queues.ios)
34
+ @consumer(Queue.ios)
35
35
  async def example_read_queue_ios(self, **kwargs):
36
36
  self.log.info('Catch ios %s', kwargs)
37
37
 
38
- @consumer(queues.android_a)
38
+ @consumer(Queue.android_a)
39
39
  async def example_read_queue_android_a(self, **kwargs):
40
40
  self.log.info('Catch android_a %s', kwargs)
41
41
 
42
- @consumer(queues.ios_a)
42
+ @consumer(Queue.ios_a)
43
43
  async def example_read_queue_ios_a(self, **kwargs):
44
44
  self.log.info('Catch ios_a %s', kwargs)
45
45
 
@@ -1,6 +1,12 @@
1
1
  import logging
2
+ import os
2
3
  import sys
3
4
 
5
+ from microagent import configure
6
+
7
+
8
+ cur_dir = os.path.dirname(os.path.realpath(__file__))
9
+ configure('file://' + os.path.join(cur_dir, 'signals.json'))
4
10
  logging.basicConfig(format=(
5
11
  '%(levelname)-8s [pid#%(process)d] %(asctime)s %(name)s '
6
12
  '%(filename)s:%(lineno)d %(message)s'
@@ -11,7 +17,6 @@ BUS = {
11
17
  'redis': {
12
18
  'backend': 'microagent.tools.redis.RedisSignalBus',
13
19
  'dsn': 'redis://localhost/7',
14
- 'prefix': 'PREF',
15
20
  },
16
21
  }
17
22
 
@@ -1,5 +1,6 @@
1
1
  {
2
2
  "version": 2,
3
+ "default_prefix": "EXAMPLES",
3
4
  "signals": [
4
5
  {"name": "started", "providing_args": []},
5
6
  {"name": "user_created", "providing_args": ["user_id"]},
@@ -1,10 +1,11 @@
1
1
  # mypy: ignore-errors
2
- import os
2
+ from microagent import MicroAgent, Signal, cron, on, periodic, receiver
3
3
 
4
- from microagent import MicroAgent, cron, load_stuff, on, periodic, receiver
5
-
6
- cur_dir = os.path.dirname(os.path.realpath(__file__))
7
- signals, queues = load_stuff('file://' + os.path.join(cur_dir, 'signals.json'))
4
+ # single-run
5
+ # import os
6
+ # from microagent import configure
7
+ # cur_dir = os.path.dirname(os.path.realpath(__file__))
8
+ # configure('file://' + os.path.join(cur_dir, 'signals.json'))
8
9
 
9
10
 
10
11
  class UserAgent(MicroAgent):
@@ -22,12 +23,12 @@ class UserAgent(MicroAgent):
22
23
  self.log.info('Run periodic task')
23
24
  await self.bus.user_comment.send('user_agent', user_id=1, text='informer text')
24
25
 
25
- @receiver(signals.user_created)
26
+ @receiver(Signal.user_created)
26
27
  async def example_signal_receiver_send_message(self, signal, sender, **kwargs):
27
28
  self.log.info('Catch signal %s from %s with %s', signal, sender, kwargs)
28
29
  await self.broker.mailer.send({'text': 'Welcome text', 'email': 'user@lwr.pw'})
29
30
 
30
- @receiver(signals.user_comment)
31
+ @receiver(Signal.user_comment)
31
32
  async def example_rpc_call(self, **kwargs):
32
33
  self.log.info('Catch signal %s', kwargs)
33
34
  value = await self.bus.rpc_comments_count.call('user_agent', user_id=1)
@@ -1,13 +1,16 @@
1
- __version__ = '1.8.3'
1
+ __version__ = '1.9.0'
2
2
 
3
3
  import importlib
4
4
  import json
5
5
  import urllib.request
6
+
6
7
  from collections import abc
7
8
  from typing import Any, NamedTuple
8
9
 
10
+ from . import bus
9
11
  from .abc import ConsumerFunc, HookFunc, PeriodicFunc, ReceiverFunc
10
12
  from .agent import MicroAgent
13
+ from .bus import _DEFAULT_PREFIX # noqa: F401
11
14
  from .hooks import Hook, HookArgs
12
15
  from .launcher import ServerInterrupt
13
16
  from .queue import Consumer, ConsumerArgs, Queue
@@ -15,8 +18,23 @@ from .signal import Receiver, ReceiverArgs, Signal
15
18
  from .timer import CRONArgs, CRONTask, PeriodicArgs, PeriodicTask, cron_parser
16
19
  from .utils import make_bound_key
17
20
 
18
- __all__ = ['Signal', 'Queue', 'MicroAgent', 'ServerInterrupt', 'receiver', 'consumer',
19
- 'periodic', 'cron', 'on', 'load_stuff', 'load_signals', 'load_queues']
21
+
22
+ __all__ = [
23
+ 'DoubleLoadError',
24
+ 'MicroAgent',
25
+ 'Queue',
26
+ 'ServerInterrupt',
27
+ 'Signal',
28
+ 'configure',
29
+ 'consumer',
30
+ 'cron',
31
+ 'load_queues',
32
+ 'load_signals',
33
+ 'load_stuff',
34
+ 'on',
35
+ 'periodic',
36
+ 'receiver',
37
+ ]
20
38
 
21
39
  MIN_TIMEOUT_SEC = .001
22
40
  JSON_TYPES = {
@@ -35,12 +53,18 @@ def get_types(value: str | list[str]) -> tuple[type, ...]:
35
53
  return tuple(JSON_TYPES[x] for x in value)
36
54
 
37
55
 
38
- def load_stuff(source: str) -> tuple[Any, Any]:
39
- '''
40
- Init signals from json-file loaded from disk or http request
41
- '''
56
+ class DoubleLoadError(RuntimeError):
57
+ ''' Raised when :func:`configure` or :func:`load_stuff` called more than once '''
58
+
59
+
60
+ def _load(source: str) -> None:
61
+ if Signal._signals or Queue._queues:
62
+ raise DoubleLoadError(
63
+ 'Signals/queues already loaded. '
64
+ 'load_stuff/configure called more than once?'
65
+ )
42
66
 
43
- data: dict[str, abc.Iterable[dict[str, Any]]] = {}
67
+ data: dict[str, Any] = {}
44
68
 
45
69
  if source.startswith('file://'):
46
70
  with open(source.replace('file://', ''), encoding='utf8') as f:
@@ -70,13 +94,63 @@ def load_stuff(source: str) -> tuple[Any, Any]:
70
94
  )
71
95
 
72
96
  if data.get('jsonlib'):
73
- jsonlib = importlib.import_module(data['jsonlib']) # type: ignore
97
+ jsonlib = importlib.import_module(data['jsonlib'])
74
98
  Signal.set_jsonlib(jsonlib)
75
99
  Queue.set_jsonlib(jsonlib)
76
100
 
101
+ if 'default_prefix' in data:
102
+ bus._DEFAULT_PREFIX = data['default_prefix']
103
+
104
+
105
+ def configure(source: str) -> None:
106
+ '''
107
+ Init signals and queues from json-file loaded from disk or http request.
108
+ Unlike :func:`load_stuff`, does not return anything.
109
+ After calling, signals and queues are accessible via
110
+ :class:`~microagent.signal.Signal` and :class:`~microagent.queue.Queue`
111
+ class attributes.
112
+
113
+ .. code-block:: python
114
+
115
+ from microagent import Queue, Signal, configure, consumer, receiver
116
+
117
+ configure('file://signals.json')
118
+
119
+ Signal.user_created # -> <Signal user_created>
120
+ Queue.mailer # -> <Queue mailer>
121
+
122
+ @receiver(Signal.user_created)
123
+ async def handler(self, **kwargs):
124
+ ...
125
+
126
+ @consumer(Queue.mailer)
127
+ async def consumer(self, **kwargs):
128
+ ...
129
+
130
+ Raises :class:`DoubleLoadError` if signals or queues already loaded.
131
+ '''
132
+ _load(source)
133
+
134
+
135
+ def load_stuff(source: str) -> tuple[Any, Any]:
136
+ '''
137
+ Init signals from json-file loaded from disk or http request.
138
+ Returns named tuples of signals and queues.
139
+
140
+ .. note::
141
+
142
+ Use :func:`configure` if you don't need the return values
143
+ and prefer :class:`~microagent.signal.Signal` /
144
+ :class:`~microagent.queue.Queue` class attribute access instead.
145
+
146
+ Raises :class:`DoubleLoadError` if signals or queues already loaded.
147
+ '''
148
+
149
+ _load(source)
150
+
77
151
  # mypy: https://github.com/python/mypy/issues/848
78
- SignalList = NamedTuple('signals', [(name, Signal) for name in Signal.get_all().keys()]) # type: ignore
79
- QueueList = NamedTuple('queues', [(name, Queue) for name in Queue.get_all().keys()]) # type: ignore
152
+ SignalList = NamedTuple('signals', [(name, Signal) for name in Signal.get_all()]) # type: ignore
153
+ QueueList = NamedTuple('queues', [(name, Queue) for name in Queue.get_all()]) # type: ignore
80
154
 
81
155
  return (
82
156
  SignalList(*Signal.get_all().values()),
@@ -124,8 +198,8 @@ def load_queues(source: str) -> NamedTuple:
124
198
 
125
199
  from microagent import load_queues
126
200
 
127
- signals_from_file = load_signals('file://queues.json')
128
- signals_from_web = load_signals('http://example.com/queues.json')
201
+ queues_from_file = load_queues('file://queues.json')
202
+ queues_from_web = load_queues('http://example.com/queues.json')
129
203
 
130
204
 
131
205
  Queues declarations (queues.json).
@@ -145,7 +219,7 @@ def load_queues(source: str) -> NamedTuple:
145
219
  return load_stuff(source)[1]
146
220
 
147
221
 
148
- def periodic(period: int | float, timeout: int | float = 1, start_after: int | float = 0
222
+ def periodic(period: float, timeout: float = 1, start_after: float = 0
149
223
  ) -> abc.Callable[[PeriodicFunc], PeriodicFunc]:
150
224
  '''
151
225
  Run decorated handler periodically.
@@ -184,7 +258,7 @@ def periodic(period: int | float, timeout: int | float = 1, start_after: int | f
184
258
  return _decorator
185
259
 
186
260
 
187
- def cron(spec: str, timeout: int | float = 1) -> abc.Callable[[PeriodicFunc], PeriodicFunc]:
261
+ def cron(spec: str, timeout: float = 1) -> abc.Callable[[PeriodicFunc], PeriodicFunc]:
188
262
  '''
189
263
  Run decorated function by schedule (cron)
190
264
 
@@ -1,7 +1,9 @@
1
1
  import logging
2
+
2
3
  from collections.abc import Awaitable, Callable
3
4
  from typing import Any, Literal, Protocol
4
5
 
6
+
5
7
  HookLabel = Literal['server', 'pre_start', 'post_start', 'pre_stop']
6
8
  HookFunc = Callable[[Any], Awaitable[None]]
7
9
  PeriodicFunc = Callable[[Any], Awaitable[None]]