Class: Amp::AmpConfig

Inherits:
Object show all
Includes:
PythonConfig
Defined in:
lib/amp/support/amp_config.rb

Overview

AmpConfig is the class that handles all configuration for the Amp program. It is different from PythonConfig because there is a sense of hierarchy in the configuration files in Mercurial, and as such, in Amp. Namely, the hgrc in /etc/mercurial/ is of lesser importance as the one in your_repo/.hg/. So, we use “parent configs” to handle this hierarchy - create an AmpConfig with a parent, and it’ll copy over all the old configurations. Then, tell it to read the repo-specific hgrc file, and the new settings will override the old ones.

Constant Summary

Constants included from PythonConfig

PythonConfig::MAX_INTERPOLATION_DEPTH, PythonConfig::VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ AmpConfig

Initializes the AmpConfig. Takes options to do that.

All other options are saved in an instance variable for later reference.

Parameters:

  • opts (Hash) (defaults to: {})

    the options for initialization.

  • [AmpConfig] (Hash)

    a customizable set of options

  • [[String], (Hash)

    a customizable set of options



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/amp/support/amp_config.rb', line 33

def initialize(opts={})
  @config = nil
  @options = opts
  
  if opts[:parent_config]
    @parent_config = opts.delete :parent_config
    @config = @parent_config.config.dup
  else
    opts[:read] = Support::rc_path
  end
  
  if opts[:read]
    @config = ConfigParser.new
    
    [*opts.delete(:read)].each do |file|
      read_file file
    end
  end
  
  if opts[:parent]
    @config = opts.delete(:parent).dup
  end
  @write_to_file ||= File.expand_path("~/.hgrc")
end

Instance Attribute Details

#configObject

The PythonConfig that actually stores all the information



19
20
21
# File 'lib/amp/support/amp_config.rb', line 19

def config
  @config
end

#parent_configObject (readonly)

Returns the value of attribute parent_config.



20
21
22
# File 'lib/amp/support/amp_config.rb', line 20

def parent_config
  @parent_config
end

Instance Method Details

#[](*args) ⇒ Object

Access configurations. Params: Section, [Config Key], [Type to convert to], [Default value if not found]

Examples:

myconfig[“ui”, “askusername”, Boolean, false]

this means look in the "ui" section for a key named "askusername". If
we find it, convert it to a Boolean and return it. If not, then just
return false.


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/amp/support/amp_config.rb', line 128

def [](*args)
  section = args[0]
  
  case args.size
  when 1
    @config[section]
  when 2
    key = args[1]
    @config[section][key]
  when 3
    key = args[1]
    force = args[2]
    @config[section][key, force]
  when 4
    key = args[1]
    force = args[2]
    default = args[3]
    @config[section][key, force, default]
  end
end

#[]=(*args) ⇒ Object

Assigning section-key values.

Examples:

config[“ui”, “askusername”] = false

Parameters:

  • args

    the [Section, Key] pair

  • value

    what you assigned it to.



155
156
157
158
159
160
# File 'lib/amp/support/amp_config.rb', line 155

def []=(*args) # def []=(*args, value)
  value = args.pop
  section = args[0]
  key = args[1]
  @config[section][key] = value
end

#read_file(file) ⇒ Object

Reads the file provided, and overwrites any settings we’ve already assigned. Does NOT raise an error if the file isn’t found.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/amp/support/amp_config.rb', line 61

def read_file(file)
  begin
    File.open(file, "r") do |fp|
      newconfig = ConfigParser.new fp
      @config.merge! newconfig
    end
    
    @config.merge! @options[:overlay] if @options[:overlay]
    @write_to_file = file
  rescue Errno::ENOENT
  end
end

#save!Object

Saves to the most recently read-in file



76
77
78
# File 'lib/amp/support/amp_config.rb', line 76

def save!
  @config.write @write_to_file
end

#to_sObject



162
# File 'lib/amp/support/amp_config.rb', line 162

def to_s; @config.to_s; end

#update_options(opts = {}) ⇒ Object

Updates our options with any settings we provide.



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/amp/support/amp_config.rb', line 82

def update_options(opts = {})
  if opts[:config]
    config = opts.delete config
    config.each do |section, settings|
      settings.each do |k,v|
        self[section,k] = v
      end
    end
  end
  @options.merge! opts
end

#usernameString

Gets the current username. First it checks some environment variables, then it looks into config files, then it gives up and makes one up.

Returns:

  • (String)

    the username for commits and shizz



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/amp/support/amp_config.rb', line 99

def username
  user = nil
  # check the env-var
  user ||= ENV["HGUSER"]
  return user if user
  # check for an email
  user ||= ENV["EMAIL"]
  return user if user
  # check for stored username
  user ||= self["ui","username"]
  return user if user
  #check for a setting
  if self["ui", "askusername", Boolean]
    user = UI.ask("enter a commit username:")
  end
  return user if user
  # figure it out based on the system
  user = "#{Support.get_username}@#{Support.get_fully_qualified_domain_name}"
  return user
end