Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

Files to Ruby droplist?

For general discussion related FlowStone

Files to Ruby droplist?

Postby adamszabo » Thu Jun 16, 2016 1:12 pm

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?
adamszabo
 
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Re: Files to Ruby droplist?

Postby KG_is_back » Thu Jun 16, 2016 4:21 pm

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.
KG_is_back
 
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: Files to Ruby droplist?

Postby tulamide » Thu Jun 16, 2016 7:57 pm

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)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Files to Ruby droplist?

Postby adamszabo » Thu Jun 16, 2016 9:11 pm

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.
adamszabo
 
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Re: Files to Ruby droplist?

Postby TheOm » Fri Jun 17, 2016 12:26 am

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.
TheOm
 
Posts: 103
Joined: Tue Jan 28, 2014 7:35 pm
Location: Germany

Re: Files to Ruby droplist?

Postby tulamide » Fri Jun 17, 2016 2:19 am

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?

Postby tulamide » Fri Jun 17, 2016 2:22 am

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)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany


Return to General

Who is online

Users browsing this forum: No registered users and 33 guests