Page 1 of 1

Files to Ruby droplist?

Posted: Thu Jun 16, 2016 1:12 pm
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?

Re: Files to Ruby droplist?

Posted: Thu Jun 16, 2016 4:21 pm
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.

Re: Files to Ruby droplist?

Posted: Thu Jun 16, 2016 7:57 pm
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?

Re: Files to Ruby droplist?

Posted: Thu Jun 16, 2016 9:11 pm
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.

Re: Files to Ruby droplist?

Posted: Fri Jun 17, 2016 12:26 am
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.

Re: Files to Ruby droplist?

Posted: Fri Jun 17, 2016 2:19 am
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.

Re: Files to Ruby droplist?

Posted: Fri Jun 17, 2016 2:22 am
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)?