squabble.message module

exception squabble.message.DuplicateMessageCodeException(dupe)[source]

Bases: squabble.SquabbleException

class squabble.message.Registry[source]

Bases: object

Singleton which maps message code values to classes.

>>> class MyMessage(Message):
...     '''My example message.'''
...     TEMPLATE = '...'
...     CODE = 5678
>>> cls = Registry.by_code(5678)
>>> cls.explain()
'My example message.'
>>> cls is MyMessage
True

Duplicate codes are not allowed, and will throw an exception.

>>> class MyDuplicateMessage(Message):
...     CODE = 5678
Traceback (most recent call last):
  ...
squabble.message.DuplicateMessageCodeException: ...
classmethod register(msg)[source]

Add msg to the registry, and assign a CODE value if not explicitly specified.

classmethod by_code(code)[source]

Return the squabble.message.Message class identified by code, raising a KeyError if it doesn’t exist.

class squabble.message.Message(**kwargs)[source]

Bases: object

Messages represent specific issues identified by a lint rule.

Each class that inherits from Message should have a docstring which explains the reasoning and context of the message, as well as a class member variable named TEMPLATE, which is used to display a brief explanation on the command line.

Messages may also have a CODE class member, which is used to identify the message. The actual value doesn’t matter much, as long as it is unique among all the loaded Message s. If no CODE is defined, one will be assigned.

>>> class TooManyColumns(Message):
...    '''
...    This may indicate poor design, consider multiple tables instead.
...    '''
...    TEMPLATE = 'table "{table}" has > {limit} columns'
...    CODE = 1234
>>> message = TooManyColumns(table='foo', limit=30)
>>> message.format()
'table "foo" has > 30 columns'
>>> message.explain()
'This may indicate poor design, consider multiple tables instead.'
TEMPLATE = None
CODE = None
format()[source]
classmethod explain()[source]

Provide more context around this message.

The purpose of this function is to explain to users _why_ the message was raised, and what they can do to resolve the issue.

The base implementation will simply return the docstring for the class, but this can be overridden if more specialized behavior is necessary.

>>> class NoDocString(Message): pass
>>> NoDocString().explain() is None
True
asdict()[source]

Return dictionary representation of message, for formatting.

>>> class SummaryMessage(Message):
...     CODE = 90291
...     TEMPLATE = 'everything is {status}'
...
>>> msg = SummaryMessage(status='wrong')
>>> msg.asdict() == {
...   'message_id': 'SummaryMessage',
...   'message_text': 'everything is wrong',
...   'message_template': SummaryMessage.TEMPLATE,
...   'message_params': {'status': 'wrong'},
...   'message_code': 90291
... }
True