Files to Ruby droplist?

For general discussion related FlowStone
Post Reply
adamszabo
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Files to Ruby droplist?

Post by adamszabo »

I have been searching online about this but no luck, basically what I am after is: to scan a specific directory (and sub directories) for text files, then convert the structure into a string that the droplist understands. So lets say I have some files:

C:\Folder\Category01\01.txt
C:\Folder\Category01\02.txt
C:\Folder\Category02\03.txt
C:\Folder\Category02\04.txt

and it converts it to something like this:

Folder,
<<,Category01,>>,
<<,<<,01,>>,>>,<<,<<,02,>>,>>,
<<,Category02,>>,
<<,<<,03,>>,>>,<<,<<,04,>>,>>

It seems very simple but for us with little Ruby knowledge seems the hardest thing to do :cry:
Anyone care to give a hand?
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: Files to Ruby droplist?

Post by KG_is_back »

Hi,
Very interesting idea. Try this ruby code:

Code: Select all

def folders_to_hierarchy(list)
   root=Hash.new{|hash,key| hash[key]=Hash.new(&hash.default_proc)}
   d=[]
   list.each{|path|
         
         d=path.split(/\\/)
         name=d.pop
         rt=root
         d.each{|fold|
         rt=rt[fold]
         }
         rt[name]=nil
         }
   str=to_hierarchy(root,1)
   str.chomp(",")
end

def to_hierarchy(hash,depth)
   if hash.nil?
      return ""
   else
      str=""
      hash.each_pair{|key,val|
         str+=("<<,"*depth+key+","+">>,"*depth)
         str+=to_hierarchy(val,depth+1)
         }
      return str
   end
end


provide the " folders_to_hierarchy" method with an array of strings (paths) and it will generate the expected string.
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Files to Ruby droplist?

Post by tulamide »

For the really tough part, getting the paths, I wanted to recommend the Dir class, because

Code: Select all

Dir.foreach(rootpath)
would be very convenient to recursively search through all folders and subfolders. Unfortunately, as soon as I use it I get a converternotfound error, because Dir seems to expect UTF8, while Flowstone only provides ASCII 8bit.

Maybe someone else could have a look at it? There might be a way to search all folders with Ruby even without conversion to UTF8?

MyCo, TheOm, Tronic? Anybody?
"There lies the dog buried" (German saying translated literally)
adamszabo
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Re: Files to Ruby droplist?

Post by adamszabo »

Thanks KG, works great!

By the way, in the user manual there is a function 'schematicLocation' which outputs where the project is located, however it outputs a string like this:

Code: Select all

{:folder=>"C:\\Users\\...", :filename=>"project.fsm"}


How can one only output the folder? It didnt really explain in the manual.
TheOm
Posts: 103
Joined: Tue Jan 28, 2014 7:35 pm
Location: Germany

Re: Files to Ruby droplist?

Post by TheOm »

tulamide wrote:For the really tough part, getting the paths, I wanted to recommend the Dir class, because

Code: Select all

Dir.foreach(rootpath)
would be very convenient to recursively search through all folders and subfolders. Unfortunately, as soon as I use it I get a converternotfound error, because Dir seems to expect UTF8, while Flowstone only provides ASCII 8bit.

Maybe someone else could have a look at it? There might be a way to search all folders with Ruby even without conversion to UTF8?

MyCo, TheOm, Tronic? Anybody?


If you just want to get it to work, you can use rootpath.force_ending("UTF-8"), but this will just pretend that the string is encoded in UTF-8 even though it actually isn't. It will fail for paths that are not representable in ASCII. I have no solution for doing it correctly.
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Files to Ruby droplist?

Post by tulamide »

adamszabo wrote:Thanks KG, works great!

By the way, in the user manual there is a function 'schematicLocation' which outputs where the project is located, however it outputs a string like this:

Code: Select all

{:folder=>"C:\\Users\\...", :filename=>"project.fsm"}


How can one only output the folder? It didnt really explain in the manual.

That's not a string but a hashtable (using symbols as keys). To get the folder path just call

Code: Select all

schematicLocation[:folder]


While in Ruby the string will be escaped, that's ok, it will pass the string in correct form to Flowstone.
"There lies the dog buried" (German saying translated literally)
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Files to Ruby droplist?

Post by tulamide »

TheOm wrote:
tulamide wrote:For the really tough part, getting the paths, I wanted to recommend the Dir class, because

Code: Select all

Dir.foreach(rootpath)
would be very convenient to recursively search through all folders and subfolders. Unfortunately, as soon as I use it I get a converternotfound error, because Dir seems to expect UTF8, while Flowstone only provides ASCII 8bit.

Maybe someone else could have a look at it? There might be a way to search all folders with Ruby even without conversion to UTF8?

MyCo, TheOm, Tronic? Anybody?


If you just want to get it to work, you can use rootpath.force_ending("UTF-8"), but this will just pretend that the string is encoded in UTF-8 even though it actually isn't. It will fail for paths that are not representable in ASCII. I have no solution for doing it correctly.

Thank you for having had a look at it. I just wonder, how Flowstone manages it, when using the FileDialog prim? Maybe we should ask MyCo to convert to and from ASCII 8bit for Dir as well (File class already works with Flowstone's path strings, e.g. the result from the FileDialog prim)?
"There lies the dog buried" (German saying translated literally)
Post Reply