much beter configurability
This commit is contained in:
parent
6d7e67a90b
commit
df8c6c10d0
1 changed files with 195 additions and 92 deletions
|
|
@ -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,72 +94,178 @@ def parse_options
|
|||
return options
|
||||
end
|
||||
|
||||
def read_config(options)
|
||||
unless options.has_key?("-c")
|
||||
options["-c"]=".ripnewsrc"
|
||||
#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
|
||||
begin
|
||||
config = IO.readlines("#{options[\"-c\"]}")
|
||||
for i in config
|
||||
if i =~ /^OPT_(.)=?(.*)$/
|
||||
options["-#{$1}"]=$2 unless
|
||||
options.has_key?("-#{$1}")
|
||||
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
|
||||
print "#{$1}=", options["#{$1}"], "\n" if Debuglevel > 1
|
||||
end
|
||||
rescue
|
||||
print "Coudn't open config file: #{options[\"-c\"]}\n"
|
||||
end
|
||||
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
|
||||
return options
|
||||
end
|
||||
|
||||
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"
|
||||
if group != ""
|
||||
print "Error parsing config: group not terminated on line #{i}\n"
|
||||
exit
|
||||
end
|
||||
unless options.has_key?("DATADIR")
|
||||
options["DATADIR"] ="."
|
||||
end
|
||||
# exit # zeer tijdelijk voor snel testen
|
||||
|
||||
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
|
||||
|
||||
options = {}
|
||||
options = parse_options
|
||||
options = read_config(options)
|
||||
check_options(options)
|
||||
|
||||
if Debuglevel > 1
|
||||
for i in options.keys
|
||||
print "Opt: #{i} val: #{options[i]}\n"
|
||||
return config
|
||||
end
|
||||
|
||||
#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
|
||||
|
||||
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"])
|
||||
|
||||
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 > 1
|
||||
if i =~ /#{options["-I"]}/i
|
||||
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 options.has_key?("TMPDIR")
|
||||
file, fullname = tmp_file(options["TMPDIR"])
|
||||
fileout, fullnameout = tmp_file(options["TMPDIR"])
|
||||
if config[group].has_key?("TMPDIR")
|
||||
file, fullname = tmp_file(config[group]["TMPDIR"])
|
||||
fileout, fullnameout = tmp_file(config[group]["TMPDIR"])
|
||||
else
|
||||
file=nil
|
||||
fullname = ""
|
||||
|
|
@ -179,17 +283,17 @@ for i in articles.get_group_subjects
|
|||
if mode
|
||||
print "mode: #{mode}\n" if Debuglevel > 0
|
||||
print "filename: #{filename}\n"
|
||||
if options.has_key?("-L")
|
||||
if config[group].has_key?("-L")
|
||||
print "longname\n" if Debuglevel > 1
|
||||
save_file(options["DATADIR"], i, body)
|
||||
save_file("#{config[group]["DATADIR"]}/#{group}", i, body)
|
||||
else
|
||||
print "shortname\n" if Debuglevel > 1
|
||||
save_file(options["DATADIR"], filename, body)
|
||||
save_file("#{config[group]["DATADIR"]}/#{group}", filename, body)
|
||||
end
|
||||
marked = articles.get_group_ids(i)
|
||||
newsrc.mark_list("alt.binaries.e-book.technical", marked)
|
||||
newsrc.mark_list(group, marked)
|
||||
newsrc.save
|
||||
marked.collect!{|x| print "marked: #{x}\n"}
|
||||
#marked.collect!{|x| print "marked: #{x}\n"}
|
||||
end
|
||||
# rm file & fileout
|
||||
File.delete(fullname)
|
||||
|
|
@ -199,6 +303,5 @@ for i in articles.get_group_subjects
|
|||
print "\n"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
articles.quit
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue