From df8c6c10d0338d0e52f1a685f8dff16fe28f779f Mon Sep 17 00:00:00 2001 From: Ward Wouts Date: Tue, 30 Apr 2002 14:08:14 +0000 Subject: [PATCH] much beter configurability --- trunk/ripnews/ripnews.rb | 287 ++++++++++++++++++++++++++------------- 1 file changed, 195 insertions(+), 92 deletions(-) diff --git a/trunk/ripnews/ripnews.rb b/trunk/ripnews/ripnews.rb index ea7579a..874267d 100755 --- a/trunk/ripnews/ripnews.rb +++ b/trunk/ripnews/ripnews.rb @@ -70,9 +70,7 @@ def tmp_file(dir) end -def parse_options - options = {} - +def parse_options(options) begin opts = GetoptLong.new( [ "-I", "--include", GetoptLong::REQUIRED_ARGUMENT ], @@ -96,109 +94,214 @@ def parse_options return options end -def read_config(options) - unless options.has_key?("-c") - options["-c"]=".ripnewsrc" - end - begin - config = IO.readlines("#{options[\"-c\"]}") - for i in config - if i =~ /^OPT_(.)=?(.*)$/ - options["-#{$1}"]=$2 unless - options.has_key?("-#{$1}") +#def read_config(options) +# unless options.has_key?("-c") +# options["-c"]=".ripnewsrc" +# end +# begin +# config = IO.readlines("#{options[\"-c\"]}") +# for i in config +# if i =~ /^OPT_(.)=?(.*)$/ +# options["-#{$1}"]=$2 unless +# options.has_key?("-#{$1}") +# else +# i =~ /([^=]*)=(.*)/ +# options["#{$1}"]=$2 +# end +# print "#{$1}=", options["#{$1}"], "\n" if Debuglevel > 1 +# end +# rescue +# print "Coudn't open config file: #{options[\"-c\"]}\n" +# exit +# end +# return options +#end + +def parse_config(default = {}) + file = File.new("#{default[\"-c\"]}") + lines = file.readlines + + i = 0 + group = "" + config = {} + + lines.collect!{|x| x.sub!(/^\s*/, ""); x.chomp} + while i < lines.length + line = lines[i] + while line.sub!(/\s*\\$/, "") != nil + line += lines[i+1] + i += 1 + end + line.sub!(/\s*$/, "") + i += 1 + if line =~ /^OPT_(.*?)=(.*)/ + line = "-#{$1}=#{$2}" + end + print "#{i}: #{line}\n" if Debuglevel > 1 + if line =~ /(.*?)\s*\+=\s*(.*)/ + if group == "" + default[$1] += $2 else - i =~ /([^=]*)=(.*)/ - options["#{$1}"]=$2 + for g in group.split + if config[g].has_key?($1) + config[g][$1] += $2 + elsif default.has_key?($1) + config[g][$1] = default[$1] + $2 + else + config[g][$1] = $2 + end + end end - print "#{$1}=", options["#{$1}"], "\n" if Debuglevel > 1 - end - rescue - print "Coudn't open config file: #{options[\"-c\"]}\n" - exit - end - return options -end - -def check_options(options) - if (Debuglevel > 1) - for i in options.keys - print "Opt: #{i} Value: #{options[i]}\n" + elsif line =~ /(.*?)\s*=\s*(.*)/ + if group == "" + default[$1] = $2 + else + for g in group.split + config[g][$1] = $2 + end + end + elsif line =~ /(.*?)\s*\{/ + group = $1 + for g in group.split + unless config.has_key?(g) + config[g] = {} + end + end + elsif line =~ /^}$/ + default.each_key{|x| + for g in group.split + unless config[g].has_key?(x) + config[g][x] = default[x] + end + end + } + group = "" + elsif line =~ /^$/ + next + else + print "Error parsing config on line: #{i}\n" + exit end end - unless options.has_key?("-I") - print "No inclusions given. Won't match anything.\n" + + if group != "" + print "Error parsing config: group not terminated on line #{i}\n" exit end - unless options.has_key?("DATADIR") - options["DATADIR"] ="." + + if Debuglevel > 2 + config.each_key{|x| + print "Group: #{x}\n" + config[x].each_key{|y| + print "Key: '#{y}' => Value: '#{config[x][y]}'\n" + } + } end -# exit # zeer tijdelijk voor snel testen + + return config end -options = {} -options = parse_options -options = read_config(options) -check_options(options) +#def check_options(options) +# if (Debuglevel > 1) +# for i in options.keys +# print "Opt: #{i} Value: #{options[i]}\n" +# end +# end +# unless options.has_key?("-I") +# print "No inclusions given. Won't match anything.\n" +# exit +# end +# unless options.has_key?("DATADIR") +# options["DATADIR"] ="." +# end +## exit # zeer tijdelijk voor snel testen +#end -if Debuglevel > 1 - for i in options.keys - print "Opt: #{i} val: #{options[i]}\n" - end +def check_config(config) + config.each_key {|i| + unless config[i].has_key?("-I") + print "No inclusions given for group #{i}. Won't match anything.\n" + exit + end + unless config[i].has_key?("DATADIR") + config[i]["DATADIR"] ="." + end + } +end + +defaults = {'-c' => '.ripnewsrc'} +defaults = parse_options(defaults) +#options = read_config(options) +config = parse_config(defaults) +check_config(config) + +if Debuglevel > 2 + config.each_key{|i| + print "Group: #{i}\n" + config[i].each_key{|j| + print "Opt: #{j} val: #{config[i][j]}\n" + } + } end newsrc = News::Newsrc.new(".newsrc") -articles = Article.new(options["NNTPSERVER"]) -#articles.get_articles("alt.binaries.e-book.flood") -articles.set_skip_ids(newsrc.marked_articles("alt.binaries.e-book.technical")) -articles.get_articles("alt.binaries.e-book.technical") +for group in config.keys + print "Getting articles for #{group}\n" + articles = Article.new(config[group]["NNTPSERVER"]) + articles.set_skip_ids(newsrc.marked_articles(group)) + articles.get_articles(group, config[group]["CACHEDIR"]) -for i in articles.get_group_subjects - print "#{i}\n" if Debuglevel > 1 - if i =~ /#{options["-I"]}/i - print "Match: #{i}\n" if Debuglevel > 0 - if articles.group_complete(i) - print "Complete: #{i}\n" if Debuglevel > 0 - if options.has_key?("TMPDIR") - file, fullname = tmp_file(options["TMPDIR"]) - fileout, fullnameout = tmp_file(options["TMPDIR"]) - else - file=nil - fullname = "" - fileout=nil - fullnameout = "" - end - mode, filename, body = articles.uudecode_group(i, file, fileout) - - if file - file.close - fileout.close - body = fullnameout - end - - if mode - print "mode: #{mode}\n" if Debuglevel > 0 - print "filename: #{filename}\n" - if options.has_key?("-L") - print "longname\n" if Debuglevel > 1 - save_file(options["DATADIR"], i, body) - else - print "shortname\n" if Debuglevel > 1 - save_file(options["DATADIR"], filename, body) - end - marked = articles.get_group_ids(i) - newsrc.mark_list("alt.binaries.e-book.technical", marked) - newsrc.save - marked.collect!{|x| print "marked: #{x}\n"} - end - # rm file & fileout - File.delete(fullname) - else - print "Not complete: #{i}\n" - end - print "\n" + unless FileTest.directory?("#{config[group]["DATADIR"]}/#{group}") or + Dir.mkdir("#{config[group]["DATADIR"]}/#{group}") + print "eeeps, couldn't create dir\n" + exit end + for i in articles.get_group_subjects + print "#{i}\n" if Debuglevel > 2 + if i =~ /#{config[group]["-I"]}/i + print "Match: #{i}\n" if Debuglevel > 0 + if articles.group_complete(i) + print "Complete: #{i}\n" if Debuglevel > 0 + if config[group].has_key?("TMPDIR") + file, fullname = tmp_file(config[group]["TMPDIR"]) + fileout, fullnameout = tmp_file(config[group]["TMPDIR"]) + else + file=nil + fullname = "" + fileout=nil + fullnameout = "" + end + mode, filename, body = articles.uudecode_group(i, file, fileout) + + if file + file.close + fileout.close + body = fullnameout + end + + if mode + print "mode: #{mode}\n" if Debuglevel > 0 + print "filename: #{filename}\n" + if config[group].has_key?("-L") + print "longname\n" if Debuglevel > 1 + save_file("#{config[group]["DATADIR"]}/#{group}", i, body) + else + print "shortname\n" if Debuglevel > 1 + save_file("#{config[group]["DATADIR"]}/#{group}", filename, body) + end + marked = articles.get_group_ids(i) + newsrc.mark_list(group, marked) + newsrc.save + #marked.collect!{|x| print "marked: #{x}\n"} + end + # rm file & fileout + File.delete(fullname) + else + print "Not complete: #{i}\n" + end + print "\n" + end + end + articles.quit end - - -articles.quit