Thank you Kent, I've done the modifications you suggested, and the code 
follows. I splitted the __init__ method in four methods (__init__, header, 
footer, dir_browser) and took the get_file_names out of pyrun class. Now I 
have two more general questions, if you don't mind.

1) (first a philosophical/methodological question) I see now that we can use 
classes in at least two ways. In my code, the class, as you said, is used as 
a container. In this case, the distinction class/instance isn't really 
important, as I will never have several instances of the same class. OTOH, I 
suppose it's not really OO programming. These statements are ok?

2) (a technical question, now) How do I test if a variable has been declared? 
In my first code, I used the try/except clause for that, but now you showed 
me a way of doing it without testing. But now I would like to add a cache in 
get_file_names(). For that, I would create a dir_cache variable of type dict, 
and store in it {'cwd': 'dirlist + filelist'} pairs. But how do I know if 
it's the first time, if I have to create the variable or just read from it?

Thanks,

Tiago.

#!/usr/bin/python

import urwid
import urwid.curses_display
import os

ui = urwid.curses_display.Screen()

ui.register_palette( [
        ('splash', 'black', 'dark red'),
        ('bg_splash', 'black', 'dark blue'),
        ('header', 'white', 'black'),
        ('footer', 'dark red', 'light gray'),
        ('browser', 'white', 'dark blue'),
        ('selected', 'white', 'dark red'),
        ('file', 'light gray', 'dark blue'),
        ('dir', 'light magenta', 'dark blue')
          ])

def run():
        size = ui.get_cols_rows()
        inst = pyrun()
        inst.main()

class pyrun:
        def __init__(self):
                self.initial_cwd = os.getcwd()
                self.cwd = self.initial_cwd
                
        def main(self):
                size = ui.get_cols_rows()
                                
                while True:
                        self.draw_screen( size )
                        keys = ui.get_input()
                        if "f10" in keys:
                                break
                        for k in keys:
                                if k == "window resize":
                                        size = ui.get_cols_rows()
                                        continue
                                elif k == "left":
                                        self.cwd = os.path.split(self.cwd)[0]
        
        def header(self):
                menu_txt = urwid.Text("F1 - Help     F2 - Options     F10 - 
Quit    
Now: %s" % self.cwd)
                return urwid.AttrWrap( menu_txt, 'header')
        
        def footer(self):
                down_txt = urwid.Text("pybrowser. Left Arrow: Parent.")
                return urwid.AttrWrap( down_txt, 'footer')
        
        def dir_browser(self):
                self.items = self.get_file_names ( self.cwd )
                return urwid.AttrWrap ( urwid.ListBox( self.items ), 'browser')
        
        def top_frame(self):
                return urwid.Frame( self.dir_browser(), self.header(), 
self.footer() )
                
        def draw_screen( self, size ):
                canvas = self.top_frame().render( size, focus=True )
                ui.draw_screen( size, canvas )
        
        def get_file_names(self, cwd):
                desc_list = os.listdir( cwd )
                dir_list = []
                file_list = []
                
                for f in desc_list:
                        if os.path.isdir(os.path.join(cwd, f)):
                                dir_list.append(f)
                        elif os.path.isfile(os.path.join(cwd,f)):
                                file_list.append(f)
                
                file_list.sort()
                dir_list.sort()
                
                file_list = [ urwid.AttrWrap ( urwid.Text(f) , 'file') for f in 
file_list ]
                dir_list = [ urwid.AttrWrap ( urwid.Text(f) , 'dir')  for f in 
dir_list ]
                
                return ( dir_list + file_list )
                
ui.run_wrapper( run )
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to