Ignore:
Timestamp:
04/19/18 15:36:50 (6 years ago)
Author:
martijk
Message:

removed rewritten print function

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/examples/new_rack_pbsmon.py.in

    r363 r365  
    109109    TERMINAL_COLOR=False
    110110
    111 ####
    112 ## Rewriting the print function, so it will work with all versions of Python
    113 def _print(*args, **kwargs):
    114     '''A wrapper function to make the functionality for the print function the same for Python2.4 and higher'''
    115     ## First try if we are running in Python3 and higher
    116     try:
    117         Print = eval('print')
    118         Print(*args, **kwargs)
    119     except SyntaxError:
    120         ## Then Python2.6 and Python2.7
    121         try:
    122             D = dict()
    123             exec('from __future__ import print_function\np=print', D)
    124             D['p'](*args, **kwargs)
    125             del D
    126         ## Finally Python2.5 or lower
    127         except SyntaxError:
    128             del D
    129             fout    = kwargs.get('file', sys.stdout)
    130             write   = fout.write
    131             if args:
    132                 write(str(args[0]))
    133                 sep = kwargs.get('sep', ' ')
    134                 for arg in args[1:]:
    135                     write(sep)
    136                     write(str(a))
    137                 write(kwargs.get('end', '\n'))
    138 
    139111class color:
    140112    PURPLE = '\033[95m'
     
    152124    return not any(x not in list_b for x in list_a)
    153125
    154 ## Import argparse here, as I need the _print function
     126## Import argparse here, as I need the print function
    155127try:
    156128    import argparse
    157129except ImportError:
    158     _print('Cannot find argparse module', file=sys.stderr)
     130    print('Cannot find argparse module', file=sys.stderr)
    159131    sys.exit(1)
    160132
     
    301273            p = PBSQuery( OPT_SERVERNAME )
    302274    except PBSError, reason:
    303         _print('Error: %s' % reason)
     275        print('Error: %s' % reason)
    304276        sys.exit( -1 )
    305277
     
    311283        nodes = p.getnodes( attr )
    312284    except PBSError, reason:
    313         _print('Error: %s' % reason)
     285        print('Error: %s' % reason)
    314286        sys.exit( -1 )
    315287
     
    435407    save_column = None
    436408   
    437     _print()   
    438     _print('  ', end=' ')
     409    print()   
     410    print('  ', end=' ')
    439411    for rack in xrange( START_RACK, racknr + 1 ):
    440412       
     
    450422                    char = save_column
    451423                    save_column = None
    452                 _print(char, end=' ')
    453         else:
    454             _print(char, end=' ')
    455     _print()   
    456 
    457     _print('  ', end=' ')
     424                print(char, end=' ')
     425        else:
     426            print(char, end=' ')
     427    print()   
     428
     429    print('  ', end=' ')
    458430    for rack in xrange( START_RACK, racknr + 1 ):
    459431       
     
    461433        if OPT_SKIP_EMPTY_RACKS:
    462434            if nodes.has_key( rack ):
    463                 _print(char, end=' ')
    464         else:
    465             _print(char, end=' ')
    466     _print()
     435                print(char, end=' ')
     436        else:
     437            print(char, end=' ')
     438    print()
    467439
    468440    for node in xrange( 1, nodenr + 1 ):
    469         _print('%2d' % node, end=' ')
     441        print('%2d' % node, end=' ')
    470442
    471443        for rack in xrange( START_RACK, racknr + 1 ):
     
    486458                if prop_color or job_color:
    487459                    if TERMINAL_COLOR and prop_color and job_color:
    488                         _print(color.GREEN + nodes[ rack ][ node ][ 'state_char' ] + color.END, end=' ')
     460                        print(color.GREEN + nodes[ rack ][ node ][ 'state_char' ] + color.END, end=' ')
    489461                    elif TERMINAL_COLOR and  prop_color:
    490                         _print(color.BLUE + nodes[ rack ][ node ][ 'state_char' ] + color.END, end=' ')
     462                        print(color.BLUE + nodes[ rack ][ node ][ 'state_char' ] + color.END, end=' ')
    491463                    elif TERMINAL_COLOR and  job_color:
    492                         _print(color.YELLOW + nodes[ rack ][ node ][ 'state_char' ] + color.END, end=' ')
     464                        print(color.YELLOW + nodes[ rack ][ node ][ 'state_char' ] + color.END, end=' ')
    493465                    else:
    494                         _print('M', end=' ')
     466                        print('M', end=' ')
    495467                else:
    496                     _print(nodes[ rack ][ node ][ 'state_char' ], end=' ')
     468                    print(nodes[ rack ][ node ][ 'state_char' ], end=' ')
    497469            except KeyError:
    498                 _print(' ', end=' ')
    499         _print()
    500     _print()
     470                print(' ', end=' ')
     471        print()
     472    print()
    501473
    502474def print_table_summary():
     
    510482            p = PBSQuery( OPT_SERVERNAME )
    511483    except PBSError, reason:
    512         _print('error: %s' % reason)
     484        print('error: %s' % reason)
    513485        sys.exit(-1)
    514486
     
    518490        nodes = p.getnodes(attr)
    519491    except PBSError, reason:
    520         _print('error: %s' % reason)
     492        print('error: %s' % reason)
    521493        sys.exit(-1)
    522494
     
    542514        if node.is_free():                          # can happen for single CPU jobs
    543515            if node.has_job():
    544 #               _print('TD: %s' % nodename, node)
     516#               print('TD: %s' % nodename, node)
    545517                state_char = PBS_STATES[pbs_ND_single]
    546518                count_states[pbs.ND_free] -=  1
     
    566538    n = 0
    567539    for state in legend:
    568         _print('  %s  %-13s : %-5d' % (PBS_STATES[state], state, count_states[state]), end=' ')
     540        print('  %s  %-13s : %-5d' % (PBS_STATES[state], state, count_states[state]), end=' ')
    569541
    570542        n = n + 1
    571543        if not (n & 1):
    572             _print()
     544            print()
    573545 
    574546    if TERMINAL_COLOR:
    575         _print()
    576         _print('Colors has been enabled for your terminal:')
    577         _print(' - ' + color.YELLOW + 'Matched jobs' + color.END)
    578         _print(' - ' + color.BLUE + 'Matched properties' + color.END)
    579         _print(' - ' + color.GREEN + 'Matched jobs and properties' + color.END)
     547        print()
     548        print('Colors has been enabled for your terminal:')
     549        print(' - ' + color.YELLOW + 'Matched jobs' + color.END)
     550        print(' - ' + color.BLUE + 'Matched properties' + color.END)
     551        print(' - ' + color.GREEN + 'Matched jobs and properties' + color.END)
    580552
    581553def print_extended(hosts=None, properties=None, jobs=None):
     
    606578        rows_str.append( row_str )
    607579
    608     _print()
    609     _print(row_header)
    610     _print(EXTENDED_PATTERNS[ 'line' ] % ( EXTENDED_PATTERNS[ 'line_char' ] * LENGTH_ROW ))
    611     _print('\n'.join( rows_str ))
    612     _print()
     580    print()
     581    print(row_header)
     582    print(EXTENDED_PATTERNS[ 'line' ] % ( EXTENDED_PATTERNS[ 'line_char' ] * LENGTH_ROW ))
     583    print('\n'.join( rows_str ))
     584    print()
    613585
    614586if __name__ == '__main__':
     
    661633        print_table(args.properties, args.jobs)
    662634    else:
    663         _print('Something is wrong, bye!', file=sys.stderr)
     635        print('Something is wrong, bye!', file=sys.stderr)
    664636        sys.exit( -1 )
    665637
Note: See TracChangeset for help on using the changeset viewer.