PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Cover a directory and put all the stuff in a TreeView
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Cover a directory and put all the stuff in a TreeView

Réponse
 
LinkBack Outils de la discussion
Vieux 12/05/2008, 16h16   #1
John Locke
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Cover a directory and put all the stuff in a TreeView

Hi,

I would like to make the following script in ruby:

Given a directory, for example /home, I would like to take ALL the
subdirectories and files that are inside /home, and put them in a
TreeView structure (from Ruby GTK), so I would have all those
subdirectories and files arranged in that structure that I could then
display as a graphic object.

I have found how to print all the stuff that is inside my current
directory:

require 'find'
Find.find('./') do |f| p f end

I could insert all what that command prints in the TreeView but then it
would be all at the first level of the TreeView.

So I need to know, at each loop, what is at what level (which is the
father, which is the son, etc), in order to insert it correctly in the
TreeView.

In other words, right now I am able to do this:

/home/a
/home/a1
/home/a2
/home/b

etc

And I would like to do this:

/home/a
/home/a1
/home/a2
/home/b

Any ideas?


Thanks.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 12/05/2008, 16h41   #2
Yossef Mendelssohn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Cover a directory and put all the stuff in a TreeView

On May 12, 10:16 am, John Locke <eloi.pl...@campus.uab.es> wrote:
> So I need to know, at each loop, what is at what level (which is the
> father, which is the son, etc), in order to insert it correctly in the
> TreeView.
>
> In other words, right now I am able to do this:
>
> /home/a
> /home/a1
> /home/a2
> /home/b
>
> etc
>
> And I would like to do this:
>
> /home/a
> /home/a1
> /home/a2
> /home/b
>
> Any ideas?
>
> Thanks.
> --
> Posted viahttp://www.ruby-forum.com/.


What makes /home/a1 and /home/a2 the children of /home/a? They're not /
home/a/1 or /home/a/2, are they?

--
-yossef

  Réponse avec citation
Vieux 12/05/2008, 16h44   #3
Martin Boese
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Cover a directory and put all the stuff in a TreeView


Call a function recursively when you hit a directory. This will output
something as you mention below, I am just using Dir[*] to get a list of
files:

def scan_files(dir, level=0)
puts "#{' '*level}#{dir}"
Dir["#{dir}/*"].each do |f|
if File.directory?(f) then
scan_files(f, level+1)
else
puts "#{' '*level}#{f}"
end
end
end

scan_files('/home/someone')


Martin


On Monday 12 May 2008 16:16:37 John Locke wrote:
> Hi,
>
> I would like to make the following script in ruby:
>
> Given a directory, for example /home, I would like to take ALL the
> subdirectories and files that are inside /home, and put them in a
> TreeView structure (from Ruby GTK), so I would have all those
> subdirectories and files arranged in that structure that I could then
> display as a graphic object.
>
> I have found how to print all the stuff that is inside my current
> directory:
>
> require 'find'
> Find.find('./') do |f| p f end
>
> I could insert all what that command prints in the TreeView but then it
> would be all at the first level of the TreeView.
>
> So I need to know, at each loop, what is at what level (which is the
> father, which is the son, etc), in order to insert it correctly in the
> TreeView.
>
> In other words, right now I am able to do this:
>
> /home/a
> /home/a1
> /home/a2
> /home/b
>
> etc
>
> And I would like to do this:
>
> /home/a
> /home/a1
> /home/a2
> /home/b
>
> Any ideas?
>
>
> Thanks.




  Réponse avec citation
Vieux 12/05/2008, 16h49   #4
John Locke
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Cover a directory and put all the stuff in a TreeView

Yossef Mendelssohn wrote:
> On May 12, 10:16 am, John Locke <eloi.pl...@campus.uab.es> wrote:
>>
>>
>> Thanks.
>> --
>> Posted viahttp://www.ruby-forum.com/.

>
> What makes /home/a1 and /home/a2 the children of /home/a? They're not /
> home/a/1 or /home/a/2, are they?



Errr yeah well, that's the idea, maybe I expressed it bad.
By the way I've found out how to make it:

require 'gtk2'

treestore = Gtk::TreeStore.new(String)
view = Gtk::TreeView.new(treestore)
view.selection.mode = Gtk::SELECTION_NONE
renderer = Gtk::CellRendererText.new
col = Gtk::TreeViewColumn.new("Filename", renderer, :text => 0)
view.append_column(col)
window = Gtk::Window.new(Gtk::Window::TOPLEVEL)
scrollwindow = Gtk::ScrolledWindow.new
scrollwindow.add(view)
window.signal_connect("delete_event") { Gtk.main_quit; exit! }
window.add(scrollwindow)
window.show_all
Dir.chdir('./')
list = Dir['**/**']
hlist = Hash.new
list.each { |file|
parent_folder = File.dirname(file)
parent_node = hlist[parent_folder]
current_node = treestore.append(parent_node)
current_node[0] = file
hlist[file] = current_node
}
Gtk.main



For the moment it seems to work.
Now I have another question: actually all this comes because I have to
provide a graphic structure for a copy server that allows me to copy
entire directories. So when one directory is copied, I have to be able
to see the whole list of subdirectories and files that are inside it,
and next to each one of those files I have to display a ProgressBar that
shows the progress of the copy of that specific file or directory, as
well as a global progress bar for the copy of the whole directory.

The specific stuff of the progress bar is provided by the copy server
itself which is another part of the project; I just make the graphic
interface.

So my problem now is how to display a progress bar next to each node of
the tree view.

When I create the treestore to administrate the treeview, I say this:


treestore = Gtk::TreeStore.new(String)


But next to that string I would also like to display a progress bar, so
how could I make it to introduce a progress bar (a widget, a graphic
element), inside the treeview.


treestore = Gtk::TreeStore.new(String, Widget) ---> obviously that
doesn't work - any ideas of how to do it? Actually, is it possible, or
the tree view only accepts normal stuff such as integers and strings in
its nodes?


Thanks.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 09/08/2008, 11h04   #5
Shailendra Kamath
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Cover a directory and put all the stuff in a TreeView

John Locke wrote:
> Hi,
>
> I would like to make the following script in ruby:
>
> Given a directory, for example /home, I would like to take ALL the
> subdirectories and files that are inside /home, and put them in a
> TreeView structure (from Ruby GTK), so I would have all those
> subdirectories and files arranged in that structure that I could then
> display as a graphic object.
>
> I have found how to print all the stuff that is inside my current
> directory:
>
> require 'find'
> Find.find('./') do |f| p f end
>
> I could insert all what that command prints in the TreeView but then it
> would be all at the first level of the TreeView.
>
> So I need to know, at each loop, what is at what level (which is the
> father, which is the son, etc), in order to insert it correctly in the
> TreeView.
>
> In other words, right now I am able to do this:
>
> /home/a
> /home/a1
> /home/a2
> /home/b
>
> etc
>
> And I would like to do this:
>
> /home/a
> /home/a1
> /home/a2
> /home/b
>
> Any ideas?
>
>
> Thanks.


Below code can be used to convert directory structure in xml.


require 'rubygems'
require 'rexml/document'

class DirectoryStructure
@@excludes = ["..","."]
@@doc = REXML:ocument.new %{ <?xml version="1.0" encoding="UTF-8" ?> }

def self.traverse_directory(root_path)

#traverse initial directory structure
folder = @@doc.add_element( 'myroot',{'id' => root_path} )

#call loop_path with root_path and parent_node
loop_path(root_path,folder)
#create xml file
open( "final_directory_structure.xml", "w" ) do |f|
f.puts @@doc
end

end

def self.loop_path(path,parent_node)

dir_entries= Dir.entries(path)

# don't need '.','..'
dir_entries = dir_entries-['.','..']

dir_entries.each do | entry |

# check if directory or file
if File.directory?(path+"/"+entry)

#Folders Loop -------------------------------------------------
#Gets the directories of the current node.
#It changes for each recursive call
folder = parent_node.add_element( 'folder' ,{'label' =>entry
,'file_path' => path+"/"+entry } )
loop_path( path+"/"+entry, folder )
else
#Files Loop ------------------------------------------------------
file = parent_node.add_element( 'file' ,{'label' =>entry ,'file_path'
=> path+"/"+entry } )

end


end

end

#first call to traverse directory Please change the path---to your
directory path
self.traverse_directory("Authentication/Authentication")

end
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h03.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18157 seconds with 13 queries