Hi Maxim!

I'm not a curses expert and I don't know why this is happening, but maybe
these suggestions will help:

  1. Have you read the tutorial Curses Programming with
Python<http://www.amk.ca/python/howto/curses/>?
  I found it very helpful.
  2. Try using curses.addstr to print text (including colors) instead of
  'print' or 'sys.std*'. Examples are in the tutorial.
  3. curses.wrapper is nice, takes care of initscr and friends.
  4. Check out the Python curses demos,
here<http://svn.python.org/projects/python/trunk/Demo/curses/>or in a
Python source tarball.
  5. Bli Kesher, you can get the color codes from terminfo without
  running 'tput' (but you don't need them when using curses.addstr):
  curses.setupterm() # not needed if you call curses.initscr()
  curses.tparm(curses.tigetstr('setaf'), 0) # same as commands.getoutput('tput
  setaf 0')

Hope I helped,
Ori.

On 5/7/07, Maxim Veksler <[EMAIL PROTECTED]> wrote:

Shalom people,

I'm new on this list, and to python in general.
Fun with python to us.

I'm attaching my sanity checking script (which in the mean time doing
it's job perfectly - getting me insanse :)

The lines for error don't print with CR/LF but only with LF; I
susspect curses is to blame but I have no experience with it and
haven't found what proper switches/flags/objects I need to pass to it
to make it work like expected.

Current output
"""
./ColorReport.py
Check directory "/bin" exists
                                              [OK]Check directory
"/usr" exists
                              [OK]Check directory "/opt/hq4ever"
exists
                                     Failed directory existance test
                                                                    Do
you have my-core rpm installed
Check directory "/opt/hq4ever" exists
                                            [FAIL]
"""

expected output
"""
./ColorReport.py
Check directory "/bin" exists
                              [OK]
Check directory "/usr" exists
                              [OK]
Check directory "/opt/hq4ever" exists
    Failed directory existence test
    Do you have my-core rpm installed
Check directory "/opt/hq4ever" exists
                          [FAIL]

the code

"""
#!/usr/bin/python -u

class ColorTerm:
    def __init__(self, Mono = False):
        pass

    def __get_tput_color_value__(colorcode):
        from commands import getoutput
        return getoutput('tput setaf ' + colorcode)

    BLACK_FG =  __get_tput_color_value__('0')
    RED_FG =            __get_tput_color_value__('1')
    GREEN_FG =  __get_tput_color_value__('2')
    YELLOW_FG =         __get_tput_color_value__('3')
    BLUE_FG =           __get_tput_color_value__('4')
    MAGENTA_FG =        __get_tput_color_value__('5')
    CYAN_FG =           __get_tput_color_value__('6')
    WHITE_FG =  __get_tput_color_value__('7')

    def black(self, msg):
        return self.BLACK_FG + msg + self.BLACK_FG

    def red(self, msg):
        return self.RED_FG + msg + self.BLACK_FG

    def green(self, msg):
        return self.GREEN_FG + msg + self.BLACK_FG

    def yellow(self, msg):
        return self.YELLOW_FG + msg + self.BLACK_FG

    def blue(self, msg):
        return self.BLUE_FG + msg + self.BLACK_FG

    def magenta(self, msg):
        return self.MAGENTA_FG + msg + self.BLACK_FG

    def cyan(self, msg):
        return self.CYAN_FG + msg + self.BLACK_FG

    def white(self, msg):
        return self.WHITE_FG + msg + self.BLACK_FG

class StatusWriter(ColorTerm):
    import curses

    def __init__(self, report_type = None):
        pass

    def initstyle_message(self, printed_msg, status = True):
        screen = self.curses.initscr(); self.curses.endwin()
        if status:
            status_msg = '[' + self.green('OK') + ']'
            status_msg_len = len('['+'OK'+']')
        else:
            status_msg = '[' + self.red('FAIL') + ']'
            status_msg_len = len('['+'FAIL'+']')

        __spaces_count = ( screen.getmaxyx()[1] -
(len(printed_msg)+status_msg_len)  )
        #print ''
        #print len(printed_msg)
        #print __spaces_count
        #print len(status_msg)
        #print '=', len(printed_msg)+__spaces_count+len(status_msg)
        return printed_msg + ' '*__spaces_count + status_msg

    def initstyle_skip_message(self, msg):
        screen = self.curses.initscr(); self.curses.endwin()
        status_msg = '[' + self.yellow('SKIP') + ']'

        __spaces_count = ( screen.getmaxyx()[1] -
(len(msg)+len(status_msg)) )
        return msg + ' '*__spaces_count + status_msg

class SanityCheck():
    cc = StatusWriter()

    def __init__(self, test_name, depends = []):
        self.status = None
        self.test_name = test_name
        self.depends = depends
        pass

    def directory_exists(self, dirpath):
        import os.path
        import sys

        usr_output_info = 'Check directory "' + dirpath + '" exists'
        sys.stdout.write(usr_output_info)

        if os.path.exists(dirpath):
            if self.status == None:
                self.status = True
            sys.stdout.write( self.cc.initstyle_message(usr_output_info) )
        else:
            self.status = False
            print
            print "Failed directory existence test"
            print "Do you have", '-'.join(self.test_name.split('_')[:2]),
"rpm installed"
            sys.stdout.write(
self.cc.initstyle_message(usr_output_info, False) )



    def file_exists(self, filepath):
        pass

    def directory_writable(self, dirpath):
        pass

    def file_executable(self, filepath):
        pass

    def oscommand(self, command):
        pass


    def docheck(self, check, predeps_check = []):
        pass
        for check_field in predeps_check:

            if not ChecksMap[check_field]:
                raise check_field + " failed!"
        pass

my_core_paths = SanityCheck('my_core_paths')

my_core_paths.directory_exists('/bin')
my_core_paths.directory_exists('/usr')
my_core_paths.directory_exists('/opt/hq4ever')
"""


Thanks for the helps guys (and girls).

Maxim.
--
Cheers,
Maxim Veksler

"Free as in Freedom" - Do u GNU ?

לענות