Changeset 285

Show
Ignore:
Timestamp:
02/13/06 22:12:11 (3 years ago)
Author:
athomas
Message:
  • Added logger mixin class.
  • Fixed bug with empty columns in print_table.
  • Added console.getch() for retrieving a single character from stdin in character buffered mode.
  • Added loader.load_all() for loading all sub-modules of an existing module. eg. import plugins; load_all(plugins)
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • pycrash/trunk/crash/console.py

    r284 r285  
    204204    colwidths = [0] * cols 
    205205    for i in range(0, cols): 
    206         colwidths[i] = max(map(lambda c: max(map(ctlen, c[i].splitlines())), table)) 
     206        colwidths[i] = max(map(lambda c: max([0] + map(ctlen, c[i].splitlines())), table)) 
    207207        if i < cols - 1: 
    208208            colwidths[i] += seplen 
     
    234234        xrow = [cwraptext(col.replace('^R', fmt), colwidths[i] - (i < cols - 1 and seplen or 0)) for i, col in enumerate(row)] 
    235235        # Pad column rows out to the maximum of all columns 
    236         maxrows = max(map(len, xrow)) 
     236        maxrows = max([0] + map(len, xrow)) 
    237237        for col in xrow: 
    238238            col += [''] * (maxrows - len(col)) 
    239         realrows = max(map(len, xrow)) 
     239        realrows = max([0] + map(len, xrow)) 
    240240        # Emit 
    241241        for i in range(0, realrows): 
     
    248248            cwrite(sys.stdout, '^N\n') 
    249249        rowalt += 1 
     250 
     251try: 
     252    import msvcrt 
     253    def getch(): 
     254        return msvcrt.getch() 
     255except ImportError: 
     256    import tty, sys, termios 
     257 
     258    def getch(): 
     259        fd = sys.stdin.fileno() 
     260        old_settings = termios.tcgetattr(fd) 
     261        try: 
     262            tty.setraw(sys.stdin.fileno()) 
     263            ch = sys.stdin.read(1) 
     264        finally: 
     265            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) 
     266        return ch 
     267 
     268getch.__doc__ = " Read a single character from stdin. " 
  • pycrash/trunk/crash/loader.py

    r269 r285  
    4949    # Return a reference to the class itself, not an instantiated object. 
    5050    return aClass 
     51 
     52def load_all(module): 
     53    """ Load all python modules in a module directory. Returns a tuple of 
     54        (success, failed) modules. """ 
     55    from dircache import listdir 
     56    import os 
     57    failed = [] 
     58    succeeded = [] 
     59    tried = [] 
     60    for p in module.__path__: 
     61        for f in listdir(p): 
     62            fp = os.path.join(p, f) 
     63            f, ext = os.path.splitext(f) 
     64            plugin = '.'.join((module.__name__, f)) 
     65            if plugin not in tried and not f.startswith('__') and (ext in  
     66                    ('.so', '.py', '.pyc') or os.path.isdir(fp)): 
     67                tried.append(plugin) 
     68                try: 
     69                    m = load_module(plugin) 
     70                    succeeded.append(m) 
     71                except Exception, e: 
     72                    failed.append((plugin, e)) 
     73    return (succeeded, failed) 
     74