Skip to content

Commit

Permalink
Add -dict-import-dir option (#343)
Browse files Browse the repository at this point in the history
Add -dict-import-dir option

This patch adds the -dict-import-dir option that can be used to set the
default search path for the Dictionary Import feature.  If the option is
not set, the current working directory is used instead.

As we no longer have to change the working directory in the
AasxDictionaryImport.Tests, we can also remove the WorkingDirectory
class.
  • Loading branch information
krahlro-sick authored Feb 22, 2021
1 parent a9690b4 commit c674d69
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 53 deletions.
21 changes: 7 additions & 14 deletions src/AasxDictionaryImport.Tests/Cdd/TestModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,45 +120,41 @@ public class GetDefaultPaths
public void MissingDirectory()
{
using var tempDir = TempDir.Create();
using var wd = WorkingDirectory.ChangeTo(tempDir);
var dataProvider = new DataProvider();

Assert.That(dataProvider.FindDefaultDataSources(), Is.Empty);
Assert.That(dataProvider.FindDefaultDataSources(tempDir), Is.Empty);
}

[Test]
public void EmptyDirectory()
{
using var tempDir = TempDir.Create();
using var wd = WorkingDirectory.ChangeTo(tempDir);
var dataProvider = new DataProvider();

var iecCddPath = Path.Combine(tempDir, "iec-cdd");
Directory.CreateDirectory(iecCddPath);

Assert.That(dataProvider.FindDefaultDataSources(), Is.Empty);
Assert.That(dataProvider.FindDefaultDataSources(tempDir), Is.Empty);
}

[Test]
public void EmptySource()
{
using var tempDir = TempDir.Create();
using var wd = WorkingDirectory.ChangeTo(tempDir);
var dataProvider = new DataProvider();

var iecCddPath = Path.Combine(tempDir, "iec-cdd");
Directory.CreateDirectory(iecCddPath);

using var sourcePath = TempDir.Create(iecCddPath);

Assert.That(dataProvider.FindDefaultDataSources(), Is.Empty);
Assert.That(dataProvider.FindDefaultDataSources(tempDir), Is.Empty);
}

[Test]
public void IncompleteSource()
{
using var tempDir = TempDir.Create();
using var wd = WorkingDirectory.ChangeTo(tempDir);
var dataProvider = new DataProvider();

var iecCddPath = Path.Combine(tempDir, "iec-cdd");
Expand All @@ -167,14 +163,13 @@ public void IncompleteSource()
using var source = TempDir.Create(iecCddPath);
CreateEmptyXls(source, GetExportFileName("class", "12345"));

Assert.That(dataProvider.FindDefaultDataSources(), Is.Empty);
Assert.That(dataProvider.FindDefaultDataSources(tempDir), Is.Empty);
}

[Test]
public void ValidSource()
{
using var tempDir = TempDir.Create();
using var wd = WorkingDirectory.ChangeTo(tempDir);
var dataProvider = new DataProvider();

var iecCddPath = Path.Combine(tempDir, "iec-cdd");
Expand All @@ -184,7 +179,7 @@ public void ValidSource()
CreateEmptyXls(source, GetExportFileName("class", "12345"));
CreateEmptyXls(source, GetExportFileName("property", "12345"));

Assert.That(dataProvider.FindDefaultDataSources(), Is.EquivalentTo(new[] {
Assert.That(dataProvider.FindDefaultDataSources(tempDir), Is.EquivalentTo(new[] {
new DataSource(dataProvider, source, Model.DataSourceType.Default),
}));
}
Expand All @@ -193,7 +188,6 @@ public void ValidSource()
public void TwoValidSources()
{
using var tempDir = TempDir.Create();
using var wd = WorkingDirectory.ChangeTo(tempDir);
var dataProvider = new DataProvider();

var iecCddPath = Path.Combine(tempDir, "iec-cdd");
Expand All @@ -207,7 +201,7 @@ public void TwoValidSources()
CreateEmptyXls(source2, GetExportFileName("class", "67890"));
CreateEmptyXls(source2, GetExportFileName("property", "67890"));

Assert.That(dataProvider.FindDefaultDataSources(), Is.EquivalentTo(new[] {
Assert.That(dataProvider.FindDefaultDataSources(tempDir), Is.EquivalentTo(new[] {
new DataSource(dataProvider, source1, Model.DataSourceType.Default),
new DataSource(dataProvider, source2, Model.DataSourceType.Default),
}));
Expand All @@ -217,7 +211,6 @@ public void TwoValidSources()
public void OneValidOneInvalidSource()
{
using var tempDir = TempDir.Create();
using var wd = WorkingDirectory.ChangeTo(tempDir);
var dataProvider = new DataProvider();

var iecCddPath = Path.Combine(tempDir, "iec-cdd");
Expand All @@ -232,7 +225,7 @@ public void OneValidOneInvalidSource()
CreateEmptyXls(source2, GetExportFileName("property", "67890"));
CreateEmptyXls(source2, GetExportFileName("class", "12345"));

Assert.That(dataProvider.FindDefaultDataSources(), Is.EquivalentTo(new[] {
Assert.That(dataProvider.FindDefaultDataSources(tempDir), Is.EquivalentTo(new[] {
new DataSource(dataProvider, source1, Model.DataSourceType.Default),
}));
}
Expand Down
22 changes: 0 additions & 22 deletions src/AasxDictionaryImport.Tests/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,4 @@ public static TempDir Generate(string baseDir)

public static implicit operator string(TempDir tempDir) => tempDir.Path;
}

internal sealed class WorkingDirectory : IDisposable
{
public string Path;

private WorkingDirectory(string path)
{
Path = path;
}

public void Dispose()
{
Directory.SetCurrentDirectory(Path);
}

public static WorkingDirectory ChangeTo(string path)
{
var oldPath = Directory.GetCurrentDirectory();
Directory.SetCurrentDirectory(path);
return new WorkingDirectory(oldPath);
}
}
}
4 changes: 2 additions & 2 deletions src/AasxDictionaryImport/Cdd/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public class DataProvider : Model.DataProviderBase
public override bool IsValidPath(string path) => Parser.IsValidDirectory(path);

/// <inheritdoc/>
protected override IEnumerable<string> GetDefaultPaths()
protected override IEnumerable<string> GetDefaultPaths(string dir)
{
var searchDirectory = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "iec-cdd");
var searchDirectory = System.IO.Path.Combine(dir, "iec-cdd");
if (!System.IO.Directory.Exists(searchDirectory))
return new List<string>();
return System.IO.Directory.GetDirectories(searchDirectory);
Expand Down
17 changes: 11 additions & 6 deletions src/AasxDictionaryImport/Import.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,16 @@ public static class Import
/// null, a new empty admin shell is created in the given environment.
/// </summary>
/// <param name="env">The AAS environment to import into</param>
/// <param name="defaultSourceDir">The search path for the default data sources, e. g. the current working
/// directory</param>
/// <param name="adminShell">The admin shell to import into, or null if a new admin shell should be
/// created</param>
/// <returns>true if at least one submodel was imported</returns>
public static bool ImportSubmodel(AdminShellV20.AdministrationShellEnv env,
AdminShellV20.AdministrationShell? adminShell = null)
string defaultSourceDir, AdminShellV20.AdministrationShell? adminShell = null)
{
adminShell ??= CreateAdminShell(env);
return PerformImport(ImportMode.Submodels, e => e.ImportSubmodelInto(env, adminShell));
return PerformImport(ImportMode.Submodels, defaultSourceDir, e => e.ImportSubmodelInto(env, adminShell));
}

/// <summary>
Expand All @@ -76,17 +78,20 @@ public static bool ImportSubmodel(AdminShellV20.AdministrationShellEnv env,
/// element (usually a submodel).
/// </summary>
/// <param name="env">The AAS environment to import into</param>
/// <param name="defaultSourceDir">The search path for the default data sources, e. g. the current working
/// directory</param>
/// <param name="parent">The parent element to import into</param>
/// <returns>true if at least one submodel element was imported</returns>
public static bool ImportSubmodelElements(AdminShell.AdministrationShellEnv env,
AdminShell.IManageSubmodelElements parent)
string defaultSourceDir, AdminShell.IManageSubmodelElements parent)
{
return PerformImport(ImportMode.SubmodelElements, e => e.ImportSubmodelElementsInto(env, parent));
return PerformImport(ImportMode.SubmodelElements, defaultSourceDir,
e => e.ImportSubmodelElementsInto(env, parent));
}

private static bool PerformImport(ImportMode importMode, Func<Model.IElement, bool> f)
private static bool PerformImport(ImportMode importMode, string defaultSourceDir, Func<Model.IElement, bool> f)
{
var dialog = new ImportDialog(importMode);
var dialog = new ImportDialog(importMode, defaultSourceDir);
if (dialog.ShowDialog() != true || dialog.Context == null)
return false;

Expand Down
4 changes: 2 additions & 2 deletions src/AasxDictionaryImport/ImportDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public string Filter
}
}

public ImportDialog(ImportMode importMode)
public ImportDialog(ImportMode importMode, string defaultSourceDir)
{
DataContext = this;

Expand All @@ -75,7 +75,7 @@ public ImportDialog(ImportMode importMode)
InitializeComponent();

foreach (var provider in DataProviders)
foreach (var source in provider.FindDefaultDataSources())
foreach (var source in provider.FindDefaultDataSources(defaultSourceDir))
ComboBoxSource.Items.Add(source);

DataSourceLabel.Content = String.Join(", ", DataProviders.Select(p => p.Name));
Expand Down
10 changes: 6 additions & 4 deletions src/AasxDictionaryImport/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ public interface IDataProvider
/// Returns a list of all default data sources for this provider, i. e. all data sources that have been shipped
/// with the AASX Package Explorer or that are freely available on the internet.
/// </summary>
/// <param name="dir">The search path for the default data sources, e. g. the current working directory</param>
/// <returns>A list of all default data sources</returns>
IEnumerable<IDataSource> FindDefaultDataSources();
IEnumerable<IDataSource> FindDefaultDataSources(string dir);

/// <summary>
/// Checks whether the given path contains valid data that can be read by this data provider. This method
Expand Down Expand Up @@ -321,9 +322,9 @@ public abstract class DataProviderBase : IDataProvider
public abstract string Name { get; }

/// <inheritdoc/>
public virtual IEnumerable<IDataSource> FindDefaultDataSources()
public virtual IEnumerable<IDataSource> FindDefaultDataSources(string dir)
{
return GetDefaultPaths()
return GetDefaultPaths(dir)
.Where(IsValidPath)
.Select(p => OpenPath(p, Model.DataSourceType.Default))
.ToList();
Expand All @@ -342,8 +343,9 @@ public virtual IDataSource OpenPath(string path)
/// <summary>
/// Returns all paths that could contain a default data source.
/// </summary>
/// <param name="dir">The search path for the default data sources, e. g. the current working directory</param>
/// <returns>A list of all possible default data sources</returns>
protected abstract IEnumerable<string> GetDefaultPaths();
protected abstract IEnumerable<string> GetDefaultPaths(string dir);

/// <summary>
/// Creates a new data source that reads the data stored at the given path and sets the data source type to the
Expand Down
9 changes: 6 additions & 3 deletions src/AasxPackageExplorer/MainWindow.CommandBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1838,9 +1838,11 @@ public void CommandBinding_ImportSubmodel()
try
{
if (ve != null && ve.theEnv != null && ve.theAas != null)
dataChanged = AasxDictionaryImport.Import.ImportSubmodel(ve.theEnv, ve.theAas);
dataChanged = AasxDictionaryImport.Import.ImportSubmodel(ve.theEnv, Options.Curr.DictImportDir,
ve.theAas);
else
dataChanged = AasxDictionaryImport.Import.ImportSubmodel(_packageCentral.Main.AasEnv);
dataChanged = AasxDictionaryImport.Import.ImportSubmodel(_packageCentral.Main.AasEnv,
Options.Curr.DictImportDir);
}
catch (Exception e)
{
Expand Down Expand Up @@ -1881,7 +1883,8 @@ public void CommandBinding_ImportSubmodelElements()
var dataChanged = false;
try
{
dataChanged = AasxDictionaryImport.Import.ImportSubmodelElements(env, submodel);
dataChanged = AasxDictionaryImport.Import.ImportSubmodelElements(env, Options.Curr.DictImportDir,
submodel);
}
catch (Exception e)
{
Expand Down
12 changes: 12 additions & 0 deletions src/AasxWpfControlLibrary/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ public class OptionsInformation
/// </summary>
public string EclassDir = null;

/// <summary>
/// Path to the directory with the default sources for the Dictionary Import feature (see
/// AasxDictionaryImport). If this option is not set, the current working directory is used.
/// </summary>
public string DictImportDir = System.IO.Directory.GetCurrentDirectory();

/// <summary>
/// Path to an image to be displayed as logo
/// </summary>
Expand Down Expand Up @@ -466,6 +472,12 @@ public static void ParseArgs(string[] args, OptionsInformation optionsInformatio
index++;
continue;
}
if (arg == "-dict-import-dir" && morearg > 0)
{
optionsInformation.DictImportDir = args[index + 1];
index++;
continue;
}
if (arg == "-qualifiers" && morearg > 0)
{
optionsInformation.QualifiersFile = args[index + 1];
Expand Down

0 comments on commit c674d69

Please sign in to comment.