X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=Config.cs;h=a4332e1a798020b76607dec0b3990884c17d0f35;hb=11835c28c3407f9bbd4aa68b875a528d13ff0d73;hp=49e7203cfaaf38bfccbc59b69a5d23701a2375ab;hpb=6c50b11e5b8e72781db4195d8472bb7d542c1d3c;p=WebThing.git diff --git a/Config.cs b/Config.cs index 49e7203..a4332e1 100644 --- a/Config.cs +++ b/Config.cs @@ -5,13 +5,25 @@ using System.Collections.Generic; using System.Text.RegularExpressions; namespace bytex64.WebThing { + public struct SearchHandlerPair { + public string Shortcut; + public string Plugin; + + public SearchHandlerPair(string s, string p) { + Shortcut = s; + Plugin = p; + } + } + public class Config { public static List ConfigPath; + public static string ConfigPathOut = null; public static Dictionary Options; public static Dictionary> PluginOptions; public static string[] Arguments; public static List Plugins; + public static List SearchHandlers; private static HashSet CommandLineOptions; private static Regex item_re = new Regex(@"^(?:([\w.]+)\.)?(\w+)$"); @@ -23,18 +35,24 @@ namespace bytex64.WebThing { PluginOptions = new Dictionary>(); CommandLineOptions = new HashSet(); Plugins = new List(); + SearchHandlers = 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 homepath = Env["HOME"] + "/.config/WebThing"; + ConfigPath.Add(homepath); + if (Directory.Exists(homepath)) + ConfigPathOut = homepath; } string LocalAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); if (LocalAppData != "") { - ConfigPath.Add(LocalAppData + "/WebThing"); + string homepath = LocalAppData + "/WebThing"; + ConfigPath.Add(homepath); + if (Directory.Exists(homepath)) + ConfigPathOut = homepath; } } @@ -47,8 +65,7 @@ namespace bytex64.WebThing { PluginOptions[plugin] = new Dictionary(); PluginOptions[plugin][name] = val; } else { // Global Option - switch(key) { - case "Plugin": + switch(key.ToLower()) { case "plugin": Plugins.Add(val); break; @@ -142,7 +159,7 @@ namespace bytex64.WebThing { public static void LoadFile(string filename) { TextReader f; try { - f = new StreamReader(File.OpenRead(filename)); + f = new StreamReader(filename); } catch (FileNotFoundException) { Console.WriteLine("Could not open configuration file {0}", filename); return; @@ -163,12 +180,19 @@ namespace bytex64.WebThing { m = file_command_re.Match(line); if (m.Success) { - string cmd = m.Groups[1].Value; + string cmd = m.Groups[1].Value.ToLower(); switch(cmd) { - case "Plugin": case "plugin": Plugins.Add(m.Groups[2].Value); break; + case "searchhandler": + string[] args = Regex.Split(m.Groups[2].Value, @"\s+"); + if (args.Length != 2) { + Console.WriteLine("Expecting two arguments for SearchHandler."); + break; + } + SearchHandlers.Add(new SearchHandlerPair(args[0], args[1])); + break; default: Console.WriteLine("Unknown Command in {0}: {1}", filename, line); break; @@ -179,5 +203,59 @@ namespace bytex64.WebThing { f.Close(); } + + public static void SaveFile(string filename) { + TextWriter f; + try { + f = new StreamWriter(filename); + } catch (IOException) { + Console.WriteLine("Could not save configuration to {0}", filename); + return; + } + + foreach (string key in Options.Keys) { + if (CommandLineOptions.Contains(key)) continue; + f.WriteLine("{0} = {1}", key, Options[key]); + } + + foreach (string plugin in PluginOptions.Keys) { + foreach (string key in PluginOptions[plugin].Keys) { + string pkey = String.Format("{0}.{1}", plugin, key); + if (CommandLineOptions.Contains(pkey)) continue; + f.WriteLine("{0} = {1}", pkey, PluginOptions[plugin][key]); + } + } + + f.Close(); + } + + public static void SaveFile(WebThingPlugin p, string filename) { + string plugin_name = p.GetType().FullName; + SaveFile(plugin_name, filename); + } + + public static void SaveFile(string plugin_name, string filename) { + // TODO: Throw exception if plugin does not exist + TextWriter f = new StreamWriter(filename); + + foreach (string key in PluginOptions[plugin_name].Keys) { + f.WriteLine("{0}.{1} = {2}", plugin_name, key, PluginOptions[plugin_name][key]); + } + + f.Close(); + } + + // Data parsers + public static bool ParseBool(string v) { + switch(v.ToLower()) { + case "true": + case "t": + case "1": + case "on": + case "yes": + return true; + } + return false; + } } }