Access resources inside library - scala

I have a project L which is published as a library.
Inside L, there is a resources folder and a snippet of code that access the content.
I created a second module in L to add a main as a test, and it works.
Now when I include L into my other project, it doesnt find the folder located inside the resources of L.
in all the below path is equal to folder_i_am_looking_for and I generate varieties with /$path, $path/, $path and /$path/. ( I am desperate at this point )
I tried:
getClass.getClassLoader.getResource(path).getPath,
getClass.getClassLoader.getResource(path).toExternalForm,
getClass.getResource(path).getPath,
getClass.getResource(path).toExternalForm,
as well as
val location: URL = this.getClass.getProtectionDomain.getCodeSource.getLocation
Paths
.get(location.toURI)
.resolve("../classes/" + path)
.normalize()
.toFile
.getAbsoluteFile
.toString
from https://stackoverflow.com/a/54142960
and for each generated path, I've tried to :
input ::
input.split('!').last ::
input.split("file:/").last ::
input.split("file:").last ::
Nil
from https://stackoverflow.com/a/5346020
each time , i get path that looks like :
/path/to/projectFolder/jar:file:/path/to/library/lib_2.11-version.jar!/folder
I let you imagine all the variety of different path I get with all the above operations.
None of them find the directory.
I am testing all path simultaneously with ZIO.validateFirstPar so if you have any idea, I can add more to test.
Thank you.

Related

Memory alias between inherited and super classes in Python

I build an inherited class (Folder) which creates an instance of itself. The problem is: the new instance which it creates copies the super class memory. I don't want this to enable recursive folder exploration. Let's say:
class FileOperator():
__dir = '.'
def __init__(self, dir:str):
self.__dir = dir
def get_dir(self):
return self.__dir
class Folder(FileOperator):
__sub_folders = {}
__files = {}
def __init__(self, dir:str):
super().__init__(dir)
def new_folder(self, name:str):
new_dir = self.get_dir() + name + '/'
folder = Folder(new_dir)
self.__sub_folders[name] = folder
return folder
def get_folder(self, name:str)
return self.__sub_folders[name]
This way we want to build new folders with same names under different folders and each instance would have different list of folders. Such as:
f = folder.get_folder('Data1').get_folder('subData1').get_folder('Data1')
When we do:
folder = Folder('./demo_data/')
sub_folder = folder.new_folder('demo_sub')
it creates a new sub_folder which has the same subfolders with its super's. sub_folder includes itself in its __sub_folders dictionary. When we try to reach a specific folder (e.g.: under categoryA, itemB), it confuses the other files and folders with same name in the dictionary. What it should do is to distinguish the two different subfolders with same name in different root folder.
So, we are not able to separate different sub folders when we access them with their names in the dictionary because each instance has the same list of subfolders generated so far. Therefore it confuses the names of different folder instances.
We need separated instances to access different folders with same name.
Should we use deepcopy here? (if yes, how?) Or should we have another meta class that controls the system with IoC (inversion of control)?

Gatling where to place JSON files?

I'm having issues with loading a JSON file in Gatling. It works with an absolute path but not with a relative. Where should JSON files be stored? I've tried /home/dev/gatling-charts-highcharts-bundle-2.3.0/user-files/data but the file could not be found.
Piece of my code:
def addCredential(status_code: Option[Seq[Int]], username: Option[String]) = {
feed(random_user)
.exec(http("[POST] /users/[user]/credentials")
.post("/users/%s/credentials".format(username getOrElse "${username}"))
.body(RawFileBody("credential.json")).asJSON
.check(status.in(202, 404, 409)))
}
The file credential.json can be found if I give the absolute path but this is not optimal because several people use the simulations.
You can configure the folder where the bodies are located in gatling.conf.
directory {
bodies = user-files/bodies # Folder where bodies are located
}
Then you can put your file in the configured path /your-project/user-files/bodies/credential.json.

How to source from online cpp open source like github from Rstudio?

I am trying to source code that exists on an online repository by:
Rcpp::sourceCpp(
url("https://github.com/slwu89/MCMC/blob/master/adaptMCMC_source.cpp")
)
I'm encounter this issue:
Error in dirname(file) : a character vector argument expected
Just use R's download.file():
library(Rcpp)
remurl <- "https://github.com/slwu89/MCMC/blob/master/adaptMCMC_source.cpp"
locfile <- "/tmp/mcmc.cpp"
download.file(url=remurl, destfile=locfile)
sourceCpp(locfile) # dozens of error for _this_ file
Edit Here is a better way with two important fixes:
You need a different URL. The one you list will download the html page. But you want raw source code, which in this case is https://raw.githubusercontent.com/slwu89/MCMC/master/adaptMCMC_source.cpp
You can create a simple helper function which takes the url, creates a tempfile with extension .cpp (hey, that argument once was my patch to base R ;-) and then returns that file name.
See below:
u2f <- function(url) {
tf <- tempfile()
download.file(url, tf, quiet=TRUE)
tf
}
library(Rcpp)
url <- "https://raw.githubusercontent.com/slwu89/MCMC/master/adaptMCMC_source.cpp"
sourceCpp( u2f( url ) )
and that compiles fine (albeit with warnings about signed/unsigned comparison).

Cannot Use Relative Path in Scala.IO.Source

I am trying to read a CSV file into Scala. I can read fine using the absolute path, but would like to be able to use the relative path.
val filename = "sample_data/yahoo_finance/AAPL/AAPL_Historical.csv"
for (line <- Source.fromFile(filename).getLines()) { println(line) }
throws the error:
java.io.FileNotFoundException: sample_data\yahoo_finance\AAPL\AAPL_Historical.csv
(The system cannot find the path specified)
However:
val filename = "C:/Users/hansb/Desktop/Scala Project/src/main/" +
"resources/sample_data/yahoo_finance/AAPL/AAPL_Historical.csv"
for (line <- Source.fromFile(filename).getLines()) { println(line) }
works just fine.
My understanding was that scala.io.Source knew to look in the resources folder for the relative path.
What am I missing?
Working code using Phasmid's suggestion:
val relativePath = "/sample_data/yahoo_finance/AAPL/AAPL_Historical.csv"
val csv = getClass.getResource(relativePath)
for (line <- Source.fromURL(csv).getLines()){ println(line) }
This is one of the worst things about Java (and, thus, Scala). I imagine many millions of hours have been spent on this kind of problem.
If you want to get a resource from a relative path (i.e. from the class path) you need to treat the resource as a resource. So, something like the following:
getClass.getResource("AAPL_Historical.csv")
while yields a URL which can then convert into a Stream, or whatever. This form will expect to find the resource in the same (relative) folder as the class, but in the resources rather than scala directory.
If you want to put the resource into the top level of the resources folder, then use:
getClass.getResource("/AAPL_Historical.csv")
It may be that there is some other magic which works but I haven't found it.

Dynamically Fill Jenkins Job Parameter with Github Directory Files

I'm aware of the Dynamic Parameter and Dynamic Extended Choice Parameter plugins. I'm looking for a way to list of text files in a directory in github as drop down list parameter. is there a groovy script or similar that can populate the dropdown with file names?
You can use the github api to fetch a list of files for a given path on a given repo.
So for example, to look inside the ratpack-groovy/src/main/java/ratpack/groovy folder of the ratpack project on github, you can do:
import groovy.json.*
def contents = new JsonSlurper().parse('https://api.github.com/repos/ratpack/ratpack/contents/ratpack-groovy/src/main/java/ratpack/groovy'.toURL())
.sort { it.type } // Directories first
.collect { it.name + (it.type == 'dir' ? '/' : '') } // Put a slash on the end of directories
assert contents = ['handling/',
'internal/',
'render/',
'script/',
'server/',
'sql/',
'template/',
'Groovy.java',
'GroovyRatpackMain.java',
'package-info.java']
Obviously you can only make 60 requests per hour