Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align features to cover those of old File_Adapter; renaming to File_Adapter #86

Merged
merged 27 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
7d5e7e2
Renaming done.
alelom Oct 15, 2020
9d2311d
Renamed folders.
alelom Oct 16, 2020
6c4e4cf
Solution name brought back to make CI happy.
alelom Oct 16, 2020
b413907
Adding missing Versioning jsons on Engine and oM
alelom Oct 16, 2020
9730be7
Modified as per comment
alelom Oct 28, 2020
9bf318e
Merge branch 'master' into Filing_Toolkit-79-renaming
alelom Oct 30, 2020
5bc3161
Correction to Push.
alelom Nov 2, 2020
97d67b5
Aligned to base BHoMAdapter changes.
alelom Nov 3, 2020
324fa93
Attempting to make BSON work
alelom Nov 3, 2020
94df8cc
Removed support for BSON for the time being. Didn't work.
alelom Nov 3, 2020
692dd05
Correction to allow `Type` as a valid request.
alelom Nov 4, 2020
0b62c54
Removed PushConfig.PushContentOnly
alelom Nov 4, 2020
b4ca14a
Corrected check in WalkDirectories
alelom Nov 5, 2020
b231dca
Allowing to chain File/Directory; minor tidy up.
alelom Nov 5, 2020
cf5f3db
Required changes
alelom Nov 6, 2020
df412ac
Bug fix: if targeting single file, content was pushed twice
alelom Nov 6, 2020
7149000
Update ReadFileContent.cs
alelom Nov 6, 2020
a1dfba6
Fixing append behaviour
alelom Nov 6, 2020
14f90d2
Switched from Regex to Wildcards only
alelom Nov 9, 2020
4904463
Adding `Modify.ProcessFileDirRequest(fdrCopy, out wildcardPattern)`
alelom Nov 9, 2020
1aef881
Removing last unescaped \ from attributes
alelom Nov 9, 2020
9664f1a
Update File_Adapter/FileAdapter.cs
alelom Nov 10, 2020
2ac5b30
Bug fix (not adding created files to the output of push)
alelom Nov 10, 2020
8aab97b
Update CreateJson.cs
alelom Nov 10, 2020
51454c5
Update CreateJson.cs
alelom Nov 10, 2020
3526ba7
WalkDirectories to check if rquest location points to single file
alelom Nov 10, 2020
090d8f2
Rename solution and `.ci` files renamings
alelom Nov 10, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.Engine.Adapters.Filing;
using BH.Engine.Adapters.File;
using BH.Engine.Reflection;
using BH.oM.Adapter;
using BH.oM.Data.Requests;
using BH.oM.Adapters.Filing;
using BH.oM.Adapters.File;
using System;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -37,9 +37,9 @@
using BH.oM.Reflection;
using BH.oM.Reflection.Attributes;

namespace BH.Adapter.Filing
namespace BH.Adapter.File
{
public partial class FilingAdapter
public partial class FileAdapter
{
/***************************************************/
/**** Methods *****/
Expand All @@ -52,7 +52,7 @@ public override oM.Reflection.Output<List<object>, bool> Execute(IExecuteCommand
if (command == null)
return new Output<List<object>, bool>();

oM.Adapters.Filing.ExecuteConfig executeConfig = actionConfig as oM.Adapters.Filing.ExecuteConfig ?? new ExecuteConfig();
oM.Adapters.File.ExecuteConfig executeConfig = actionConfig as oM.Adapters.File.ExecuteConfig ?? new ExecuteConfig();

if (m_Execute_enableWarning && !executeConfig.DisableWarnings)
{
Expand Down
95 changes: 95 additions & 0 deletions File_Adapter/AdapterActions/Pull.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2020, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.Engine.Adapters.File;
using BH.Engine.Reflection;
using BH.oM.Adapter;
using BH.oM.Data.Requests;
using BH.oM.Adapters.File;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BH.Engine.Base;

namespace BH.Adapter.File
{
public partial class FileAdapter
{
/***************************************************/
/**** Methods *****/
/***************************************************/

public override bool SetupPullRequest(object request, out IRequest pullRequest)
{
pullRequest = request as IRequest;

// If there is no input request, but a target filepath was specified through the Adapter constructor, use that.
if (!string.IsNullOrWhiteSpace(m_defaultFilePath) && (request == null || request is Type))
{
if (request is Type)
{
pullRequest = new FileContentRequest() { File = m_defaultFilePath, Types = new List<Type>() { request as Type } };
BH.Engine.Reflection.Compute.RecordNote($"Type interpreted as new {nameof(FileContentRequest)} targeting the Adapter targetLocation: `{m_defaultFilePath}` and filtering per type `{(request as Type).Name}`.");
}
else if (request is IEnumerable<Type>)
{
pullRequest = new FileContentRequest() { File = m_defaultFilePath, Types = (request as IEnumerable<Type>).ToList() };
BH.Engine.Reflection.Compute.RecordNote($"Type interpreted as new {nameof(FileContentRequest)} targeting the Adapter targetLocation: `{m_defaultFilePath}` and filtering per types: {String.Join(", ",(request as IEnumerable<Type>).Select(t => t.Name))}.");
}
else
{
pullRequest = new FileContentRequest() { File = m_defaultFilePath };
BH.Engine.Reflection.Compute.RecordNote($"Pulling file contents from the Adapter targetLocation: `{m_defaultFilePath}`.");
}

return true;
}

if (request == null && string.IsNullOrWhiteSpace(m_defaultFilePath))
{
BH.Engine.Reflection.Compute.RecordError($"Please specify a valid Request, or create the {nameof(FileAdapter)} with the constructor that takes inputs to specify a target Location.");
return false;
}

if ((request as IRequest) != null && !string.IsNullOrWhiteSpace(m_defaultFilePath))
{
BH.Engine.Reflection.Compute.RecordWarning($"Both request and targetLocation have been specified. Requests take precedence. Pulling as specified by the input `{request.GetType().Name}`.");
return true;
}

return base.SetupPullRequest(request, out pullRequest);
}

public override IEnumerable<object> Pull(IRequest request, PullType pullType = PullType.AdapterDefault, ActionConfig actionConfig = null)
{
return Read(request as dynamic, actionConfig as PullConfig ?? new PullConfig());
}

/***************************************************/

}
}
171 changes: 171 additions & 0 deletions File_Adapter/AdapterActions/Push.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2020, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.Engine.Adapters.File;
using BH.Engine.Reflection;
using BH.oM.Adapter;
using BH.oM.Data.Requests;
using BH.oM.Adapters.File;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BH.Engine.Base;
using BH.oM.Base;

namespace BH.Adapter.File
{
public partial class FileAdapter
{
/***************************************************/
/**** Methods *****/
/***************************************************/

public override bool SetupPushType(PushType pushType, out PushType pt)
{
pt = pushType;

if (pushType == PushType.AdapterDefault)
pt = m_AdapterSettings.DefaultPushType;

if (pushType == PushType.FullPush)
{
BH.Engine.Reflection.Compute.RecordError($"The specified {nameof(PushType)} {nameof(PushType.FullPush)} is not supported.");
return false;
}

return true;
}

public override bool SetupPushConfig(ActionConfig actionConfig, out ActionConfig pushConfig)
{
PushConfig pushCfg = actionConfig as PushConfig ?? new PushConfig();
pushConfig = pushCfg;

if (pushCfg.BeautifyJson && pushCfg.UseDatasetSerialization)
{
BH.Engine.Reflection.Compute.RecordError($"Input `{nameof(PushConfig.BeautifyJson)}` and `{nameof(PushConfig.UseDatasetSerialization)}` cannot be both set to True.");
return false;
}

return true;
}

public override List<object> Push(IEnumerable<object> objects, string tag = "", PushType pushType = PushType.AdapterDefault, ActionConfig actionConfig = null)
{
PushConfig pushConfig = actionConfig as PushConfig;

if (string.IsNullOrWhiteSpace(m_defaultFilePath)) // = if we are about to push multiple files/directories
if (pushType == PushType.DeleteThenCreate && m_Push_enableDeleteWarning && !pushConfig.DisableWarnings )
{
BH.Engine.Reflection.Compute.RecordWarning($"You have selected the {nameof(PushType)} {nameof(PushType.DeleteThenCreate)}." +
$"\nThis has the potential of deleting files and folders with their contents." +
$"\nMake sure that you know what you are doing. This warning will not be repeated." +
$"\nRe-enable the component to continue.");

m_Push_enableDeleteWarning = false;

return new List<object>();
}

List<IResource> createdFiles = new List<IResource>();

List<IResource> filesOrDirs = objects.OfType<IResource>().Select(fd => fd.GetShallowClone() as IResource).ToList();
List<object> remainder = objects.Where(o => !(o is IResource)).ToList();

if (remainder.Any())
{
if (filesOrDirs.Any())
{
BH.Engine.Reflection.Compute.RecordError($"Input objects are both of type `{nameof(BH.oM.Adapters.File.File)}`/`{nameof(BH.oM.Adapters.File.Directory)}` and generic objects." +
$"\nIn order to push them:" +
$"\n\t- for the `{nameof(BH.oM.Adapters.File.File)}`/`{nameof(BH.oM.Adapters.File.Directory)}` objects, use a Push using a {nameof(FileAdapter)} with no targetLocation input;"+
$"\n\t- for the generic objects, use a Push using a {nameof(FileAdapter)} that specifies a targetLocation.");
return null;
}
else if (string.IsNullOrWhiteSpace(m_defaultFilePath))
{
BH.Engine.Reflection.Compute.RecordError($"To Push objects that are not of type `{nameof(BH.oM.Adapters.File.File)}` or `{nameof(BH.oM.Adapters.File.Directory)}`," +
$"\nyou need to specify a target Location by creating the {nameof(FileAdapter)} through the constructor with inputs.");
return null;
}
}

if (m_defaultFilePath != null)
{
if (filesOrDirs.Any())
BH.Engine.Reflection.Compute.RecordWarning($"A `targetLocation` has been specified in the File_Adapter constructor." +
$"\nObjects of type `{nameof(BH.oM.Adapters.File.File)}` or `{nameof(BH.oM.Adapters.File.Directory)}` will be appended to the file at `targetLocation`." +
$"\nIf you want to target multiple files, you need create the {nameof(FileAdapter)} through the constructor without inputs.");
}

foreach (IResource fileOrDir in filesOrDirs)
{
if (fileOrDir == null)
continue;

if (fileOrDir is IFile)
{
string extension = Path.GetExtension(fileOrDir.IFullPath());

if (string.IsNullOrWhiteSpace(extension))
{
BH.Engine.Reflection.Compute.RecordNote($"File {fileOrDir.IFullPath()} has no extension specified. Defaults to JSON.");
extension = ".json";
fileOrDir.Name += extension;
}

if (extension != ".json")
{
BH.Engine.Reflection.Compute.RecordWarning($"Cannot create File {fileOrDir.IFullPath()}. Currently only JSON extension is supported.");
continue;
}
}

IResource created = Create(fileOrDir as dynamic, pushType, pushConfig);
createdFiles.Add(created);
}

if (remainder.Any())
{
string defaultDirectory = Path.GetDirectoryName(m_defaultFilePath);
string defaultFileName = Path.GetFileName(m_defaultFilePath);

FSFile file = CreateFSFile(defaultDirectory, defaultFileName, remainder);

IResource created = Create(file, pushType, pushConfig);

if (created != null)
filesOrDirs.Add(created);
}

return createdFiles.OfType<object>().ToList();
}

/***************************************************/

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.Engine.Adapters.Filing;
using BH.Engine.Adapters.File;
using BH.Engine.Reflection;
using BH.oM.Adapter;
using BH.oM.Data.Requests;
using BH.oM.Adapters.Filing;
using BH.oM.Adapters.File;
using System;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -35,9 +35,9 @@
using System.Threading.Tasks;
using BH.Engine.Base;

namespace BH.Adapter.Filing
namespace BH.Adapter.File
{
public partial class FilingAdapter
public partial class FileAdapter
{
/***************************************************/
/**** Methods *****/
Expand All @@ -52,7 +52,7 @@ public override int Remove(IRequest request, ActionConfig actionConfig = null)
return 0;
}

oM.Adapters.Filing.RemoveConfig removeConfig = actionConfig as oM.Adapters.Filing.RemoveConfig ?? new RemoveConfig();
oM.Adapters.File.RemoveConfig removeConfig = actionConfig as oM.Adapters.File.RemoveConfig ?? new RemoveConfig();

if (m_Remove_enableDeleteWarning && !removeConfig.DisableWarnings)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,42 @@
*/

using BH.oM.Base;
using MongoDB.Bson.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using BH.Engine.Serialiser;
using BH.oM.Adapter;
using BH.Engine.Adapters.Filing;
using BH.oM.Adapters.Filing;
using BH.Engine.Adapters.File;
using BH.oM.Adapters.File;

namespace BH.Adapter.Filing
namespace BH.Adapter.File
{
public partial class FilingAdapter : BHoMAdapter
public partial class FileAdapter : BHoMAdapter
{
/***************************************************/
/**** Public Methods ****/
/***************************************************/

public static IFSContainer Create(ILocatableResource resource, PushType pushType, PushConfig pushConfig)
public IFSContainer Create(ILocatableResource resource, PushType pushType, PushConfig pushConfig)
{
if (resource == null)
return null;
return null;

IFSContainer fileOrDir = resource.ToFiling();
return Create(fileOrDir, pushType, pushConfig);
return Create(fileOrDir, pushType, pushConfig);
}

/***************************************************/

public static IFSContainer Create(IFSContainer fileOrDir, PushType pushType, PushConfig pushConfig)
public IFSContainer Create(IFSContainer fileOrDir, PushType pushType, PushConfig pushConfig)
{
pushConfig = pushConfig ?? new PushConfig();

if (fileOrDir == null)
return null;

string extension = Path.GetExtension(fileOrDir.IFullPath());

if (extension == ".json")
return CreateJson((FSFile)fileOrDir, pushType, pushConfig) as IFSContainer;

if (extension == ".bson")
if (fileOrDir is IFile)
return CreateJson((FSFile)fileOrDir, pushType, pushConfig) as IFSContainer;

if (fileOrDir is IDirectory)
Expand Down
Loading