From 9f203fc1820c2a2d3630b4a8d62036fcaaaa6887 Mon Sep 17 00:00:00 2001 From: "Jeroen van Meeuwen (Kolab Systems)" Date: Thu, 10 May 2012 15:25:31 +0100 Subject: Provide required function for multi-line messages while keeping the source code somewhat clean --- pykolab/utils.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pykolab/utils.py b/pykolab/utils.py index 5e32880..6326bf8 100644 --- a/pykolab/utils.py +++ b/pykolab/utils.py @@ -123,6 +123,33 @@ def generate_password(): return output +def multiline_message(message): + _msg = "" + + column_width = 80 + + # First, replace all occurences of "\n" + message = message.replace(" ", "") + message = message.replace("\n", " ") + + lines = [] + line_length = 0 + + line = "" + for word in message.split(): + if (len(line) + len(word)) > column_width: + lines.append(line) + line = word + else: + if line == "": + line = word + else: + line += " %s" % (word) + + lines.append(line) + + return "\n".join(lines) + def normalize(_object): if type(_object) == list: result = [] -- cgit v0.12