##// END OF EJS Templates
allow utils.text.indent to [optionally] flatten existing indentation.
MinRK -
Show More
@@ -161,7 +161,7 b' class Application(SingletonConfigurable):'
161 161 help = trait.get_metadata('help')
162 162 print alias, "(%s)"%longname, ':', trait.__class__.__name__
163 163 if help:
164 print indent(help)
164 print indent(help, flatten=True)
165 165 print
166 166
167 167 def print_flag_help(self):
@@ -176,7 +176,7 b' class Application(SingletonConfigurable):'
176 176
177 177 for m, (cfg,help) in self.flags.iteritems():
178 178 print '--'+m
179 print indent(help)
179 print indent(help, flatten=True)
180 180 print
181 181
182 182 def print_help(self):
@@ -44,7 +44,6 b' class MultipleInstanceError(ConfigurableError):'
44 44 # Configurable implementation
45 45 #-----------------------------------------------------------------------------
46 46
47
48 47 class Configurable(HasTraits):
49 48
50 49 config = Instance(Config,(),{})
@@ -151,7 +150,7 b' class Configurable(HasTraits):'
151 150 header = "%s.%s : %s" % (cls.__name__, k, v.__class__.__name__)
152 151 final_help.append(header)
153 152 if help is not None:
154 final_help.append(indent(help))
153 final_help.append(indent(help, flatten=True))
155 154 return '\n'.join(final_help)
156 155
157 156 @classmethod
@@ -391,15 +391,39 b' def igrep(pat,list):'
391 391 return grep(pat,list,case=0)
392 392
393 393
394 def indent(str,nspaces=4,ntabs=0):
394 def indent(instr,nspaces=4, ntabs=0, flatten=False):
395 395 """Indent a string a given number of spaces or tabstops.
396 396
397 397 indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.
398
399 Parameters
400 ----------
401
402 instr : basestring
403 The string to be indented.
404 nspaces : int (default: 4)
405 The number of spaces to be indented.
406 ntabs : int (default: 0)
407 The number of tabs to be indented.
408 flatten : bool (default: False)
409 Whether to scrub existing indentation. If True, all lines will be
410 aligned to the same indentation. If False, existing indentation will
411 be strictly increased.
412
413 Returns
414 -------
415
416 str|unicode : string indented by ntabs and nspaces.
417
398 418 """
399 if str is None:
419 if instr is None:
400 420 return
401 421 ind = '\t'*ntabs+' '*nspaces
402 outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind))
422 if flatten:
423 pat = re.compile(r'^\s*', re.MULTILINE)
424 else:
425 pat = re.compile(r'^', re.MULTILINE)
426 outstr = re.sub(pat, ind, instr)
403 427 if outstr.endswith(os.linesep+ind):
404 428 return outstr[:-len(ind)]
405 429 else:
General Comments 0
You need to be logged in to leave comments. Login now