diff options
author | Stephen Gregory <stephengregorynl@gmail.com> | 2018-01-15 02:49:07 +0300 |
---|---|---|
committer | Stephen Gregory <stephengregorynl@gmail.com> | 2018-04-12 04:10:42 +0300 |
commit | 46170de84ece88d5297aaaa98af32b8225ade15a (patch) | |
tree | 32956cf7c4b173908a7633bcfc89a3d7a5ebe033 /Duplicati/Library/Modules | |
parent | 3361e27e77f3562c5b6b0292f581593644e77341 (diff) |
Extract result serialization to file
Diffstat (limited to 'Duplicati/Library/Modules')
6 files changed, 79 insertions, 69 deletions
diff --git a/Duplicati/Library/Modules/Builtin/Duplicati.Library.Modules.Builtin.csproj b/Duplicati/Library/Modules/Builtin/Duplicati.Library.Modules.Builtin.csproj index a23687eed..e46227a13 100644 --- a/Duplicati/Library/Modules/Builtin/Duplicati.Library.Modules.Builtin.csproj +++ b/Duplicati/Library/Modules/Builtin/Duplicati.Library.Modules.Builtin.csproj @@ -67,6 +67,7 @@ <Compile Include="HyperVOptions.cs" />
<Compile Include="HttpOptions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="ResultSerializer.cs" />
<Compile Include="SendHttpMessage.cs" />
<Compile Include="SendMail.cs" />
<Compile Include="RunScript.cs" />
diff --git a/Duplicati/Library/Modules/Builtin/ResultSerializer.cs b/Duplicati/Library/Modules/Builtin/ResultSerializer.cs new file mode 100644 index 000000000..1795bdabf --- /dev/null +++ b/Duplicati/Library/Modules/Builtin/ResultSerializer.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace Duplicati.Library.Modules.Builtin +{ + class ResultSerializer + { + public static void SerializeResult(string file, object result) + { + using (StreamWriter sw = new StreamWriter(file)) + { + if (result == null) + { + sw.WriteLine("null?"); + } + else if (result is System.Collections.IEnumerable) + { + System.Collections.IEnumerable ie = (System.Collections.IEnumerable)result; + System.Collections.IEnumerator ien = ie.GetEnumerator(); + ien.Reset(); + + while (ien.MoveNext()) + { + object c = ien.Current; + if (c == null) + continue; + + if (c.GetType().IsGenericType && !c.GetType().IsGenericTypeDefinition && c.GetType().GetGenericTypeDefinition() == typeof(KeyValuePair<,>)) + { + object key = c.GetType().GetProperty("Key").GetValue(c, null); + object value = c.GetType().GetProperty("Value").GetValue(c, null); + sw.WriteLine("{0}: {1}", key, value); + } + else + sw.WriteLine(c); + } + } + else if (result.GetType().IsArray) + { + Array a = (Array)result; + + for (int i = a.GetLowerBound(0); i <= a.GetUpperBound(0); i++) + { + object c = a.GetValue(i); + + if (c == null) + continue; + + if (c.GetType().IsGenericType && !c.GetType().IsGenericTypeDefinition && c.GetType().GetGenericTypeDefinition() == typeof(KeyValuePair<,>)) + { + object key = c.GetType().GetProperty("Key").GetValue(c, null); + object value = c.GetType().GetProperty("Value").GetValue(c, null); + sw.WriteLine("{0}: {1}", key, value); + } + else + sw.WriteLine(c); + } + } + else if (result is Exception) + { + //No localization, must be parseable by script + Exception e = (Exception)result; + sw.WriteLine("Failed: {0}", e.Message); + sw.WriteLine("Details: {0}", e); + } + else + { + Utility.Utility.PrintSerializeObject(result, sw); + } + } + } + } +} diff --git a/Duplicati/Library/Modules/Builtin/RunScript.cs b/Duplicati/Library/Modules/Builtin/RunScript.cs index d900c0362..93bb7f0e9 100644 --- a/Duplicati/Library/Modules/Builtin/RunScript.cs +++ b/Duplicati/Library/Modules/Builtin/RunScript.cs @@ -115,77 +115,12 @@ namespace Duplicati.Library.Modules.Builtin using (TempFile tmpfile = new TempFile())
{
- SerializeResult(tmpfile, result);
+ ResultSerializer.SerializeResult(tmpfile, result);
Execute(m_finishScript, "AFTER", m_operationName, ref m_remoteurl, ref m_localpath, m_timeout, false, m_options, tmpfile, level);
}
}
#endregion
- public static void SerializeResult(string file, object result)
- {
- using(StreamWriter sw = new StreamWriter(file))
- {
- if (result == null)
- {
- sw.WriteLine("null?");
- }
- else if (result is System.Collections.IEnumerable)
- {
- System.Collections.IEnumerable ie = (System.Collections.IEnumerable)result;
- System.Collections.IEnumerator ien = ie.GetEnumerator();
- ien.Reset();
-
- while (ien.MoveNext())
- {
- object c = ien.Current;
- if (c == null)
- continue;
-
- if (c.GetType().IsGenericType && !c.GetType().IsGenericTypeDefinition && c.GetType().GetGenericTypeDefinition() == typeof(KeyValuePair<,>))
- {
- object key = c.GetType().GetProperty("Key").GetValue(c, null);
- object value = c.GetType().GetProperty("Value").GetValue(c, null);
- sw.WriteLine("{0}: {1}", key, value);
- }
- else
- sw.WriteLine(c);
- }
- }
- else if (result.GetType().IsArray)
- {
- Array a = (Array)result;
-
- for(int i = a.GetLowerBound(0); i <= a.GetUpperBound(0); i++)
- {
- object c = a.GetValue(i);
-
- if (c == null)
- continue;
-
- if (c.GetType().IsGenericType && !c.GetType().IsGenericTypeDefinition && c.GetType().GetGenericTypeDefinition() == typeof(KeyValuePair<,>))
- {
- object key = c.GetType().GetProperty("Key").GetValue(c, null);
- object value = c.GetType().GetProperty("Value").GetValue(c, null);
- sw.WriteLine("{0}: {1}", key, value);
- }
- else
- sw.WriteLine(c);
- }
- }
- else if (result is Exception)
- {
- //No localization, must be parseable by script
- Exception e = (Exception)result;
- sw.WriteLine("Failed: {0}", e.Message);
- sw.WriteLine("Details: {0}", e);
- }
- else
- {
- Utility.Utility.PrintSerializeObject(result, sw);
- }
- }
- }
-
private static void Execute(string scriptpath, string eventname, string operationname, ref string remoteurl, ref string[] localpath, int timeout, bool requiredScript, IDictionary<string, string> options, string datafile, ParsedResultType? level)
{
try
diff --git a/Duplicati/Library/Modules/Builtin/SendHttpMessage.cs b/Duplicati/Library/Modules/Builtin/SendHttpMessage.cs index 662eacd2d..389cb68f0 100644 --- a/Duplicati/Library/Modules/Builtin/SendHttpMessage.cs +++ b/Duplicati/Library/Modules/Builtin/SendHttpMessage.cs @@ -321,7 +321,7 @@ namespace Duplicati.Library.Modules.Builtin { if (input.IndexOf("%RESULT%", StringComparison.OrdinalIgnoreCase) >= 0) using (TempFile tf = new TempFile()) { - RunScript.SerializeResult(tf, result); + ResultSerializer.SerializeResult(tf, result); input = Regex.Replace(input, "\\%RESULT\\%", System.IO.File.ReadAllText(tf), RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); } diff --git a/Duplicati/Library/Modules/Builtin/SendJabberMessage.cs b/Duplicati/Library/Modules/Builtin/SendJabberMessage.cs index 2f4481666..160fac106 100644 --- a/Duplicati/Library/Modules/Builtin/SendJabberMessage.cs +++ b/Duplicati/Library/Modules/Builtin/SendJabberMessage.cs @@ -357,7 +357,7 @@ namespace Duplicati.Library.Modules.Builtin if (input.IndexOf("%RESULT%", StringComparison.OrdinalIgnoreCase) >= 0)
using (TempFile tf = new TempFile())
{
- RunScript.SerializeResult(tf, result);
+ ResultSerializer.SerializeResult(tf, result);
input = Regex.Replace(input, "\\%RESULT\\%", System.IO.File.ReadAllText(tf), RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
}
diff --git a/Duplicati/Library/Modules/Builtin/SendMail.cs b/Duplicati/Library/Modules/Builtin/SendMail.cs index bc5ca49da..62f3162c3 100644 --- a/Duplicati/Library/Modules/Builtin/SendMail.cs +++ b/Duplicati/Library/Modules/Builtin/SendMail.cs @@ -460,7 +460,7 @@ namespace Duplicati.Library.Modules.Builtin if (input.IndexOf("%RESULT%", StringComparison.OrdinalIgnoreCase) >= 0)
using(TempFile tf = new TempFile())
{
- RunScript.SerializeResult(tf, result);
+ ResultSerializer.SerializeResult(tf, result);
input = Regex.Replace(input, "\\%RESULT\\%", System.IO.File.ReadAllText(tf), RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
}
}
|