How to access args in multiple languages
In an oJob it’s possible to use a mix of programming languages. In each you will probably want to access the args dictionary/map to perform different actions.
The following example uses the same args.file to count the corresponding files’ lines in Python, Perl, Shell script and OpenAF javascript and return the result in args.lines:
todo:
- Init python to be fair
- Count lines in python
- Count lines in shell
- Count lines in js
- Count lines in perl
- ojob final report
ojob:
includeOJob: true
jobs:
# -----------------------------
- name : Init python to be fair
lang : python
exec : |
print("args = " + _d(args)) # dumps the current args
# ----------------------------
- name : Count lines in python
to : ojob output
args : &OUTARGS
__key : args
__path: lines
check: &CHECKFILE
in:
file: isString
lang : python
exec : |
with open(args['file'], 'r') as f:
lines = 0
for line in f:
lines += 1
args['lines'] = lines
# ---------------------------
- name : Count lines in shell
to : ojob output
args : *OUTARGS
check: *CHECKFILE
lang : shell
exec : |
echo "{ lines: `wc -l < {{file}}` }"
# --------------------------
- name : Count lines in perl
to : ojob output
args : *OUTARGS
check: *CHECKFILE
lang : perl
exec : |
open(my $fh, '<', '{{file}}');
my $count = 0;
while (<$fh>) {
$count++;
}
print "{ lines: $count }";
# ------------------------
- name : Count lines in js
to : ojob output
args : *OUTARGS
check: *CHECKFILE
exec : |
args.lines = io.readFileAsArray(args.file).length
Then to execute:
ojob myexample.yaml file=myexample.yaml
OpenAF javascript
As expected the args map is available as a Javascript map to used.
Python
In oJob Python is a “first-class” language so after initializing it has the args dictionary available for use in Python.
the _d() function allows for converting any Python json or dictionary into the corresponding stringified version.
Shell and Perl script
For non OpenAF/Python languages the exec code is treated like an OpenAF template receiving the args map.
The output is parsed from a JSON output through stdout.