Groovy copy file, modify values and save it - groovy

I need to use Groovy to copy a "template" file and replace some text (a variable) with current values and then save the file in a new file.
bay_equipment.each{
hname="Network-${it['hostname']}-${it['function']}"
nodeName="${it['hostname']}"
nodeIP="${it['ip_address']}"
fname="${it['hostname']}-${it['function']}-Network.cfg"
def src= new File("../config-files/nagios-switch-bayg20.cfg")
def dst= new File("../dest-files/$fname")
dst << src.text
//everything work ups to here
//now trying to open the dst file and make modifications then save it.
//This string <%=#deviceType%>-<%=#deviceGroup%>-<%=#nodeName%> needs to be replaced with hname variable and likewise the <%=#nodeIP%> with nodeIP
dst = (dst =~ /<%=#deviceType%>-<%=#deviceGroup%>-<%=#nodeName%>/).replaceFirst("$hname")
dst = (dst =~ /<%=#nodeIP%>/).replaceFirst("$nodeIP")
dst.write(dst)
}
I'm getting an error when I run it:
Caught: groovy.lang.MissingMethodException: No signature of method: java.lang.String.write() is applicable for argument types: (String) values: [../dest-files/u100en00700-BAYG20-Network.cfg]
Possible solutions: wait(), wait(long), with(groovy.lang.Closure), trim(), size(), toSet()
groovy.lang.MissingMethodException: No signature of method: java.lang.String.write() is applicable for argument types: (String) values: [../dest-files/u100en00700-BAYG20-Network.cfg]
Possible solutions: wait(), wait(long), with(groovy.lang.Closure), trim(), size(), toSet()
at nsr_nagios$_run_closure4.doCall(nsr_nagios:53)
at nsr_nagios.run(nsr_nagios:38)

For an example of how you can work with this, the following code:
def equipment = [
[hostname: 'foo', function: "bar", ip_address: '1.2.3.4'],
[hostname: 'alice', function: "raven", ip_address: '5.6.7.8']
]
equipment.each {
def binding = [
hname: "Network-${it['hostname']}-${it['function']}",
nodeName: it.hostname,
nodeIP: it.ip_address,
fname: "${it['hostname']}-${it['function']}-Network.cfg"
]
def srcData = new File("src-file.txt").text
def template = new groovy.text.StreamingTemplateEngine().createTemplate(srcData)
new File(binding.fname).text = template.make(binding)
}
given the following source file:
<%= hname %>-<%= nodeName %>-<%= nodeIP %>
will create two files, both templated. Example execution sequence:
─➤ ls -la
total 24
drwxrwxr-x 2 mbjarland mbjarland 4096 Mar 4 17:55 .
drwxr-xr-x 112 mbjarland mbjarland 12288 Mar 4 17:47 ..
-rw-rw-r-- 1 mbjarland mbjarland 634 Mar 4 17:55 solution.groovy
-rw-rw-r-- 1 mbjarland mbjarland 42 Mar 4 17:55 src-file.txt
─➤ cat src-file.txt
<%= hname %>-<%= nodeName %>-<%= nodeIP %>
─➤ groovy solution.groovy
─➤ ls -la
total 32
drwxrwxr-x 2 mbjarland mbjarland 4096 Mar 4 17:56 .
drwxr-xr-x 112 mbjarland mbjarland 12288 Mar 4 17:47 ..
-rw-rw-r-- 1 mbjarland mbjarland 33 Mar 4 17:56 alice-raven-Network.cfg
-rw-rw-r-- 1 mbjarland mbjarland 27 Mar 4 17:56 foo-bar-Network.cfg
-rw-rw-r-- 1 mbjarland mbjarland 634 Mar 4 17:55 solution.groovy
-rw-rw-r-- 1 mbjarland mbjarland 42 Mar 4 17:55 src-file.txt
─➤ cat foo-bar-Network.cfg
Network-foo-bar-foo-1.2.3.4
─➤ cat alice-raven-Network.cfg
Network-alice-raven-alice-5.6.7.8
─➤
where the template engine is a groovy built in class for doing just these kinds of things.

Related

transforms.route.topic.expression and groovy expression

I'm trying to use debezium transforms.route.topic.expression
Here the entries in connector configuration
"transforms": "dropPrefix,unwrapi,route",
"transforms.dropPrefix.type": "org.apache.kafka.connect.transforms.RegexRouter",
"transforms.dropPrefix.regex": "rocketlawyer.dbo.(.*)",
"transforms.dropPrefix.replacement": "$1",
"transforms.unwrap.type": "io.debezium.transforms.ExtractNewRecordState",
"transforms.unwrap.drop.tombstones": "false",
"transforms.route.type": "io.debezium.transforms.ContentBasedRouter",
"transforms.route.language": "jsr223.groovy",
"transforms.route.topic.expression": "value.snapshotequals('true') ? ${topic} : cdc.$1"
when I'm applying config call I'm getting following error
iguenkin_rocketlawyer_com#dbadm-r208:~/confluent$ curl -d #mssql_trg_cf.json -H "Content-Type: application/json" -X PUT http://localhost:8083/connectors/mssql_trg/config | jq
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1326 100 242 100 1084 30250 132k --:--:-- --:--:-- --:--:-- 161k
{
"error_code": 500,
"message": "Unexpected character ('\"' (code 34)): was expecting comma to separate Object entries\n at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 832]"
}
column: 832
"transforms.route.topic.expression": "value.snapshotequals('true') ? ${topic} : cdc.$1"
column 830 is a blank space in string : cdc
Here the questions
how can I be sure that jsr223.groovy is installed and accessible to transformations
as I am understand it is a part of dibezium connector, so it is not listed as a separate plugin
here the list of jars whee debezium is installed
`-rw-r--r-- 1 iguenkin_rocketlawyer_com 1234193955 20272 Dec 9 20:44 debezium-api-1.3.1.Final.jar
-rw-r--r-- 1 iguenkin_rocketlawyer_com 1234193955 91369 Dec 9 20:44 debezium-connector-sqlserver-1.3.1.Final.jar
-rw-r--r-- 1 iguenkin_rocketlawyer_com 1234193955 844500 Dec 9 20:44 debezium-core-1.3.1.Final.jar
-rw-r--r-- 1 iguenkin_rocketlawyer_com 1234193955 19090 Dec 15 22:01 debezium-scripting-1.3.1.Final.jar
-rw-r--r-- 1 iguenkin_rocketlawyer_com 1234193955 16839 Dec 16 01:04 groovy-jsr223-3.0.7-indy.jar`
if groovy is installed what is wrong with expression ? I've used following documentation to set connector https://debezium.io/documentation/reference/configuration/content-based-routing.html

Python PATH .is_file() evaluates symlink as a file

In my Python3 program, I take a bunch of paths and do things based on what they are. When I evaluate the following symlinks (snippet):
lrwxrwxrwx 1 513 513 5 Aug 19 10:56 console -> ttyS0
lrwxrwxrwx 1 513 513 11 Aug 19 10:56 core -> /proc/kcore
lrwxrwxrwx 1 513 513 13 Aug 19 10:56 fd -> /proc/self/fd
the results are:
symlink console -> ttyS0
file core -> /proc/kcore
symlink console -> ttyS0
It evaluates core as if it were a file (vs a symlink). What is the best way for me to evaluate it as a symlink vs a file? code below
#!/usr/bin/python3
import sys
import os
from pathlib import Path
def filetype(filein):
print(filein)
if Path(filein).is_file():
return "file"
if Path(filein).is_symlink():
return "symlink"
else:
return "doesn't match anything"
if __name__ == "__main__":
file = sys.argv[1]
print(str(file))
print(filetype(file))
The result of is_file is intended to answer the question "if I open this name, will I open a file". For a symlink, the answer is "yes" if the target is a file, hence the return value.
If you want to know if the name is a symlink, ask is_symlink.

Syntaxnet Turkish Language Data Set Non Existent Map Files

I am new to Syntaxnet and i tried to use pre-trained model of Turkish language through the instructions here
Point-1 : Although I set the MODEL_DIRECTORY environment variable, tokenize.sh didn't find the related path and it gives error like below :
root#4562a2ee0202:/opt/tensorflow/models/syntaxnet# echo "Eray eve geldi." | syntaxnet/models/parsey_universal/tokenize.sh
F syntaxnet/term_frequency_map.cc:62] Check failed: ::tensorflow::Status::OK() == (tensorflow::Env::Default()->NewRandomAccessFile(filename, &file)) (OK vs. **Not found: label-map**)
Point-2 : So, I changed the tokenize.sh through commenting the MODEL_DIR=$1 and set my Turkish language model path like below to go on :
PARSER_EVAL=bazel-bin/syntaxnet/parser_eval
CONTEXT=syntaxnet/models/parsey_universal/context.pbtxt
INPUT_FORMAT=stdin-untoken
MODEL_DIR=$1
MODEL_DIR=syntaxnet/models/etiya-smart-tr
Point-3 : After that when I run it as told, it gives error like below :
root#4562a2ee0202:/opt/tensorflow/models/syntaxnet# echo "Eray eve geldi" | syntaxnet/models/parsey_universal/tokenize.sh
I syntaxnet/term_frequency_map.cc:101] Loaded 29 terms from syntaxnet/models/etiya-smart-tr/label-map.
I syntaxnet/embedding_feature_extractor.cc:35] Features: input.char input(-1).char input(1).char; input.digit input(-1).digit input(1).digit; input.punctuation-amount input(-1).punctuation-amount input(1).punctuation-amount
I syntaxnet/embedding_feature_extractor.cc:36] Embedding names: chars;digits;puncts
I syntaxnet/embedding_feature_extractor.cc:37] Embedding dims: 16;16;16
F syntaxnet/term_frequency_map.cc:62] Check failed: ::tensorflow::Status::OK() == (tensorflow::Env::Default()->NewRandomAccessFile(filename, &file)) (OK vs. **Not found: syntaxnet/models/etiya-smart-tr/char-map**)
I had downloaded the Turkish package through tracing the link pattern indicated like download.tensorflow.org/models/parsey_universal/.zip
and my language mapping file list like below :
-rw-r----- 1 root root 50646 Sep 22 07:24 char-ngram-map
-rw-r----- 1 root root 329 Sep 22 07:24 label-map
-rw-r----- 1 root root 133477 Sep 22 07:24 morph-label-set
-rw-r----- 1 root root 5553526 Sep 22 07:24 morpher-params
-rw-r----- 1 root root 1810 Sep 22 07:24 morphology-map
-rw-r----- 1 root root 10921546 Sep 22 07:24 parser-params
-rw-r----- 1 root root 39990 Sep 22 07:24 prefix-table
-rw-r----- 1 root root 28958 Sep 22 07:24 suffix-table
-rw-r----- 1 root root 561 Sep 22 07:24 tag-map
-rw-r----- 1 root root 5234212 Sep 22 07:24 tagger-params
-rw-r----- 1 root root 172869 Sep 22 07:24 word-map
QUESTION-1 :
I am aware that there is no char-map file in the directory so I got the error written # Point-3 above. So, does anyone have an opinion about how the Turkish language test could be done and the result was shared as %93,363 for Part-of-Speech for example?
QUESTION-2:
How can I find the char-map file for Turkish language?
QUESTION-3:
If there is no char-map file, must I train through tracing the steps indicated as SyntaxNet's Obtain Data & Training?
QUESTION-4:
Is there a way to generate word-map, char-map... etc. files? Is it the well known word2vec approach that can be used to generate map files which will be able to be processed wt. Syntaxnet tokenizers?
Try this https://github.com/tensorflow/models/issues/830 issue - it contains an (at this moment) temporary solution.

Opening .xlsx file with Apache POI gives NoClassDefFoundError InvalidFormatException

I'm having trouble getting the import of all the required .jar files working correctly.
My end goal is simply to read in an excel spreadsheet (more specifically .xlsx files) to check whether the speed of Apache POI will work for future applications.
My question is why am I getting thrown the error...
java.lang.NoClassDefFoundError: org/apache/poi/openxml4j/exceptions/InvalidFormatException
...when I can see in the class InvalidFormatException at /org/apache/poi/openxml4j/exceptions/ in the 'file poi-ooxml-3.9-20121203.jar'?
I haven't done programming in Java in a while and I'm hoping for my own sanity that I've made a stupid mistake. My code seems to be compiling correctly but I have the above error message when I try run the program. I have done some research into my issue but haven't been able to find someone in my situation. They usually just forget/don't have a required .jar file. Maybe I'm in the same boat but hopefully you'll be able to tell me.
I'll give a breakdown of my logic. Here is the code in TestExcel.java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
public class TestExcel
{
public static void main(String[] args)
{
InputStream inp;
try
{
inp = new FileInputStream("cost.xlsx");
Workbook wb;
try {
wb = WorkbookFactory.create(inp);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
catch (InvalidFormatException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Here is the file 'c_test' used to compile and run the java code:
javac -classpath "/home/robsco/Webdev/java/poi/*" TestExcel.java
java TestExcel
Here is the directory listing of the classpath above:
robsco#roblaptop:~/Webdev/java/poi$ ls -al
total 26616
drwxrwxr-x 2 robsco robsco 4096 Aug 18 18:13 .
drwxrwxr-x 6 robsco robsco 4096 Aug 18 18:12 ..
-rwxr-xr-- 1 robsco robsco 52915 Sep 18 2009 commons-logging-1.1.jar
-rwxr-xr-- 1 robsco robsco 313898 Apr 5 2009 dom4j-1.6.1.jar
-rwxr-xr-- 1 robsco robsco 121070 Apr 5 2009 junit-3.8.1.jar
-rwxr-xr-- 1 robsco robsco 358180 Apr 5 2009 log4j-1.2.13.jar
-rwxrw-r-- 1 robsco robsco 14841622 Aug 18 13:15 ooxml-schemas-1.1.jar
-rwxr-xr-- 1 robsco robsco 1869113 Nov 26 2012 poi-3.9-20121203.jar
-rwxr-xr-- 1 robsco robsco 30446 Nov 26 2012 poi-excelant-3.9-20121203.jar
-rwxr-xr-- 1 robsco robsco 936648 Nov 26 2012 poi-ooxml-3.9-20121203.jar
-rwxr-xr-- 1 robsco robsco 4802621 Nov 26 2012 poi-ooxml-schemas-3.9-20121203.jar
-rwxr-xr-- 1 robsco robsco 1226511 Nov 26 2012 poi-scratchpad-3.9-20121203.jar
-rwxr-xr-- 1 robsco robsco 2666695 Apr 5 2009 xmlbeans-2.3.0.jar
and the output of executing 'c_test':
robsco#roblaptop:~/Webdev/java$ ./c_test
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/openxml4j/exceptions/InvalidFormatException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2521)
at java.lang.Class.getMethod0(Class.java:2764)
at java.lang.Class.getMethod(Class.java:1653)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.openxml4j.exceptions.InvalidFormatException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more
I am using:
OS: Lubuntu 12.10 x86
java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
robsco#roblaptop:~/Webdev/java/poi$ javac -version
javac 1.7.0_25
The problem is with how you're launching your program. I see you currently have a shell script of:
javac -classpath "/home/robsco/Webdev/java/poi/*" TestExcel.java
java TestExcel
That compiles with all the jars, but doesn't run with them, so your program won't have its dependencies available. (Unlike with some c programs, compiling with Java won't inline the depdendencies, nor will it add local links to the files. There are tools that will do that for you if you really need it though)
If you change your shell script to be:
javac -classpath "/home/robsco/Webdev/java/poi/*" TestExcel.java
java -classpath ".:/home/robsco/Webdev/java/poi/*" TestExcel
It should work fine. Note the addition of the current directory in the classpath in order for 'java' to find your class (in this case TestExcel).
Alternately, you could do something a little icky with environment variables, using the CLASSPATH envvar that Java checks, eg
export CLASSPATH=.:`ls -1d --color=no /home/robsco/Webdev/java/poi/* | xargs echo | sed 's/ /\:/g'`
javac TestExcel.java
java TestExcel

Convert string to class object in Python

I have a list of strings. Each string of the list has same format. I would like to convert each string into a class object (if that is the best option), so I can do some analysis of the list of class object.
As an example,
I have the following list
ls_list = ['-rw-r--r-- 1 ahmed None 0 Apr 21 17:10 bar1',
'-rw-r--r-- 1 ahmed None 0 Apr 21 17:10 bar2',
'-rw-r--r-- 1 ahmed None 0 Apr 21 17:10 foo1',
'-rw-r--r-- 1 ahmed None 0 Apr 21 17:10 foo2']
I would like to convert each of the above string into a class that has Nine members (perm, etc).
You don't want to do that. Use os.listdir() and os.stat() to get the information you want.
You do it something like this:
import os
data = {}
for file in os.listdir('.'):
data[file] = os.stat(file)
This gives you the information for all the files in the current directory, as objects, as you requested. These can then be inspected, and you can use other functions to figure out the username of the userid you get, etc.
Here's one way of doing what you ask, but as others have noted, don't use this for parsing the output of ls. Also, it assumes that there are only 8 areas of whitespace in your input string separating your data. If any of the data substrings also contain whitespace, this code will fail:
class FileAttribs(object):
ORDERED_ATTRIB_NAMES = ["permissions", "links", "owner",
"groups", "size", "month", "day", "time", "name"]
def __init__(self, lsString):
for (attrib, s) in zip(self.ORDERED_ATTRIB_NAMES, lsString.split()):
setattr(self, attrib, s)
if __name__ == '__main__':
ls_list = ['-rw-r--r-- 1 ahmed None 0 Apr 21 17:10 bar1',
'-rw-r--r-- 1 ahmed None 0 Apr 21 17:10 bar2',
'-rw-r--r-- 1 ahmed None 0 Apr 21 17:10 foo1',
'-rw-r--r-- 1 ahmed None 0 Apr 21 17:10 foo2']
files = []
for s in ls_list:
files.append(FileAttribs(s))
#do stuff with files, e.g.
for f in files:
print f.permissions, f.name

Resources