Init style output with python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maxim Veksler

    #1

    Init style output with python?

    Hi list,

    I'm working on writing sanity check script, and for aesthetic reasons
    I would like the output be in the formatted like the gentoo init
    script output, that is:
    """
    Check for something ............... ............... .... [OK]
    Check for something else ............... ...........[FAIL]
    """

    Is there are frame work or something in python that would allow me to
    do this (quickly) ?
    If not, ideas how I should I be getting this boring task of:
    1. get screen width
    2. get output string length
    3. get out status length
    4. calculate space
    5. print string, print space, print status, print newline

    what happens if user changes textual terminal "resolution " ?

    p.s.
    I would also like to "OK" and "FAIL" output to be colored. I haven't
    found anything for python to would allow to to output to ansi (linux,
    rxvt, xterm). Here's a quick class I've written (in the hope it proves
    to be useful to the next guy).

    """
    #!/usr/bin/env python
    """ This stuff is under GPL, as always"""

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

    def __get_tput_colo r_value__(color code):
    from commands import getoutput
    return getoutput('tput setaf ' + colorcode)

    BLACK_FG = __get_tput_colo r_value__('0')
    RED_FG = __get_tput_colo r_value__('1')
    GREEN_FG = __get_tput_colo r_value__('2')
    YELLOW_FG = __get_tput_colo r_value__('3')
    BLUE_FG = __get_tput_colo r_value__('4')
    MAGENTA_FG = __get_tput_colo r_value__('5')
    CYAN_FG = __get_tput_colo r_value__('6')
    WHITE_FG = __get_tput_colo r_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


    cc = ColorTerm()
    print cc.red('Cool!') + cc.yellow('?'), cc.green('Sure is!!!')
    print "Now setting your terminal text color to blue" + cc.BLUE_FG
    print "well don't be blue about this, here let me set it back for you"
    print cc.BLACK_FG + "see, nothing to worry about"
    """

    --
    Cheers,
    Maxim Veksler

    "Free as in Freedom" - Do u GNU ?
  • John Machin

    #2
    Re: Init style output with python?

    On May 6, 9:27 am, "Maxim Veksler" <hq4e...@gmail. comwrote:
    Hi list,
    >
    I'm working on writing sanity check script, and for aesthetic reasons
    I would like the output be in the formatted like the gentoo init
    script output, that is:
    """
    Check for something ............... ............... .... [OK]
    Check for something else ............... ...........[FAIL]
    """
    >
    Is there are frame work or something in python that would allow me to
    do this (quickly) ?
    If not, ideas how I should I be getting this boring task of:
    1. get screen width
    Is it not (a) safe (b) sensible to assume a minimum width (say 79) and
    avoid the whole question of determining the terminal width?
    2. get output string length
    3. get out status length
    4. calculate space
    5. print string, print space, print status, print newline
    Surely you don't need assistance with steps 2 - 5 ...
    what happens if user changes textual terminal "resolution " ?
    Something rather unaesthetic, I imagine.

    Comment

    • Tina I

      #3
      Re: Init style output with python?

      Maxim Veksler wrote:
      Is there are frame work or something in python that would allow me to
      do this (quickly) ?
      If not, ideas how I should I be getting this boring task of:
      1. get screen width
      You can look into the 'curses' module and do something like:

      screen = curses.initscre en()
      maxheight, maxwith = screen.getmaxyx ()

      In my experience curses can be a bit tricky to work with but the online
      tutorials have some nice examples that help you avoid some of the
      pitfalls (like messing up your terminal)

      Tina

      Comment

      • Maxim Veksler

        #4
        Re: Init style output with python?

        On 5/6/07, Tina I <tinaweb@bestem selv.comwrote:
        Maxim Veksler wrote:
        >
        Is there are frame work or something in python that would allow me to
        do this (quickly) ?
        If not, ideas how I should I be getting this boring task of:
        1. get screen width
        >
        You can look into the 'curses' module and do something like:
        >
        screen = curses.initscre en()
        maxheight, maxwith = screen.getmaxyx ()
        >
        In my experience curses can be a bit tricky to work with but the online
        tutorials have some nice examples that help you avoid some of the
        pitfalls (like messing up your terminal)
        >
        Tina
        --

        >
        Fine! Thank you.

        curses is very helpful, I'm attaching the code.
        I see it has support for colors as well, but I haven't found any
        tutorial that would explain how to use them.

        Please note that this is just a draft, I'm not catching any
        KeyboardInterru pt nor nothing.

        """#!/usr/bin/env python

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

        def __get_tput_colo r_value__(color code):
        from commands import getoutput
        return getoutput('tput setaf ' + colorcode)

        BLACK_FG = __get_tput_colo r_value__('0')
        RED_FG = __get_tput_colo r_value__('1')
        GREEN_FG = __get_tput_colo r_value__('2')
        YELLOW_FG = __get_tput_colo r_value__('3')
        BLUE_FG = __get_tput_colo r_value__('4')
        MAGENTA_FG = __get_tput_colo r_value__('5')
        CYAN_FG = __get_tput_colo r_value__('6')
        WHITE_FG = __get_tput_colo r_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(Co lorTerm):
        import curses

        def __init__(self, report_type = None):
        pass

        def initstyle_messa ge(self, msg, status = True):
        screen = self.curses.ini tscr(); self.curses.end win()
        if status:
        status_msg = '[' + self.green('OK' ) + ']'
        else:
        status_msg = '[' + self.red('FAIL' ) + ']'

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

        cc = StatusWriter()
        while 1:
        print cc.initstyle_me ssage('The end is at hand')
        print cc.initstyle_me ssage('Lets party', False)
        print cc.initstyle_me ssage('Why like this?', True)
        """

        --
        Cheers,
        Maxim Veksler

        "Free as in Freedom" - Do u GNU ?

        Comment

        Working...