Here in this post, I want to leave one more piece of code, which will be useful later on, like a groovy script, you can make a python script call and receive a return (Json).
The examples may seem a little exaggerated, they could be simpler, but I really want to show the example with some complexity.
The python script, once called, loads a list of dictionaries, sorts and filters, transforms it to Json and prints the result.
Groovy, after calling Python, captures it. result, transforms it into Json again and loops with print, to demonstrate the result.
any_script.py
import json
class Node_Folders():
def sortPath(self, lst_Folders):
return sorted(lst_Folders, key=lambda i: (self.fn_IP_Int(i['IP']), i['Path']))
def filterPath(self, lst_Folders, lst_Filter):
return list(filter(lambda d: d.get('Path') in lst_Filter, lst_Folders))
def fn_IP_Int(self, str_IP: str):
h = list(map(int, str_IP.split(".")))
return (h[0] << 24) + (h[1] << 16) + (h[2] << 8) + (h[3] << 0)
def main():
lst_dict_Data = [{'IP': '21.96.62.214', 'Link': False, 'Exists': False, 'Path': '/mnt/heapdump'},
{'IP': '21.96.62.214', 'Link': False, 'Exists': False, 'Path': '/mnt/cassandra/saved_caches'},
{'IP': '21.96.62.214', 'Link': False, 'Exists': False, 'Path': '/mnt/cassandra/hint'},
{'IP': '21.96.62.214', 'Link': False, 'Exists': False, 'Path': '/mnt/cassandra/commitlog'},
{'IP': '253.102.1.103', 'Link': False, 'Exists': False, 'Path': '/mnt/heapdump'},
{'IP': '253.102.1.103', 'Link': False, 'Exists': False, 'Path': '/mnt/cassandra/saved_caches'},
{'IP': '253.102.1.103', 'Link': False, 'Exists': False, 'Path': '/mnt/cassandra/hint'},
{'IP': '253.102.1.103', 'Link': False, 'Exists': False, 'Path': '/mnt/cassandra/commitlog'},
{'IP': '122.190.69.93', 'Link': False, 'Exists': False, 'Path': '/mnt/heapdump'},
{'IP': '122.190.69.93', 'Link': False, 'Exists': False, 'Path': '/mnt/cassandra/saved_caches'},
{'IP': '122.190.69.93', 'Link': False, 'Exists': False, 'Path': '/mnt/cassandra/hint'},
{'IP': '122.190.69.93', 'Link': False, 'Exists': False, 'Path': '/mnt/cassandra/commitlog'},
{'IP': '174.40.30.197', 'Link': False, 'Exists': False, 'Path': '/mnt/cassandra/saved_caches'},
{'IP': '174.40.30.197', 'Link': False, 'Exists': False, 'Path': '/mnt/cassandra/hint'},
{'IP': '174.40.30.197', 'Link': False, 'Exists': False, 'Path': '/mnt/cassandra/commitlog'},
{'IP': '226.14.168.44', 'Link': False, 'Exists': False, 'Path': '/mnt/cassandra/saved_caches'},
{'IP': '226.14.168.44', 'Link': False, 'Exists': False, 'Path': '/mnt/cassandra/hint'},
{'IP': '226.14.168.44', 'Link': False, 'Exists': False, 'Path': '/mnt/cassandra/commitlog'},
{'IP': '222.53.239.60', 'Link': False, 'Exists': False, 'Path': '/mnt/heapdump'},
{'IP': '222.53.239.60', 'Link': False, 'Exists': False, 'Path': '/mnt/cassandra/saved_caches'},
{'IP': '222.53.239.60', 'Link': False, 'Exists': False, 'Path': '/mnt/cassandra/hint'},
{'IP': '222.53.239.60', 'Link': False, 'Exists': False, 'Path': '/mnt/cassandra/commitlog'}]
obj_Node_Folders = Node_Folders()
lst_tmp_Data = obj_Node_Folders.filterPath(lst_dict_Data, ['/mnt/cassandra/commitlog', '/mnt/cassandra/hint'])
lst_tmp_Data = obj_Node_Folders.sortPath(lst_tmp_Data)
json_lst_tmp_Data = json.dumps(lst_tmp_Data, indent=2)
print(json_lst_tmp_Data)
if __name__ == "__main__":
main()
#globals()[sys.argv[1]](sys.argv[1])
#python any_script.py main 1
main.groovy
import groovy.json.JsonSlurper
static void main(String[] args) {
def cmd_out = new StringBuilder();
def cmd_err = new StringBuilder();
def cmdArray = ["python", "any_script.py"]
Process cmd = cmdArray.execute()
cmd.waitFor()
cmd.consumeProcessOutput(cmd_out, cmd_err);
if (cmd_err.toString() == '') {
def str_Return = cmd_out.toString()
def obj_Json_parser = new JsonSlurper()
try {
def obj_json = obj_Json_parser.parseText(str_Return)
obj_json.each {item -> {
println(item)
}}
} catch (Exception ignored) {
println(str_Return)
}
}
else
{
println(cmd_err.toString())
}
}
Groovy script output.
[IP:21.96.62.214, Link:false, Exists:false, Path:/mnt/cassandra/commitlog]
[IP:21.96.62.214, Link:false, Exists:false, Path:/mnt/cassandra/hint]
[IP:122.190.69.93, Link:false, Exists:false, Path:/mnt/cassandra/commitlog]
[IP:122.190.69.93, Link:false, Exists:false, Path:/mnt/cassandra/hint]
[IP:174.40.30.197, Link:false, Exists:false, Path:/mnt/cassandra/commitlog]
[IP:174.40.30.197, Link:false, Exists:false, Path:/mnt/cassandra/hint]
[IP:222.53.239.60, Link:false, Exists:false, Path:/mnt/cassandra/commitlog]
[IP:222.53.239.60, Link:false, Exists:false, Path:/mnt/cassandra/hint]
[IP:226.14.168.44, Link:false, Exists:false, Path:/mnt/cassandra/commitlog]
[IP:226.14.168.44, Link:false, Exists:false, Path:/mnt/cassandra/hint]
[IP:253.102.1.103, Link:false, Exists:false, Path:/mnt/cassandra/commitlog]
[IP:253.102.1.103, Link:false, Exists:false, Path:/mnt/cassandra/hint]
Leave a Reply