diff options
author | 2017-09-14 13:06:21 -0400 | |
---|---|---|
committer | 2017-09-14 14:50:51 -0400 | |
commit | 0d4c19a9389a933cf5b5e83173f97f8cd72b7f5e (patch) | |
tree | c160ab6cdaf600f8025d75e91898d133ebc000f8 /dcae-cli/dcae_cli/commands/data_format | |
parent | 3b22c6518e8dbb4a4ddf7aa615a8dccdc2d280b7 (diff) |
Merge in changes there were made since seeding
* Fix DR config keys issue
* Add data format generate command
* Improve error messaging
* Add in support for inputs otherwise known as "sourced at deployment"
Change-Id: I9d97c30aeba587315d7fd1a18c38f71d8199d42b
Issue-Id: DCAEGEN2-91
Signed-off-by: Michael Hwang <mhwang@research.att.com>
Diffstat (limited to 'dcae-cli/dcae_cli/commands/data_format')
-rw-r--r-- | dcae-cli/dcae_cli/commands/data_format/commands.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/dcae-cli/dcae_cli/commands/data_format/commands.py b/dcae-cli/dcae_cli/commands/data_format/commands.py index 9114688..b942442 100644 --- a/dcae-cli/dcae_cli/commands/data_format/commands.py +++ b/dcae-cli/dcae_cli/commands/data_format/commands.py @@ -26,12 +26,22 @@ import json import click +import genson + +import sys + +import os + +from jsonschema import Draft4Validator + from dcae_cli.util import load_json from dcae_cli.util.logger import get_logger + from dcae_cli.commands import util from dcae_cli.commands.util import create_table, parse_input from dcae_cli.catalog.exc import MissingEntry +from dcae_cli.catalog.exc import DcaeException logger = get_logger('DataFormatCommand') @@ -96,3 +106,59 @@ def publish(obj, data_format): click.echo("Data format has been published") else: click.echo("Data format could not be published") + +@data_format.command() +@click.option('--keywords', is_flag=True, help='Adds a template of possible descriptive keywords', default=False) +@click.argument('name_version', metavar="name:version", required = True) +@click.argument('file-or-dir-path', type=click.Path(resolve_path=True, exists=True, dir_okay=True, file_okay=True, readable=True), metavar="file-or-dir-path") +@click.pass_obj +def generate(obj, name_version, file_or_dir_path, keywords): + '''Create schema from a file or directory examples''' + name, version = parse_input(name_version) + if version == None: + version = "" + schema = genson.Schema() + if os.path.isfile(file_or_dir_path): + addfile(file_or_dir_path, schema) + else: + foundJSON = False + for root, dirs, files in os.walk(file_or_dir_path): + for filename in files: + fullfilename = os.path.join(file_or_dir_path, filename) + addfile(fullfilename,schema) + foundJSON = True + if foundJSON == False: + raise DcaeException('No JSON files found in ' + file_or_dir_path) + + json_obj = json.loads(schema.to_json()) + json_obj['$schema'] = "http://json-schema.org/draft-04/schema#" + jschema = json.dumps(json_obj) + jschema = jschema.replace('"required":', '"additionalproperties": true, "required":') + jschema = jschema.replace('"type":', ' "description": "", "type":') + + if (keywords): + jschema = jschema.replace('"type": "string"', ' "maxLength": 0, "minLength": 0, "pattern": "", "type": "string"') + jschema = jschema.replace('"type": "integer"', ' "maximum": 0, "mininimum": 0, "multipleOf": 0, "type": "integer"') + jschema = jschema.replace('"type": "array"', ' "maxItems": 0, "minItems": 0, "uniqueItems": "false", "type": "array"') + + jschema = '{ "self": { "name": "' + name + '", "version": "' + version + '", "description": ""} , "dataformatversion": "1.0.0", "jsonschema": ' + jschema + '}' + #Draft4Validator.check_schema(json.loads(jschema)) + try: + print(json.dumps(json.loads(jschema), sort_keys=True, indent=4 )) + except ValueError: + raise DcaeException('Problem with JSON generation') + +def addfile(filename, schema): + try: + fileadd = open(filename, "r") + except IOError: + raise DcaeException('Cannot open' + filename) + try: + json_object = json.loads(fileadd.read()) + schema.add_object(json_object) + except ValueError: + raise DcaeException('Bad JSON file: ' + filename) + finally: + fileadd.close() + + |