dansguardian 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog CHANGED
@@ -1,3 +1,6 @@
1
+ 0.1.0
2
+
3
+ * edit config files
1
4
 
2
5
  0.0.1
3
6
 
@@ -5,9 +5,10 @@ autoload :Set, 'set'
5
5
  require 'configfiles'
6
6
 
7
7
  module DansGuardian
8
- VERSION = '0.0.2'
8
+ VERSION = '0.1.0'
9
9
 
10
10
  autoload :Config, 'dansguardian/config'
11
11
  autoload :Parser, 'dansguardian/parser'
12
+ autoload :Updater, 'dansguardian/updater'
12
13
  autoload :Inclusion, 'dansguardian/inclusion'
13
14
  end
@@ -35,7 +35,7 @@ module DansGuardian
35
35
  end
36
36
 
37
37
  # get filtergroup configuration data
38
- def filtergroup(n=1, opt_h={:cached => false})
38
+ def filtergroup(n=1, opt_h={:cached => true})
39
39
  if opt_h[:cached]
40
40
  if @filtergroups[n]
41
41
  return @filtergroups[n]
@@ -4,7 +4,7 @@ module DansGuardian
4
4
  class Config
5
5
 
6
6
  # Useful constants
7
- INFINITY = ::Float::INFINITY
7
+ INFINITY = ::Float::INFINITY # do not use, JSON issues!!!
8
8
 
9
9
  # Recurring converters
10
10
  BOOL = {
@@ -10,12 +10,16 @@ module DansGuardian
10
10
  "FIXME: unknown parameter: value = #{s}"
11
11
  end
12
12
 
13
- parameter :groupmode,
13
+ GROUPMODE = {
14
14
  '0' => :banned,
15
15
  '1' => :filtered,
16
16
  '2' => :unfiltered
17
-
17
+ }
18
+ parameter :groupmode, GROUPMODE
19
+ default :groupmode, :filtered
20
+
18
21
  parameter :groupname
22
+ default :groupname, ''
19
23
 
20
24
  [
21
25
  :bannedphraselist,
@@ -140,7 +140,7 @@ module DansGuardian
140
140
  parameter :maxuploadsize do |str|
141
141
  case str
142
142
  when '-1'
143
- INFINITY
143
+ -1 # should be easily interpreted as 'unlimited'
144
144
  else
145
145
  str.to_i * 1024
146
146
  end
@@ -213,13 +213,7 @@ module DansGuardian
213
213
  parameter par, :to_i
214
214
  end
215
215
 
216
- parameter :maxips do |str|
217
- if str == '0'
218
- INFINITY
219
- else
220
- str.to_i
221
- end
222
- end
216
+ parameter :maxips, :to_i # 0 = unlimited
223
217
 
224
218
  [:ipcfilename, :urlipcfilename, :ipipcfilename, :pidfilename].each do |p|
225
219
  parameter p
@@ -0,0 +1,79 @@
1
+ autoload :Tempfile, 'tempfile'
2
+ autoload :FileUtils, 'fileutils'
3
+
4
+ module DansGuardian
5
+ module Updater
6
+
7
+ def self.update!(file, data)
8
+ tmp = Tempfile.new 'ruby_dansguardian_updater'
9
+ update file, data do |line|
10
+ tmp.puts line
11
+ end
12
+ tmp.close
13
+ FileUtils.cp tmp.path, file
14
+ tmp.unlink
15
+ end
16
+
17
+ def self.update(io_or_file, data)
18
+ if io_or_file.is_a? IO
19
+ io = io_or_file
20
+ else
21
+ io = File.open io_or_file
22
+ end
23
+
24
+ already_written = []
25
+ io.each_line do |line|
26
+ line.strip!
27
+ case line
28
+ # even replace commented lines
29
+ when /^(#\s*)?([^=\s#]+)\s*=\s*([^=\s#']*)/
30
+ key = $2
31
+ value = $3
32
+ when /^(#\s*)?[\s#]*([^=\s#]+)\s*=\s*'(.*)'/
33
+ key = $2
34
+ value = $3.gsub(/\\'/, "'")
35
+ else
36
+ yield line # not a key/val line: leave untouched
37
+ next
38
+ end
39
+
40
+ # At this point, it is actually a key/value line
41
+
42
+ next if
43
+ already_written.include?(key) or
44
+ already_written.include?(key.to_sym)
45
+
46
+ new_value = ( data[key] || data[key.to_sym] )
47
+ if new_value
48
+ if new_value.respond_to? :each # multiple values
49
+ new_value.each do |val|
50
+ yield "#{key} = #{val}"
51
+ end
52
+ else
53
+ yield "#{key} = #{new_value}"
54
+ end
55
+ already_written << key
56
+ # next # "optimized out"
57
+ else # not a key/val pair to edit: leave untouched
58
+ yield line
59
+ # next # "optimized out"
60
+ end
61
+ end
62
+
63
+ to_be_written =
64
+ data.keys.map{|k| k.to_sym} - already_written.map{|k| k.to_sym}
65
+ if to_be_written.length > 0
66
+ yield ""
67
+ yield "# Added by ::#{self} :"
68
+ to_be_written.each do |k|
69
+ yield "#{k} = #{data[k] || data[k.to_s]}"
70
+ end
71
+ end
72
+
73
+ io.close unless io_or_file.is_a? IO
74
+ end
75
+
76
+ end
77
+ end
78
+
79
+
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 1
7
8
  - 0
8
- - 2
9
- version: 0.0.2
9
+ version: 0.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Guido De Rosa
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-02-03 00:00:00 +00:00
17
+ date: 2011-02-07 00:00:00 +00:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -50,6 +50,7 @@ files:
50
50
  - lib/dansguardian/config.rb
51
51
  - lib/dansguardian/inclusion.rb
52
52
  - lib/dansguardian/parser.rb
53
+ - lib/dansguardian/updater.rb
53
54
  has_rdoc: true
54
55
  homepage: http://github.com/gderosa/dansguardian.rb
55
56
  licenses: []