Merge branch 'python_play'
[yaffs2.git] / direct / python / yaffs_browser.py
1 #!/usr/bin/python 
2 import Tkinter as tk
3 from yaffsfs import *
4 #import examples
5 import ctypes
6
7 yaffs_StartUp()
8 yaffs_mount("yaffs2/")
9 root_window =tk.Tk()
10 root_window.title("YAFFS Browser")
11 mount_list_text_variable=tk.StringVar()
12
13 mount_list_text_variable.set("yaffs2/")
14 current_directory_dict={}
15 open_windows_list=[]
16
17 class editor():
18     yaffs_handle=0
19     file_editor_root =0
20     save_button=[]
21     file_contents=0
22     file_path=0
23     def save_file(self):
24         #global current_directory_dict
25         print "saving the file"
26         print self.file_editor_text.get("1.0", tk.END) ##"1.0" is the index of the first line of text
27         yaffs_lseek(self.yaffs_handle, 0, 0)
28         data_to_be_written=self.file_editor_text.get("1.0", tk.END)
29         print "data to be saved", data_to_be_written
30         x=len(data_to_be_written)
31         print "length of data to be written",x  
32         output=yaffs_write(self.yaffs_handle,data_to_be_written , x)
33         print "output", output
34         yaffs_close(self.yaffs_handle)
35         self.yaffs_handle = yaffs_open(self.file_path,66,0666)
36         
37     def __init__(self):
38         global current_directory_dict
39         x=name_list_box.curselection()
40         self.id=int(x[0])
41         self.file_editor_root =tk.Toplevel()
42         self.save_button=tk.Button(self.file_editor_root, text="save", command=self.save_file)
43         self.save_button.pack()
44
45         self.file_path=current_directory_dict[self.id]["path"]
46         print "file path", self.file_path
47         self.file_editor_root.title(current_directory_dict[self.id]["path"])
48         self.file_editor_text=tk.Text(self.file_editor_root)
49         self.yaffs_handle = yaffs_open(current_directory_dict[self.id]["path"],66,0666)
50         length_of_file=yaffs_lseek(self.yaffs_handle, 0, 2) ##seeks to the end of the file
51         yaffs_lseek(self.yaffs_handle, 0, 0)## returns the handle to the front of th file
52         self.file_contents=ctypes.create_string_buffer(length_of_file)
53         yaffs_read(self.yaffs_handle,self.file_contents,length_of_file)
54         print "file contents", self.file_contents.raw
55         self.file_editor_text.insert(tk.END, self.file_contents.raw)
56         self.file_editor_text.pack()
57
58
59 def load_dir():
60     global current_directory_dict
61     print "loading a new directory*******************************************************************"
62     name_list_box.delete(0, tk.END)
63     current_directory_dict=yaffs_ls(mount_list_text_variable.get())
64     print "new directory", current_directory_dict
65     ##copy directory into file box
66     for x in range(0,len(current_directory_dict)):
67         name_list_box.insert(x,(current_directory_dict[x]["inodes"]+"  "+ current_directory_dict[x]["type"]+"  "+ current_directory_dict[x]["size"]+"  "+ current_directory_dict[x]["path"]))
68     name_list_box.grid(column=0, row=1)
69     return current_directory_dict
70 def save_file():
71     print file_editor_text.get(0, end)
72     pass
73 def load_file():
74     global open_windows_list
75     open_windows_list.append(editor())
76     
77
78
79     
80
81    # file_editor_text.insert(0, "this is a text box\n")
82     #use file_editor_text_box.get(0,  END) to retreve text
83     
84 def load_command(self=0):
85     global current_directory_dict
86     print "you loaded a file/dir"
87     x=name_list_box.curselection()
88     x=int(x[0])
89     print "cursor selection", x
90     print "dict", current_directory_dict
91
92     print "file inode is:", current_directory_dict[x]["inodes"]
93     
94     
95     print "file path is:", current_directory_dict[x]["path"]
96     if current_directory_dict[x]["type"]=="dir":
97         ##open dir
98         print "open directory"
99         mount_list_text_variable.set(current_directory_dict[x]["path"])
100         print mount_list_text_variable.get()
101         print "old directory dict", current_directory_dict
102         current_directory_dict=load_dir()
103         print "new directory dict passed back"
104
105     elif current_directory_dict[x]["type"]=="file" :
106         ##open file
107         print "open file"
108         load_file()
109     
110
111     ##mount_list_text_variable.set(mount_list_text_variable.get()+str(list[0][0]))
112     
113 def back_a_directory(self=0):
114     x=len(mount_list_text_variable.get())
115     string=mount_list_text_variable.get()
116     slashes_id=[]
117     #print "length of path", x 
118     #print "string been sorted:",  string
119     for i in range(0, x):
120         if string[i]=='/':
121             slashes_id.append(i)
122             #print "slash found at:", i
123         
124     print slashes_id
125     ##slashes_id.sort() not needed because the list is already in acending order
126     ##print "sorted",slashes_id
127
128     string=string[0: slashes_id[len(slashes_id)-2]+1]
129     print string
130     mount_list_text_variable.set(string)
131     load_dir()
132
133
134     
135     
136     
137         
138 def yaffs_ls(dname):
139     ls_dict=[]
140
141     if dname[-1] != "/": dname = dname + "/"
142     dc = yaffs_opendir(dname)
143     if dc != 0 :
144         sep = yaffs_readdir(dc)
145         while bool(sep):
146             
147             se = sep.contents
148             fullname = dname + se.d_name
149             st = yaffs_stat_struct()
150             result = yaffs_stat(fullname,byref(st))
151             perms = st.st_mode & 0777
152             isFile = True if st.st_mode & 0x8000 else False
153             isDir  = True if st.st_mode & 0x4000 else False
154
155             if isFile :
156                 ls_dict.append ({"type" :"file",  "inodes" :  str(se.d_ino),   "permissions" : str(hex(perms)),  "size": str(st.st_size), "path":  fullname})
157             elif isDir :
158                 ls_dict.append({"type":"dir", "inodes" :str(se.d_ino), "permissions":str( hex(perms)),"size":"0",   "path": fullname+"/"})
159
160 #            else :
161 #               ls_dict.append( "Other ("+hex(st.st_mode)+") "+se.d_ino, hex(perms)+ fullname)
162             sep = yaffs_readdir(dc)
163         yaffs_closedir(dc)
164         return ls_dict
165     else:
166         print "Could not open directory"
167         return -1
168
169     
170
171 ##toolbar 
172 toolbar_frame=tk.Frame(root_window)
173 button_open=tk.Button(toolbar_frame, command=load_command, text="load")
174 button_open.grid(column=0, row=0)
175 button_back=tk.Button(toolbar_frame, command=back_a_directory, text="back")
176 button_back.grid(column=1, row=0)
177 toolbar_frame.grid(row=0, column=0,  columnspan=3)
178
179
180
181 ##mount list entry box init
182 mount_list_frame=tk.Frame(root_window)
183 mount_list_label=tk.Label(mount_list_frame, text="mount list")
184 mount_list_label.pack(side=tk.RIGHT)
185
186 mount_list_entry_box= tk.Entry(mount_list_frame,textvariable=mount_list_text_variable)
187 mount_list_entry_box.pack(side=tk.RIGHT)
188 mount_list_frame.grid(row=1, column=0, columnspan=2)
189
190
191
192
193 list_frame=tk.Frame(root_window)
194
195
196
197 name_list_box=tk.Listbox(list_frame,exportselection=0, height=20, width=30)
198
199 load_dir()
200
201 #print "current_dir_dict", current_directory_dict
202
203
204 list_frame.grid()
205
206 name_list_box.bind("<Double-Button-1>", load_command)
207
208
209
210
211 root_window.mainloop()
212 yaffs_unmount("yaffs2/")
213
214 print"saving"
215