PageRenderTime 165ms CodeModel.GetById 157ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Manos/Manos/ManosConfig.cs

http://github.com/jacksonh/manos
C# | 160 lines | 92 code | 23 blank | 45 comment | 1 complexity | e60f2d52214d2d7f7efe8202bd6a331e MD5 | raw file
  1//
  2// ManosConfig - configuration management for Manos
  3// Author: Axel Callabed <axelc@github.com>
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining
  6// a copy of this software and associated documentation files (the
  7// "Software"), to deal in the Software without restriction, including
  8// without limitation the rights to use, copy, modify, merge, publish,
  9// distribute, sublicense, and/or sell copies of the Software, and to
 10// permit persons to whom the Software is furnished to do so, subject to
 11// the following conditions:
 12// 
 13// The above copyright notice and this permission notice shall be
 14// included in all copies or substantial portions of the Software.
 15// 
 16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 17// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 18// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 19// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 20// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 21// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 22// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 23//
 24//
 25
 26using System;
 27using Nini.Config;
 28using System.IO;
 29
 30namespace Manos
 31{
 32	/// <summary>
 33	/// Manos config. The config file being loaded is in .ini format that
 34	/// contains of one or more sections, the main section is what this
 35	/// API uses. However if wanting to access different sections, one can
 36	/// go through the Source to access the nini IConfigSource.
 37	/// 
 38	/// The configs are loaded from $MANOS_CONFIG first, but if that variable
 39	/// is not set it will look for manos.config within the current directory.
 40	/// If neither are found, the config is left empty.
 41	/// 
 42	/// Here is an example config:
 43	/// 
 44	/// [manos]
 45	/// database_user = ahall
 46	/// database_password = temp123
 47	/// database_name = mydb
 48	/// database_hostname = localhost
 49	/// database_type = postgresql
 50	/// 
 51	/// Getting the db user is as simple as ManosConfig.GetString("database_user");
 52	/// </summary>
 53	public static class ManosConfig {
 54		public static IConfigSource Source { get; private set; }
 55		public static IConfig Main { get; private set; }
 56		private const string MAIN_SECTION = "manos";
 57		
 58		public static void Load ()
 59		{
 60			string source = Environment.GetEnvironmentVariable ("MANOS_CONFIG") ??
 61                    Path.Combine (Environment.CurrentDirectory, "manos.config");
 62			if (!File.Exists(source))
 63				return;
 64
 65			Source = new IniConfigSource(source);
 66			Main = Source.Configs[MAIN_SECTION];
 67		}
 68		
 69		public static void Set (string key, object value)
 70		{
 71			Main.Set(key, value);
 72		}
 73		
 74		public static string Get (string key)
 75		{
 76			return Main.Get (key);
 77		}
 78		
 79		public static string Get (string key, string defaultValue)
 80		{
 81			return Main.Get (key, defaultValue);
 82		}
 83		
 84		public static string GetString (string key)
 85		{
 86			return Main.GetString (key);
 87		}
 88		
 89		public static string GetString (string key, string defaultValue)
 90		{
 91			return Main.GetString (key, defaultValue);
 92		}
 93		
 94		public static double GetDouble (string key)
 95		{
 96			return Main.GetDouble (key);
 97		}
 98		
 99		public static double GetDouble (string key, double defaultValue)
100		{
101			return Main.GetDouble (key, defaultValue);
102		}
103		
104		public static string GetExpanded (string key)
105		{
106			return Main.GetExpanded (key);
107		}
108		
109		public static float GetFloat (string key)
110		{
111			return Main.GetFloat (key);
112		}
113		
114		public static float GetFloat (string key, float defaultValue)
115		{
116			return Main.GetFloat (key, defaultValue);
117		}
118		
119		public static int GetInt (string key)
120		{
121			return Main.GetInt (key);
122		}
123		
124		public static int GetInt (string key, int defaultValue)
125		{
126			return Main.GetInt (key, defaultValue);
127		}
128		
129		public static bool GetBoolean (string key)
130		{
131			return Main.GetBoolean (key);
132		}
133		
134		public static bool GetBoolean (string key, bool defaultValue)
135		{
136			return Main.GetBoolean (key, defaultValue);
137		}
138		
139		public static string[] GetKeys ()
140		{
141			return Main.GetKeys();
142		}
143		
144		public static string[] GetValues ()
145		{
146			return Main.GetValues();
147		}
148		
149		public static long GetLong (string key)
150		{
151			return Main.GetLong (key);
152		}
153		
154		public static long GetLong (string key, long defaultValue)
155		{
156			return Main.GetLong (key, defaultValue);
157		}
158    }
159}
160