source: trunk/Monitoring/Watch/repo-state @ 871

Last change on this file since 871 was 871, checked in by jripsl, 11 years ago

Improve "config-card" file handling.
Use explicite function for "repo_io" module initialization

  • Property svn:executable set to *
File size: 10.4 KB
Line 
1#!/usr/bin/env python
2
3# --- ncurses sample 1
4
5#import curses
6#from curses import panel
7#
8#class Menu(object):
9#
10#       def __init__(self, items, stdscreen):
11#               self.window = stdscreen.subwin(0,0)
12#               self.window.keypad(1)
13#               self.panel = panel.new_panel(self.window)
14#               self.panel.hide()
15#               panel.update_panels()
16#
17#               self.position = 0
18#               self.items = items
19#               self.items.append(('exit','exit'))
20#
21#       def navigate(self, n):
22#               self.position += n
23#               if self.position < 0:
24#                       self.position = 0
25#               elif self.position >= len(self.items):
26#                       self.position = len(self.items)-1
27#
28#       def display(self):
29#               self.panel.top()
30#               self.panel.show()
31#               self.window.clear()
32#
33#
34#               while True:
35#                       self.window.refresh()
36#                       curses.doupdate()
37#                       for index, item in enumerate(self.items):
38#                               if index == self.position:
39#                                       mode = curses.A_REVERSE
40#                               else:
41#                                       mode = curses.A_NORMAL
42#
43#                               msg = '%d. %s' % (index, item[0])
44#                               self.window.addstr(1+index, 1, msg, mode)
45#
46#                       key = self.window.getch()
47#
48#                       if key in [curses.KEY_ENTER, ord('\n')]:
49#                               if self.position == len(self.items)-1:
50#                                       break
51#                               else:
52#                                       self.items[self.position][1]()
53#
54#                       elif key == curses.KEY_UP:
55#                               self.navigate(-1)
56#
57#                       elif key == curses.KEY_DOWN:
58#                               self.navigate(1)
59#
60#               self.window.clear()
61#               self.panel.hide()
62#               panel.update_panels()
63#               curses.doupdate()
64#
65#
66#class MyApp(object):
67#
68#       def __init__(self, stdscreen):
69#               self.screen = stdscreen
70#               curses.curs_set(0)
71#
72#               submenu_items = [
73#                               ('beep', curses.beep),
74#                               ('flash', curses.flash)
75#                               ]
76#               submenu = Menu(submenu_items, self.screen)
77#
78#               main_menu_items = [
79#                               ('beep', curses.beep),
80#                               ('flash', curses.flash),
81#                               ('submenu', submenu.display)
82#                               ]
83#               main_menu = Menu(main_menu_items, self.screen)
84#               main_menu.display()
85
86#if __name__ == '__main__':
87#       curses.wrapper(MyApp)
88
89
90
91
92
93# --- ncurses sample 2
94
95# Author: Nikolai Tschacher
96# Date: 02.06.2013
97#
98#class BoxSelector:
99#    """ Originally designed for accman.py.
100#        Display options build from a list of strings in a (unix) terminal.
101#        The user can browser though the textboxes and select one with enter.
102#    """
103#   
104#    def __init__(self, L):
105#        """ Create a BoxSelector object.
106#            L is a list of strings. Each string is used to build
107#            a textbox.
108#        """
109#        self.L = L
110#        # Element parameters. Change them here.
111#        self.TEXTBOX_WIDTH = 50
112#        self.TEXTBOX_HEIGHT = 6
113#
114#        self.PAD_WIDTH = 400
115#        self.PAD_HEIGHT = 10000
116#       
117#    def pick(self):
118#        """ Just run this when you want to spawn the selction process. """
119#        self._init_curses()
120#        self._create_pad()
121#       
122#        windows = self._make_textboxes()
123#        picked = self._select_textbox(windows)
124#       
125#        self._end_curses()
126#       
127#        return picked
128#       
129#    def _init_curses(self):
130#        """ Inits the curses appliation """
131#        # initscr() returns a window object representing the entire screen.
132#        self.stdscr = curses.initscr()
133#        # turn off automatic echoing of keys to the screen
134#        curses.noecho()
135#        # Enable non-blocking mode. Keys are read directly, without hitting enter.
136#        curses.cbreak()
137#        # Disable the mouse cursor.
138#        curses.curs_set(0)
139#        self.stdscr.keypad(1)
140#        # Enable colorous output.
141#        curses.start_color()
142#        curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_GREEN)
143#        curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK)
144#        self.stdscr.bkgd(curses.color_pair(2))
145#        self.stdscr.refresh()
146#
147#    def _end_curses(self):
148#        """ Terminates the curses application. """
149#        curses.nocbreak()
150#        self.stdscr.keypad(0)
151#        curses.echo()
152#        curses.endwin()
153#       
154#    def _create_pad(self):
155#        """ Creates a big self.pad to place the textboxes in. """
156#        self.pad = curses.newpad(self.PAD_HEIGHT, self.PAD_WIDTH)
157#        self.pad.box()
158#       
159#    def _make_textboxes(self):
160#        """ Build the textboxes in the pad center and put them in the
161#            horizontal middle of the pad. """
162#        # Get the actual screensize.
163#        maxy, maxx = self.stdscr.getmaxyx()
164#       
165#        windows = []
166#        i = 1
167#        for s in self.L:
168#            windows.append(self.pad.derwin(self.TEXTBOX_HEIGHT,
169#                    self.TEXTBOX_WIDTH, i, self.PAD_WIDTH//2-self.TEXTBOX_WIDTH//2))
170#            i += self.TEXTBOX_HEIGHT
171#           
172#        for k in range(len(windows)):
173#            windows[k].box()
174#            windows[k].addstr(4, 4, '0x{0:X} - {1}'.format(k, self.L[k]))
175#           
176#        return windows
177#
178#    def _center_view(self, window):
179#        """ Centers and aligns the view according to the window argument given.
180#            Returns the (y, x) coordinates of the centered window. """
181#        # The refresh() and noutrefresh() methods of a self.pad require 6 arguments
182#        # to specify the part of the self.pad to be displayed and the location on
183#        # the screen to be used for the display. The arguments are pminrow,
184#        # pmincol, sminrow, smincol, smaxrow, smaxcol; the p arguments refer
185#        # to the upper left corner of the self.pad region to be displayed and the
186#        # s arguments define a clipping box on the screen within which the
187#        # self.pad region is to be displayed.
188#        cy, cx = window.getbegyx()
189#        maxy, maxx = self.stdscr.getmaxyx()
190#        self.pad.refresh(cy, cx, 1, maxx//2 - self.TEXTBOX_WIDTH//2, maxy-1, maxx-1)
191#        return (cy, cx)
192#       
193#    def _select_textbox(self, windows):
194#        # See at the root textbox.
195#        topy, topx = self._center_view(windows[0])
196#       
197#        current_selected = 0
198#        last = 1
199#        top_textbox = windows[0]
200#       
201#        while True:
202#            # Highligth the selected one, the last selected textbox should
203#            # become normal again.
204#            windows[current_selected].bkgd(curses.color_pair(1))
205#            windows[last].bkgd(curses.color_pair(2))
206#           
207#            # While the textbox can be displayed on the page with the current
208#            # top_textbox, don't alter the view. When this becomes impossible,
209#            # center the view to last displayable textbox on the previous view.
210#            maxy, maxx = self.stdscr.getmaxyx()
211#            cy, cx = windows[current_selected].getbegyx()
212#           
213#            # The current window is to far down. Switch the top textbox.
214#            if ((topy + maxy - self.TEXTBOX_HEIGHT) <= cy):
215#                top_textbox = windows[current_selected]
216#           
217#            # The current window is to far up. There is a better way though...
218#            if topy >= cy + self.TEXTBOX_HEIGHT:
219#                top_textbox = windows[current_selected]
220#           
221#            if last != current_selected:
222#                last = current_selected
223#               
224#            topy, topx = self._center_view(top_textbox)
225#           
226#            c = self.stdscr.getch()
227#           
228#            # Vim like KEY_UP/KEY_DOWN with j(DOWN) and k(UP).
229#            if c == ord('j'):
230#                if current_selected >= len(windows)-1:
231#                    current_selected = 0 # wrap around.
232#                else:
233#                    current_selected += 1
234#            elif c == ord('k'):
235#                if current_selected <= 0:
236#                    current_selected = len(windows)-1 # wrap around.
237#                else:
238#                    current_selected -= 1
239#            elif c == ord('q'): # Quit without selecting.
240#                break
241#            # At hitting enter, return the index of the selected list element.
242#            elif c == curses.KEY_ENTER or c == 10:
243#                return int(current_selected)
244#
245#
246#if __name__ == '__main__':
247#    # As simple as that.
248#    L = [
249#         'I wish I was a wizard',
250#         'Sometimes it all just makes sense',
251#         'This string is here because I need it',
252#         'Being or not being!',
253#         'Python is worse then PHP ;)',
254#         'a -> b <=> if a then b'
255#        ]
256#       
257#    choice = BoxSelector(L).pick()
258#    print('[+] Your choice was "{0}"'.format(L[choice]))
259
260
261
262
263
264
265
266# --- ncurses sample 3
267
268
269
270import sys
271import curses
272#import pprint
273
274# line below is to include "smon" package in the search path
275sys.path.append("/home/jripsl/snapshot/Monitoring")
276
277from smon import repo_io
278import smon.types
279
280
281repo_io.init() # open DB connection
282
283repo_io.populate_tables_with_sample()
284
285simulations=repo_io.retrieve_simulations()
286
287mylines = ["Line {0} ".format(id)*3 for id in range(1,11)]
288
289
290def main(stdscr):
291  hlines = begin_y = begin_x = 5 ; wcols = 10
292  # calculate total content size
293  padhlines = len(mylines)
294  padwcols = 0
295  for line in mylines:
296    if len(line) > padwcols: padwcols = len(line)
297  padhlines += 2 ; padwcols += 2 # allow border
298  #stdscr.addstr("padhlines "+str(padhlines)+" padwcols "+str(padwcols)+"; ")
299  # both newpad and subpad are <class '_curses.curses window'>:
300  mypadn = curses.newpad(padhlines, padwcols)
301  mypads = stdscr.subpad(padhlines, padwcols, begin_y, begin_x+padwcols+4)
302  #stdscr.addstr(str(type(mypadn))+" "+str(type(mypads)) + "\n")
303  mypadn.scrollok(1)
304  mypadn.idlok(1)
305  mypads.scrollok(1)
306  mypads.idlok(1)
307
308  mypadn.border(0) # first ...
309  mypads.border(0) # ... border
310
311  for line in mylines:
312    mypadn.addstr(padhlines-1,1, line)
313    mypadn.scroll(1)
314    mypads.addstr(padhlines-1,1, line)
315    mypads.scroll(1)
316
317  mypadn.border(0) # second ...
318  mypads.border(0) # ... border
319
320  # refresh parent first, to render the texts on top
321  #~ stdscr.refresh()
322
323  # refresh the pads next
324  mypadn.refresh(0,0, begin_y,begin_x, begin_y+hlines, begin_x+padwcols)
325  mypads.refresh()
326  mypads.touchwin()
327  mypadn.touchwin()
328  stdscr.touchwin() # no real effect here
329  #stdscr.refresh() # not here! overwrites newpad!
330  mypadn.getch()
331  # even THIS command erases newpad!
332  # (unless stdscr.refresh() previously):
333  stdscr.getch()
334
335curses.wrapper(main)
336
337
338
339
340repo_io.free() # close DB connection
341
342
343
344
345
346
347
348
349
350# --- urwid sample 1
351
352#import urwid
353#
354#txt = urwid.Text(u"Hello World")
355#fill = urwid.Filler(txt, 'top')
356#loop = urwid.MainLoop(fill)
357#loop.run()
358
359
360# --- urwid sample 2
361
362#import urwid
363#
364#def show_or_exit(key):
365#    if key in ('q', 'Q'):
366#        raise urwid.ExitMainLoop()
367#    txt.set_text(repr(key))
368#
369#txt = urwid.Text(u"Hello World")
370#fill = urwid.Filler(txt, 'top')
371#loop = urwid.MainLoop(fill, unhandled_input=show_or_exit)
372#loop.run()
373
Note: See TracBrowser for help on using the repository browser.