| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | |
|---|
| 3 | """ |
|---|
| 4 | The PyLucid page message system |
|---|
| 5 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 6 | |
|---|
| 7 | A small Wrapper aound djangos user messages system. |
|---|
| 8 | |
|---|
| 9 | enhanced features: |
|---|
| 10 | - easy callable to print a messages |
|---|
| 11 | - simple color output: self.page_msg.green() |
|---|
| 12 | - use pprint for dicts and lists |
|---|
| 13 | - special debug mode: |
|---|
| 14 | Inserts informationen, where from the message has come. |
|---|
| 15 | |
|---|
| 16 | Can also used in Plugin for storing internal messages. |
|---|
| 17 | |
|---|
| 18 | Links |
|---|
| 19 | ~~~~~ |
|---|
| 20 | http://www.pylucid.org/_goto/134/plugin-output/ |
|---|
| 21 | http://www.djangoproject.com/documentation/authentication/#messages |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | Last commit info: |
|---|
| 25 | ~~~~~~~~~~~~~~~~~ |
|---|
| 26 | $LastChangedDate$ |
|---|
| 27 | $Rev$ |
|---|
| 28 | $Author$ |
|---|
| 29 | |
|---|
| 30 | :copyleft: 2008 by the PyLucid team, see AUTHORS for more details. |
|---|
| 31 | :license: GNU GPL v3 or above, see LICENSE for more details. |
|---|
| 32 | """ |
|---|
| 33 | |
|---|
| 34 | import os, sys, pprint, inspect |
|---|
| 35 | |
|---|
| 36 | from django.utils.safestring import mark_safe, SafeData |
|---|
| 37 | from django.utils.encoding import force_unicode |
|---|
| 38 | |
|---|
| 39 | from django.conf import settings |
|---|
| 40 | |
|---|
| 41 | from PyLucid.tools.utils import escape |
|---|
| 42 | |
|---|
| 43 | #_____________________________________________________________________________ |
|---|
| 44 | |
|---|
| 45 | class PageMessages(object): |
|---|
| 46 | """ |
|---|
| 47 | The page message container. |
|---|
| 48 | """ |
|---|
| 49 | def __init__(self, request, use_django_msg=True, html=True): |
|---|
| 50 | self.html = html # Should we generate colored html output? |
|---|
| 51 | |
|---|
| 52 | self.messages = [] |
|---|
| 53 | if use_django_msg: |
|---|
| 54 | try: |
|---|
| 55 | self.messages = request.user.get_and_delete_messages() |
|---|
| 56 | except AttributeError: |
|---|
| 57 | # In the _install section we have no user |
|---|
| 58 | pass |
|---|
| 59 | |
|---|
| 60 | self.debug_mode = getattr(request, "debug", False) |
|---|
| 61 | |
|---|
| 62 | self._charset = settings.DEFAULT_CHARSET |
|---|
| 63 | |
|---|
| 64 | #_________________________________________________________________________ |
|---|
| 65 | |
|---|
| 66 | def write(self, *msg): |
|---|
| 67 | self.append_color_data("blue", *msg) |
|---|
| 68 | |
|---|
| 69 | def __call__(self, *msg): |
|---|
| 70 | """ Alte Methode um Daten "auszugeben", Text ist dann schwarz """ |
|---|
| 71 | self.append_color_data("blue", *msg) |
|---|
| 72 | |
|---|
| 73 | def DEBUG(self, *msg): |
|---|
| 74 | self.append_color_data("gray", *msg) |
|---|
| 75 | |
|---|
| 76 | def black(self, *msg): |
|---|
| 77 | self.append_color_data("black", *msg) |
|---|
| 78 | |
|---|
| 79 | def green(self, *msg): |
|---|
| 80 | self.append_color_data("green", *msg) |
|---|
| 81 | |
|---|
| 82 | def red(self, *msg): |
|---|
| 83 | self.append_color_data("red", *msg) |
|---|
| 84 | |
|---|
| 85 | #_________________________________________________________________________ |
|---|
| 86 | |
|---|
| 87 | def append_color_data(self, color, *msg): |
|---|
| 88 | if self.html: |
|---|
| 89 | msg = '<span style="color:%s;">%s</span>' % ( |
|---|
| 90 | color, self.prepare(*msg) |
|---|
| 91 | ) |
|---|
| 92 | else: |
|---|
| 93 | msg = self.prepare(*msg) |
|---|
| 94 | |
|---|
| 95 | #~ self.request.user.message_set.create(message=msg) |
|---|
| 96 | msg = mark_safe(msg) # turn djngo auto-escaping off |
|---|
| 97 | self.messages.append(msg) |
|---|
| 98 | |
|---|
| 99 | def _get_fileinfo(self): |
|---|
| 100 | """ |
|---|
| 101 | Append the fileinfo: Where from the announcement comes? |
|---|
| 102 | """ |
|---|
| 103 | try: |
|---|
| 104 | self_basename = os.path.basename(__file__) |
|---|
| 105 | if self_basename.endswith(".pyc"): |
|---|
| 106 | # cut: ".pyc" -> ".py" |
|---|
| 107 | self_basename = self_basename[:-1] |
|---|
| 108 | # result.append("1%s1" % self_basename) |
|---|
| 109 | |
|---|
| 110 | for stack_frame in inspect.stack(): |
|---|
| 111 | # go forward in the stack, to outside of this file. |
|---|
| 112 | filename = stack_frame[1] |
|---|
| 113 | lineno = stack_frame[2] |
|---|
| 114 | # result.append("2%s2" % os.path.basename(filename)) |
|---|
| 115 | if os.path.basename(filename) != self_basename: |
|---|
| 116 | # result.append("\n") |
|---|
| 117 | break |
|---|
| 118 | |
|---|
| 119 | filename = "...%s" % filename[-25:] |
|---|
| 120 | fileinfo = "%-25s line %3s: " % (filename, lineno) |
|---|
| 121 | except Exception, e: |
|---|
| 122 | fileinfo = "(inspect Error: %s)" % e |
|---|
| 123 | |
|---|
| 124 | return fileinfo |
|---|
| 125 | |
|---|
| 126 | def prepare(self, *msg): |
|---|
| 127 | """ |
|---|
| 128 | -if debug_mode is on: insert a info from where the message sended. |
|---|
| 129 | -for dict, list use pprint ;) |
|---|
| 130 | """ |
|---|
| 131 | if self.debug_mode == True: |
|---|
| 132 | result = [self._get_fileinfo()] |
|---|
| 133 | else: |
|---|
| 134 | result = [] |
|---|
| 135 | |
|---|
| 136 | for item in msg: |
|---|
| 137 | if isinstance(item, dict) or isinstance(item, list): |
|---|
| 138 | item = pprint.pformat(item) |
|---|
| 139 | item = item.split("\n") |
|---|
| 140 | for line in item: |
|---|
| 141 | line = self.encode_and_prepare(line) |
|---|
| 142 | if self.html: |
|---|
| 143 | result.append("%s<br />\n" % line) |
|---|
| 144 | else: |
|---|
| 145 | result.append("%s\n" % line) |
|---|
| 146 | else: |
|---|
| 147 | item = self.encode_and_prepare(item) |
|---|
| 148 | result.append(item) |
|---|
| 149 | result.append(" ") |
|---|
| 150 | |
|---|
| 151 | result = "".join(result).strip() |
|---|
| 152 | return result |
|---|
| 153 | |
|---|
| 154 | def encode_and_prepare(self, txt): |
|---|
| 155 | """ |
|---|
| 156 | Pass "safe" strings, all other would be escaped. |
|---|
| 157 | """ |
|---|
| 158 | if isinstance(txt, SafeData): |
|---|
| 159 | return txt |
|---|
| 160 | |
|---|
| 161 | if not isinstance(txt, unicode): |
|---|
| 162 | txt = force_unicode(txt) |
|---|
| 163 | |
|---|
| 164 | return escape(txt) |
|---|
| 165 | |
|---|
| 166 | #________________________________________________________________ |
|---|
| 167 | |
|---|
| 168 | def __repr__(self): |
|---|
| 169 | return "page messages: %s" % repr(self.messages) |
|---|
| 170 | |
|---|
| 171 | def __str__(self): |
|---|
| 172 | return "pages messages: %s" % ", ".join(self.messages) |
|---|
| 173 | |
|---|
| 174 | def __unicode__(self): |
|---|
| 175 | return u"page messages: %s" % u", ".join(self.messages) |
|---|
| 176 | |
|---|
| 177 | #________________________________________________________________ |
|---|
| 178 | # Some methods for the django template engine: |
|---|
| 179 | |
|---|
| 180 | def __iter__(self): |
|---|
| 181 | """ |
|---|
| 182 | used in: {% for message in page_msg %} |
|---|
| 183 | """ |
|---|
| 184 | for message in self.messages: |
|---|
| 185 | yield message |
|---|
| 186 | |
|---|
| 187 | def __len__(self): |
|---|
| 188 | """ |
|---|
| 189 | used in: {% if page_msg %} |
|---|
| 190 | """ |
|---|
| 191 | return len(self.messages) |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|