Skip to content

Commit

Permalink
Merge pull request #9 from jdunkerley/DateTimeParser
Browse files Browse the repository at this point in the history
Date time parser
  • Loading branch information
jdunkerley committed Mar 1, 2016
2 parents 37dea9e + 90a563a commit 9041557
Show file tree
Hide file tree
Showing 20 changed files with 673 additions and 38 deletions.
21 changes: 21 additions & 0 deletions AlteryxDateParser/AlteryxDateParser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Attributes\InputPropertyNameAttribute.cs" />
<Compile Include="Attributes\OrderingAttribute.cs" />
<Compile Include="DateTimeParserTool.cs" />
<Compile Include="Framework\BaseEngine.cs" />
<Compile Include="Framework\FieldDescription.cs" />
<Compile Include="Framework\InputFieldTypeConverter.cs" />
<Compile Include="Framework\InputProperty.cs" />
<Compile Include="Framework\BaseTool.cs" />
<Compile Include="Attributes\CharLabelAttribute.cs" />
Expand Down Expand Up @@ -90,7 +94,24 @@
<ItemGroup>
<EmbeddedResource Include="CircuitBreakerTool.png" />
</ItemGroup>
<ItemGroup>
<None Include="Install.bat">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Uninstall.bat">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Framework\BaseInput.png" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="DateTimeParserTool.png" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>echo "$(TargetDir)Install.bat"</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
49 changes: 49 additions & 0 deletions AlteryxDateParser/Attributes/InputPropertyNameAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace JDunkerley.Alteryx.Attributes
{
using System;
using System.Xml;

/// <summary>
/// Specifies associated input field for configs
/// </summary>
public class InputPropertyNameAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="InputPropertyNameAttribute"/> class.
/// </summary>
/// <param name="fieldName">Name of the field.</param>
/// <param name="engineType">Type of the engine</param>
/// <param name="fieldTypes">List of valid types (defaults to all)</param>
public InputPropertyNameAttribute(string fieldName, Type engineType, params AlteryxRecordInfoNet.FieldType[] fieldTypes)
{
this.FieldName = fieldName;
this.EngineType = engineType;

this.FieldTypes = fieldTypes;
if (fieldTypes.Length == 0)
{
this.FieldTypes = (AlteryxRecordInfoNet.FieldType[])Enum.GetValues(typeof(AlteryxRecordInfoNet.FieldType));
}
}

/// <summary>
/// Gets the name of the field.
/// </summary>
public string FieldName { get; }

/// <summary>
/// Gets the type of the engine.
/// </summary>
public Type EngineType { get; }

/// <summary>
/// Gets the field types list.
/// </summary>
public AlteryxRecordInfoNet.FieldType[] FieldTypes { get; }

/// <summary>
/// Gets or sets the current meta data.
/// </summary>
public static XmlElement[] CurrentMetaData { get; set; }
}
}
11 changes: 3 additions & 8 deletions AlteryxDateParser/CircuitBreakerTool.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
namespace JDunkerley.Alteryx
{
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms.VisualStyles;

using AlteryxGuiToolkit.Gallery;
using AlteryxGuiToolkit.Plugins;

using AlteryxRecordInfoNet;
Expand All @@ -30,7 +25,7 @@ public class Config
/// <summary>
/// Engine for Circuit Breaker
/// </summary>
/// <seealso cref="JDunkerley.Alteryx.Framework.BaseEngine{JDunkerley.Alteryx.CircuitBreakerTool.Config}" />
/// <seealso cref="JDunkerley.Alteryx.Framework.BaseEngine{Config}" />
public class Engine : BaseEngine<Config>
{
private Queue<Record> _inputRecords;
Expand Down Expand Up @@ -90,7 +85,7 @@ public Engine()
{
return false;
}

if (this.Breaker.State == ConnectionState.Closed)
{
this.Output?.PushRecord(r);
Expand All @@ -116,7 +111,7 @@ public Engine()

[CharLabel('B')]
[Ordering(1)]
public InputProperty Breaker { get; }
public InputProperty Breaker { get; }

[CharLabel('I')]
[Ordering(2)]
Expand Down
13 changes: 7 additions & 6 deletions AlteryxDateParser/DateTimeInputTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/// <summary>
/// Simple Date Time Input Control
/// </summary>
public class DateTimeInputTool :
public class DateTimeInputTool :
BaseTool<DateTimeInputTool.Config, DateTimeInputTool.Engine>, IPlugin
{
public enum DateToReturn
Expand Down Expand Up @@ -65,12 +65,13 @@ public class Engine : BaseEngine<Config>
public override bool PI_PushAllRecords(long nRecordLimit)
{
var config = this.GetConfigObject();
string outputFieldName = config?.OutputFieldName ?? "Date";
string fieldName = config?.OutputFieldName ?? "Date";

var recordInfo = new RecordInfo();
recordInfo.AddField(outputFieldName, config?.ReturnDateTime ?? false ? FieldType.E_FT_DateTime : FieldType.E_FT_Date);
var recordInfo =
Utilities.CreateRecordInfo(
new FieldDescription(fieldName, config?.ReturnDateTime ?? false ? FieldType.E_FT_DateTime : FieldType.E_FT_Date));

this.Output?.Init(recordInfo, "Output", null, this.XmlConfig);
this.Output?.Init(recordInfo, nameof(this.Output), null, this.XmlConfig);
if (nRecordLimit == 0)
{
this.Engine?.OutputMessage(this.NToolId, MessageStatus.STATUS_Complete, "");
Expand Down Expand Up @@ -102,7 +103,7 @@ public override bool PI_PushAllRecords(long nRecordLimit)
}

var recordOut = recordInfo.CreateRecord();
recordInfo.GetFieldByName(outputFieldName, false)?.SetFromString(recordOut, dateOutput.ToString("yyyy-MM-dd HH:mm:ss"));
recordInfo.GetFieldByName(fieldName, false)?.SetFromString(recordOut, dateOutput.ToString("yyyy-MM-dd HH:mm:ss"));
this.Output?.PushRecord(recordOut.GetRecord());
this.Output?.UpdateProgress(1.0);
this.Output?.OutputRecordCount(true);
Expand Down
165 changes: 165 additions & 0 deletions AlteryxDateParser/DateTimeParserTool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
namespace JDunkerley.Alteryx
{
using System;
using System.ComponentModel;
using System.Globalization;

using AlteryxGuiToolkit.Plugins;

using AlteryxRecordInfoNet;

using JDunkerley.Alteryx.Attributes;
using JDunkerley.Alteryx.Framework;

public class DateTimeParserTool :
BaseTool<DateTimeParserTool.Config, DateTimeParserTool.Engine>, IPlugin
{
public enum OutputType
{
Date,
DateTime,
String
}

public class Config
{
/// <summary>
/// Gets or sets the type of the output.
/// </summary>
public OutputType OutputType { get; set; }

/// <summary>
/// Gets or sets the name of the output field.
/// </summary>
public string OutputFieldName { get; set; } = "Date";

/// <summary>
/// Gets or sets the name of the input field.
/// </summary>
[TypeConverter(typeof(InputFieldTypeConverter))]
[InputPropertyName(nameof(Engine.Input), typeof(Engine))]
public string InputFieldName { get; set; } = "DateInput";

/// <summary>
/// Gets or sets the input format.
/// </summary>
public string InputFormat { get; set; }

/// <summary>
/// ToString used for annotation
/// </summary>
/// <returns></returns>
public override string ToString() => $"{this.InputFieldName}=>{this.OutputFieldName}";

/// <summary>
/// Create a FieldDescription Object
/// </summary>
/// <returns></returns>
public FieldDescription OutputDescription()
{
switch (this.OutputType)
{
case OutputType.Date:
return new FieldDescription(this.OutputFieldName, FieldType.E_FT_Date);
case OutputType.DateTime:
return new FieldDescription(this.OutputFieldName, FieldType.E_FT_DateTime);
case OutputType.String:
return new FieldDescription(this.OutputFieldName, FieldType.E_FT_String)
{
Size = 19,
Source = nameof(DateTimeParserTool),
Description = $"{this.InputFieldName} parsed as a DateTime"
};
}

return null;
}

}

public class Engine : BaseEngine<Config>
{
private FieldBase _inputFieldBase;

private RecordCopier _copier;

private RecordInfo _outputRecordInfo;

private FieldBase _outputFieldBase;

public Engine()
{
this.Input = new InputProperty(
initFunc: this.InitFunc,
progressAction: d => this.Output.UpdateProgress(d),
pushFunc: this.PushFunc);
}

private bool InitFunc(RecordInfo info)
{
var config = this.GetConfigObject();
var fieldDescription = config.OutputDescription();
if (fieldDescription == null)
{
return false;
}

this._inputFieldBase = info.GetFieldByName(config.InputFieldName, false);
if (this._inputFieldBase == null)
{
return false;
}

var newRecordInfo = Utilities.CreateRecordInfo(info, fieldDescription);

this._outputRecordInfo = newRecordInfo;
this._outputFieldBase = newRecordInfo.GetFieldByName(config.OutputFieldName, false);
this.Output?.Init(newRecordInfo, nameof(this.Output), null, this.XmlConfig);

// Create the Copier
this._copier = Utilities.CreateCopier(info, newRecordInfo, config.OutputFieldName);

return true;
}

private bool PushFunc(RecordData r)
{
var fmt = this.GetConfigObject().InputFormat;

var record = this._outputRecordInfo.CreateRecord();
this._copier.Copy(record, r);

string input = this._inputFieldBase.GetAsString(r);

DateTime dt;
bool result = string.IsNullOrWhiteSpace(fmt)
? DateTime.TryParse(input, CultureInfo.CurrentCulture, DateTimeStyles.AllowWhiteSpaces, out dt)
: DateTime.TryParseExact(input, fmt, CultureInfo.CurrentCulture, DateTimeStyles.AllowWhiteSpaces, out dt);

if (result)
{
this._outputFieldBase.SetFromString(record, dt.ToString("yyyy-MM-dd HH:mm:ss"));
}
else
{
this._outputFieldBase.SetNull(record);
}

this.Output?.PushRecord(record.GetRecord());
return true;
}

/// <summary>
/// Gets the input stream.
/// </summary>
[CharLabel('I')]
public InputProperty Input { get; }

/// <summary>
/// Gets or sets the output stream.
/// </summary>
[CharLabel('O')]
public PluginOutputConnectionHelper Output { get; set; }
}
}
}
Binary file added AlteryxDateParser/DateTimeParserTool.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion AlteryxDateParser/Framework/BaseEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public abstract class BaseEngine<T> : INetPlugin
private readonly Dictionary<string, PropertyInfo> _outputs;

/// <summary>
/// Initializes a new instance of the <see cref="InputProperty{T}"/> class.
/// Initializes a new instance of the <see cref="BaseEngine{T}"/> class.
/// </summary>
protected BaseEngine()
{
Expand Down
Binary file added AlteryxDateParser/Framework/BaseInput.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions AlteryxDateParser/Framework/BaseTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/// <typeparam name="TConfig">The type of the config object</typeparam>
/// <typeparam name="TEngine">The type of the engine.</typeparam>
/// <seealso cref="AlteryxGuiToolkit.Plugins.IPlugin" />
public abstract class BaseTool<TConfig, TEngine>
public abstract class BaseTool<TConfig, TEngine>
where TConfig: new()
{
private readonly Lazy<Image> _icon;
Expand Down Expand Up @@ -52,7 +52,7 @@ private Image GetEmbeddedImage()
.Select(n => ass.GetManifestResourceStream(n))
.FirstOrDefault()
?? ass.GetManifestResourceNames()
.Where(n => n.Contains("BaseTool") && n.EndsWith(".png"))
.Where(n => n.Contains(this._inputConnections.Length == 0 ? "BaseInput" : "BaseTool") && n.EndsWith(".png"))
.Select(n => ass.GetManifestResourceStream(n))
.First();

Expand Down
Binary file modified AlteryxDateParser/Framework/BaseTool.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 9041557

Please sign in to comment.