2
# This file is part of GNU Enterprise.
4
# GNU Enterprise is free software; you can redistribute it
5
# and/or modify it under the terms of the GNU General Public
6
# License as published by the Free Software Foundation; either
7
# version 2, or (at your option) any later version.
9
# GNU Enterprise is distributed in the hope that it will be
10
# useful, but WITHOUT ANY WARRANTY; without even the implied
11
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
# PURPOSE. See the GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public
15
# License along with program; see the file COPYING. If not,
16
# write to the Free Software Foundation, Inc., 59 Temple Place
17
# - Suite 330, Boston, MA 02111-1307, USA.
19
# Copyright 2001-2005 Free Software Foundation
25
# Common text-related utilities
36
# very simple lineWrap
37
def lineWrap(message,maxWidth, preserveNewlines=1, alignment=ALIGN_LEFT, eol=1):
39
A simple linewrap function.
41
@param message: The string to wrap
42
@param maxWidth: The maximum string width allowed.
43
@param preserveNewlines: If true then newlines are left in the string
44
otherwise they are removed.
45
@param alignment: The alignment of the returned string based upon
48
Possible values (ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER)
50
@param eol: If true then the last character of the return value
53
@return: The properly wrapped text string.
59
temptext = string.strip(str(message))
61
buff = string.split(temptext,"\n")
66
while len(strings) > maxWidth:
69
for sep in [' ',',',':','.',';']:
70
ind = string.rfind(strings,sep,0,maxWidth-1)+1
71
if ind > index: index = ind
73
if index > maxWidth or index==0:
76
line = strings[:index]
77
if alignment == ALIGN_CENTER:
78
line = ' ' * ( (maxWidth - len(line)) /2 ) + line
79
elif alignment == ALIGN_RIGHT:
80
line = ' ' * ( (maxWidth - len(line)) ) + line
82
strings = strings[index:]
85
if alignment == ALIGN_CENTER:
86
line = ' ' * ( (maxWidth - len(line)) /2 ) + line
87
elif alignment == ALIGN_RIGHT:
88
line = ' ' * ( (maxWidth - len(line)) ) + line
97
def textToMeasurement(text, multiplier=1):
99
Converts most standard measurements to inches.
103
>>> textToMeasurement('72pt')
105
>>> # convert to 72 point output
106
... textToMeasurement('1.27cm',72)
109
@param text: A string containing the measurement to convert
110
The following measurements are accepted
113
@param multiplier: A multiplier used to adjust the output
114
to values other inches.
115
@type multipler: number
117
@return: The converted value
120
if text[-1] in ('0','1','2','3','4','5','6','7','8','9'):
121
value = float(text) / 72 * multiplier
125
value = float(text) / {'pt':72, 'in':1, 'cm':2.54,
126
'mm': 25.4}[unit.lower()] * multiplier
133
Convert an integer to a roman numeral.
140
@param num: The number to convert
143
@return: The roman numeral equivalent.
146
n = int(num) # just in case
149
(1000, 'M'), (900, 'CM', ),
150
(500, 'D'), (400, 'CD'), (100, 'C'),
151
(90, 'XC'),(50, 'L'), (40, 'XL'),
152
(10, 'X'), (9, 'IX'), (5, 'V'),
153
(4, 'IV'), (1, 'I') ):
160
def dollarToText(num):
162
Convert a number to "dollar" notation (suitable for use on checks)
163
e.g., 123.24 --> "One Hundred Twenty Three and 24/100"
167
>>> dollarToText(1120.15)
168
'One Thousand One Hundred Twenty and 15/100'
170
@param num: The numeric amount
173
@return: The full text equivalent of the number.
177
cents = round((num-whole)*100)
178
rv = 'and %02d/100' % cents
182
whole, segment = divmod(whole, 1000)
183
hundreds, tens = divmod(segment, 100)
185
rv = _smallDollarMap[tens] + _thirdDollarMap[thirdRange] + rv
187
ten, ones = divmod(tens,10)
188
rv = _tenDollarMap[ten] + _smallDollarMap[ones] + _thirdDollarMap[thirdRange] + rv
190
rv = _smallDollarMap[hundreds] + 'Hundred ' + rv
198
_smallDollarMap = ('', 'One ', 'Two ', 'Three ', 'Four ', 'Five ',
199
'Six ', 'Seven ', 'Eight ', 'Nine ', 'Ten ',
200
'Eleven ', 'Twelve ', 'Thirteen ', 'Fourteen ',
201
'Fifteen ', 'Sixteen ', 'Seventeen ', 'Eighteen ',
203
_tenDollarMap = ('', '', 'Twenty ', 'Thirty ', 'Forty ', 'Fifty ',
204
'Sixty ', 'Seventy ', 'Eighty ', 'Ninety ')
205
_thirdDollarMap = ('', 'Thousand ', 'Million ', 'Billion ', 'Trillion ')
210
def comify(num, decimals=2, parenthesis=0):
212
Comify a number (e.g., print -9900 as -9,900.00)
216
>>> comify(-9999.934, 2)
218
>>> comify(-9999.934, 2, 1)
221
@param num: The number to reformat
223
@param decimals: The number of decimal places to retain
224
@type decimals: number
225
@param parenthesis: If true then negative numbers will be returned inside parenthesis
226
@type parenthesis: number
228
@return: A properly formatted number
233
whole, dec = (string.split(string.strip(("%%12.%sf" % decimals) % abs(num)),'.') + [""])[:2]
239
for i in range(divmod(len(whole),3)[0]+1):
241
s = "," + whole[j > 3 and j-3 or 0:j] + s