[smuxi/experiments/msg_buffer_tool: 1/3] WIP



commit 8f36649535229cfd1ceb9d49e5eadc06b0b907b1
Author: Mirco Bauer <meebey meebey net>
Date:   Wed Mar 5 21:51:39 2014 +0100

    WIP

 src/Engine-MessageBuffer/AssemblyInfo.cs           |   35 +++
 .../Engine-MessageBuffer.csproj                    |   60 +++++
 src/Engine-MessageBuffer/Main.cs                   |  247 ++++++++++++++++++++
 src/Engine/MessageBuffers/SqliteMessageBuffer.cs   |   15 ++
 src/smuxi.sln                                      |   10 +
 5 files changed, 367 insertions(+), 0 deletions(-)
---
diff --git a/src/Engine-MessageBuffer/AssemblyInfo.cs b/src/Engine-MessageBuffer/AssemblyInfo.cs
new file mode 100644
index 0000000..b04913d
--- /dev/null
+++ b/src/Engine-MessageBuffer/AssemblyInfo.cs
@@ -0,0 +1,35 @@
+/*
+ * Smuxi - Smart MUltipleXed Irc
+ *
+ * Copyright (c) 2014 Mirco Bauer <meebey meebey net>
+ *
+ * Full GPL License: <http://www.gnu.org/licenses/gpl.txt>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+ */
+
+using System;
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+[assembly: AssemblyTitle("Smuxi - MessageBuffer tool")]
+[assembly: AssemblyCopyright("2014 (C) Mirco Bauer <meebey meebey net>")]
+
+[assembly: AssemblyDelaySign(false)]
+[assembly: AssemblyKeyFile("")]
+
+[assembly: CLSCompliant(true)]
+[assembly: ComVisible(false)]
diff --git a/src/Engine-MessageBuffer/Engine-MessageBuffer.csproj 
b/src/Engine-MessageBuffer/Engine-MessageBuffer.csproj
new file mode 100644
index 0000000..7bc4e18
--- /dev/null
+++ b/src/Engine-MessageBuffer/Engine-MessageBuffer.csproj
@@ -0,0 +1,60 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" 
xmlns="http://schemas.microsoft.com/developer/msbuild/2003";>
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProductVersion>10.0.0</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{7964E3E4-1E98-4AE0-AF1B-1B940CDF0A99}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <RootNamespace>Smuxi-Engine</RootNamespace>
+    <AssemblyName>smuxi-message-buffer</AssemblyName>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>..\..\bin\debug</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
+    <ConsolePause>false</ConsolePause>
+    
<Commandlineparameters>--source-file=/home/meebey/.local/share/smuxi/buffers/local/irc/freenode/#ubuntu.db4o</Commandlineparameters>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <Optimize>true</Optimize>
+    <OutputPath>..\..\bin\release</OutputPath>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <Externalconsole>true</Externalconsole>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+    <Reference Include="log4net">
+      <HintPath>..\..\lib\log4net.dll</HintPath>
+    </Reference>
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Main.cs" />
+    <Compile Include="AssemblyInfo.cs" />
+    <Compile Include="..\AssemblyVersion.cs">
+      <Link>AssemblyVersion.cs</Link>
+    </Compile>
+  </ItemGroup>
+  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+  <ItemGroup>
+    <ProjectReference Include="..\Engine\Engine.csproj">
+      <Project>{BCD52FCF-B7A8-42FB-AA8D-CF9921FF593E}</Project>
+      <Name>Engine</Name>
+    </ProjectReference>
+    <ProjectReference Include="..\Common\Common.csproj">
+      <Project>{73DEF91D-03FF-41E3-B2E1-3259AF247CA7}</Project>
+      <Name>Common</Name>
+    </ProjectReference>
+    <ProjectReference Include="..\..\lib\ServiceStack.Text\src\ServiceStack.Text\ServiceStack.Text.csproj">
+      <Project>{579B3FDB-CDAD-44E1-8417-885C38E49A0E}</Project>
+      <Name>ServiceStack.Text</Name>
+    </ProjectReference>
+  </ItemGroup>
+</Project>
\ No newline at end of file
diff --git a/src/Engine-MessageBuffer/Main.cs b/src/Engine-MessageBuffer/Main.cs
new file mode 100644
index 0000000..248e6f2
--- /dev/null
+++ b/src/Engine-MessageBuffer/Main.cs
@@ -0,0 +1,247 @@
+// Smuxi - Smart MUltipleXed Irc
+//
+// Copyright (c) 2014 Mirco Bauer <meebey meebey net>
+//
+// Full GPL License: <http://www.gnu.org/licenses/gpl.txt>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+using System;
+using System.IO;
+using System.Reflection;
+using NDesk.Options;
+using ServiceStack.Text;
+using Smuxi.Common;
+using Smuxi.Engine.Dto;
+
+namespace Smuxi.Engine
+{
+    public class MainClass
+    {
+        static readonly log4net.ILog Logger = 
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
+        static readonly string LibraryTextDomain = "smuxi-message-buffer";
+
+        public static void Main(string[] args)
+        {
+            System.Threading.Thread.CurrentThread.Name = "Main";
+
+            // initialize log level
+            log4net.Repository.ILoggerRepository repo = log4net.LogManager.GetRepository();
+            repo.Threshold = log4net.Core.Level.Error;
+
+            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)"),
+                    val => {
+                        if (val == "auto") {
+                            val = "";
+                        }
+                        sourceFormat = val;
+                    }
+                },
+                { "destination-file=", _("Destination file path or -/empty for stdout"),
+                    val => {
+                        if (val == "-") {
+                            val = "";
+                        }
+                        destinationFile = val;
+                    }
+                },
+                { "destination-format=", _("Destination format (valid values: auto, db4o, sqlite)"),
+                    val => {
+                        if (val == "auto") {
+                            val = "";
+                        }
+                        destinationFormat = val;
+                    }
+                },
+                { "<>",
+                    val => {
+                        throw new OptionException(
+                            String.Format(_("Unknown option: '{0}'"), val),
+                            val
+                        );
+                    }
+                }
+            };
+            parser.Add("h|help", _("Show this help"),
+                val => {
+                    Console.WriteLine(_("Usage: smuxi-message-buffer [options]"));
+                    Console.WriteLine();
+                    Console.WriteLine(_("Options:"));
+                    parser.WriteOptionDescriptions(Console.Out);
+                    Environment.Exit(0);
+                }
+            );
+
+            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;
+            }
+        }
+
+        static void Copy(string sourceFile, string sourceFormat,
+                         string destinationFile, string destinationFormat)
+        {
+            if (String.IsNullOrEmpty(sourceFile)) {
+                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
+                        )
+                    );
+                }
+            }
+
+            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 (destinationBuffer == null) {
+                    Console.WriteLine("]");
+                }
+            } else {
+                foreach (var msg in sourceBuffer) {
+                    destinationBuffer.Add(msg);
+                }
+                destinationBuffer.Flush();
+                destinationBuffer.Dispose();
+            }
+            sourceBuffer.Dispose();
+        }
+
+        static MessageBufferType ParseMessageBufferType(string fileName, string type)
+        {
+            if (String.IsNullOrEmpty(type)) {
+                if (fileName.EndsWith(".sqlite3")) {
+                    return MessageBufferType.Sqlite;
+                } else if (fileName.EndsWith(".db4o")) {
+                    return MessageBufferType.Db4o;
+                } else {
+                    throw new ArgumentException(
+                        String.Format(
+                            _("Unknown file format: '{0}'"),
+                            fileName
+                        ),
+                        "fileName"
+                    );
+                }
+            }
+            return (MessageBufferType) Enum.Parse(typeof(MessageBufferType),
+                                                  fileName, true);
+        }
+
+        static IMessageBuffer CreateMessageBuffer(string fileName,
+                                                  MessageBufferType bufferType)
+        {
+            switch (bufferType) {
+                case MessageBufferType.Db4o:
+                    return new Db4oMessageBuffer(fileName);
+                case MessageBufferType.Sqlite:
+                    return new SqliteMessageBuffer(fileName);
+                default:
+                    throw new ArgumentException(
+                        String.Format(
+                            _("Unsupported buffer type: '{0}'"),
+                            bufferType
+                        ),
+                        "bufferType"
+                    );
+            }
+        }
+
+        static void InitLocale()
+        {
+            string appDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
+            string localeDir = Path.Combine(appDir, "locale");
+            if (!Directory.Exists(localeDir)) {
+                localeDir = Path.Combine(Defines.InstallPrefix, "share");
+                localeDir = Path.Combine(localeDir, "locale");
+            }
+
+            LibraryCatalog.Init("smuxi-message-buffer", localeDir);
+            Logger.Debug("Using locale data from: " + localeDir);
+        }
+
+        static string _(string msg)
+        {
+            return LibraryCatalog.GetString(msg, LibraryTextDomain);
+        }
+    }
+
+    public enum MessageBufferType {
+        None,
+        Pipe,
+        Db4o,
+        Sqlite
+    }
+}
diff --git a/src/Engine/MessageBuffers/SqliteMessageBuffer.cs 
b/src/Engine/MessageBuffers/SqliteMessageBuffer.cs
index bb3f89e..2cfe2aa 100644
--- a/src/Engine/MessageBuffers/SqliteMessageBuffer.cs
+++ b/src/Engine/MessageBuffers/SqliteMessageBuffer.cs
@@ -59,6 +59,21 @@ namespace Smuxi.Engine
                               base(sessionUsername, protocol, networkId, chatId)
         {
             DBPath = GetBufferPath() + ".sqlite3";
+            Init();
+        }
+
+        public SqliteMessageBuffer(string dbPath)
+        {
+            if (dbPath == null) {
+                throw new ArgumentNullException("dbPath");
+            }
+
+            DBPath = dbPath;
+            Init();
+        }
+
+        void Init()
+        {
             Connection = new SqliteConnection(
                 "Data Source=" + DBPath + ";" +
                 // enable Write-Ahead-Log (WAL)
diff --git a/src/smuxi.sln b/src/smuxi.sln
index 32e352d..f2a5b29 100644
--- a/src/smuxi.sln
+++ b/src/smuxi.sln
@@ -67,6 +67,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StarkSoftProxy", "..\lib\St
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Frontend-GNOME-Twitter", 
"Frontend-GNOME-Twitter\Frontend-GNOME-Twitter.csproj", "{298A5895-D8C8-45EB-AC57-1B6AAD05D6AD}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Engine-MessageBuffer", 
"Engine-MessageBuffer\Engine-MessageBuffer.csproj", "{7964E3E4-1E98-4AE0-AF1B-1B940CDF0A99}"
+EndProject
 Global
        GlobalSection(SolutionConfigurationPlatforms) = preSolution
                Release|Any CPU = Release|Any CPU
@@ -219,6 +221,14 @@ Global
                {73DEF91D-03FF-41E3-B2E1-3259AF247CA7}.Release|Any CPU.Build.0 = Release|Any CPU
                {73DEF91D-03FF-41E3-B2E1-3259AF247CA7}.Release-Win32|x86.ActiveCfg = Release|Any CPU
                {73DEF91D-03FF-41E3-B2E1-3259AF247CA7}.Release-Win32|x86.Build.0 = Release|Any CPU
+               {7964E3E4-1E98-4AE0-AF1B-1B940CDF0A99}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+               {7964E3E4-1E98-4AE0-AF1B-1B940CDF0A99}.Debug|Any CPU.Build.0 = Debug|Any CPU
+               {7964E3E4-1E98-4AE0-AF1B-1B940CDF0A99}.Debug-Win32|x86.ActiveCfg = Debug|Any CPU
+               {7964E3E4-1E98-4AE0-AF1B-1B940CDF0A99}.Debug-Win32|x86.Build.0 = Debug|Any CPU
+               {7964E3E4-1E98-4AE0-AF1B-1B940CDF0A99}.Release|Any CPU.ActiveCfg = Release|Any CPU
+               {7964E3E4-1E98-4AE0-AF1B-1B940CDF0A99}.Release|Any CPU.Build.0 = Release|Any CPU
+               {7964E3E4-1E98-4AE0-AF1B-1B940CDF0A99}.Release-Win32|x86.ActiveCfg = Release|Any CPU
+               {7964E3E4-1E98-4AE0-AF1B-1B940CDF0A99}.Release-Win32|x86.Build.0 = Release|Any CPU
                {7B1E3AB2-FB8C-4439-A2B4-2129EDBD79F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
                {7B1E3AB2-FB8C-4439-A2B4-2129EDBD79F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
                {7B1E3AB2-FB8C-4439-A2B4-2129EDBD79F8}.Debug-Win32|x86.ActiveCfg = Debug|Any CPU


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]