comparison cmd2.py @ 319:c58cd7e48db7

begin to switch settable to TextLineList
author catherine@dellzilla
date Thu, 11 Feb 2010 13:07:05 -0500
parents de23e595bb5c
children b9f19255d4b7
comparison
equal deleted inserted replaced
318:f44ad8de0d17 319:c58cd7e48db7
269 exc = self.myException 269 exc = self.myException
270 exc.loc = loc 270 exc.loc = loc
271 exc.pstr = instring 271 exc.pstr = instring
272 raise exc 272 raise exc
273 273
274 class TextLineList(list):
275 '''A list that "wants" to consist of separate lines of text.
276 Splits multi-line strings and flattens nested lists to
277 achieve that.
278 Also omits blank lines, strips leading/trailing whitespace.
279
280 >>> tll = TextLineList(['my', 'dog\\nhas', '', [' fleas', 'and\\nticks']])
281 >>> tll
282 ['my', 'dog', 'has', 'fleas', 'and', 'ticks']
283 >>> tll.append(['and', ['spiders', 'and']])
284 >>> tll
285 ['my', 'dog', 'has', 'fleas', 'and', 'ticks', 'and', 'spiders', 'and']
286 >>> tll += 'fish'
287 >>> tll
288 ['my', 'dog', 'has', 'fleas', 'and', 'ticks', 'and', 'spiders', 'and', 'fish']
289 '''
290 def flattened(self, texts):
291 result = []
292 if isinstance(texts, basestring):
293 result.extend(texts.splitlines())
294 else:
295 for text in texts:
296 result.extend(self.flattened(text))
297 result = [r.strip() for r in result if r.strip()]
298 return result
299 def flatten(self):
300 list.__init__(self, self.flattened(self))
301 def __init__(self, values):
302 list.__init__(self, values)
303 self.flatten()
304 def append(self, value):
305 list.append(self, value)
306 self.flatten()
307 def extend(self, values):
308 list.extend(self, values)
309 self.flatten()
310 def __setitem__(self, idx, value):
311 list.__setitem__(self, idx, value)
312 self.flatten()
313 def __iadd__(self, value):
314 if isinstance(value, basestring):
315 self.append(value)
316 else:
317 list.__iadd__(self, value)
318 self.flatten()
319 return self
320
274 def replace_with_file_contents(fname): 321 def replace_with_file_contents(fname):
275 if fname: 322 if fname:
276 try: 323 try:
277 result = open(os.path.expanduser(fname[0])).read() 324 result = open(os.path.expanduser(fname[0])).read()
278 except IOError: 325 except IOError:
334 current_script_dir = None 381 current_script_dir = None
335 reserved_words = [] 382 reserved_words = []
336 feedback_to_output = False # Do include nonessentials in >, | output 383 feedback_to_output = False # Do include nonessentials in >, | output
337 quiet = False # Do not suppress nonessential output 384 quiet = False # Do not suppress nonessential output
338 debug = True 385 debug = True
339 settable = ''' 386 settable = TextLineList('''
340 prompt 387 prompt
341 colors Colorized output (*nix only) 388 colors Colorized output (*nix only)
342 continuation_prompt 389 continuation_prompt
343 debug 390 debug
344 default_file_name for `save`, `load`, etc. 391 default_file_name for `save`, `load`, etc.
347 feedback_to_output include nonessentials in `|`, `>` results 394 feedback_to_output include nonessentials in `|`, `>` results
348 quiet 395 quiet
349 echo Echo command issued into output 396 echo Echo command issued into output
350 timing Report execution times 397 timing Report execution times
351 abbrev Accept abbreviated commands 398 abbrev Accept abbreviated commands
352 '''.splitlines() 399 ''')
353 400
354 def poutput(self, msg): 401 def poutput(self, msg):
355 if msg: 402 if msg:
356 self.stdout.write(msg) 403 self.stdout.write(msg)
357 if msg[-1] != '\n': 404 if msg[-1] != '\n':