Is unix file path a valid URL? - url

Suppose we have a string /path/to/file, when I use urlparse of Python, it will treat the string as a valid URL and return the following result:
from urllib.parse import urlparse
urlparse('/path/to/file')
# ParseResult(scheme='', netloc='', path='/path/to/file', params='', query='', fragment='')
But if I do the same thing with JavaScript, it will throw the following error
new URL('/path/to/file')
// Uncaught TypeError: Failed to construct 'URL': Invalid URL
I am wondering which implemetation is right. Shall a unix file path treat as a valid URL? I don't dive in to the RFCs, but personally I prefer the way of Python as it makes it clear to distingiush local file or remote resource by parsing it and checking the scheme field. It's useful when you write a program that allow use to load file from local or remote.
Update:
According to the document provided by Alexei Levenkov
: The Fine Manual on JavaScript it says
url: A string ... that represents an absolute or relative URL. If url is a relative URL, base is required, and will be used as the base URL. If url is an absolute URL, a given base will be ignored.
In JavaScript it will not throw error if base parameters is provided
new URL('/path/to/file', 'file://')
But it still have gap with the implement of python when input string is a relative path ./path/to/file.
In JavaScript it will ignore the ./ but Python will still keep it.
JavaScript:
new URL('/path/to/file', 'file://')
new URL('./path/to/file', 'file://')
// both yeild the same result:
// URLĀ {origin: 'file://', protocol: 'file:', pathname: '/path/to/file'}
Python:
from urllib.parse import urlparse
urlparse('/path/to/file')
# ParseResult(scheme='', netloc='', path='/path/to/file', params='', query='', fragment='')
urlparse('./path/to/file')
# ParseResult(scheme='', netloc='', path='./path/to/file', params='', query='', fragment='')

Related

How to change new File method in Groovy?

How do I replace the new File method with a secure one? Is it possible to create a python script and connect it?
Part of the code where I have a problem:
def template Name = new File(file: "${template}").normalize.name.replace(".html", "").replace(".yaml", "")
But when I run my pipeline, I get the error
java.lang.SecurityException: Unable to find constructor: new java.io .File java.util.LinkedHashMap
This method is prohibited and is blacklisted. How do I replace it and with what?
If you're reading the contents of the file, you can replace that "new File" with "readFile".
See https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#readfile-read-file-from-workspace
readFile: Read file from workspace
Reads a file from a relative path (with root in current directory, usually > workspace) and returns its content as a plain string.
file : String
Relative (/-separated) path to file within a workspace to read.
encoding : String (optional)
The encoding to use when reading the file. If left blank, the platform default encoding will be used. Binary files can be read into a Base64-encoded string by specifying "Base64" as the encoding.

static final object changes identity

I have a browser application written in Dart. I noticed a strange error appearing where my StageXL ResourceManager was missing the resources that it previously had. After debugging the program for a while I ended up with this situation:
In global.dart:
class Global {
static final ResourceManager resourceManager = new ResourceManager();
}
In main function:
var resources = Global.resourceManager;
resources.addBitmapData("Player", "images/player_base.png");
await resources.load();
print("in main: ${identityHashCode(Global.resourceManager)} = "
" ${Global.resourceManager.resources}, isolate: ${identityHashCode(
Isolate.current)}");
In another function where I need to access the resource afterwards:
print("elsewhere: ${identityHashCode(Global.resourceManager)} = "
" ${Global.resourceManager.resources}, isolate: ${identityHashCode(
Isolate.current)}");
Expected output (identityHashCodes match and so do the object contents):
in main: 12345678 = [ResourceManagerResource [kind=BitmapData, name=Player,
url = images/player_base.png]], isolate: 09876543
elsewhere: 12345678 = [ResourceManagerResource [kind=BitmapData,
name=Player, url = images/player_base.png]], isolate: 09876543
Actual output (note the identityHashCode mismatch):
in main: 516570559 = [ResourceManagerResource
[kind=BitmapData, name=Player, url = images/player_base.png]],
isolate: 843028171
elsewhere: 419835243 = [], isolate: 843028171
I thought this may have something to do with running in a different isolate (not familiar with them) but as you can see, the current isolates identityHashCodes match.
That is surprising. My best guess is that you are importing the same library twice, using different URIs. The fact that one of your files is a "main" file supports this, since its a common mistake to specify the main file on the command line as a file and have it import a package library using a relative reference.
Is your "main" file in a package lib directory, and does it import the resource file using a relative path? If so, try changing that import to a package:packageName/thepath URI instead and see if it changes anything.
(My personal recommendation is to never have a Dart library URL that contains lib, whether in an import/export or on the command line. Always use a package: URI in that case instead.)

Filename not assigned to csv export in HighCharts on Mac

I am using the EXPORT-CSV plugin for Highcharts to export data to csv. (Thank you to the developers of this plugin!) When testing in Safari on a Mac, however, the exported csv file does not take the filename as expected from
exporting: {
filename: "FancyFileName"
}
and instead just uses the default Highcharts name "chart". All the built-in export types do use the desired filename from Safari, and the csv also gets the desired filename from all the other standard browsers I have tested.
Here is a fiddle.
How can Safari be convinced to use the filename I give it? Thanks for your help.
The hard coded filename is coming from the php script written on the server. You can find that script on php script for download of file with hardcoded file name
You can use that same code on your local machine server. To do that
create a php file on your local server.
copy the above code in it.
change url www.highcharts.com/studies/csv-export/csv.php in export.csv library to your local server url.
change the name whatever you wish to using $_POST['variablename']
Variable name should be pass when making post call from export.csv library and use in php file using $_POST
Highcharts.post(url, {
data: content,
name: name,
type: MIME,
extension: extension
});
This is the code used in export.csv library for making post call. I have added extra parameter name to be used in my php script for dynamic filename.

How to load N-TRIPLE file in apache jena?

I am quite new to RDF and Jena. I want load a .nt (N- TRIPLE) file to a model. I have tried read(inputStream, "N-TRIPLE") but did not help.
It throws
org.apache.jena.riot.RiotException: Element or attribute do not match QName production: QName::=(NCName':')?NCName.
Can anyone point me out what is wrong?
Here is the link for the N-TRiple file which I tried to load : http://dbpedia.org/data/Berlin.ntriples
read(inputStream, string) uses the string argument as the base URI, not the syntax language. It's trying the default, which is RDF/XML. Check the javadoc for Model#read(InputStream in, String base) and Model#read(InputStream in, String base, String lang) for more information.
model.read(inputStream, null, "N-TRIPLES") ;
or
RDFDataMgr.read(model, inputStream, LANG.NTRIPLES) ;
If you are just opening the stream from a file (or URL) then Apache Jena will sort out the details. E.g.,
RDFDataMgr.read(model, "file:///myfile.nt") ;
There are various related operations. See the javadoc for Model and RDFDataMgr.

downloading and storing files from given url to given path in lua

I'm new with lua but working on an application that works on specific files with given path. Now, I want to work on files that I download. Is there any lua libraries or line of codes that I can use for downloading and storing it on my computer ?
You can use the LuaSocket library and its http.request function to download using HTTP from an URL.
The function has two flavors:
Simple call: http.request('http://stackoverflow.com')
Advanced call: http.request { url = 'http://stackoverflow.com', ... }
The simple call returns 4 values - the entire content of the URL in a string, HTTP response code, headers and response line. You can then save the content to a file using the io library.
The advanced call allows you to set several parameters like HTTP method and headers. An important parameter is sink. It represents a LTN12-style sink. For storing to file, you can use sink.file:
local file = ltn12.sink.file(io.open('stackoverflow', 'w'))
http.request {
url = 'http://stackoverflow.com',
sink = file,
}

Resources