Module: MadbitConfig
- Defined in:
- lib/jirarest2/madbitconfig.rb
Overview
Module to handle configuration files
Defined Under Namespace
Classes: FileExistsException
Class Method Summary collapse
-
.read_configfile(config_file, whitespace = false) ⇒ Hash
reads a config-file and returns a hash The config file can either contain key = value pairs or JSON data JSON and simple “key = value\n” lines work TODO make it work with “key = [value,value]\n” or even “key = value1 \n value2 \n …”.
-
.write_configfile(config_file, configoptions, save = :noforce) ⇒ Object
write a configfile.
Class Method Details
.read_configfile(config_file, whitespace = false) ⇒ Hash
reads a config-file and returns a hash The config file can either contain key = value pairs or JSON data JSON and simple “key = value\n” lines work TODO make it work with “key = [value,value]\n” or even “key = value1 \n value2 \n …”
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/jirarest2/madbitconfig.rb', line 37 def self.read_configfile(config_file,whitespace = false) if whitespace then regexp = Regexp.new(/"|\[|\]/) else regexp = Regexp.new(/\s+|"|\[|\]/) end temp = Array.new vars = Hash.new file = String.new if config_file == "-" then # caller wants STDIN file = ARGF.read else # caller wants real file config_file = File.(config_file) unless File.exists?(config_file) then raise IOError, "Unable to find config file \"#{config_file}\"" end file = IO.read(config_file) end if file =~ /^\s*{/ then vars = JSON.parse(file) else file.each_line { |line| if line.match(/^\s*#/) # don't care about lines starting with an # (even after whitespace) next elsif line.match(/^\s*$/) # no text, no content next else # Right now I don't know what to use scan for. It will escape " nice enough. But once that is excaped the regexp doesn't work any longer. temp[0],temp[1] = line.to_s.scan(/^.*$/).to_s.split("=") #temp[0],temp[1] = line.to_s.split("=") temp.collect! { |val| val.gsub(regexp, "") } vars[temp[0]] = temp[1] end } end # JSON or not? return vars end |
.write_configfile(config_file, configoptions, save = :noforce) ⇒ Object
write a configfile
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/jirarest2/madbitconfig.rb', line 87 def self.write_configfile(config_file, , save = :noforce) config_file = File.(config_file) # Be save # First we make sure we don't overwrite a file if the save - flag is set. if save != :force then if File.exists?(config_file) then raise FileExistsException, config_file end end # write the file File.open(config_file, File::CREAT|File::TRUNC|File::RDWR,0600) { |f| .each { |option,value| f.write( option + " = " + value + "\n") } } # File end |