How to retrieve the full directory path from Lucene.Net.Store.Directory? - lucene.net

I'm trying to access the directory path from Lucene.Net.Store.Directory, and I can't find any properties where it exists. If I call ToString() I see the path along with the type name, etc. and I would just like the path.

The Lucene.Net.Store.Directory class does not have a DirectoryInfo (or Directory) property since its a abstract class and does not directly access the file system, with even some derived classes not using the file system at all to store the index (take for instance the RAMDirectory).
However, for the Lucene.Net.Store.SimpleFSDirectory directory class, you can access the Directory property, type DirectoryInfo and then its FullName property to get the absolute directory path in use,
var directoryInfo = simpleFSDirectory.Directory;
var fullPath = directoryInfo.FullName;

Related

Powershell Factory Pattern as static method

I am looking to implement a tool that can manage files with a variety of locations; local files, UNC path files, files hosted on AWS, files hosted on a Synology NAS, etc. This will be done in PS 5.1 using classes and a number of specific patterns.
My thinking is I need a base path type, that has a property for the path itself. Then I need classes for UNC paths, local paths, AWS paths etc. that inherit form the base Path class. Then I can have a Copy class that handles the copy differently depending on the method signature. So UNC > UNC is handled differently from AWS > UNC. So, Strategy Pattern.
As for the path types, this seems like a place for the Factory Pattern. And every single example I have found on the internet for the Factory Pattern, in PowerShell, involves a dedicated Factory class. But it seems to me that logically, the Factory to create Path derivatives could/should be part of the Path class itself, as a static method. I searched on static factory method, and the concept is well documented for other languages, but not PowerShell. So I decided to just try it. To that end, I have this as a proof of concept...
class pxPath {
hidden $_path
pxPath () {}
pxPath ([string]$path) {
$this._path = $path
}
static [pxPath] Factory ([String]$path) {
[pxPath]$newPath = [uncFileSystem]::new($path)
return $newPath
}
}
class uncFileSystem : pxPath {
uncFileSystem ([String]$path) : base ([String]$path) {
}
}
$testPath = [pxPath]::Factory('\\Server\folder\file.txt')
$testPath.getType().FullName
$testPath._path
It doesn't have the multiple derived types needed for the Strategy Pattern, it just creates the one derived type. But it proves that a Factory as static method works.
My question is, is there some argument against this that I am not seeing, that explains why I have never seen an example like this for PowerShell? It makes SO much sense to me, and yet I have never seen it done, which really gives me pause.

Matlab code completion for class method in a class folder and namespace

I have a class that is located in a namespace folder, and within this namespace folder it is inside a class folder. Thus, the directory structure is
inpath/+namespace/#ClassName/ClassName.m
I'd like to provide some code suggestions for a method defined in ClassName.m.
I have generated a functionSignature.json file and placed it in
inpath/+namespace/#ClassName/
which did not work, and also in
inpath/+namespace/
which didn't work either.
Furthermore, I have tried the following function names:
"Classname.Methodname"
and
"namespace.ClassName.MethodName"
in both locations, but it did not work.
The answer to this question says that the syntax "ClassName.MethodName" is correct for class methods, but the class was not located in a namespace (and the answer does not say whether the class was defined in a class directory).
To be sure that there are no other errors, here is my original functionSignature.json file (using "namespace.ClassName.MethodName"):
{
"SICM.SICMScan.FromFile":
{
"inputs":
[
{"name":"in1", "kind":"ordered", "type":"file=*.sicm,*.sic,*.ras"}
]
}
}
My questions are:
Where to put this file in the above case?
How to name the method?
it seems that my initial hunch is correct: the functionSignature.json file needs to be in whichever directory you add to the MATLAB path to use the functions. Since namespace directories are not directly added to the path, but their parent directory is, the functionSignature.json directory needs to be in the parent directory of the namespace directory.

Create an object within constructor of another object

I want to modify the ACL of a specific folder. To do this, I need to remove the inherited FileSystemAccessRules, and change them to ReadAndExecute permissions. To do this, I decided to use the ModifyAccessRule() method.
My problem is that I need to use the ModifyAccessRule method, and as part of the constructor, I need a FileSystemAccessRule object that would take the same input.
I am aware that this could be simply solved by not using a pipeline, but I am trying to keep it efficient and within one, hence why no external variables are being used (with the exception of the initial path).
Get-Item $folder_path |
%{$_.GetAccessControl()} |
%{$_.ModifyAccessRule(
"Add",
New-Object System.Security.AccessControl.FileSystemAccessRule(
$_.Access.IdentityReference,
[System.Security.AccessControl.FileSystemRights]::ReadAndExecute,
[System.Security.AccessControl.InheritanceFlags]::ContainerInherit,
[System.Security.AccessControl.PropagationFlags]::None,
[System.Security.AccessControl.AccessControlType]::Allow
)
)}
This is my code at the moment, but it does not work, due to New-Object not being executable inside a constructor. I have searched for a possible replacement, but have not found anything. As you can see, though, all the parts necessary for this new object are available.
Is there some type of format or method I could use to create this FileSystemAccessRule object within another object's constructor?
PS: As a bonus, can anyone confirm that PropagationFlags and InheritanceFlags are at the right values for the folder to not inherit parent permissions but propagate to children? ^-^

InputStream to a list in Scala

I am trying to read a file as input stream and then convert the contents of the file into a list in scala. Here is my code
val fileStream = getClass.getResourceAsStream("src/main/scala-2.11/com/dc/returnsModel/constants/abc.txt")
val item_urls = Source.fromInputStream(fileStream).getLines.toList
This does now work. I get a NullPointer Exception.
How do I correct this?
However, this works(but I cant use it in a JAR File)
val item_urls = Source.fromFile("src/main/scala-2.11/com/dc/returnsModel/constants/aa.txt").getLines.toList
getClass.getResourceAsStream does not expect a full path, it searches for the requested file in the classpath using the same class loader as the current class.
Fixing this depends a bit on the structure of your project and class that calls this code:
If the class returned by getClass is in the same package as the file you're trying to load (com.dc.returnsModel.constants), then you should simply reference the file name only:
getClass.getResourceAsStream("abc.txt")
If the class returned by getClass resides in a different package, path should start with a / which represents the root of the classpath, hence the package name must follow:
getClass.getResourceAsStream("/com/dc/returnsModel/constants/abc.txt")
You have to provide the correct path starting from root.
Here root is start of your src/scala-2.11 folder in your case.
One example
object SO extends App {
val resourceStream = SO.getClass.getResourceAsStream("/com/sm.txt")
println(Source.fromInputStream(resourceStream).getLines.toList)
}

How do I create an IEditorInput instance from an IFile instance?

I'm currently implementing a custom MultiPageEditorPart where I want to have multiple pages when editing file type .xxx (so that file with same base name and .yyy appears in another page).
I have obtained the IFile reference of the .yyy file corresponding to the .xxx file, and now wish to instanciate a new TextEditor instance but it requires an IEditorInput instance for initialization.
How do I create an instance of IEditorInput using my IFile instance? I also of course have access to the IProject and the usual Eclipse classes.
Try
new FileEditorInput(iFile)