iniwap

iniwap的博客

他的个人主页  他的博客

pys60之手机文件管理器的基本架构

iniwap  2009年08月12日 星期三 19:58 | 1024次浏览 | 0条评论

以目录的形式显示手机C盘和E盘的文件以及文件夹,逐级可查看,选中返回所选文件路径。添加,删除,新建等未做处理,这些大家都会,都是标准库的函数。感觉PY着方面真的很方便!

# -*- coding: utf-8 -*-
# (c) iniwap
# wtx2zhm@126.com
# License: GPL3
import os,appuifw,e32,graphics
from key_codes import EKeyLeftArrow
class FileManager:
    def __init__(self):
        self.lock=e32.Ao_lock()
        self.dir_stack=[]
        self.__path__=''
        self.current_dir=['C','E']
    def chn(self,x):
        return x.decode('utf8')
    def rn(self,x):
        return x.encode('utf8')
    def file_select(self):
        self.lb=appuifw.Listbox([self.chn(item) for item in self.current_dir],self.update)
        self.lb.bind(EKeyLeftArrow,lambda:self.update(-1))
        appuifw.app.title=self.chn('open file')
        appuifw.app.body=self.lb
        appuifw.app.exit_key_handler=self.lock.signal
        self.lock.wait()
    def join_path(self,current_path):
        return current_path[0]+':\\'+'\\'.join(current_path[1:])
    def list_dir(self,path_stack):
        path=self.join_path(path_stack)
        for dir_list in os.listdir(path):
            if os.path.isdir(path+'\\'+dir_list):
                self.current_dir.insert(0,dir_list)
            elif dir_list.find('.png')!=-1 or dir_list.find('.jpg')!=-1:
                self.current_dir.append(dir_list)   
    def update(self,i=None):
        if not i==None:
            index=i
        else:
            index=self.lb.current()
        if len(self.dir_stack)==0 and index!=-1:
            self.dir_stack.append(self.current_dir[index])
            self.current_dir=[]
            self.list_dir(self.dir_stack)
        elif index==-1:
            if len(self.dir_stack)>0:
                self.dir_stack.pop()
                if len(self.dir_stack)==0:
                    self.current_dir=['C','E']
                else:
                    self.current_dir=[]
                    self.list_dir(self.dir_stack)
        elif os.path.isdir(self.join_path(self.dir_stack)+'\\'+self.current_dir[index]):
            self.dir_stack.append(self.rn(self.chn(self.current_dir[index])))
            self.current_dir=[]
            self.list_dir(self.dir_stack)
        else:
            dir_stack=[self.chn(i) for i in self.dir_stack]
            self.__path__=os.path.join(self.join_path(dir_stack),self.chn(self.current_dir[index]))
        items=[self.chn(s) for s in self.current_dir]
        if len(items)==0:
            items=[self.chn('(empty)')]
        self.lb.set_list(items,0)
fm=FileManager()
fm.file_select()
path=fm.__path__

 

评论

我的评论:

发表评论

请 登录 后发表评论。还没有在Zeuux哲思注册吗?现在 注册 !

暂时没有评论

Zeuux © 2024

京ICP备05028076号