From 85b244419aec2552d114990aa86900aeac83a7e0 Mon Sep 17 00:00:00 2001 From: Chip Black Date: Sat, 6 Jun 2009 03:46:44 -0500 Subject: [PATCH] Move configuration to Config module Created a generic configuration mechanism that can grab configuration from the command line or configuration files. --- .gitignore | 1 + Config.cs | 134 ++++++++++++++++++++++++++++++++++++++++ Makefile | 6 +- WebThing.cs | 90 ++------------------------- plugins/DefaultPage.cs | 6 +- plugins/LoadProgress.cs | 8 +-- 6 files changed, 152 insertions(+), 93 deletions(-) create mode 100644 Config.cs diff --git a/.gitignore b/.gitignore index 818493c..fffe541 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ *.exe *.so cookies.txt +tags diff --git a/Config.cs b/Config.cs new file mode 100644 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 Options; + public static Dictionary> PluginOptions; + public static string[] Arguments; + private static HashSet 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(); + PluginOptions = new Dictionary>(); + CommandLineOptions = new HashSet(); + } + + 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(); + 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 q = new Queue(); + 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 args = new List(); + + 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(); + } + } +} diff --git a/Makefile b/Makefile index ea5eb46..8cf0ed8 100644 --- 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 diff --git a/WebThing.cs b/WebThing.cs index 5e4ca34..6f8a164 100644 --- a/WebThing.cs +++ b/WebThing.cs @@ -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 Plugins; - public Dictionary 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(); - ParseArgs(); + + Config.ParseCommandLine(); + Config.Load(); + //Config.DumpOptions(); soup_settings(); @@ -116,66 +94,6 @@ namespace bytex64.WebThing { Application.Run(); } - protected void ParseArgs() { - Options = new Dictionary(); - Queue q = new Queue(); - 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 args = new List(); - - 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(); diff --git a/plugins/DefaultPage.cs b/plugins/DefaultPage.cs index e0f65d6..f7076e5 100644 --- a/plugins/DefaultPage.cs +++ b/plugins/DefaultPage.cs @@ -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/"); } diff --git a/plugins/LoadProgress.cs b/plugins/LoadProgress.cs index 08ffbf6..9da760a 100644 --- a/plugins/LoadProgress.cs +++ b/plugins/LoadProgress.cs @@ -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(); } -- 2.25.1