Robert Klemme
5/1/2008 4:30:00 PM
On 01.05.2008 18:08, yermej wrote:
> On May 1, 9:46 am, Kyle Schmitt <kyleaschm...@gmail.com> wrote:
>> I'm writing some scripts to help handle some ornery samba servers we
>> have: part of that is unfortunately reading the config scripts that
>> have built up over the years.
>>
>> I was hoping to use the standard string method as a quick &
>> not-so-dirty way of parsing the files, given that samba uses a very
>> simple format.
>>
>> #the sample_data variable is defined below
>> irb(main):sample_data.split(/\[[a-z0-9]+\]/i)
>> => ["", "\ncomment = shared directory for the shop\npath =
>> /dept/shop\nvalid u ....(truncated)
>> Gives good results, but omits what's between the brackets. I expected
>> that part.
>>
>> irb(main):sample_data.split(/(\[[a-z0-9]+\])/i)
>> => ["", "[shop]", "\ncomment = shared directory for the shop\npath =
>> /dept/sho ....(truncated)
>> Neat, gives me the data between the brackets in an element before the
>> data itself.
>>
>> I know quite well I can zip through that array again, but I was
>> wondering, hoping, that there would be a way of accessing that back
>> reference in a block as part of the split.
>>
>> Is there any way to do that that I'm just missing?
>>
>> Thanks,
>> Kyle
>>
>> sample_data=%{[shop]
>> comment = shared directory for the shop
>> path = /dept/shop
>> valid users = @shop @admin
>> public = no
>> writable = yes
>> force group = shop
>> create mask = 0770
>> [bob]
>> comment = User files for bob
>> path = /users/bob
>> valid users = bob @admin
>> public = no
>> writable = yes
>> create mask = 0770}
>
> I think you might want scan instead of split.
>
> sample_data.scan( /(\[[a-z0-9]+\])([^\[]*)/i) do |share, opts|
> # create your hash or whatever here
> end
Yes. Two suggestions:
robert@fussel /cygdrive/c/Temp
$ ./smpars.rb
{"shop"=>
{"public"=>"no",
"writable"=>"yes",
"create mask"=>"0770",
"valid users"=>"@shop @admin",
"path"=>"/dept/shop",
"comment"=>"shared directory for the shop",
"force group"=>"shop"},
"bob"=>
{"public"=>"no",
"writable"=>"yes",
"create mask"=>"0770",
"valid users"=>"bob @admin",
"path"=>"/users/bob",
"comment"=>"User files for bob"}}
{"shop"=>
{"public"=>"no",
"writable"=>"yes",
"create mask"=>"0770",
"valid users"=>"@shop @admin",
"path"=>"/dept/shop",
"comment"=>"shared directory for the shop",
"force group"=>"shop"},
"bob"=>
{"public"=>"no",
"writable"=>"yes",
"create mask"=>"0770",
"valid users"=>"bob @admin",
"path"=>"/users/bob",
"comment"=>"User files for bob"}}
robert@fussel /cygdrive/c/Temp
$ cat smpars.rb
#!/bin/env ruby
require 'pp'
sample_data = <<EOS
[shop]
comment = shared directory for the shop
path = /dept/shop
valid users = @shop @admin
public = no
writable = yes
force group = shop
create mask = 0770
[bob]
comment = User files for bob
path = /users/bob
valid users = bob @admin
public = no
writable = yes
create mask = 0770
EOS
def parse1(dat)
conf = {}
key = nil
dat.each do |line|
case line
when /^\s*\[([^\]]+)\]\s*$/
key = $1
conf[key] ||= {}
when /^\s*([^=]*?)\s*=\s*(.*)$/
conf[key][$1] = $2
end
end
conf
end
def parse2(dat)
conf = {}
key = nil
dat.scan %r{
^\s*\[([^\]]+)\]\s*$
| ^\s*([^=]*?)\s*=\s*(.*)$
}x do |m|
if m[0]
key = m[0]
conf[key] ||= {}
else
conf[key][m[1]] = m[2]
end
end
conf
end
pp parse1(sample_data)
pp parse2(sample_data)
robert@fussel /cygdrive/c/Temp
$
Cheers
robert