Michael Lando | f5f13c4 | 2017-02-19 12:35:04 +0200 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | ############################################################################## |
| 4 | ### |
| 5 | ### parse-json.py |
| 6 | ### |
| 7 | ### A utility to parse a cassnadra-commands file and return the commands per type |
| 8 | ### An Example for a json file: |
| 9 | ### { |
| 10 | ### "create":{ |
| 11 | ### "choice_or_other":"CREATE TYPE IF NOT EXISTS choice_or_other (results text)", |
| 12 | ### "vendor_license_model": "CREATE TABLE IF NOT EXISTS vendor_license_model (vlm_id text PRIMARY KEY, name text, description text, icon text)", |
| 13 | ### "license_agreement": "CREATE TABLE IF NOT EXISTS license_agreement (vlm_id text, la_id text, name text, description text, type text, contract text, req_const text, fg_ids set<text>, PRIMARY KEY (vlm_id, la_id))", |
| 14 | ### "feature_group": "CREATE TABLE IF NOT EXISTS feature_group (vlm_id text, fg_id text, name text, description text, ep_ids set<text>, lkg_ids set<text>, refd_by_las set<text>, PRIMARY KEY (vlm_id, fg_id))", |
| 15 | ### "license_key_group": "CREATE TABLE IF NOT EXISTS license_key_group (vlm_id text,lkg_id text,name text,description text, type text, operational_scope text, ref_fgs set<text>,PRIMARY KEY (vlm_id, lkg_id))", |
| 16 | ### } |
| 17 | ### } |
| 18 | ### |
| 19 | ### The return for "create" will be: |
| 20 | ### CREATE TYPE IF NOT EXISTS choice_or_other (results text) |
| 21 | ### CREATE TABLE IF NOT EXISTS vendor_license_model (vlm_id text PRIMARY KEY, name text, description text, icon text) |
| 22 | ### CREATE TABLE IF NOT EXISTS license_agreement (vlm_id text, la_id text, name text, description text, type text, contract text, req_const text, fg_ids set<text>, PRIMARY KEY (vlm_id, la_id)) |
| 23 | ### CREATE TABLE IF NOT EXISTS feature_group (vlm_id text, fg_id text, name text, description text, ep_ids set<text>, lkg_ids set<text>, refd_by_las set<text>, PRIMARY KEY (vlm_id, fg_id)) |
| 24 | ### CREATE TABLE IF NOT EXISTS license_key_group (vlm_id text,lkg_id text,name text,description text, type text, operational_scope text, ref_fgs set<text>,PRIMARY KEY (vlm_id, lkg_id)) |
| 25 | ### Usage: |
| 26 | ### |
| 27 | ### parse-json.py -t create -f cassandra-commands.json |
| 28 | ### |
| 29 | ### For example: |
| 30 | ### |
| 31 | ### |
| 32 | ### Author: Avi Ziv |
| 33 | ### Version 1.0 |
| 34 | ### Date: 3 May 2016 |
| 35 | ### |
| 36 | ############################################################################## |
| 37 | |
| 38 | import sys, getopt |
| 39 | import json as json |
| 40 | from collections import OrderedDict |
| 41 | |
| 42 | |
| 43 | def readJsonFile(file, type): |
| 44 | with open(file, 'r') as f: |
| 45 | data = json.load(f, object_pairs_hook=OrderedDict) |
| 46 | return data[type] |
| 47 | |
| 48 | def printJsonTypeEntries(jsonData): |
| 49 | for i in jsonData.keys(): |
| 50 | print jsonData[i] + ';' |
| 51 | |
| 52 | |
| 53 | def usage(): |
| 54 | print 'parseJsonFile.py [-f <json-file> & -t <cql-type: drop|create|insert|update|select]' |
| 55 | |
| 56 | def main(argv): |
| 57 | action = '' |
| 58 | |
| 59 | try: |
| 60 | opts, args = getopt.getopt(argv, "h:f:t:") |
| 61 | except getopt.GetoptError: |
| 62 | usage() |
| 63 | sys.exit(2) |
| 64 | for opt, arg in opts: |
| 65 | if opt == '-h': |
| 66 | usage() |
| 67 | sys.exit() |
| 68 | elif opt == '-f': |
| 69 | jsonFile = arg |
| 70 | action = 'file' |
| 71 | elif opt == '-t': |
| 72 | type = arg |
| 73 | |
| 74 | if action == 'file': |
| 75 | sJson = readJsonFile(jsonFile, type) |
| 76 | printJsonTypeEntries(sJson) |
| 77 | sys.exit() |
| 78 | else: |
| 79 | usage() |
| 80 | |
| 81 | |
| 82 | if __name__ == "__main__": |
| 83 | main(sys.argv[1:]) |