Mercurial > p > roundup > code
comparison roundup/support.py @ 3644:f35ece8f8ff7
added StringHTMLProperty wrapped() method to wrap long lines in issue display
also fixed a circular import in roundup.support
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Mon, 31 Jul 2006 01:30:05 +0000 |
| parents | fa7becc62534 |
| children | 193f316dbbe9 |
comparison
equal
deleted
inserted
replaced
| 3643:ff234c87b28a | 3644:f35ece8f8ff7 |
|---|---|
| 2 places in Roundup code. | 2 places in Roundup code. |
| 3 """ | 3 """ |
| 4 | 4 |
| 5 __docformat__ = 'restructuredtext' | 5 __docformat__ = 'restructuredtext' |
| 6 | 6 |
| 7 import os, time, sys | 7 import os, time, sys, re |
| 8 import hyperdb | |
| 9 | 8 |
| 10 from sets import Set | 9 from sets import Set |
| 11 | 10 |
| 12 class TruthDict: | 11 class TruthDict: |
| 13 '''Returns True for valid keys, False for others. | 12 '''Returns True for valid keys, False for others. |
| 153 | 152 |
| 154 def append(self, name): | 153 def append(self, name): |
| 155 """Append a property to self.children. Will create a new | 154 """Append a property to self.children. Will create a new |
| 156 propclass for the child. | 155 propclass for the child. |
| 157 """ | 156 """ |
| 157 import hyperdb | |
| 158 if name in self.propnames: | 158 if name in self.propnames: |
| 159 return self.propnames [name] | 159 return self.propnames [name] |
| 160 propclass = self.props [name] | 160 propclass = self.props [name] |
| 161 cls = None | 161 cls = None |
| 162 props = None | 162 props = None |
| 190 for p in self.children: | 190 for p in self.children: |
| 191 yield p | 191 yield p |
| 192 for c in p: | 192 for c in p: |
| 193 yield c | 193 yield c |
| 194 | 194 |
| 195 LEFT = 'left' | |
| 196 LEFTN = 'left no strip' | |
| 197 RIGHT = 'right' | |
| 198 CENTER = 'center' | |
| 199 | |
| 200 def align(line, width=70, alignment=LEFTN): | |
| 201 ''' Code from http://www.faqts.com/knowledge_base/view.phtml/aid/4476 ''' | |
| 202 if alignment == CENTER: | |
| 203 line = line.strip() | |
| 204 space = width - len(line) | |
| 205 return ' '*(space/2) + line + ' '*(space/2 + space%2) | |
| 206 elif alignment == RIGHT: | |
| 207 line = line.rstrip() | |
| 208 space = width - len(line) | |
| 209 return ' '*space + line | |
| 210 else: | |
| 211 if alignment == LEFT: | |
| 212 line = line.lstrip() | |
| 213 space = width - len(line) | |
| 214 return line + ' '*space | |
| 215 | |
| 216 | |
| 217 def format_line(columns, positions, contents, spacer=' | ', | |
| 218 collapse_whitespace=True, wsre=re.compile(r'\s+')): | |
| 219 ''' Fill up a single row with data from the contents ''' | |
| 220 l = [] | |
| 221 data = 0 | |
| 222 for i in range(len(columns)): | |
| 223 width, alignment = columns[i] | |
| 224 content = contents[i] | |
| 225 col = '' | |
| 226 while positions[i] < len(content): | |
| 227 word = content[positions[i]] | |
| 228 # if we hit a newline, honor it | |
| 229 if '\n' in word: | |
| 230 # chomp | |
| 231 positions[i] += 1 | |
| 232 break | |
| 233 | |
| 234 # make sure this word fits | |
| 235 if col and len(word) + len(col) > width: | |
| 236 break | |
| 237 | |
| 238 # no whitespace at start-of-line | |
| 239 if collapse_whitespace and wsre.match(word) and not col: | |
| 240 # chomp | |
| 241 positions[i] += 1 | |
| 242 continue | |
| 243 | |
| 244 col += word | |
| 245 # chomp | |
| 246 positions[i] += 1 | |
| 247 if col: | |
| 248 data = 1 | |
| 249 col = align(col, width, alignment) | |
| 250 l.append(col) | |
| 251 | |
| 252 if not data: | |
| 253 return '' | |
| 254 return spacer.join(l).rstrip() | |
| 255 | |
| 256 | |
| 257 def format_columns(columns, contents, spacer=' | ', collapse_whitespace=True, | |
| 258 splitre=re.compile(r'(\n|\r\n|\r|[ \t]+|\S+)')): | |
| 259 ''' Format the contents into columns, with 'spacing' between the | |
| 260 columns | |
| 261 ''' | |
| 262 assert len(columns) == len(contents), \ | |
| 263 'columns and contents must be same length' | |
| 264 | |
| 265 # split the text into words, spaces/tabs and newlines | |
| 266 for i in range(len(contents)): | |
| 267 contents[i] = splitre.findall(contents[i]) | |
| 268 | |
| 269 # now process line by line | |
| 270 l = [] | |
| 271 positions = [0]*len(contents) | |
| 272 while 1: | |
| 273 l.append(format_line(columns, positions, contents, spacer, | |
| 274 collapse_whitespace)) | |
| 275 | |
| 276 # are we done? | |
| 277 for i in range(len(contents)): | |
| 278 if positions[i] < len(contents[i]): | |
| 279 break | |
| 280 else: | |
| 281 break | |
| 282 return '\n'.join(l) | |
| 283 | |
| 284 def wrap(text, width=75, alignment=LEFTN): | |
| 285 return format_columns(((width, alignment),), [text], | |
| 286 collapse_whitespace=False) | |
| 287 | |
| 195 # vim: set et sts=4 sw=4 : | 288 # vim: set et sts=4 sw=4 : |
