MCPcopy Create free account
hub / github.com/Python-Markdown/markdown / Registry

Class Registry

markdown/util.py:270–410  ·  view source on GitHub ↗

A priority sorted registry. A `Registry` instance provides two public methods to alter the data of the registry: `register` and `deregister`. Use `register` to add items and `deregister` to remove items. See each method for specifics. When registering an item, a "name" and a "

Source from the content-addressed store, hash-verified

268
269
270class Registry(Generic[_T]):
271 """
272 A priority sorted registry.
273
274 A `Registry` instance provides two public methods to alter the data of the
275 registry: `register` and `deregister`. Use `register` to add items and
276 `deregister` to remove items. See each method for specifics.
277
278 When registering an item, a "name" and a "priority" must be provided. All
279 items are automatically sorted by "priority" from highest to lowest. The
280 "name" is used to remove ("deregister") and get items.
281
282 A `Registry` instance it like a list (which maintains order) when reading
283 data. You may iterate over the items, get an item and get a count (length)
284 of all items. You may also check that the registry contains an item.
285
286 When getting an item you may use either the index of the item or the
287 string-based "name". For example:
288
289 registry = Registry()
290 registry.register(SomeItem(), 'itemname', 20)
291 # Get the item by index
292 item = registry[0]
293 # Get the item by name
294 item = registry['itemname']
295
296 When checking that the registry contains an item, you may use either the
297 string-based "name", or a reference to the actual item. For example:
298
299 someitem = SomeItem()
300 registry.register(someitem, 'itemname', 20)
301 # Contains the name
302 assert 'itemname' in registry
303 # Contains the item instance
304 assert someitem in registry
305
306 The method `get_index_for_name` is also available to obtain the index of
307 an item using that item's assigned "name".
308 """
309
310 def __init__(self):
311 self._data: dict[str, _T] = {}
312 self._priority: list[_PriorityItem] = []
313 self._is_sorted = False
314
315 def __contains__(self, item: str | _T) -> bool:
316 if isinstance(item, str):
317 # Check if an item exists by this name.
318 return item in self._data.keys()
319 # Check if this instance exists.
320 return item in self._data.values()
321
322 def __iter__(self) -> Iterator[_T]:
323 self._sort()
324 return iter([self._data[k] for k, p in self._priority])
325
326 @overload
327 def __getitem__(self, key: str | int) -> _T: # pragma: no cover

Callers 2

__getitem__Method · 0.85
extendMarkdownMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected