Class: Amazon::Config

Inherits:
Hash
  • Object
show all
Defined in:
lib/amazon.rb

Overview

A Class for dealing with configuration files, such as /etc/amazonrc and ~/.amazonrc.

Defined Under Namespace

Classes: ConfigError

Instance Method Summary collapse

Constructor Details

#initialize(config_str) ⇒ Config

A configuration may be passed in as a string. Otherwise, the files /etc/amazonrc and ~/.amazonrc are read if they exist and are readable.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/amazon.rb', line 74

def initialize(config_str)
  locale = nil

  if config_str

	# We have been passed a config file as a string.
	#
    config_files = [ config_str ]
	config_class = File

  else
	puts 'No config file specified'

  end

  config_files.each do |cf|

	# We must determine whether the file is readable.
	#
	readable = File.exists?( cf ) && File.readable?( cf )

	if readable

	  Amazon.dprintf( 'Opening %s ...', cf ) if config_class == File

	  config_class.open( cf ) { |f| lines = f.readlines }.each do |line|
 line.chomp!

 # Skip comments and blank lines.
 #
 next if line =~ /^(#|$)/

 Amazon.dprintf( 'Read: %s', line )

 # Determine whether we're entering the subsection of a new locale.
 #
 if match = line.match( /^\[(\w+)\]$/ )
   locale = match[1]
   Amazon.dprintf( "Config locale is now '%s'.", locale )
   next
 end

 # Store these, because we'll probably find a use for these later.
 #
 begin
  	      match = line.match( /^\s*(\S+)\s*=\s*(['"]?)([^'"]+)(['"]?)/ )
   key, begin_quote, val, end_quote = match[1, 4]
   raise ConfigError if begin_quote != end_quote

 rescue NoMethodError, ConfigError
   raise ConfigError, "bad config line: #{line}"
 end

 if locale && locale != 'global'
   self[locale] ||= {}
   self[locale][key] = val
 else
   self[key] = val
 end

	  end
	else
	  puts "could not open file"
	end

  end

end