module Pycf

github.com/python/cpython/blob/2.7/Lib/ConfigParser.py (License: github.com/python/cpython/blob/2.7/LICENSE)

github.com/python/cpython/blob/2.7/Lib/ConfigParser.py (License: github.com/python/cpython/blob/2.7/LICENSE)

github.com/python/cpython/blob/2.7/Lib/ConfigParser.py (License: github.com/python/cpython/blob/2.7/LICENSE)

Constants

KEYCRE
OPTCRE_NV
SECTCRE
VERSION

Public Class Methods

dump(hash) click to toggle source
# File lib/pycf/dump.rb, line 4
def dump(hash)
  unless hash.is_a?(Hash)
    raise TypeError, "wrong argument type #{hash.class} (expected Hash)"
  end

  python_config = []

  hash.each do |section, key_values|
    python_config << "[#{section}]"

    key_values.each do |key, value|
      value = value.to_s.gsub("\n", "\n\t")
      value = '""' if value.empty?
      python_config << "#{key} = #{value}"
    end
  end

  python_config.join("\n")
end
load(python_config, option = {}) click to toggle source
# File lib/pycf/load.rb, line 8
def load(python_config, option = {})
  hash = {}

  cursect = nil
  optname = nil

  python_config.split("\n").each_with_index do |line, lineno_minus_1|
    lineno = lineno_minus_1 + 1

    if line.split.empty? or line =~ /\A[#;]/
      next
    end

    if line =~ /\Arem\s+/i
      next
    end

    if line =~ /\A\s/ and cursect and optname
      line.strip!
      cursect[optname] << "\n#{line}" unless line.empty?
    else
      if line =~ SECTCRE
        sectname = $~[:header]

        unless hash.has_key?(sectname)
          hash[sectname] = {}
        end

        cursect = hash[sectname]
        optname = nil
      elsif not cursect
        raise MissingSectionHeaderError.new(lineno, line)
      else
        if line =~ OPTCRE_NV
          optname = $~[:option].downcase.rstrip
          vi = $~[:vi]
          optval = $~[:value]

          if optval
            if %w(= :).include?(vi)
              optval.sub!(/\s;.*\z/, '')
            end

            optval.strip!

            if optval == '""'
              optval = ''
            end
          elsif not option[:allow_no_value]
            raise ParsingError.new(lineno, line)
          end

          cursect[optname] = optval
        else
          raise ParsingError.new(lineno, line)
        end
      end
    end
  end

  if option[:interpolation]
    hash.each do |section, key_values|
      key_values.each do |key, value|
        value.gsub!(KEYCRE) do
          preword = $1
          key = $2

          if preword == '%'
            "%(#{key})s"
          else
            key_values[key.downcase]
          end
        end
      end
    end
  end

  hash
end

Private Instance Methods

dump(hash) click to toggle source
# File lib/pycf/dump.rb, line 4
def dump(hash)
  unless hash.is_a?(Hash)
    raise TypeError, "wrong argument type #{hash.class} (expected Hash)"
  end

  python_config = []

  hash.each do |section, key_values|
    python_config << "[#{section}]"

    key_values.each do |key, value|
      value = value.to_s.gsub("\n", "\n\t")
      value = '""' if value.empty?
      python_config << "#{key} = #{value}"
    end
  end

  python_config.join("\n")
end
load(python_config, option = {}) click to toggle source
# File lib/pycf/load.rb, line 8
def load(python_config, option = {})
  hash = {}

  cursect = nil
  optname = nil

  python_config.split("\n").each_with_index do |line, lineno_minus_1|
    lineno = lineno_minus_1 + 1

    if line.split.empty? or line =~ /\A[#;]/
      next
    end

    if line =~ /\Arem\s+/i
      next
    end

    if line =~ /\A\s/ and cursect and optname
      line.strip!
      cursect[optname] << "\n#{line}" unless line.empty?
    else
      if line =~ SECTCRE
        sectname = $~[:header]

        unless hash.has_key?(sectname)
          hash[sectname] = {}
        end

        cursect = hash[sectname]
        optname = nil
      elsif not cursect
        raise MissingSectionHeaderError.new(lineno, line)
      else
        if line =~ OPTCRE_NV
          optname = $~[:option].downcase.rstrip
          vi = $~[:vi]
          optval = $~[:value]

          if optval
            if %w(= :).include?(vi)
              optval.sub!(/\s;.*\z/, '')
            end

            optval.strip!

            if optval == '""'
              optval = ''
            end
          elsif not option[:allow_no_value]
            raise ParsingError.new(lineno, line)
          end

          cursect[optname] = optval
        else
          raise ParsingError.new(lineno, line)
        end
      end
    end
  end

  if option[:interpolation]
    hash.each do |section, key_values|
      key_values.each do |key, value|
        value.gsub!(KEYCRE) do
          preword = $1
          key = $2

          if preword == '%'
            "%(#{key})s"
          else
            key_values[key.downcase]
          end
        end
      end
    end
  end

  hash
end