Changeset View
Changeset View
Standalone View
Standalone View
wallace/module_footer.py
Show All 28 Lines | |||||
from pykolab.translate import _ | from pykolab.translate import _ | ||||
log = pykolab.getLogger('pykolab.wallace') | log = pykolab.getLogger('pykolab.wallace') | ||||
conf = pykolab.getConf() | conf = pykolab.getConf() | ||||
mybasepath = '/var/spool/pykolab/wallace/footer/' | mybasepath = '/var/spool/pykolab/wallace/footer/' | ||||
def __init__(): | def __init__(): | ||||
Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
modules.register('footer', execute, description=description()) | modules.register('footer', execute, description=description()) | ||||
def description(): | def description(): | ||||
Lint: PEP8 E302 expected 2 blank lines, found 1 Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
return """Append a footer to messages.""" | return """Append a footer to messages.""" | ||||
def execute(*args, **kw): | def execute(*args, **kw): | ||||
Lint: PEP8 E302 expected 2 blank lines, found 1 Lint: PEP8 E302: expected 2 blank lines, found 1 | |||||
if not os.path.isdir(mybasepath): | if not os.path.isdir(mybasepath): | ||||
os.makedirs(mybasepath) | os.makedirs(mybasepath) | ||||
for stage in ['incoming', 'ACCEPT' ]: | for stage in ['incoming', 'ACCEPT' ]: | ||||
Lint: PEP8 E202 whitespace before ']' Lint: PEP8 E202: whitespace before ']' | |||||
if not os.path.isdir(os.path.join(mybasepath, stage)): | if not os.path.isdir(os.path.join(mybasepath, stage)): | ||||
os.makedirs(os.path.join(mybasepath, stage)) | os.makedirs(os.path.join(mybasepath, stage)) | ||||
# TODO: Test for correct call. | # TODO: Test for correct call. | ||||
filepath = args[0] | filepath = args[0] | ||||
if kw.has_key('stage'): | if kw.has_key('stage'): | ||||
log.debug(_("Issuing callback after processing to stage %s") % (kw['stage']), level=8) | log.debug(_("Issuing callback after processing to stage %s") % (kw['stage']), level=8) | ||||
Lint: PEP8 E501 line too long (94 > 79 characters) Lint: PEP8 E501: line too long (94 > 79 characters) | |||||
log.debug(_("Testing cb_action_%s()") % (kw['stage']), level=8) | log.debug(_("Testing cb_action_%s()") % (kw['stage']), level=8) | ||||
if hasattr(modules, 'cb_action_%s' % (kw['stage'])): | if hasattr(modules, 'cb_action_%s' % (kw['stage'])): | ||||
log.debug(_("Attempting to execute cb_action_%s()") % (kw['stage']), level=8) | log.debug(_("Attempting to execute cb_action_%s()") % (kw['stage']), level=8) | ||||
Lint: PEP8 E501 line too long (89 > 79 characters) Lint: PEP8 E501: line too long (89 > 79 characters) | |||||
exec('modules.cb_action_%s(%r, %r)' % (kw['stage'],'optout',filepath)) | exec('modules.cb_action_%s(%r, %r)' % (kw['stage'],'optout',filepath)) | ||||
Lint: PEP8 E231 missing whitespace after ',' Lint: PEP8 E231: missing whitespace after ',' | |||||
Lint: PEP8 E231 missing whitespace after ',' Lint: PEP8 E231: missing whitespace after ',' | |||||
Lint: PEP8 E501 line too long (82 > 79 characters) Lint: PEP8 E501: line too long (82 > 79 characters) | |||||
return | return | ||||
log.debug(_("Executing module footer for %r, %r") % (args, kw), level=8) | log.debug(_("Executing module footer for %r, %r") % (args, kw), level=8) | ||||
new_filepath = os.path.join('/var/spool/pykolab/wallace/footer/incoming', os.path.basename(filepath)) | new_filepath = os.path.join('/var/spool/pykolab/wallace/footer/incoming', os.path.basename(filepath)) | ||||
Lint: PEP8 E501 line too long (105 > 79 characters) Lint: PEP8 E501: line too long (105 > 79 characters) | |||||
os.rename(filepath, new_filepath) | os.rename(filepath, new_filepath) | ||||
filepath = new_filepath | filepath = new_filepath | ||||
# parse message | # parse message | ||||
message = message_from_file(open(filepath, 'r')) | message = message_from_file(open(filepath, 'r')) | ||||
# Possible footer answers are limited to ACCEPT only | # Possible footer answers are limited to ACCEPT only | ||||
answers = [ 'ACCEPT' ] | answers = [ 'ACCEPT' ] | ||||
Lint: PEP8 E201 whitespace after '[' Lint: PEP8 E201: whitespace after '[' | |||||
Lint: PEP8 E202 whitespace before ']' Lint: PEP8 E202: whitespace before ']' | |||||
footer = {} | footer = {} | ||||
footer_html_file = conf.get('wallace', 'footer_html') | footer_html_file = conf.get('wallace', 'footer_html') | ||||
footer_text_file = conf.get('wallace', 'footer_text') | footer_text_file = conf.get('wallace', 'footer_text') | ||||
if not os.path.isfile(footer_text_file) and not os.path.isfile(footer_html_file): | if not os.path.isfile(footer_text_file) and not os.path.isfile(footer_html_file): | ||||
Lint: PEP8 E501 line too long (85 > 79 characters) Lint: PEP8 E501: line too long (85 > 79 characters) | |||||
log.warning(_("No contents configured for footer module")) | log.warning(_("No contents configured for footer module")) | ||||
exec('modules.cb_action_%s(%r, %r)' % ('ACCEPT','footer', filepath)) | exec('modules.cb_action_%s(%r, %r)' % ('ACCEPT','footer', filepath)) | ||||
Lint: PEP8 E231 missing whitespace after ',' Lint: PEP8 E231: missing whitespace after ',' | |||||
return | return | ||||
if os.path.isfile(footer_text_file): | if os.path.isfile(footer_text_file): | ||||
footer['plain'] = open(footer_text_file, 'r').read() | footer['plain'] = open(footer_text_file, 'r').read() | ||||
if not os.path.isfile(footer_html_file): | if not os.path.isfile(footer_html_file): | ||||
footer['html'] = '<p>' + footer['plain'] + '</p>' | footer['html'] = '<p>' + footer['plain'] + '</p>' | ||||
else: | else: | ||||
footer['html'] = open(footer_html_file, 'r').read() | footer['html'] = open(footer_html_file, 'r').read() | ||||
if footer['html'] == "": | if footer['html'] == "": | ||||
footer['html'] = '<p>' + footer['plain'] + '</p>' | footer['html'] = '<p>' + footer['plain'] + '</p>' | ||||
if footer['plain'] == "" and footer['html'] == "<p></p>": | if footer['plain'] == "" and footer['html'] == "<p></p>": | ||||
exec('modules.cb_action_%s(%r, %r)' % ('ACCEPT','footer', filepath)) | exec('modules.cb_action_%s(%r, %r)' % ('ACCEPT','footer', filepath)) | ||||
Lint: PEP8 E231 missing whitespace after ',' Lint: PEP8 E231: missing whitespace after ',' | |||||
return | return | ||||
footer_added = False | footer_added = False | ||||
try: | try: | ||||
_footer_added = message.get("X-Wallace-Footer") | _footer_added = message.get("X-Wallace-Footer") | ||||
except: | except: | ||||
pass | pass | ||||
if _footer_added == "YES": | if _footer_added == "YES": | ||||
exec('modules.cb_action_%s(%r, %r)' % ('ACCEPT','footer', filepath)) | exec('modules.cb_action_%s(%r, %r)' % ('ACCEPT','footer', filepath)) | ||||
Lint: PEP8 E231 missing whitespace after ',' Lint: PEP8 E231: missing whitespace after ',' | |||||
return | return | ||||
for part in message.walk(): | for part in message.walk(): | ||||
disposition = None | disposition = None | ||||
try: | try: | ||||
content_type = part.get_content_type() | content_type = part.get_content_type() | ||||
except: | except: | ||||
continue | continue | ||||
try: | try: | ||||
disposition = part.get("Content-Disposition") | disposition = part.get("Content-Disposition") | ||||
except: | except: | ||||
pass | pass | ||||
log.debug("Walking message part: %s; disposition = %r" % (content_type, disposition), level=8) | log.debug("Walking message part: %s; disposition = %r" % (content_type, disposition), level=8) | ||||
Lint: PEP8 E501 line too long (102 > 79 characters) Lint: PEP8 E501: line too long (102 > 79 characters) | |||||
if not disposition == None: | if not disposition == None: | ||||
Lint: PEP8 E711 comparison to None should be 'if cond is None:' Lint: PEP8 E711: comparison to None should be 'if cond is None:' | |||||
continue | continue | ||||
if content_type == "text/plain": | if content_type == "text/plain": | ||||
content = part.get_payload() | content = part.get_payload(None, True) | ||||
content += "\n\n-- \n%s" % (footer['plain']) | content += "\n\n-- \n%s" % (footer['plain']) | ||||
part.set_payload(content) | part.set_payload(content) | ||||
footer_added = True | footer_added = True | ||||
log.debug("Text footer attached.", level=6) | log.debug("Text footer attached.", level=6) | ||||
elif content_type == "text/html": | elif content_type == "text/html": | ||||
content = part.get_payload() | content = part.get_payload(None, True) | ||||
append = "\n<!-- footer appended by Wallace -->\n" + footer['html'] | append = "\n<!-- footer appended by Wallace -->\n" + footer['html'] | ||||
if "</body>" in content: | if "</body>" in content: | ||||
part.set_payload(content.replace("</body>", append + "</body>")) | part.set_payload(content.replace("</body>", append + "</body>")) | ||||
Lint: PEP8 E501 line too long (80 > 79 characters) Lint: PEP8 E501: line too long (80 > 79 characters) | |||||
else: | else: | ||||
part.set_payload("<html><body>" + content + append + "</body></html>") | part.set_payload("<html><body>" + content + append + "</body></html>") | ||||
Lint: PEP8 E501 line too long (86 > 79 characters) Lint: PEP8 E501: line too long (86 > 79 characters) | |||||
footer_added = True | footer_added = True | ||||
log.debug("HTML footer attached.", level=6) | log.debug("HTML footer attached.", level=6) | ||||
if footer_added: | if footer_added: | ||||
log.debug("Footer attached.") | log.debug("Footer attached.") | ||||
message.add_header("X-Wallace-Footer", "YES") | message.add_header("X-Wallace-Footer", "YES") | ||||
(fp, new_filepath) = tempfile.mkstemp(dir="/var/spool/pykolab/wallace/footer/ACCEPT") | (fp, new_filepath) = tempfile.mkstemp(dir="/var/spool/pykolab/wallace/footer/ACCEPT") | ||||
Lint: PEP8 E501 line too long (89 > 79 characters) Lint: PEP8 E501: line too long (89 > 79 characters) | |||||
os.write(fp, message.as_string()) | os.write(fp, message.as_string()) | ||||
os.close(fp) | os.close(fp) | ||||
os.unlink(filepath) | os.unlink(filepath) | ||||
exec('modules.cb_action_%s(%r, %r)' % ('ACCEPT','footer', new_filepath)) | exec('modules.cb_action_%s(%r, %r)' % ('ACCEPT','footer', new_filepath)) | ||||
Lint: PEP8 E231 missing whitespace after ',' Lint: PEP8 E231: missing whitespace after ',' |
expected 2 blank lines, found 1