Move configuration to Config module
authorChip Black <bytex64@bytex64.net>
Sat, 6 Jun 2009 08:46:44 +0000 (03:46 -0500)
committerChip Black <bytex64@bytex64.net>
Sat, 6 Jun 2009 08:46:44 +0000 (03:46 -0500)
Created a generic configuration mechanism that can grab configuration
from the command line or configuration files.

.gitignore
Config.cs [new file with mode: 0644]
Makefile
WebThing.cs
plugins/DefaultPage.cs
plugins/LoadProgress.cs

index 818493c..fffe541 100644 (file)
@@ -4,3 +4,4 @@
 *.exe
 *.so
 cookies.txt
+tags
diff --git a/Config.cs b/Config.cs
new file mode 100644 (file)
index 0000000..9ad8095
--- /dev/null
+++ b/Config.cs
@@ -0,0 +1,134 @@
+using System;
+using System.IO;
+using System.Collections.Generic;
+using System.Text.RegularExpressions;
+
+namespace bytex64.WebThing {
+    public class Config {
+        public static string ConfigurationPrefix = "/etc";
+
+        public static Dictionary<string,string> Options;
+        public static Dictionary<string,Dictionary<string,string>> PluginOptions;
+        public static string[] Arguments;
+        private static HashSet<string> CommandLineOptions;
+
+        public static Regex item_re = new Regex(@"^(?:([\w.]+)\.)?(\w+)$");
+        public static Regex file_var_re = new Regex(@"^([\w.]+)\s*=\s*(.*)$");
+
+        static Config() {
+            Options = new Dictionary<string,string>();
+            PluginOptions = new Dictionary<string,Dictionary<string,string>>();
+            CommandLineOptions = new HashSet<string>();
+        }
+
+        private static void ParseOption(string key, string val) {
+            Match m = item_re.Match(key);
+            string plugin = m.Groups[1].Value;
+            string name = m.Groups[2].Value;
+            if (plugin.Length > 0) {   // Plugin Option
+                if (!PluginOptions.ContainsKey(plugin))
+                    PluginOptions[plugin] = new Dictionary<string,string>();
+                PluginOptions[plugin][name] = val;
+            } else {                   // Global Option
+                Options[name] = val;
+            }
+        }
+
+        public static void DumpOptions() {
+            Console.WriteLine("Options:");
+            foreach (string key in Options.Keys)
+                Console.WriteLine("   {0}\t{1}", key, Options[key]);
+
+            Console.WriteLine("Plugin Options:");
+            foreach (string plugin in PluginOptions.Keys) {
+                Console.WriteLine("    {0}", plugin);
+                foreach (string key in PluginOptions[plugin].Keys)
+                    Console.WriteLine("        {0}\t{1}", key, PluginOptions[plugin][key]);
+            }
+
+            Console.WriteLine("Arguments: {0}", String.Join(" ", Arguments));
+        }
+
+        public static void ParseCommandLine() {
+            Queue<string> q = new Queue<string>();
+            foreach (string s in System.Environment.GetCommandLineArgs()) {
+                q.Enqueue(s);
+            }
+            q.Dequeue();   // Remove self argument
+
+            Regex SimpleOpt = new Regex(@"^-([\w.]+)(?:[:=](.*))?$");
+            Regex LongOpt   = new Regex(@"^--([\w.]+)$");
+            List<string> args = new List<string>();
+
+            while (q.Count > 0) {
+                string opt = q.Dequeue();
+                Match m;
+                   
+                m = SimpleOpt.Match(opt);
+                if (m.Success) {
+                    string key = m.Groups[1].Value;
+                    string val = m.Groups[2].Value;
+                    if (val.Length > 0) {
+                        ParseOption(key, val);
+                    } else {
+                        ParseOption(key, null);
+                    }
+                    CommandLineOptions.Add(key);
+                    continue;
+                }
+
+                m = LongOpt.Match(opt);
+                if (m.Success) {
+                    string key = m.Groups[1].Value;
+                    if (q.Count > 0) {
+                        string val = q.Peek();
+                        if (!(SimpleOpt.IsMatch(val) || LongOpt.IsMatch(val))) {
+                            q.Dequeue();
+                            ParseOption(key, val);
+                        } else {
+                            ParseOption(key, null);
+                        }
+                    } else {
+                        ParseOption(key, null);
+                    }
+                    CommandLineOptions.Add(key);
+                    continue;
+                }
+
+                // else
+
+                args.Add(opt);
+            }
+            Arguments = args.ToArray();
+        }
+
+        public static void Load() {
+            LoadFile(ConfigurationPrefix + "/WebThing.conf");
+            LoadFile(Environment.GetEnvironmentVariable("HOME") + "/.config/WebThing/WebThing.conf");
+            if (Options.ContainsKey("config"))
+                LoadFile(Options["config"]);
+        }
+
+        public static void LoadFile(string filename) {
+            TextReader f;
+            try {
+                f = new StreamReader(File.OpenRead(filename));
+            } catch (FileNotFoundException) {
+                Console.WriteLine("Could not open configuration file {0}", filename);
+                return;
+            }
+
+            string line;
+            while ((line = f.ReadLine()) != null) {
+                Match m = file_var_re.Match(line.Trim());
+                if (m.Success) {
+                    string key = m.Groups[1].Value;
+                    if (!CommandLineOptions.Contains(key))
+                        ParseOption(key, m.Groups[2].Value);
+                }
+            }
+
+            f.Close();
+        }
+    }
+}
index ea5eb46..8cf0ed8 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,10 +6,14 @@ references = -r:webkit-sharp.dll -pkg:gtk-sharp-2.0
 
 all: WebThingMain.exe plugins
 
+.PHONY: tags
+tags:
+       ctags --extra=+fq --fields=+ianmzS '--c#-kinds=cimnp' *.cs
+
 WebThingMain.exe: WebThingMain.cs WebThing.dll SoupSettings.so
        $(CS) $(CSFLAGS) -r:WebThing.dll -out:$@ WebThingMain.cs
 
-WebThing.dll: WebThing.cs WebThingView.cs WebThingPlugin.cs
+WebThing.dll: WebThing.cs WebThingView.cs WebThingPlugin.cs Config.cs
        $(CS) $(CSFLAGS) $(references) -target:library -out:$@ $^
 
 SoupSettings.so: SoupSettings.c
index 5e4ca34..6f8a164 100644 (file)
@@ -40,36 +40,11 @@ namespace bytex64.WebThing {
         public Gtk.Notebook Tabs {
             get { return _Tabs; }
         }
-        /*
-        private int _CurrentTab;
-        public int CurrentTab {
-            get { return _CurrentTab; }
-            set {
-                if (value >= _Tabs.Count)
-                    _CurrentTab = _Tabs.Count - 1;
-                else if (value < 0)
-                    _CurrentTab = 0;
-                else
-                    _CurrentTab = value;
-
-                _WebView.Hide();
-                _ScrolledWindow.Remove(_WebView);
-                _WebView = _Tabs[_CurrentTab];
-                _ScrolledWindow.Add(_WebView);
-                foreach (WebView view in Tabs)
-                    Console.WriteLine(view.MainFrame.Uri);
-                _WebView.Show();
-                _WebView.GrabFocus();
-            }
-        }
-        */
 
         private Gtk.Table WidgetGrid;
         private Gtk.Alignment InteriorOverlay;
 
         public Dictionary<string,WebThingPlugin> Plugins;
-        public Dictionary<string,string> Options;
-        public string[] Arguments;
 
         [DllImport ("SoupSettings.dll")]
         private static extern void soup_settings();
@@ -77,7 +52,10 @@ namespace bytex64.WebThing {
         public void Run() {
             Application.Init();
             Plugins = new Dictionary<string,WebThingPlugin>();
-            ParseArgs();
+
+            Config.ParseCommandLine();
+            Config.Load();
+            //Config.DumpOptions();
 
             soup_settings();
 
@@ -116,66 +94,6 @@ namespace bytex64.WebThing {
             Application.Run();
         }
 
-        protected void ParseArgs() {
-            Options = new Dictionary<string,string>();
-            Queue<string> q = new Queue<string>();
-            foreach (string s in System.Environment.GetCommandLineArgs()) {
-                q.Enqueue(s);
-            }
-            q.Dequeue();   // Remove self argument
-
-            Regex SimpleOpt = new Regex(@"^-([a-zA-Z0-9]+)$");
-            Regex LongOpt   = new Regex(@"^--([\w-]+)$");
-            string OptLast = null;
-            List<string> args = new List<string>();
-
-            while (q.Count > 0) {
-                string opt = q.Dequeue();
-                Match m;
-                   
-                m = SimpleOpt.Match(opt);
-                if (m.Success) {
-                    string s = m.Groups[1].Value;
-                    if (s.Length > 1) {
-                        foreach (char c in s) {
-                            Options[c.ToString()] = "";
-                        }
-                        OptLast = null;
-                    } else {
-                        Options[s] = "";
-                        OptLast = s;
-                    }
-                    continue;
-                }
-
-                m = LongOpt.Match(opt);
-                if (m.Success) {
-                    string s = m.Groups[1].Value;
-                    Options[s] = "";
-                    OptLast = s;
-                    continue;
-                }
-
-                // else
-
-                if (OptLast != null) {
-                    Options[OptLast] = opt;
-                    OptLast = null;
-                } else {
-                    args.Add(opt);
-                }
-            }
-            Arguments = args.ToArray();
-
-            /*
-            Console.WriteLine("Options:");
-            foreach (string key in Options.Keys)
-                Console.WriteLine("   {0}\t{1}", key, Options[key]);
-
-            Console.WriteLine("Arguments: {0}", String.Join(" ", Arguments));
-            */
-        }
-
         public void LoadPlugin(string assemblyname) {
             Assembly a = Assembly.LoadFile("plugins/" + assemblyname + ".dll");
             Type[] types = a.GetTypes();
index e0f65d6..f7076e5 100644 (file)
@@ -3,8 +3,10 @@ using bytex64.WebThing;
 
 public class DefaultPage : WebThingPlugin {
     public override void Init(WebThing wt) {
-        if (wt.Arguments.Length > 0) {
-            wt.OpenUri(wt.Arguments[0]);
+        if (Config.Arguments.Length > 0) {
+            wt.OpenUri(Config.Arguments[0]);
+        } else if (Config.Options.ContainsKey("DefaultPage")) {
+            wt.OpenUri(Config.Options["DefaultPage"]);
         } else {
             wt.OpenUri("http://dominionofawesome.com/");
         }
index 08ffbf6..9da760a 100644 (file)
@@ -52,7 +52,7 @@ public class LoadThrobber : Gtk.DrawingArea {
 
     void WebView_LoadStarted(object o, LoadStartedArgs e) {
         this.Show();
-        Console.WriteLine("Loading Started");
+        //Console.WriteLine("Loading Started");
         LoadState = Mode.LoadStarted;
         idletimer = GLib.Timeout.Add(100, delegate {
             QueueDraw();
@@ -61,7 +61,7 @@ public class LoadThrobber : Gtk.DrawingArea {
     }
 
     void WebView_LoadCommitted(object o, LoadCommittedArgs e) {
-        Console.WriteLine("Loading Committed");
+        //Console.WriteLine("Loading Committed");
         LoadState = Mode.LoadInProgress;
         GLib.Source.Remove(idletimer);
         r = 0;
@@ -69,13 +69,13 @@ public class LoadThrobber : Gtk.DrawingArea {
     }
 
     void WebView_LoadProgressChanged(object o, LoadProgressChangedArgs e) {
-        Console.WriteLine("Loading Progress: {0}", e.Progress);
+        //Console.WriteLine("Loading Progress: {0}", e.Progress);
         r = (int) ((e.Progress / 100.0) * 360);
         QueueDraw();
     }
 
     void WebView_LoadFinished(object o, LoadFinishedArgs e) {
-        Console.WriteLine("Loading Finished");
+        //Console.WriteLine("Loading Finished");
         LoadState = Mode.LoadFinished;
         this.Hide();
     }