diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2010-09-28 03:38:13 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2010-09-28 03:38:13 -0700 |
commit | 1475d4085a97d1851d6c3b351596a4682cc0a479 (patch) | |
tree | 6f15fe6f1ce992510950230a482c0a9934466b2c /lognotify.rb | |
parent | f6828d119a0b5f919edb8fadb84db3e5f77c423f (diff) | |
download | lognotify-1475d4085a97d1851d6c3b351596a4682cc0a479.tar.gz lognotify-1475d4085a97d1851d6c3b351596a4682cc0a479.tar.xz |
Add a configuration file parsing method.
Diffstat (limited to 'lognotify.rb')
-rwxr-xr-x | lognotify.rb | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/lognotify.rb b/lognotify.rb index cc2a975..affc9f2 100755 --- a/lognotify.rb +++ b/lognotify.rb @@ -6,3 +6,37 @@ # Copyright 2010 David Vazgenovich Shakaryan <dvshakaryan@gmail.com> # Distributed under the terms of the GNU General Public License v3. # See http://www.gnu.org/licenses/gpl.txt for the full license text. + +CONFIG_DIR="~/.config/lognotify" + +# Configuration file parser. +def parse identifier + conf = Hash.new + file = File.expand_path(CONFIG_DIR + '/' + identifier + '.conf') + + File.open(file) do |contents| + contents.each_line do |line| + # Remove whitespace from beginning of line, allowing for indentation. + line.lstrip! + + # Ignore empty lines and comments. + unless line.empty? or line[0,1] == '#' + key, val = line.split('=', 2) + + # Raise an error if line does not contain a key/value pair. + if val.nil? + raise 'Check line ' + contents.lineno.to_s + ' for errors.' + end + + conf[key.strip.to_sym] = val.strip + end + end + end + + return conf +end + +# Test code... +ARGV.each do |identifier| + puts parse(identifier).inspect +end |