~ubuntu-branches/debian/sid/simpleitk/sid

« back to all changes in this revision

Viewing changes to ExpandTemplateGenerator/JSONQuery.lua

  • Committer: Package Import Robot
  • Author(s): Ghislain Antony Vaillant
  • Date: 2017-11-02 08:49:18 UTC
  • Revision ID: package-import@ubuntu.com-20171102084918-7hs09ih668xq87ej
Tags: upstream-1.0.1
ImportĀ upstreamĀ versionĀ 1.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
--!/usr/bin/env lua
 
2
--=========================================================================
 
3
--
 
4
--  Copyright Insight Software Consortium
 
5
--
 
6
--  Licensed under the Apache License, Version 2.0 (the "License");
 
7
--  you may not use this file except in compliance with the License.
 
8
--  You may obtain a copy of the License at
 
9
--
 
10
--         http://www.apache.org/licenses/LICENSE-2.0.txt
 
11
--
 
12
--  Unless required by applicable law or agreed to in writing, software
 
13
--  distributed under the License is distributed on an "AS IS" BASIS,
 
14
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
15
--  See the License for the specific language governing permissions and
 
16
--  limitations under the License.
 
17
--
 
18
--=========================================================================
 
19
 
 
20
-- This script parses a JSON file, and print specified json fields provided by JSON paths.
 
21
--
 
22
-- It is currently expected that path specified is the lua table access
 
23
-- string to the correct field. There currently is no real validation
 
24
-- or error checking.
 
25
 
 
26
package.path = debug.getinfo(1).source:match("@?(.*/)")..'?.lua;' .. package.path
 
27
 
 
28
require "json"
 
29
 
 
30
if #arg < 2 then
 
31
   print("usage: "..arg[0].." [json file] [json path]+")
 
32
   os.exit(1)
 
33
end
 
34
 
 
35
--print("Reading \""..arg[1].."\"...")
 
36
 
 
37
local f = assert(io.open(arg[1]))
 
38
 
 
39
local str = f:read("*all")
 
40
f:close()
 
41
 
 
42
json_table = decode(str)
 
43
 
 
44
bad_count = 0
 
45
 
 
46
for i=2,#arg do
 
47
    json_path=arg[i]
 
48
    cmd = loadstring("return json_table."..json_path)
 
49
    if not cmd then
 
50
        io.stderr:write("Warning: bad path -> "..json_path.."\n")
 
51
        bad_count = bad_count+1
 
52
    else
 
53
        out = cmd()
 
54
        if not out then
 
55
            io.stderr:write(json_path.." NOT FOUND\n")
 
56
        else
 
57
            print(json_path..": \""..cmd().."\"")
 
58
        end
 
59
    end
 
60
end
 
61
 
 
62
os.exit( bad_count )