config-parser 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ .idea
2
+ *.gem
3
+
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ #MIT LICENSE
2
+ #
3
+ #Copyright (c) 2012 SUSE Linux Products GmbH
4
+ #
5
+ #Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ #of this software and associated documentation files (the "Software"), to deal
7
+ #in the Software without restriction, including without limitation the rights
8
+ #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ #copies of the Software, and to permit persons to whom the Software is
10
+ #furnished to do so, subject to the following conditions:
11
+ #
12
+ #The above copyright notice and this permission notice shall be included in
13
+ #all copies or substantial portions of the Software.
14
+ #
15
+ #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ #THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ == Config parser gem
2
+
3
+ Parsing an options.yml file into a Hash with convenience methods like
4
+ overwriting variables per Rails environment and overwriting variables with a local
5
+ options_local.yml file.
6
+
7
+ === Installation
8
+
9
+ The best way to install is with RubyGems:
10
+
11
+ $ [sudo] gem install config-parser
12
+
13
+ Or better still, just add it to your Gemfile:
14
+
15
+ gem 'config-parser'
16
+
17
+ === Defaults
18
+
19
+ Per default the parser will search for the config file at config/options.yml and config/options-local.yml.
20
+
21
+
22
+ === Example
23
+
24
+ Example config file:
25
+
26
+ default:
27
+ supported_languages: en
28
+ mailer_delivery_method: local
29
+
30
+ production:
31
+ mailer_delivery_method: smtp
32
+
33
+ When running in the production environment, the mailer_delivery_method will be set to
34
+ 'smtp'. The same works for all other environments. The optional options_local.yml file
35
+ would have the same layout.
36
+ When used from a Rails app, just include in your application.rb:
37
+
38
+ OPTS = Common::Options.new
39
+
40
+ and you can use the config variables like:
41
+
42
+ OPTS.<variable_name>
@@ -0,0 +1,15 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = "config-parser"
3
+ gem.summary = %Q{Parsing an options.yml file into a Hash with convenience.}
4
+ gem.description = "Parsing an options.yml file into a Hash with convenience methods like
5
+ overwriting variables per Rails environment and overwriting variables with a local
6
+ options_local.yml file."
7
+ gem.homepage = "https://github.com/openSUSE/rubygem_config-parser"
8
+ gem.authors = ['cschum@suse.de', 'tom@opensuse.org']
9
+ gem.files = `git ls-files`.split("\n")
10
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
11
+ gem.require_paths = ['lib']
12
+ gem.version = '0.1'
13
+ end
14
+
15
+
@@ -0,0 +1,168 @@
1
+ #MIT LICENSE
2
+ #
3
+ #Copyright (c) 2012 SUSE Linux Products GmbH
4
+ #
5
+ #Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ #of this software and associated documentation files (the "Software"), to deal
7
+ #in the Software without restriction, including without limitation the rights
8
+ #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ #copies of the Software, and to permit persons to whom the Software is
10
+ #furnished to do so, subject to the following conditions:
11
+ #
12
+ #The above copyright notice and this permission notice shall be included in
13
+ #all copies or substantial portions of the Software.
14
+ #
15
+ #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ #THE SOFTWARE.
22
+
23
+ require 'yaml'
24
+ require "#{File.dirname(__FILE__)}/utils"
25
+
26
+ module Common
27
+
28
+ class Options
29
+
30
+ def initialize
31
+ Common::Utils::suppress_warnings do
32
+ @cfg_file = 'config/options.yml'
33
+ @local_cfg_file = 'config/options-local.yml'
34
+ @tmp_cmdl_file = 'tmp/options-cmd-line.yml'
35
+ @persistent_local_cfg_file = '/etc/options.yml'
36
+ if defined? Rails
37
+ @cfg_file = Rails.root.to_s + '/' + @cfg_file
38
+ @local_cfg_file = Rails.root.to_s + '/' + @local_cfg_file
39
+ @persistent_local_cfg_file = "/etc/config/#{Rails.root.to_s.split("/")[-1]}/options.yml"
40
+ end
41
+ end
42
+ @options = {}
43
+ reload!
44
+ end
45
+
46
+ def reload!
47
+ cmd_line_args = {}
48
+
49
+ if File.exists? @tmp_cmdl_file
50
+ cmd_line_args = YAML.load_file(@tmp_cmdl_file)
51
+
52
+ # Don't remove tmp_cmdl_file if the keep_tmp_cmdl_file flag is set.
53
+ if defined? KEEP_TMP_CMDL_FILE and KEEP_TMP_CMDL_FILE
54
+ Object.instance_eval { remove_const :KEEP_TMP_CMDL_FILE }
55
+ else
56
+ FileUtils.rm @tmp_cmdl_file
57
+ end
58
+ end
59
+
60
+ cmd_line_args['environment'] = Rails.env.to_s if defined? Rails
61
+
62
+ if cmd_line_args['environment'] == 'test' and cmd_line_args['verbose'].nil?
63
+ cmd_line_args['verbose'] = 'silent'
64
+ end
65
+
66
+ if cmd_line_args['environment'].nil?
67
+ defaults = read_options({:environment => 'default',
68
+ :config_file => cmd_line_args['config-file'],
69
+ :verbose => 'silent'})
70
+ cmd_line_args['environment'] = defaults['environment'] || 'development'
71
+ end
72
+
73
+ @options = read_options({:environment => cmd_line_args['environment'],
74
+ :config_file => cmd_line_args['config-file'],
75
+ :verbose => cmd_line_args['verbose']}, cmd_line_args)
76
+ end
77
+
78
+ # Read the options from the config file.
79
+ # cfg_file is the default config file. If local_cfg_file exists, the options
80
+ # specified there overrides those in cfg_file. If :config_file is specified,
81
+ # it is used in place of local_cfg_file.
82
+ # If :environment is specified, the options in the corresponding environment
83
+ # is loaded and merged with those in 'default'.
84
+ def read_options args={}, update_options={}
85
+ args = {:environment => nil,
86
+ :config_file => nil,
87
+ :verbose => 'silent'
88
+ }.update(args)
89
+
90
+ options = {}
91
+
92
+ if File.exists? @cfg_file
93
+ vputs "Loading '#{@cfg_file}'", args[:verbose]
94
+ options = YAML.load_file @cfg_file
95
+ end
96
+
97
+ if args[:config_file]
98
+ if !File.exists? args[:config_file]
99
+ vputs "ERROR: Config file '#{args[:config_file]}' not found!", args[:verbose]
100
+ exit
101
+ end
102
+ vputs "Loading '#{args[:config_file]}'", args[:verbose]
103
+ update_options(args[:config_file], options)
104
+ elsif @persistent_local_cfg_file && File.exists?(@persistent_local_cfg_file)
105
+ vputs "Loading '#{@persistent_local_cfg_file}'", args[:verbose]
106
+ update_options(@persistent_local_cfg_file, options)
107
+ elsif File.exists? @local_cfg_file
108
+ vputs "Loading '#{@local_cfg_file}'", args[:verbose]
109
+ update_options(@local_cfg_file, options)
110
+ end
111
+
112
+ if args[:environment]
113
+ vputs "Using environment '#{args[:environment]}'", args[:verbose]
114
+ options = (options['default']||{}).update(options[args[:environment]]||{})
115
+ end
116
+
117
+ options.update(update_options)
118
+ options['environment'] = 'development' if options['environment'].nil?
119
+ options['verbose'] = false if options['verbose'].nil?
120
+
121
+ if args[:verbose] == true
122
+ len = options.keys.map { |k| k.size }.sort.last
123
+ vputs "Loaded options:", args[:verbose]
124
+ options.each { |k, v| puts " #{k.ljust(len)} => #{v}" }
125
+ end
126
+
127
+ options
128
+ end
129
+
130
+
131
+ private
132
+
133
+ def vputs msg, verbose
134
+ return if verbose == 'silent' || verbose.nil?
135
+ puts "** P#{Process.pid} #{msg}"
136
+ end
137
+
138
+ # Allows retrieval of option value (i.e. options.option_name) that matches
139
+ # the key name in the config file.
140
+ def method_missing method_name, *arg
141
+ if method_name.to_s =~ /(.*)=$/
142
+ key = $1
143
+ if @options.has_key? key
144
+ @options[key] = arg[0]
145
+ return @options[key]
146
+ end
147
+ else
148
+ key = method_name.to_s
149
+ if @options.has_key? key
150
+ return @options[key]
151
+ end
152
+ end
153
+ raise NoMethodError.new("undefined method `#{key}' for Options:Class", "unknown_key")
154
+ end
155
+
156
+ # Update options.
157
+ def update_options file, options
158
+ YAML.load_file(file).each do |k, v|
159
+ next unless v
160
+ if options[k]
161
+ options[k].update(v)
162
+ else
163
+ options[k] = v
164
+ end
165
+ end
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,36 @@
1
+ #MIT LICENSE
2
+ #
3
+ #Copyright (c) 2012 SUSE Linux Products GmbH
4
+ #
5
+ #Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ #of this software and associated documentation files (the "Software"), to deal
7
+ #in the Software without restriction, including without limitation the rights
8
+ #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ #copies of the Software, and to permit persons to whom the Software is
10
+ #furnished to do so, subject to the following conditions:
11
+ #
12
+ #The above copyright notice and this permission notice shall be included in
13
+ #all copies or substantial portions of the Software.
14
+ #
15
+ #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ #THE SOFTWARE.
22
+
23
+ module Common
24
+
25
+ class Utils
26
+ # Suppress all warnings raised within the block of code.
27
+ # i.e. suppress_warnings { code_that_raise_warnings }
28
+ def self.suppress_warnings
29
+ v = $VERBOSE
30
+ $VERBOSE = nil
31
+ yield
32
+ ensure
33
+ $VERBOSE = v
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ module Common
2
+ end
3
+
4
+ require 'common/options'
5
+ require 'common/utils'
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: config-parser
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - cschum@suse.de
9
+ - tom@opensuse.org
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-08-22 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description: ! 'Parsing an options.yml file into a Hash with convenience methods like
16
+
17
+ overwriting variables per Rails environment and overwriting variables with a local
18
+
19
+ options_local.yml file.'
20
+ email:
21
+ executables: []
22
+ extensions: []
23
+ extra_rdoc_files: []
24
+ files:
25
+ - .gitignore
26
+ - LICENSE
27
+ - README.rdoc
28
+ - config-parser.gemspec
29
+ - lib/common/options.rb
30
+ - lib/common/utils.rb
31
+ - lib/config-parser.rb
32
+ homepage: https://github.com/openSUSE/rubygem_config-parser
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.11
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Parsing an options.yml file into a Hash with convenience.
56
+ test_files: []