/usr/lib/python3/dist-packages/cement/ext/ext_dummy.py is in python3-cement 2.10.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | """
The Dummy Extension provides several 'placeholder' type handlers to either
mock operations or provide local-only usage during development. A perfect
example is the :class:`DummyMailHandler` that can be use during development
or staging to prevent real email messages from being sent externally.
Requirements
------------
* No external dependencies
Configuration
-------------
* See each handler's documentation regarding what configurations they
support.
Usage
-----
.. code-block:: python
from cement.core.foundation import CementApp
class MyApp(CementApp):
class Meta:
label = 'myapp'
extensions = ['dummy']
output_handler = 'dummy'
mail_handler = 'dummy'
with MyApp() as app:
app.run()
"""
from ..core import output, mail
from ..utils.misc import minimal_logger
LOG = minimal_logger(__name__)
class DummyOutputHandler(output.CementOutputHandler):
"""
This class is an internal implementation of the
:class:`cement.core.output.IOutput` interface. It does not take any
parameters on initialization, and does not actually output anything.
"""
class Meta:
"""Handler meta-data"""
#: The interface this class implements.
interface = output.IOutput
#: The string identifier of this handler.
label = 'dummy'
#: Whether or not to include ``dummy`` as an available to choice
#: to override the ``output_handler`` via command line options.
overridable = False
def render(self, data_dict, template=None, **kw):
"""
This implementation does not actually render anything to output, but
rather logs it to the debug facility.
:param data_dict: The data dictionary to render.
:param template: The template parameter is not used by this
implementation at all.
:returns: None
"""
LOG.debug("not rendering any output to console")
LOG.debug("DATA: %s" % data_dict)
return None
class DummyMailHandler(mail.CementMailHandler):
"""
This class implements the :class:`cement.core.mail.IMail`
interface, but is intended for use in development as no email is actually
sent.
**Usage**
.. code-block:: python
class MyApp(CementApp):
class Meta:
label = 'myapp'
mail_handler = 'dummy'
with MyApp() as app:
app.run()
app.mail.send('This is my fake message',
subject='This is my subject',
to=['john@example.com', 'rita@example.com'],
from_addr='me@example.com',
)
The above will print the following to console:
.. code-block:: text
======================================================================
DUMMY MAIL MESSAGE
----------------------------------------------------------------------
To: john@example.com, rita@example.com
From: me@example.com
CC:
BCC:
Subject: This is my subject
---
This is my fake message
----------------------------------------------------------------------
**Configuration**
This handler supports the following configuration settings:
* **to** - Default ``to`` addresses (list, or comma separated depending
on the ConfigHandler in use)
* **from_addr** - Default ``from_addr`` address
* **cc** - Default ``cc`` addresses (list, or comma separated depending
on the ConfigHandler in use)
* **bcc** - Default ``bcc`` addresses (list, or comma separated depending
on the ConfigHandler in use)
* **subject** - Default ``subject``
* **subject_prefix** - Additional string to prepend to the ``subject``
You can add these to any application configuration file under a
``[mail.dummy]`` section, for example:
**~/.myapp.conf**
.. code-block:: text
[myapp]
# set the mail handler to use
mail_handler = dummy
[mail.dummy]
# default to addresses (comma separated list)
to = me@example.com
# default from address
from = someone_else@example.com
# default cc addresses (comma separated list)
cc = jane@example.com, rita@example.com
# default bcc addresses (comma separated list)
bcc = blackhole@example.com, someone_else@example.com
# default subject
subject = This is The Default Subject
# additional prefix to prepend to the subject
subject_prefix = MY PREFIX >
"""
class Meta:
"""Handler meta-data."""
#: Unique identifier for this handler
label = 'dummy'
def _get_params(self, **kw):
params = dict()
for item in ['to', 'from_addr', 'cc', 'bcc', 'subject']:
config_item = self.app.config.get(self._meta.config_section, item)
params[item] = kw.get(item, config_item)
# also grab the subject_prefix
params['subject_prefix'] = self.app.config.get(
self._meta.config_section,
'subject_prefix'
)
return params
def send(self, body, **kw):
"""
Mimic sending an email message, but really just print what would be
sent to console. Keyword arguments override configuration
defaults (cc, bcc, etc).
:param body: The message body to send
:type body: ``multiline string``
:keyword to: List of recipients (generally email addresses)
:type to: ``list``
:keyword from_addr: Address (generally email) of the sender
:type from_addr: ``str``
:keyword cc: List of CC Recipients
:type cc: ``list``
:keyword bcc: List of BCC Recipients
:type bcc: ``list``
:keyword subject: Message subject line
:type subject: ``str``
:returns: Boolean (``True`` if message is sent successfully, ``False``
otherwise)
**Usage**
.. code-block:: python
# Using all configuration defaults
app.mail.send('This is my message body')
# Overriding configuration defaults
app.mail.send('My message body'
to=['john@example.com'],
from_addr='me@example.com',
cc=['jane@example.com', 'rita@example.com'],
subject='This is my subject',
)
"""
# shorted config values
params = self._get_params(**kw)
msg = "\n" + "=" * 77 + "\n"
msg += "DUMMY MAIL MESSAGE\n"
msg += "-" * 77 + "\n\n"
msg += "To: %s\n" % ', '.join(params['to'])
msg += "From: %s\n" % params['from_addr']
msg += "CC: %s\n" % ', '.join(params['cc'])
msg += "BCC: %s\n" % ', '.join(params['bcc'])
if params['subject_prefix'] not in [None, '']:
msg += "Subject: %s %s\n\n---\n\n" % (params['subject_prefix'],
params['subject'])
else:
msg += "Subject: %s\n\n---\n\n" % params['subject']
msg += body + "\n"
msg += "\n" + "-" * 77 + "\n"
print(msg)
return True
def load(app):
app.handler.register(DummyOutputHandler)
app.handler.register(DummyMailHandler)
|