Merge branch 'master' of ssh://www.aleph1.co.uk/home/aleph1/git/yaffs2
[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         length_of_file=len(data_to_be_written)
31         print "length of data to be written",length_of_file 
32         output=yaffs_write(self.yaffs_handle,data_to_be_written , length_of_file)
33         print "output", output
34         yaffs_ftruncate(self.yaffs_handle, length_of_file)
35         yaffs_close(self.yaffs_handle)
36         
37         self.yaffs_handle = yaffs_open(self.file_path,66,0666)
38         load_dir()
39         
40     def __init__(self):
41         global current_directory_dict
42         x=name_list_box.curselection()
43         self.id=int(x[0])
44         self.file_editor_root =tk.Toplevel()
45         self.save_button=tk.Button(self.file_editor_root, text="save", command=self.save_file)
46         self.save_button.pack()
47
48         self.file_path=current_directory_dict[self.id]["path"]
49         print "file path", self.file_path
50         self.file_editor_root.title(current_directory_dict[self.id]["path"])
51         self.file_editor_text=tk.Text(self.file_editor_root)
52         self.yaffs_handle = yaffs_open(current_directory_dict[self.id]["path"],66,0666)
53         length_of_file=yaffs_lseek(self.yaffs_handle, 0, 2) ##seeks to the end of the file
54         yaffs_lseek(self.yaffs_handle, 0, 0)## returns the handle to the front of th file
55         self.file_contents=ctypes.create_string_buffer(length_of_file)
56         yaffs_read(self.yaffs_handle,self.file_contents,length_of_file)
57         print "file contents", self.file_contents.raw
58         self.file_editor_text.insert(tk.END, self.file_contents.raw)
59         self.file_editor_text.pack()
60         ##self.file_editor_text.bind("<Control-s>", self.save_file)
61         ##doesn't work because it can't pass "self"
62
63 def load_dir():
64     global current_directory_dict
65     print "loading a new directory*******************************************************************"
66     name_list_box.delete(0, tk.END)
67     current_directory_dict=yaffs_ls(mount_list_text_variable.get())
68     print "new directory", current_directory_dict
69     ##copy directory into file box
70     for x in range(0,len(current_directory_dict)):
71         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"]))
72     name_list_box.grid(column=0, row=1)
73     return current_directory_dict
74
75 def load_file():
76     global open_windows_list
77     open_windows_list.append(editor())
78
79 def load_command(self=0):
80     global current_directory_dict
81     print "you loaded a file/dir"
82     x=name_list_box.curselection()
83     x=int(x[0])
84     print "cursor selection", x
85     print "dict", current_directory_dict
86
87     print "file inode is:", current_directory_dict[x]["inodes"]
88     
89     
90     print "file path is:", current_directory_dict[x]["path"]
91     if current_directory_dict[x]["type"]=="dir":
92         ##open dir
93         print "open directory"
94         mount_list_text_variable.set(current_directory_dict[x]["path"])
95         print mount_list_text_variable.get()
96         print "old directory dict", current_directory_dict
97         #current_directory_dict=load_dir()
98         load_dir()
99
100         print "new directory dict passed back"
101
102     elif current_directory_dict[x]["type"]=="file" :
103         ##open file
104         print "open file"
105         load_file()
106     
107
108     ##mount_list_text_variable.set(mount_list_text_variable.get()+str(list[0][0]))
109     
110 def back_a_directory(self=0):
111     x=len(mount_list_text_variable.get())
112     string=mount_list_text_variable.get()
113     slashes_id=[]
114     #print "length of path", x 
115     #print "string been sorted:",  string
116     for i in range(0, x):
117         if string[i]=='/':
118             slashes_id.append(i)
119             #print "slash found at:", i
120         
121     print slashes_id
122     ##slashes_id.sort() not needed because the list is already in acending order
123     ##print "sorted",slashes_id
124
125     string=string[0: slashes_id[len(slashes_id)-2]+1]
126     print string
127     mount_list_text_variable.set(string)
128     load_dir()
129
130
131     
132     
133     
134         
135 def yaffs_ls(dname):
136     ls_dict=[]
137
138     if dname[-1] != "/": dname = dname + "/"
139     dc = yaffs_opendir(dname)
140     if dc != 0 :
141         sep = yaffs_readdir(dc)
142         while bool(sep):
143             
144             se = sep.contents
145             fullname = dname + se.d_name
146             st = yaffs_stat_struct()
147             result = yaffs_stat(fullname,byref(st))
148             perms = st.st_mode & 0777
149             isFile = True if st.st_mode & 0x8000 else False
150             isDir  = True if st.st_mode & 0x4000 else False
151
152             if isFile :
153                 ls_dict.append ({"type" :"file",  "inodes" :  str(se.d_ino),   "permissions" : str(hex(perms)),  "size": str(st.st_size), "path":  fullname})
154             elif isDir :
155                 ls_dict.append({"type":"dir", "inodes" :str(se.d_ino), "permissions":str( hex(perms)),"size":"0",   "path": fullname+"/"})
156
157 #            else :
158 #               ls_dict.append( "Other ("+hex(st.st_mode)+") "+se.d_ino, hex(perms)+ fullname)
159             sep = yaffs_readdir(dc)
160         yaffs_closedir(dc)
161         return ls_dict
162     else:
163         print "Could not open directory"
164         return -1
165
166
167 ##toolbar 
168 toolbar_frame=tk.Frame(root_window)
169 button_open=tk.Button(toolbar_frame, command=load_command, text="load")
170 button_open.grid(column=0, row=0)
171 button_back=tk.Button(toolbar_frame, command=back_a_directory, text="back")
172 button_back.grid(column=1, row=0)
173 toolbar_frame.grid(row=0, column=0,  columnspan=3)
174
175
176
177 class new_file():
178     path_entry_box=0
179     new_file_window=0
180     def open_the_file(self):
181         global mount_list_text_variable
182         print "trying to create", mount_list_text_variable.get()+self.path_entry_box.get()
183         yaffs_handle=yaffs_open(self.path_entry_box.get(),66,0666)
184         yaffs_close(yaffs_handle)
185         self.new_file_window.destroy()
186         load_dir()
187
188     def cancel(self):
189         ##del self
190         self.new_file_window.destroy()
191     def __init__(self):
192         global mount_list_text_variable
193         self.new_file_window =tk.Toplevel(takefocus=True)
194         path_frame=tk.Frame(self.new_file_window)
195         path_label=tk.Label(path_frame, text="file path")
196         path_label.pack(side=tk.LEFT)
197         text=tk.StringVar()
198         text.set(mount_list_text_variable.get())
199         print "############################",mount_list_text_variable.get()
200         self.path_entry_box= tk.Entry(path_frame, textvariable=text)
201         self.path_entry_box.pack(side=tk.RIGHT)
202         path_frame.pack()
203         button_frame=tk.Frame(self.new_file_window)
204         create_button=tk.Button(button_frame, text="Create", command=self.open_the_file)
205         create_button.pack(side=tk.LEFT)
206         cancel_button=tk.Button(button_frame, text="Cancel", command=self.cancel)
207         cancel_button.pack(side=tk.RIGHT)
208         button_frame.pack()
209
210 class new_folder():
211     path_entry_box=0
212     new_folder_window=0
213     def create_the_folder(self):
214         global mount_list_text_variable
215         print "trying to create", mount_list_text_variable.get()+self.path_entry_box.get()
216         yaffs_mkdir(self.path_entry_box.get(),0666)
217         self.new_folder_window.destroy()
218
219         load_dir()
220
221     def cancel(self):
222         ##del self
223         self.new_folder_window.destroy()
224     def __init__(self):
225         global mount_list_text_variable
226         self.new_folder_window =tk.Toplevel(takefocus=True)
227         path_frame=tk.Frame(self.new_folder_window)
228         path_label=tk.Label(path_frame, text="directory path")
229         path_label.pack(side=tk.LEFT)
230         text=tk.StringVar()
231         text.set(mount_list_text_variable.get())
232         print "############################",mount_list_text_variable.get()
233         self.path_entry_box= tk.Entry(path_frame, textvariable=text)
234         self.path_entry_box.pack(side=tk.RIGHT)
235         path_frame.pack()
236         button_frame=tk.Frame(self.new_folder_window)
237         create_button=tk.Button(button_frame, text="Create", command=self.create_the_folder)
238         create_button.pack(side=tk.LEFT)
239         cancel_button=tk.Button(button_frame, text="Cancel", command=self.cancel)
240         cancel_button.pack(side=tk.RIGHT)
241         button_frame.pack()
242
243
244
245
246 ##mount list entry box init
247 mount_list_frame=tk.Frame(root_window)
248 mount_list_label=tk.Label(mount_list_frame, text="mount list")
249 mount_list_label.pack(side=tk.RIGHT)
250
251 mount_list_entry_box= tk.Entry(mount_list_frame,textvariable=mount_list_text_variable)
252 mount_list_entry_box.pack(side=tk.RIGHT)
253 mount_list_frame.grid(row=1, column=0, columnspan=2)
254
255
256 list_frame=tk.Frame(root_window)
257 name_list_box=tk.Listbox(list_frame,exportselection=0, height=30, width=50)
258 load_dir()
259 list_frame.grid()
260
261 name_list_box.bind("<Double-Button-1>", load_command)
262
263 browser_menu_bar=tk.Menu(root_window)
264 browser_file_menu=tk.Menu(browser_menu_bar)
265
266 browser_file_menu.add_command(label="Reload", command=load_dir)
267 browser_file_menu.add_command(label="Open")
268 browser_file_menu.add_command(label="Save")
269 browser_menu_bar.add_cascade(label="File", menu=browser_file_menu)
270 root_window.config(menu=browser_menu_bar)
271
272
273 browser_edit_menu=tk.Menu(browser_menu_bar)
274
275 browser_edit_menu.add_command(label="New File", command=new_file)
276 browser_edit_menu.add_command(label="New Folder", command=new_folder)
277 browser_edit_menu.add_command(label="Rename File")
278 browser_menu_bar.add_cascade(label="Edit", menu=browser_edit_menu)
279
280
281
282
283
284
285 root_window.mainloop()
286
287 print"unmounting yaffs:", yaffs_unmount("yaffs2/")
288
289