[smuxi/experiments/msg_buffer_tool: 2/3] So much code for so little gain that your eyes will bleed
- From: Mirco M. M. Bauer <mmmbauer src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [smuxi/experiments/msg_buffer_tool: 2/3] So much code for so little gain that your eyes will bleed
- Date: Thu, 6 Mar 2014 17:25:55 +0000 (UTC)
commit 64f7761ab75c2b49bd7dbc6457f836d32703e03c
Author: Mirco Bauer <meebey meebey net>
Date: Wed Mar 5 23:45:04 2014 +0100
So much code for so little gain that your eyes will bleed
src/Engine-MessageBuffer/AssemblyInfo.cs | 3 +
.../Engine-MessageBuffer.csproj | 8 +-
src/Engine-MessageBuffer/Main.cs | 258 ++++++++++++++------
.../smuxi-message-buffer.exe.config | 16 ++
4 files changed, 208 insertions(+), 77 deletions(-)
---
diff --git a/src/Engine-MessageBuffer/AssemblyInfo.cs b/src/Engine-MessageBuffer/AssemblyInfo.cs
index b04913d..96ff696 100644
--- a/src/Engine-MessageBuffer/AssemblyInfo.cs
+++ b/src/Engine-MessageBuffer/AssemblyInfo.cs
@@ -33,3 +33,6 @@ using System.Runtime.InteropServices;
[assembly: CLSCompliant(true)]
[assembly: ComVisible(false)]
+
+// let log4net use .exe.config file
+[assembly: log4net.Config.XmlConfigurator]
diff --git a/src/Engine-MessageBuffer/Engine-MessageBuffer.csproj
b/src/Engine-MessageBuffer/Engine-MessageBuffer.csproj
index 7bc4e18..b30333a 100644
--- a/src/Engine-MessageBuffer/Engine-MessageBuffer.csproj
+++ b/src/Engine-MessageBuffer/Engine-MessageBuffer.csproj
@@ -20,7 +20,7 @@
<WarningLevel>4</WarningLevel>
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
<ConsolePause>false</ConsolePause>
-
<Commandlineparameters>--source-file=/home/meebey/.local/share/smuxi/buffers/local/irc/freenode/#ubuntu.db4o</Commandlineparameters>
+ <Commandlineparameters>cat
/home/meebey/.local/share/smuxi/buffers/local/irc/freenode/#ubuntu.db4o</Commandlineparameters>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<Optimize>true</Optimize>
@@ -34,6 +34,7 @@
<Reference Include="log4net">
<HintPath>..\..\lib\log4net.dll</HintPath>
</Reference>
+ <Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="Main.cs" />
@@ -57,4 +58,9 @@
<Name>ServiceStack.Text</Name>
</ProjectReference>
</ItemGroup>
+ <ItemGroup>
+ <None Include="smuxi-message-buffer.exe.config">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
+ </ItemGroup>
</Project>
\ No newline at end of file
diff --git a/src/Engine-MessageBuffer/Main.cs b/src/Engine-MessageBuffer/Main.cs
index 248e6f2..5222286 100644
--- a/src/Engine-MessageBuffer/Main.cs
+++ b/src/Engine-MessageBuffer/Main.cs
@@ -19,11 +19,13 @@
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.IO;
+using System.Linq;
using System.Reflection;
using NDesk.Options;
using ServiceStack.Text;
using Smuxi.Common;
using Smuxi.Engine.Dto;
+using System.Collections.Generic;
namespace Smuxi.Engine
{
@@ -43,43 +45,128 @@ namespace Smuxi.Engine
InitLocale();
var debug = false;
- var import = false;
- var sourceFile = "";
- var sourceFormat = "";
- var export = false;
- var destinationFile = "";
- var destinationFormat = "";
-
- // --import --import-file=foo.db4o --import-format=db4o --into-file=foo.sqlite
--into-format=sqlite
- // --export --export-file=foo.db4o -> JSON
-
- // --source-file=foo.db4o --target-file=foo.sqlite
- // --source-file=foo.db4o
var parser = new OptionSet() {
{ "d|debug", _("Enable debug output"),
val => {
debug = true;
}
- },
- { "source-file=", _("Source file path"),
- val => {
- sourceFile = val;
- }
- },
- { "source-format=", _("Source format (valid values: auto, db4o, sqlite)"),
+ }
+ };
+ parser.Add("h|help", _("Show this help"),
+ val => {
+ Console.WriteLine(_("Usage: smuxi-message-buffer [options] action action-options"));
+ Console.WriteLine();
+ Console.WriteLine(_("Actions:"));
+ Console.WriteLine(" cat");
+ Console.WriteLine(" convert/copy/cp");
+ Console.WriteLine();
+ Console.WriteLine(_("Options:"));
+ parser.WriteOptionDescriptions(Console.Out);
+ Environment.Exit(0);
+ }
+ );
+
+ try {
+ var mainArgs = args.TakeWhile(x => x.StartsWith("-"));
+ parser.Parse(mainArgs);
+ if (debug) {
+ repo.Threshold = log4net.Core.Level.Debug;
+ }
+
+ var action = args.Skip(mainArgs.Count()).First();
+ var actionArgs = args.Skip(mainArgs.Count() + 1);
+ switch (action.ToLower()) {
+ case "cat":
+ CatAction(action, actionArgs);
+ break;
+ case "convert":
+ case "copy":
+ case "cp":
+ CopyAction(action, actionArgs);
+ break;
+ default:
+ throw new OptionException(
+ String.Format(
+ _("Unknown action: '{0}'"),
+ action
+ ),
+ "action"
+ );
+ }
+ } catch (OptionException ex) {
+ Console.Error.WriteLine(_("Command line error: {0}"), ex.Message);
+ Environment.Exit(1);
+ } catch (Exception e) {
+ Logger.Fatal(e);
+ }
+ }
+
+ static void CatAction(string action, IEnumerable<string> args)
+ {
+ var dbFormat = "";
+ var parser = new OptionSet() {
+ { "format=", _("Database format (valid values: auto, db4o, sqlite)"),
val => {
if (val == "auto") {
val = "";
}
- sourceFormat = val;
+ dbFormat = val;
}
},
- { "destination-file=", _("Destination file path or -/empty for stdout"),
+ /*
+ { "<>",
+ val => {
+ if (!val.StartsWith("-")) {
+ return;
+ }
+ throw new OptionException(
+ String.Format(_("Unknown {0} option: '{1}'"),
+ action, val),
+ val
+ );
+ }
+ }
+ */
+ };
+ parser.Add("h|help", _("Show this help"),
+ val => {
+ Console.WriteLine(
+ String.Format(
+ _("Usage: smuxi-message-buffer {0} [action-options] db_path"),
+ action
+ )
+ );
+ Console.WriteLine();
+ Console.WriteLine(" db_path " + _("Database path"));
+ Console.WriteLine();
+ Console.WriteLine(_("Options:"));
+ parser.WriteOptionDescriptions(Console.Out);
+ Environment.Exit(0);
+ }
+ );
+
+ var parameters = parser.Parse(args);
+ if (parameters.Count < 1) {
+ throw new OptionException(
+ _("db_path is required"),
+ action
+ );
+ }
+ var dbPath = parameters[0];
+ Copy(dbPath, dbFormat, null, null);
+ }
+
+ static void CopyAction(string action, IEnumerable<string> args)
+ {
+ var sourceFormat = "";
+ var destinationFormat = "";
+ var parser = new OptionSet() {
+ { "source-format=", _("Source format (valid values: auto, db4o, sqlite)"),
val => {
- if (val == "-") {
+ if (val == "auto") {
val = "";
}
- destinationFile = val;
+ sourceFormat = val;
}
},
{ "destination-format=", _("Destination format (valid values: auto, db4o, sqlite)"),
@@ -90,18 +177,32 @@ namespace Smuxi.Engine
destinationFormat = val;
}
},
+ /*
{ "<>",
val => {
+ if (!val.StartsWith("-")) {
+ return;
+ }
throw new OptionException(
- String.Format(_("Unknown option: '{0}'"), val),
+ String.Format(_("Unknown {0} option: '{1}'"),
+ action, val),
val
);
}
}
+ */
};
parser.Add("h|help", _("Show this help"),
val => {
- Console.WriteLine(_("Usage: smuxi-message-buffer [options]"));
+ Console.WriteLine(
+ String.Format(
+ _("Usage: smuxi-message-buffer {0} [action-options] source_db destination_db"),
+ action
+ )
+ );
+ Console.WriteLine();
+ Console.WriteLine(" source_db " + _("Source file path"));
+ Console.WriteLine(" destination_db " + _("Destination file path or -/empty for
stdout"));
Console.WriteLine();
Console.WriteLine(_("Options:"));
parser.WriteOptionDescriptions(Console.Out);
@@ -109,21 +210,19 @@ namespace Smuxi.Engine
}
);
- try {
- parser.Parse(args);
- if (debug) {
- repo.Threshold = log4net.Core.Level.Debug;
- }
-
- Copy(sourceFile, sourceFormat, destinationFile, destinationFormat);
- } catch (OptionException ex) {
- Console.Error.WriteLine(_("Command line error: {0}"), ex.Message);
- Environment.Exit(1);
- } catch (Exception e) {
- Logger.Fatal(e);
- // rethrow the exception for console output
- throw;
+ var parameters = parser.Parse(args);
+ if (parameters.Count < 2) {
+ throw new OptionException(
+ _("source_db and destination_db are required"),
+ action
+ );
}
+ var sourceFile = parameters[0];
+ var destinationFile = parameters[1];
+ if (destinationFile == "-") {
+ destinationFile = "";
+ }
+ Copy(sourceFile, sourceFormat, destinationFile, destinationFormat);
}
static void Copy(string sourceFile, string sourceFormat,
@@ -133,50 +232,57 @@ namespace Smuxi.Engine
throw new ArgumentException(_("sourceFile must not be empty."));
}
- var sourceBufferType = ParseMessageBufferType(sourceFile, sourceFormat);
- var sourceBuffer = CreateMessageBuffer(sourceFile, sourceBufferType);
-
- IMessageBuffer destinationBuffer = null;
- if (!String.IsNullOrEmpty(destinationFile)) {
- var destinationBufferType = ParseMessageBufferType(destinationFile,
- destinationFormat);
- destinationBuffer = CreateMessageBuffer(destinationFile,
- destinationBufferType);
- if (destinationBuffer.Count > 0) {
- throw new InvalidOperationException(
- String.Format(
- _("Destination database {0} must be empty!"),
- destinationFile
- )
- );
- }
- }
+ IMessageBuffer sourceBuffer = null, destinationBuffer = null;
+ try {
+ var sourceBufferType = ParseMessageBufferType(sourceFile, sourceFormat);
+ sourceBuffer = CreateMessageBuffer(sourceFile, sourceBufferType);
- if (destinationBuffer == null) {
- // JSON pipe
- Console.WriteLine("[");
- var msgCount = sourceBuffer.Count;
- var i = 0;
- foreach (var msg in sourceBuffer) {
- var dto = new MessageDtoModelV1(msg);
- var json = JsonSerializer.SerializeToString(dto);
- if (i++ < msgCount - 1) {
- Console.WriteLine("{0},", json);
- } else {
- Console.WriteLine(json);
+ if (!String.IsNullOrEmpty(destinationFile)) {
+ var destinationBufferType = ParseMessageBufferType(destinationFile,
+ destinationFormat);
+ destinationBuffer = CreateMessageBuffer(destinationFile,
+ destinationBufferType);
+ if (destinationBuffer.Count > 0) {
+ throw new InvalidOperationException(
+ String.Format(
+ _("Destination database {0} must be empty!"),
+ destinationFile
+ )
+ );
}
}
+
if (destinationBuffer == null) {
- Console.WriteLine("]");
+ // JSON pipe
+ Console.WriteLine("[");
+ var msgCount = sourceBuffer.Count;
+ var i = 0;
+ foreach (var msg in sourceBuffer) {
+ var dto = new MessageDtoModelV1(msg);
+ var json = JsonSerializer.SerializeToString(dto);
+ if (i++ < msgCount - 1) {
+ Console.WriteLine("{0},", json);
+ } else {
+ Console.WriteLine(json);
+ }
+ }
+ if (destinationBuffer == null) {
+ Console.WriteLine("]");
+ }
+ } else {
+ foreach (var msg in sourceBuffer) {
+ destinationBuffer.Add(msg);
+ }
+ destinationBuffer.Flush();
+ }
+ } finally {
+ if (sourceBuffer != null) {
+ sourceBuffer.Dispose();
}
- } else {
- foreach (var msg in sourceBuffer) {
- destinationBuffer.Add(msg);
+ if (destinationBuffer != null) {
+ destinationBuffer.Dispose();
}
- destinationBuffer.Flush();
- destinationBuffer.Dispose();
}
- sourceBuffer.Dispose();
}
static MessageBufferType ParseMessageBufferType(string fileName, string type)
diff --git a/src/Engine-MessageBuffer/smuxi-message-buffer.exe.config
b/src/Engine-MessageBuffer/smuxi-message-buffer.exe.config
new file mode 100644
index 0000000..d8a3872
--- /dev/null
+++ b/src/Engine-MessageBuffer/smuxi-message-buffer.exe.config
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+ <configSections>
+ <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
+ </configSections>
+ <log4net>
+ <root>
+ <appender-ref ref="ConsoleAppender" />
+ </root>
+ <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
+ <layout type="log4net.Layout.PatternLayout">
+ <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
+ </layout>
+ </appender>
+ </log4net>
+</configuration>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]