Class: PythonConfig::ConfigParser

Inherits:
Object
  • Object
show all
Defined in:
lib/amp/dependencies/python_config.rb

Overview

This is the main class that handles configurations. You parse, modify, and output through this class. See the README for tons of examples.

Constant Summary collapse

SECTION_REGEXP =
/\[([^\[\]]*)\]/
ASSIGNMENT_REGEXP =
/([^:=\s]+)\s*[:=]\s*([^\n]*?)$/
LONG_HEADER_REGEXP =
/^([ \t]+)([^\n]+)$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io = nil) ⇒ ConfigParser

Creates a new ConfigParser. If io is provided, the configuration file is read from the io.



67
68
69
70
71
72
# File 'lib/amp/dependencies/python_config.rb', line 67

def initialize(io = nil)
  @sections = {}
  io.each do |line|
    parse_line line
  end unless io.nil?
end

Instance Attribute Details

#sectionsObject (readonly)

Returns the value of attribute sections.



61
62
63
# File 'lib/amp/dependencies/python_config.rb', line 61

def sections
  @sections
end

Instance Method Details

#[](section) ⇒ Object

Returns the section given by section



114
115
116
117
# File 'lib/amp/dependencies/python_config.rb', line 114

def [](section)
  result = (@sections[section] || add_section(section))
  result
end

#add_section(section_name, values = {}) ⇒ Object

Creates a new section, with the values as provided by the (optional) values parameter



102
103
104
105
106
107
108
109
110
111
# File 'lib/amp/dependencies/python_config.rb', line 102

def add_section(section_name, values={})
  newsection = ConfigSection.new(values)
  @sections[section_name] = newsection
  self.instance_eval %Q{
    def #{section_name.downcase.gsub('-','_')}
      @sections["#{section_name}"]
    end
  }
  newsection
end

#cloneObject Also known as: dup



74
75
76
77
78
79
80
# File 'lib/amp/dependencies/python_config.rb', line 74

def clone
  newconfig = ConfigParser.new
  @sections.each do |key, val|
    newconfig.add_section key, val.source_hash.dup
  end
  newconfig
end

#merge!(other_config) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/amp/dependencies/python_config.rb', line 138

def merge! other_config
  other_config.sections.each do |name, other_section|
    newsection = (@sections[name] || add_section(name))
    other_section.each do |key, value|
      newsection[key] = value # avoid interpolation
    end
  end
end

#parse_line(line) ⇒ Object

:nodoc:



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/amp/dependencies/python_config.rb', line 83

def parse_line(line) #:nodoc:

  if line =~ SECTION_REGEXP
    section_name = $1
    @cursection = add_section section_name
  elsif line =~ ASSIGNMENT_REGEXP
    @cursection[$1] = $2
    @cur_assignment = $1
  elsif line =~ LONG_HEADER_REGEXP
    @cursection[@cur_assignment] += " " + $2
  end
end

#section_namesObject

Returns the names of all the sections, which can be used for keys into the sections



97
98
99
# File 'lib/amp/dependencies/python_config.rb', line 97

def section_names
  @sections.keys
end

#to_sObject

Returns the configuration as a string that can be output to a file. Does not perform interpolation before writing.



121
122
123
124
125
126
127
128
# File 'lib/amp/dependencies/python_config.rb', line 121

def to_s
  output = ""
  @sections.each do |k,v|
    output << "[#{k}]\n"
    output << v.to_s
  end
  output
end

#write(file) ⇒ Object Also known as: save

Writes the configuration to a given file.



131
132
133
134
135
# File 'lib/amp/dependencies/python_config.rb', line 131

def write(file)
  File.open(file, "w") do |out|
    out.write self.to_s
  end
end