Skip to content
This repository was archived by the owner on Dec 29, 2017. It is now read-only.

Commit 12a1bc8

Browse files
authored
Move registry to submodule (#30)
1 parent c780be1 commit 12a1bc8

File tree

9 files changed

+93
-79
lines changed

9 files changed

+93
-79
lines changed

docs/references.rst

+7-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ SirBot
1717
Registry
1818
--------
1919

20-
.. autoclass:: sirbot.core.RegistrySingleton
20+
.. autoclass:: sirbot.registry.RegistrySingleton
21+
:members:
22+
23+
.. autoexception:: sirbot.registry.RegistryError
24+
:members:
25+
26+
.. autoexception:: sirbot.registry.FrozenRegistryError
2127
:members:
2228

2329
.. _references_plugin:
@@ -48,8 +54,3 @@ Exceptions
4854
.. autoexception:: sirbot.core.errors.SirBotError
4955
:members:
5056

51-
.. autoexception:: sirbot.core.errors.RegistryError
52-
:members:
53-
54-
.. autoexception:: sirbot.core.errors.FrozenRegistryError
55-
:members:

setup.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ addopts = --verbose --cov=sirbot/ tests/ --cov-report term-missing
77
[build_sphinx]
88
source-dir = docs/
99
build-dir = docs/_build
10+
warning-is-error = 1
1011

1112
[aliases]
1213
test=pytest

setup.py

+2
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ def parse_readme():
6565
'sirbot.core',
6666
'sirbot.utils',
6767
'sirbot.cli',
68+
'sirbot.registry'
6869
],
6970
package_dir={
7071
'sirbot': 'sirbot',
7172
'sirbot.core': 'sirbot/core',
7273
'sirbot.utils': 'sirbot/utils',
7374
'sirbot.cli': 'sirbot/cli',
75+
'sirbot.registry': 'sirbot/registry'
7476
},
7577
package_data={
7678
'sirbot.core': ['config.yml'],

sirbot/core/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from .core import SirBot, registry # noQa
1+
from .core import SirBot # noQa
22
from .__meta__ import DATA as METADATA # noQa
33
from .hookimpl import hookimpl # noQa
44
from .plugin import Plugin # noQa
5+
from ..registry import registry # noQa

sirbot/core/core.py

+2-53
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@
1414
import pluggy
1515
import yaml
1616

17-
from collections import defaultdict, MutableMapping
17+
from collections import defaultdict
1818
from aiohttp import web
1919

2020
from sirbot.utils import merge_dict
21+
from sirbot.registry import registry
2122

2223
from . import hookspecs
23-
from .errors import FrozenRegistryError
2424

2525
logger = logging.getLogger(__name__)
26-
registry = None
2726

2827
if sys.version_info[:2] == (3, 5):
2928
ModuleNotFoundError = ImportError
@@ -39,8 +38,6 @@ class SirBot:
3938
loop (asyncio.AbstractEventLoop): Event loop
4039
"""
4140
def __init__(self, config=None, *, loop=None):
42-
global registry
43-
registry = RegistrySingleton()
4441
self.config = config or {}
4542
self._configure()
4643
logger.info('Initializing Sir Bot-a-lot')
@@ -259,51 +256,3 @@ def run(self, host: str = '0.0.0.0', port: int = 8080):
259256
"""
260257
self._loop.run_until_complete(self._configure_plugins())
261258
web.run_app(self._app, host=host, port=port) # pragma: no cover
262-
263-
264-
class RegistrySingleton(MutableMapping):
265-
"""
266-
Class regrouping all available plugin factory.
267-
268-
The plugin factory is called each time the registry is queried.
269-
It is good practice to keep interacting on the same factory result without
270-
querying the registry each time.
271-
272-
Similar behaviour as a dictionary
273-
"""
274-
def __init__(self):
275-
super().__init__()
276-
self._frozen = False
277-
self._plugins = dict()
278-
279-
@property
280-
def frozen(self):
281-
return self._frozen
282-
283-
@frozen.setter
284-
def frozen(self, _):
285-
raise ValueError('Read only property')
286-
287-
def freeze(self):
288-
self._frozen = True
289-
290-
def __getitem__(self, item):
291-
return self._plugins[item]()
292-
293-
def __setitem__(self, key, value):
294-
if not self._frozen:
295-
self._plugins[key] = value
296-
else:
297-
raise FrozenRegistryError()
298-
299-
def __delitem__(self, key):
300-
raise FrozenRegistryError()
301-
302-
def __iter__(self):
303-
return iter(self._plugins)
304-
305-
def __len__(self):
306-
return len(self._plugins)
307-
308-
def __contains__(self, item):
309-
return item in self._plugins

sirbot/core/errors.py

-12
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,3 @@ class SirBotError(Exception):
22
"""
33
Top level sirbot error
44
"""
5-
6-
7-
class RegistryError(SirBotError):
8-
"""
9-
Class to compose all registry related errors
10-
"""
11-
12-
13-
class FrozenRegistryError(RegistryError):
14-
"""
15-
Registry if frozen
16-
"""

sirbot/core/registry.py

Whitespace-only changes.

sirbot/registry/__init__.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from collections import MutableMapping
2+
from sirbot.core.errors import SirBotError
3+
4+
5+
class RegistryError(SirBotError):
6+
"""
7+
Class to compose all registry related errors
8+
"""
9+
10+
11+
class FrozenRegistryError(RegistryError):
12+
"""
13+
Registry if frozen
14+
"""
15+
16+
17+
class RegistrySingleton(MutableMapping):
18+
"""
19+
Class regrouping all available plugin factory.
20+
21+
The plugin factory is called each time the registry is queried.
22+
It is good practice to keep interacting on the same factory result without
23+
querying the registry each time.
24+
25+
Similar behaviour as a dictionary
26+
"""
27+
def __init__(self):
28+
super().__init__()
29+
self._frozen = False
30+
self._plugins = dict()
31+
32+
@property
33+
def frozen(self):
34+
return self._frozen
35+
36+
@frozen.setter
37+
def frozen(self, _):
38+
raise ValueError('Read only property')
39+
40+
def freeze(self):
41+
self._frozen = True
42+
43+
def __getitem__(self, item):
44+
return self._plugins[item]()
45+
46+
def __setitem__(self, key, value):
47+
if not self._frozen:
48+
self._plugins[key] = value
49+
else:
50+
raise FrozenRegistryError()
51+
52+
def __delitem__(self, key):
53+
raise FrozenRegistryError()
54+
55+
def __iter__(self):
56+
return iter(self._plugins)
57+
58+
def __len__(self):
59+
return len(self._plugins)
60+
61+
def __contains__(self, item):
62+
return item in self._plugins
63+
64+
65+
registry = RegistrySingleton()

tests/core/test_core.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
}
2424

2525

26+
@pytest.fixture
27+
def registry(monkeypatch):
28+
new_registry = sirbot.registry.RegistrySingleton()
29+
monkeypatch.setattr('sirbot.core.core.registry', new_registry)
30+
monkeypatch.setattr('sirbot.registry.registry', new_registry)
31+
32+
2633
def test_bot_is_starting(loop, test_server):
2734
bot = sirbot.SirBot(loop=loop)
2835
loop.run_until_complete(test_server(bot._app))
@@ -39,7 +46,7 @@ def test_load_config(loop):
3946
assert bot.config == config
4047

4148

42-
def test_logging_config(loop):
49+
def test_logging_config(loop, registry):
4350
config = {
4451
'logging': {
4552
'version': 1,
@@ -61,7 +68,7 @@ def test_logging_config(loop):
6168
assert logging.getLogger('sirbot').level == 40
6269

6370

64-
def test_plugin_import(loop, test_server):
71+
def test_plugin_import(loop, test_server, registry):
6572
bot = sirbot.SirBot(loop=loop, config=CONFIG)
6673
loop.run_until_complete(test_server(bot._app))
6774
assert bot._pm.has_plugin('tests.core.test_plugin.sirbot')
@@ -76,33 +83,33 @@ def test_plugin_import_error(loop):
7683
bot._import_plugins()
7784

7885

79-
def test_initialize_plugins(loop):
86+
def test_initialize_plugins(loop, registry):
8087
bot = sirbot.SirBot(loop=loop, config=CONFIG)
8188
assert isinstance(bot._plugins['test']['plugin'], PluginTest)
8289

8390

84-
def test_plugin_configure(loop, test_server):
91+
def test_plugin_configure(loop, test_server, registry):
8592
bot = sirbot.SirBot(loop=loop, config=CONFIG)
8693
loop.run_until_complete(bot._configure_plugins())
8794

8895
assert bot._plugins['test']['plugin'].config == CONFIG['test']
8996

9097

91-
def test_start_plugins(loop, test_server):
98+
def test_start_plugins(loop, test_server, registry):
9299
bot = sirbot.SirBot(loop=loop, config=CONFIG)
93100
loop.run_until_complete(test_server(bot._app))
94101
assert 'test' in bot._tasks
95102

96103

97-
def test_plugin_task_error(loop, test_server, capsys):
104+
def test_plugin_task_error(loop, test_server, registry):
98105
config = deepcopy(CONFIG)
99106
config['sirbot']['plugins'] = ['tests.core.test_plugin.sirbot_start_error']
100107
bot = sirbot.SirBot(loop=loop, config=config)
101108
with pytest.raises(ValueError):
102109
loop.run_until_complete(test_server(bot._app))
103110

104111

105-
def test_plugin_priority(loop, test_server):
112+
def test_plugin_priority(loop, registry):
106113
config = deepcopy(CONFIG)
107114
config['test']['priority'] = 80
108115
config['test-error'] = {'priority': 70}

0 commit comments

Comments
 (0)