Move configuration to Config module
Created a generic configuration mechanism that can grab configuration
from the command line or configuration files.
*.exe
*.so
cookies.txt
+tags
+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();
+ }
+ }
+}
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
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();
public void Run() {
Application.Init();
Plugins = new Dictionary<string,WebThingPlugin>();
- ParseArgs();
+
+ Config.ParseCommandLine();
+ Config.Load();
+ //Config.DumpOptions();
soup_settings();
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();
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/");
}
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();
}
void WebView_LoadCommitted(object o, LoadCommittedArgs e) {
- Console.WriteLine("Loading Committed");
+ //Console.WriteLine("Loading Committed");
LoadState = Mode.LoadInProgress;
GLib.Source.Remove(idletimer);
r = 0;
}
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();
}