How to pass regular expression in a list in Groovy? - regex

I have tried these things but it is not working for me:
def fileNames = [/ReadyAPI-\d.\d.\d.vmoptions/, 'loadtestrunner.bat', 'ready-api.bat', 'securitytestrunner.bat']
def fileNames = ['ReadyAPI-\\d.\\d.\\d.vmoptions', 'loadtestrunner.bat', 'ready-api.bat', 'securitytestrunner.bat']
What I am trying to do is that there will be one of the two files that will be present in the system which are:
'ReadyAPI-1.2.2.vmoptions' OR 'ReadyAPI-1.3.1.vmoptions'.
I am relatively new to Groovy so may be I am not seeing common problem. So, please bear with me. Thanks

You will need to get the filename first, then put it into the list. You can use Groovy's FileNameByRegexFinder from groovy.utils like this to find that file with a regex:
def optionsFile = new FileNameByRegexFinder()
.getFileNames('.', /ReadyAPI-\d.\d.\d.vmoptions/).first()
def fileNames = [optionsFile, 'loadtestrunner.bat', ...]
Note that the first parameter to getFileNames() is the base directory to search in, you will likely need to change that for your scenario.

Related

How can I use regex to construct an API call in my Jekyll plugin?

I'm trying to write my own Jekyll plugin to construct an api query from a custom tag. I've gotten as far as creating the basic plugin and tag, but I've run into the limits of my programming skills so looking to you for help.
Here's my custom tag for reference:
{% card "Arbor Elf | M13" %}
Here's the progress on my plugin:
module Jekyll
class Scryfall < Liquid::Tag
def initialize(tag_name, text, tokens)
super
#text = text
end
def render(context)
# Store the name of the card, ie "Arbor Elf"
#card_name =
# Store the name of the set, ie "M13"
#card_set =
# Build the query
#query = "https://api.scryfall.com/cards/named?exact=#{#card_name}&set=#{#card_set}"
# Store a specific JSON property
#card_art =
# Finally we render out the result
"<img src='#{#card_art}' title='#{#card_name}' />"
end
end
end
Liquid::Template.register_tag('cards', Jekyll::Scryfall)
For reference, here's an example query using the above details (paste it into your browser to see the response you get back)
https://api.scryfall.com/cards/named?exact=arbor+elf&set=m13
My initial attempts after Googling around was to use regex to split the #text at the |, like so:
#card_name = "#{#text}".split(/| */)
This didn't quite work, instead it output this:
[“A”, “r”, “b”, “o”, “r”, “ “, “E”, “l”, “f”, “ “, “|”, “ “, “M”, “1”, “3”, “ “]
I'm also then not sure how to access and store specific properties within the JSON response. Ideally, I can do something like this:
#card_art = JSONRESPONSE.image_uri.large
I'm well aware I'm asking a lot here, but I'd love to try and get this working and learn from it.
Thanks for reading.
Actually, your split should work – you just need to give it the correct regex (and you can call that on #text directly). You also need to escape the pipe character in the regex, because pipes can have special meaning. You can use rubular.com to experiment with regexes.
parts = #text.split(/\|/)
# => => ["Arbor Elf ", " M13"]
Note that they also contain some extra whitespace, which you can remove with strip.
#card_name = parts.first.strip
#card_set = parts.last.strip
This might also be a good time to answer questions like: what happens if the user inserts multiple pipes? What if they insert none? Will your code give them a helpful error message for this?
You'll also need to escape these values in your URL. What if one of your users adds a card containing a & character? Your URL will break:
https://api.scryfall.com/cards/named?exact=Sword of Dungeons & Dragons&set=und
That looks like a URL with three parameters, exact, set and Dragons. You need to encode the user input to be included in a URL:
require 'cgi'
query = "https://api.scryfall.com/cards/named?exact=#{CGI.escape(#card_name)}&set=#{CGI.escape(#card_set)}"
# => "https://api.scryfall.com/cards/named?exact=Sword+of+Dungeons+%26+Dragons&set=und"
What comes after that is a little less clear, because you haven't written the code yet. Try making the call with the Net::HTTP module and then parsing the response with the JSON module. If you have trouble, come back here and ask a new question.

How to use regex param in Nexus v3 groovy scripting

I need to get in my raw Nexus v3 repository every files that contains a '-' character
Ive found how to use storageFacet to make some queries but I cant find how to use a regex with it:
def repo = repository.repositoryManager.get("myRepo")
StorageFacet storageFacet = repo.facet(StorageFacet)
def tx = storageFacet.txSupplier().get()
tx.begin()
Iterable<Component> components =
tx.findComponents(Query.builder().where('name = ').param('/^.*\b([-]+)\b.*$').build(), [repo])
tx.commit()
tx.close()
It works when I put a string name instead of param('/^.\b([-]+)\b.$') .
If you have any documentation about how this query system works I will be more than happy to read it.
I finaly used a Like %-%
But I still dont know how to use a Regex in this case.

Generate url for Django Simple History historical object

Given an model called Stuff, I want the url to a HistoricalStuff object.
In other words, how does one implement get_historical_url in the below code snippet?
stuff = Stuff.objects.first()
stuff.pk
-> 100
historical_stuff = stuff.history.first() # we want to get url for this
historical_stuff.pk
-> 1
get_historical_url(historical_stuff)
-> /path/to/admin/stuff/100/history/1
Obviously the dumb solution would be to use a format string but I'd rather use urlresolvers
After much digging around, I found in the simple history source code that the url name is similar to the admin change names, namely admin_%s_%s_simple_history.
With this knowledge, get_historical_url looks like
def get_simplehistory_url(history_obj):
parent_obj = history_obj.history_object
return urlresolvers.reverse('admin:{}_{}_simple_history'.format(
parent_obj._meta.app_label, parent_obj._meta.model_name), args=(parent_obj.id, history_obj.pk))

Groovy and XMLUnit: compare webservice results

Using Groovy and XMLUnit I am trying to write a script to compare the xml output of web services from multiple endpoints. Attempting to get it working from one endpoint then iterate over endpoints to compare output, however, I continue to get the following error:
Caught: groovy.lang.GroovyRuntimeException:
Could not find matching constructor for:
org.custommonkey.xmlunit.Diff(groovy.util.Node, groovy.util.Node)
groovy.lang.GroovyRuntimeException:
Could not find matching constructor for:
org.custommonkey.xmlunit.Diff(groovy.util.Node, groovy.util.Node)
I am pretty sure it has to do with my inexperience with both XmlParser/XmlSlurper and XMLUnit (a.k.a. newbie). I greatly appreciate any pointers in the right direction. Here is sample code that causes the exception:
#Grab(group='xmlunit', module='xmlunit', version='1.5')
import org.custommonkey.xmlunit.*
def url = "http://www.webservicex.net//geoipservice.asmx/GetGeoIP?IPAddress=173.201.44.188"
def xmlParserResults = new XmlParser().parse("$url")
//same thing happens if I use...
//def xmlSlurperResults = new XmlSlurper().parse("$url")
def xmlDiff = new Diff(xmlParserResults, xmlParserResults)
assert xmlDiff.identical()
Thank you in advance!
The url returns xml and Diff takes two Strings to compare (in your case you are comparing Nodes). So the easiest way to compare would be to use URL instead of trying it to parse using XmlParser or XmlSlurper.
def url =
"http://www.webservicex.net//geoipservice.asmx/GetGeoIP?IPAddress=173.201.44.188"
def xmlString = new URL(url).text
def xmlDiff = new Diff(xmlString, xmlString)
assert xmlDiff.identical()
In case the above is just a sample and not a working example of hitting multiple endpoints then the point is to represent the xml output as string and then compare.

how to read only URL from txt file in MATLAB

I have a text file having multiple URLs with other information of the URL. How can I read the txt file and save the URLs only in an array to download it? I want to use
C = textscan(fileId, formatspec);
What should I mention in formatspec for URL as format?
This is not a job for textscan; you should use regular expressions for this. In MATLAB, regexes are described here.
For URLs, also refer here or here for examples in other languages.
Here's an example in MATLAB:
% This string is obtained through textscan or something
str = {...
'pre-URL garbage http://www.example.com/index.php?query=test&otherStuf=info more stuff here'
'other foolish stuff ftp://localhost/home/ruler_of_the_world/awesomeContent.py 1 2 3 4 misleading://';
};
% find URLs
C = regexpi(str, ...
['((http|https|ftp|file)://|www\.|ftp\.)',...
'[-A-Z0-9+&##/%=~_|$?!:,.]*[A-Z0-9+&##/%=~_|$]'], 'match');
C{:}
Result:
ans =
'http://www.example.com/index.php?query=test&otherStuf=info'
ans =
'ftp://localhost/home/ruler_of_the_world/awesomeContent.py'
Note that this regex requires you to have the protocol included, or have a leading www. or ftp.. Something like example.com/universal_remote.cgi?redirect= is NOT matched.
You could go on and make the regex cover more and more cases. However, eventually you'll stumble upon the the most important conclusion (as made here for example; where I got my regex from): given the full definition of what precisely constitutes a valid URL, there is no single regex able to always match every valid URL. That is, there are valid URLs you can dream up that are not captured by any of the regexes shown.
But please keep in mind that this last statement is more theoretical rather than practical -- those non-matchable URLs are valid but not often encountered in practice :) In other words, if your URLs have a pretty standard form, you're pretty much covered with the regex I gave you.
Now, I fooled around a bit with the Java suggestion by pm89. As I suspected, it is an order of magnitude slower than just a regex, since you introduce another "layer of goo" to the code (in my timings, the difference was about 40x slower, excluding the imports). Here's my version:
import java.net.URL;
import java.net.MalformedURLException;
str = {...
'pre-URL garbage http://www.example.com/index.php?query=test&otherStuf=info more stuff here'
'pre--URL garbage example.com/index.php?query=test&otherStuf=info more stuff here'
'other foolish stuff ftp://localhost/home/ruler_of_the_world/awesomeContent.py 1 2 3 4 misleading://';
};
% Attempt to convert each item into an URL.
for ii = 1:numel(str)
cc = textscan(str{ii}, '%s');
for jj = 1:numel(cc{1})
try
url = java.net.URL(cc{1}{jj})
catch ME
% rethrow any non-url related errors
if isempty(regexpi(ME.message, 'MalformedURLException'))
throw(ME);
end
end
end
end
Results:
url =
'http://www.example.com/index.php?query=test&otherStuf=info'
url =
'ftp://localhost/home/ruler_of_the_world/awesomeContent.py'
I'm not too familiar with java.net.URL, but apparently, it is also unable to find URLs without leading protocol or standard domain (e.g., example.com/path/to/page).
This snippet can undoubtedly be improved upon, but I would urge you to consider why you'd want to do this for this longer, inherently slower and far uglier solution :)
As I suspected you could use java.net.URL according to this answer.
To implement the same code in Matlab:
First read the file into a string, using fileread for example:
str = fileread('Sample.txt');
Then split the text with respect to spaces, using strsplit:
spl_str = strsplit(str);
Finally use java.net.URL to detect the URLs:
for k = 1:length(spl_str)
try
url = java.net.URL(spl_str{k})
% Store or save the URL contents here
catch e
% it's not a URL.
end
end
You can write the URL contents into a file using urlwrite. But first convert the URLs obtained from java.net.URL to char:
url = java.net.URL(spl_str{k});
urlwrite(char(url), 'test.html');
Hope it helps.