X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=Config.cs;h=49e7203cfaaf38bfccbc59b69a5d23701a2375ab;hb=84890340eccbe66e303780a992fe152356974394;hp=9ad80956a8fae57d085c914fc6342b7f0463df17;hpb=85b244419aec2552d114990aa86900aeac83a7e0;p=WebThing.git diff --git a/Config.cs b/Config.cs index 9ad8095..49e7203 100644 --- a/Config.cs +++ b/Config.cs @@ -1,24 +1,41 @@ using System; using System.IO; +using System.Collections; using System.Collections.Generic; using System.Text.RegularExpressions; namespace bytex64.WebThing { public class Config { - public static string ConfigurationPrefix = "/etc"; + public static List ConfigPath; public static Dictionary Options; public static Dictionary> PluginOptions; public static string[] Arguments; - private static HashSet CommandLineOptions; + public static List Plugins; - public static Regex item_re = new Regex(@"^(?:([\w.]+)\.)?(\w+)$"); - public static Regex file_var_re = new Regex(@"^([\w.]+)\s*=\s*(.*)$"); + private static HashSet CommandLineOptions; + private static Regex item_re = new Regex(@"^(?:([\w.]+)\.)?(\w+)$"); + private static Regex file_var_re = new Regex(@"^([\w.]+)\s*=\s*(.*)$"); + private static Regex file_command_re = new Regex(@"^([\w]+)\s*(.*)$"); static Config() { Options = new Dictionary(); PluginOptions = new Dictionary>(); CommandLineOptions = new HashSet(); + Plugins = new List(); + + // Set up ConfigPath + IDictionary Env = Environment.GetEnvironmentVariables(); + ConfigPath = new List(); + ConfigPath.Add("/etc/WebThing"); + if (Env.Contains("HOME")) { + ConfigPath.Add(Env["HOME"] + "/.config/WebThing"); + ConfigPath.Add(Env["HOME"] + "/.WebThing"); + } + string LocalAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); + if (LocalAppData != "") { + ConfigPath.Add(LocalAppData + "/WebThing"); + } } private static void ParseOption(string key, string val) { @@ -30,7 +47,15 @@ namespace bytex64.WebThing { PluginOptions[plugin] = new Dictionary(); PluginOptions[plugin][name] = val; } else { // Global Option - Options[name] = val; + switch(key) { + case "Plugin": + case "plugin": + Plugins.Add(val); + break; + default: + Options[key] = val; + break; + } } } @@ -103,8 +128,13 @@ namespace bytex64.WebThing { } public static void Load() { - LoadFile(ConfigurationPrefix + "/WebThing.conf"); - LoadFile(Environment.GetEnvironmentVariable("HOME") + "/.config/WebThing/WebThing.conf"); + foreach (string path in ConfigPath) { + if (!Directory.Exists(path)) continue; + string[] ConfigFiles = Directory.GetFiles(path, "*.conf"); + foreach (string file in ConfigFiles) { + LoadFile(file); + } + } if (Options.ContainsKey("config")) LoadFile(Options["config"]); } @@ -120,11 +150,30 @@ namespace bytex64.WebThing { string line; while ((line = f.ReadLine()) != null) { - Match m = file_var_re.Match(line.Trim()); + Match m; + + line = line.Trim(); + m = file_var_re.Match(line); if (m.Success) { string key = m.Groups[1].Value; if (!CommandLineOptions.Contains(key)) ParseOption(key, m.Groups[2].Value); + continue; + } + + m = file_command_re.Match(line); + if (m.Success) { + string cmd = m.Groups[1].Value; + switch(cmd) { + case "Plugin": + case "plugin": + Plugins.Add(m.Groups[2].Value); + break; + default: + Console.WriteLine("Unknown Command in {0}: {1}", filename, line); + break; + } + continue; } }