Skip to content

Commit ba2f41d

Browse files
author
Pietro Albini
committed
Renamed init_shared_memory to prepare_memory
This also renames add_shared_memory_initializer to add_memory_preparer. The old method names are now deprecated, and they will continue to work until botogram 1.0.
1 parent 845e0ca commit ba2f41d

File tree

8 files changed

+85
-43
lines changed

8 files changed

+85
-43
lines changed

botogram/bot.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ def __init__(self, api_connection):
5454
self._shared_memory = shared.SharedMemory()
5555

5656
# Register bot's shared memory initializers
57-
inits = self._main_component._get_shared_memory_inits()
57+
inits = self._main_component._get_memory_preparers()
5858
maincompid = self._main_component._component_id
59-
self._shared_memory.register_inits_list(maincompid, inits)
59+
self._shared_memory.register_preparers_list(maincompid, inits)
6060

6161
# Setup the scheduler
6262
self._scheduler = tasks.Scheduler()
@@ -142,11 +142,17 @@ def __(func):
142142
return func
143143
return __
144144

145-
def init_shared_memory(self, func):
146-
"""Register a shared memory's initializer"""
147-
self._main_component.add_shared_memory_initializer(func)
145+
def prepare_memory(self, func):
146+
"""Register a shared memory's preparer"""
147+
self._main_component.add_memory_preparer(func)
148148
return func
149149

150+
@utils.deprecated("@bot.init_shared_memory", "1.0", "Rename the decorator "
151+
"to @bot.prepare_memory")
152+
def init_shared_memory(self, func):
153+
"""This decorator is deprecated, and it calls @prepare_memory"""
154+
return self.prepare_memory(func)
155+
150156
def use(self, *components, only_init=False):
151157
"""Use the provided components in the bot"""
152158
for component in components:
@@ -157,8 +163,8 @@ def use(self, *components, only_init=False):
157163

158164
# Register initializers for the shared memory
159165
compid = component._component_id
160-
inits = component._get_shared_memory_inits()
161-
self._shared_memory.register_inits_list(compid, inits)
166+
preparers = component._get_memory_preparers()
167+
self._shared_memory.register_preparers_list(compid, preparers)
162168

163169
# Register tasks
164170
self._scheduler.register_tasks_list(component._get_timers())

botogram/components.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __new__(cls, *args, **kwargs):
2727
self.__processors = []
2828
self.__no_commands = []
2929
self.__before_processors = []
30-
self.__shared_inits = []
30+
self.__memory_preparers = []
3131
self.__timers = []
3232

3333
self._component_id = str(uuid.uuid4())
@@ -122,13 +122,19 @@ def add_timer(self, interval, func):
122122

123123
self.__timers.append(job)
124124

125-
def add_shared_memory_initializer(self, func):
125+
def add_memory_preparer(self, func):
126126
"""Add a new shared memory's initializer"""
127127
if not callable(func):
128-
raise ValueError("A shared memory initializer must be callable")
128+
raise ValueError("A memory preparer must be callable")
129+
130+
hook = hooks.MemoryPreparerHook(func, self)
131+
self.__memory_preparers.append(hook)
129132

130-
hook = hooks.SharedMemoryInitializerHook(func, self)
131-
self.__shared_inits.append(hook)
133+
@utils.deprecated("Component.add_shared_memory_initializer", "1.0",
134+
"Rename the method to Component.add_memory_preparer")
135+
def add_shared_memory_initializer(self, func):
136+
"""This method is deprecated, and it calls add_memory_preparer"""
137+
self.add_memory_preparer(func)
132138

133139
def _add_no_commands_hook(self, func):
134140
"""Register an hook which will be executed when no commands matches"""
@@ -151,9 +157,9 @@ def _get_commands(self):
151157
"""Get all the commands this component implements"""
152158
return self.__commands
153159

154-
def _get_shared_memory_inits(self):
160+
def _get_memory_preparers(self):
155161
"""Get a list of all the shared memory initializers"""
156-
return self.__shared_inits
162+
return self.__memory_preparers
157163

158164
def _get_timers(self):
159165
"""Get a list of all the timers"""

botogram/frozenbot.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,17 @@ def timer(self, interval):
125125
"""Register a new timer"""
126126
raise FrozenBotError("Can't add timers to a bot at runtime")
127127

128-
def init_shared_memory(self, func):
129-
"""Add a shared memory initializer"""
130-
raise FrozenBotError("Can't register a shared memory initializer to a "
128+
def prepare_memory(self, func):
129+
"""Add a shared memory preparer"""
130+
raise FrozenBotError("Can't register a shared memory preparer to a "
131131
"bot at runtime")
132132

133+
@utils.deprecated("@bot.init_shared_memory", "1.0", "Rename the decorator "
134+
"to @bot.prepare_memory")
135+
def init_shared_memory(self, func):
136+
"""This decorator is deprecated, and it calls @prepare_memory"""
137+
return self.prepare_memory(func)
138+
133139
# Those are shortcuts to send messages directly to someone
134140

135141
def send(self, chat, message, preview=True, reply_to=None, syntax=None,

botogram/hooks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ class ProcessMessageHook(Hook):
6666
pass
6767

6868

69-
class SharedMemoryInitializerHook(Hook):
70-
"""Underlying hook for @bot.init_shared_memory"""
69+
class MemoryPreparerHook(Hook):
70+
"""Underlying hook for @bot.prepare_memory"""
7171

7272
def call(self, memory):
7373
return self.func(memory)

botogram/shared.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def __init__(self, driver=None):
102102
driver = LocalDriver()
103103
self.driver = driver
104104

105-
self._inits = {}
105+
self._preparers = {}
106106

107107
def __reduce__(self):
108108
return rebuild, (self.driver,)
@@ -111,33 +111,33 @@ def _key_of(self, *parts):
111111
"""Get the key for a shared item"""
112112
return ":".join(parts)
113113

114-
def register_inits_list(self, component, inits):
115-
"""Register a new list to pick initializers from"""
114+
def register_preparers_list(self, component, inits):
115+
"""Register a new list to pick preparers from"""
116116
# Ignore the request if a list was already registered
117-
if component in self._inits:
117+
if component in self._preparers:
118118
return
119119

120-
self._inits[component] = inits
120+
self._preparers[component] = inits
121121

122122
def of(self, bot, component):
123123
"""Get the shared memory of a specific component"""
124124
memory, is_new = self.driver.get(self._key_of(bot, component))
125125

126126
# Be sure to initialize the shared memory if it's needed
127127
if is_new:
128-
self.apply_inits(component, memory)
128+
self.apply_preparers(component, memory)
129129

130130
# Add the lock method to the object
131131
memory.lock = functools.partial(self.lock, bot, component)
132132
return memory
133133

134-
def apply_inits(self, component, memory):
135-
"""Apply all the inits of a component to a memory"""
136-
if component not in self._inits:
134+
def apply_preparers(self, component, memory):
135+
"""Apply all the preparers of a component to a memory"""
136+
if component not in self._preparers:
137137
return
138138

139-
for init in self._inits[component]:
140-
init.call(memory)
139+
for preparer in self._preparers[component]:
140+
preparer.call(memory)
141141

142142
def switch_driver(self, driver=None):
143143
"""Use another driver for this shared memory"""

docs/api/bot.rst

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ components.
207207
208208
:param int interval: The execution interval, in seconds.
209209

210-
.. py:decoratormethod:: init_shared_memory
210+
.. py:decoratormethod:: prepare_memory
211211
212212
The function decorated with this decorator will be called the first time
213213
you access your bot's shared memory. This allows you to set the initial
@@ -218,11 +218,11 @@ components.
218218

219219
The decorated function will be called providing as first argument a
220220
dict-like object representing your bot's shared memory. Use it to
221-
initialize the things you want in the shared memory.
221+
prepare the things you want in the shared memory.
222222

223223
.. code-block:: python
224224
225-
@bot.init_shared_memory
225+
@bot.prepare_memory
226226
def initialize(shared):
227227
shared["messages"] = 0
228228
@@ -236,6 +236,18 @@ components.
236236
def count(shared, chat, message, args):
237237
chat.send("This bot received %s messages" % shared["messages"])
238238
239+
.. versionchanged:: 0.2
240+
241+
Before it was called ``init_shared_memory``.
242+
243+
.. py:decoratormethod:: init_shared_memory
244+
245+
This decorator was renamed to
246+
:py:meth:`~botogram.Bot.prepare_memory` in botogram 0.2.
247+
Please use that instead of this.
248+
249+
.. deprecated:: 0.2 it will be removed in botogram 1.0
250+
239251
.. py:method:: use(component)
240252
241253
Use the provided component in your bot, so the hooks the component

docs/api/components.rst

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ about how to create them in the ":ref:`custom-components`" chapter.
175175
:param int interval: The execution interval, in seconds.
176176
:param callable func: The function you want to use.
177177

178-
.. py:method:: add_shared_memory_initializer(func)
178+
.. py:method:: add_memory_preparer(func)
179179
180180
The function provided to this method will be called the first time you
181181
access your component's shared memory. This allows you to set the initial
@@ -196,7 +196,7 @@ about how to create them in the ":ref:`custom-components`" chapter.
196196
component_name = "counter"
197197
198198
def __init__(self):
199-
self.add_shared_memory_initializer(self.initialize)
199+
self.add_memory_preparer(self.initialize)
200200
self.add_process_message_hook(self.increment)
201201
self.add_command("count", self.count)
202202
@@ -210,3 +210,15 @@ about how to create them in the ":ref:`custom-components`" chapter.
210210
211211
def count(self, shared, chat, message, args):
212212
chat.send("This bot received %s messages" % shared["messages"])
213+
214+
.. versionchanged:: 0.2
215+
216+
Before it was called ``add_shared_memory_initializer``.
217+
218+
.. py:method:: add_shared_memory_initializer(func)
219+
220+
This method was renamed to
221+
:py:meth:`~botogram.Component.add_memory_preparer` in botogram 0.2.
222+
Please use that instead of this.
223+
224+
.. deprecated:: 0.2 it will be removed in botogram 1.0

docs/shared-memory.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,24 @@ command which displays the current messages count calculated by the hook above:
6868
6969
.. _shared-memory-inits:
7070

71-
Shared memory initializers
72-
==========================
71+
Shared memory preparers
72+
=======================
7373

7474
In the example above, a big part of the code is just to handle the case when
7575
the shared memory doesn't contain the ``count`` key, and that's possible only
7676
at startup. In order to solve this problem, you can use the
77-
:py:meth:`botogram.Bot.init_shared_memory` decorator.
77+
:py:meth:`~botogram.Bot.prepare_memory` decorator.
7878

7979
Functions decorated with that decorator will be called only the first time you
8080
require the shared memory. This means you can use them to set the initial value
8181
of all the keys you want to use in the shared memory.
8282

83-
For example, let's refactor the code above to use an initializer:
83+
For example, let's refactor the code above to use a preparer:
8484

8585
.. code-block:: python
8686
87-
@bot.init_shared_memory
88-
def init_shared_memory(shared):
87+
@bot.prepare_memory
88+
def prepare_memory(shared):
8989
shared["messages"] = 0
9090
9191
@bot.process_message
@@ -115,8 +115,8 @@ component is used by multiple bots.
115115
Using shared memory within a component is the same as using it in your bot's
116116
main code: just require the ``shared`` argument to your component's function
117117
and botogram will make sure it receives the component's shared memories. To
118-
add a shared memory initializer, you can instead provide the function to the
119-
:py:meth:`botogram.Component.add_shared_memory_initializer` method.
118+
add a shared memory preparer, you can instead provide the function to the
119+
:py:meth:`~botogram.Component.add_memory_preparer` method.
120120

121121
.. _shared-memory-locks:
122122

0 commit comments

Comments
 (0)