/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. using System;
  26. using Nini.Config;
  27. using System.IO;
  28. namespace Manos
  29. {
  30. /// <summary>
  31. /// Manos config. The config file being loaded is in .ini format that
  32. /// contains of one or more sections, the main section is what this
  33. /// API uses. However if wanting to access different sections, one can
  34. /// go through the Source to access the nini IConfigSource.
  35. ///
  36. /// The configs are loaded from $MANOS_CONFIG first, but if that variable
  37. /// is not set it will look for manos.config within the current directory.
  38. /// If neither are found, the config is left empty.
  39. ///
  40. /// Here is an example config:
  41. ///
  42. /// [manos]
  43. /// database_user = ahall
  44. /// database_password = temp123
  45. /// database_name = mydb
  46. /// database_hostname = localhost
  47. /// database_type = postgresql
  48. ///
  49. /// Getting the db user is as simple as ManosConfig.GetString("database_user");
  50. /// </summary>
  51. public static class ManosConfig {
  52. public static IConfigSource Source { get; private set; }
  53. public static IConfig Main { get; private set; }
  54. private const string MAIN_SECTION = "manos";
  55. public static void Load ()
  56. {
  57. string source = Environment.GetEnvironmentVariable ("MANOS_CONFIG") ??
  58. Path.Combine (Environment.CurrentDirectory, "manos.config");
  59. if (!File.Exists(source))
  60. return;
  61. Source = new IniConfigSource(source);
  62. Main = Source.Configs[MAIN_SECTION];
  63. }
  64. public static void Set (string key, object value)
  65. {
  66. Main.Set(key, value);
  67. }
  68. public static string Get (string key)
  69. {
  70. return Main.Get (key);
  71. }
  72. public static string Get (string key, string defaultValue)
  73. {
  74. return Main.Get (key, defaultValue);
  75. }
  76. public static string GetString (string key)
  77. {
  78. return Main.GetString (key);
  79. }
  80. public static string GetString (string key, string defaultValue)
  81. {
  82. return Main.GetString (key, defaultValue);
  83. }
  84. public static double GetDouble (string key)
  85. {
  86. return Main.GetDouble (key);
  87. }
  88. public static double GetDouble (string key, double defaultValue)
  89. {
  90. return Main.GetDouble (key, defaultValue);
  91. }
  92. public static string GetExpanded (string key)
  93. {
  94. return Main.GetExpanded (key);
  95. }
  96. public static float GetFloat (string key)
  97. {
  98. return Main.GetFloat (key);
  99. }
  100. public static float GetFloat (string key, float defaultValue)
  101. {
  102. return Main.GetFloat (key, defaultValue);
  103. }
  104. public static int GetInt (string key)
  105. {
  106. return Main.GetInt (key);
  107. }
  108. public static int GetInt (string key, int defaultValue)
  109. {
  110. return Main.GetInt (key, defaultValue);
  111. }
  112. public static bool GetBoolean (string key)
  113. {
  114. return Main.GetBoolean (key);
  115. }
  116. public static bool GetBoolean (string key, bool defaultValue)
  117. {
  118. return Main.GetBoolean (key, defaultValue);
  119. }
  120. public static string[] GetKeys ()
  121. {
  122. return Main.GetKeys();
  123. }
  124. public static string[] GetValues ()
  125. {
  126. return Main.GetValues();
  127. }
  128. public static long GetLong (string key)
  129. {
  130. return Main.GetLong (key);
  131. }
  132. public static long GetLong (string key, long defaultValue)
  133. {
  134. return Main.GetLong (key, defaultValue);
  135. }
  136. }
  137. }