-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working version for
validate
command CLI (#152)
- Loading branch information
1 parent
a666513
commit 4981b7b
Showing
12 changed files
with
281 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3.12.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
|
||
python3 ./cli/beeAI.py $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright © 2025 IBM | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""BeeAI | ||
Usage: | ||
beeAI validate SCHEMA_FILE YAML_FILE [options] | ||
beeAI (-h | --help) | ||
beeAI (-v | --version) | ||
Options: | ||
--verbose Show all output. | ||
-h --help Show this screen. | ||
-v --version Show version. | ||
""" | ||
import os, sys, traceback | ||
|
||
from docopt import docopt | ||
from cli import * | ||
|
||
if __name__ == '__main__': | ||
args = docopt(__doc__, version='beeAI CLI v0.0.0') | ||
command = CLI(args).command() | ||
rc = command.execute() | ||
if rc != 0: | ||
Console.error("executing command: {rc}".format(rc=rc)) | ||
sys.exit(rc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright © 2025 IBM | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import io, sys | ||
|
||
from commands import * | ||
from common import * | ||
|
||
class CLI: | ||
def __init__(self, args): | ||
self.args = args | ||
if self.args['--verbose']: | ||
VERBOSE = True | ||
|
||
def command(self): | ||
if self.args.get('validate') and self.args['validate']: | ||
return Validate(self.args) | ||
else: | ||
raise Exception("Invalid command") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Copyright © 2025 IBM | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import io, sys, yaml, json, jsonschema | ||
|
||
from common import Console | ||
|
||
# Base class for all commands | ||
class Command: | ||
def __init__(self, args): | ||
self.args = args | ||
|
||
def println(self, msg): | ||
self.print(msg + "\n") | ||
|
||
def print(self, msg): | ||
Console.print(msg) | ||
|
||
def warn(self, msg): | ||
Console.warn(msg) | ||
|
||
def verbose(self): | ||
return self.args['--verbose'] | ||
|
||
def execute(self): | ||
func = self.dispatch() | ||
rc = func() | ||
if rc == None: | ||
return 0 | ||
else: | ||
if isinstance(rc, int): | ||
return rc | ||
else: | ||
return 1 | ||
|
||
def dispatch(self): | ||
if self.args['validate']: | ||
return self.validate | ||
else: | ||
raise Exception("Invalid subcommand") | ||
|
||
# validate command group | ||
class Validate(Command): | ||
def __init__(self, args): | ||
self.args = args | ||
super().__init__(self.args) | ||
|
||
def SCHEMA_FILE(self): | ||
return self.args['SCHEMA_FILE'] | ||
|
||
def YAML_FILE(self): | ||
return self.args['SCHEMA_FILE'] | ||
|
||
def name(self): | ||
return "validate" | ||
|
||
def validate(self): | ||
Console.print("validate {yaml_file} with schema {schema_file}".format(yaml_file=self.YAML_FILE(), schema_file=self.SCHEMA_FILE())) | ||
with open(self.SCHEMA_FILE(), 'r') as f: | ||
schema = json.load(f) | ||
with open(self.YAML_FILE(), 'r') as f: | ||
yamls = yaml.safe_load_all(f) | ||
for yaml_data in yamls: | ||
json_data = json.dumps(yaml_data, indent=4) | ||
jsonschema.validate(yaml_data, schema) | ||
Console.print("YAML file is valid.") | ||
return 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Copyright © 2025 IBM | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import sys | ||
from random import randint | ||
|
||
VERBOSE=False | ||
|
||
class Colors: | ||
HEADER = '\033[95m' | ||
OKBLUE = '\033[94m' | ||
OKGREEN = '\033[92m' | ||
WARNING = '\033[93m' | ||
FAIL = '\033[91m' | ||
ENDC = '\033[0m' | ||
BOLD = '\033[1m' | ||
UNDERLINE = '\033[4m' | ||
|
||
class Console: | ||
def verbose(msg): | ||
if VERBOSE: | ||
print(f"{Colors.OKBLUE}{msg}{Colors.ENDC}".format(msg=str(msg))) | ||
|
||
def print(msg=''): | ||
print(msg) | ||
|
||
def println(no=1): | ||
for i in range(no): | ||
print() | ||
|
||
def ok(msg): | ||
print(f"{Colors.OKGREEN}{msg}{Colors.ENDC}".format(msg=str(msg))) | ||
|
||
def error(msg): | ||
Console.fail(msg) | ||
|
||
def fail(msg): | ||
print(f"{Colors.FAIL}Error: {msg}{Colors.ENDC}".format(msg=str(msg))) | ||
|
||
def warn(msg): | ||
print(f"{Colors.WARNING}Warning: {msg}{Colors.ENDC}".format(msg=str(msg))) | ||
|
||
def progress(count, total, status=''): | ||
bar_len = 60 | ||
filled_len = int(round(bar_len * count / float(total))) | ||
|
||
percents = round(100.0 * count / float(total), 1) | ||
bar = '=' * filled_len + '-' * (bar_len - filled_len) | ||
|
||
sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percents, '%', status)) | ||
sys.stdout.flush() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright © 2025 IBM | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""BeeAI | ||
Usage: | ||
beeAI validate SCHEMA_FILE YAML_FILE [options] | ||
beeAI (-h | --help) | ||
beeAI (-v | --version) | ||
Options: | ||
--verbose Show all output. | ||
-h --help Show this screen. | ||
-v --version Show version. | ||
""" | ||
import os, sys, traceback | ||
|
||
import warnings | ||
|
||
# TODO: remoce this after solving doctopt warnings or using different CLI library | ||
with warnings.catch_warnings(): | ||
warnings.simplefilter("ignore", SyntaxWarning) | ||
|
||
from docopt import docopt | ||
|
||
from cli import * | ||
|
||
if __name__ == '__main__': | ||
args = docopt(__doc__, version='beeAI CLI v0.0.0') | ||
command = CLI(args).command() | ||
rc = command.execute() | ||
if rc != 0: | ||
Console.error("executing command: {rc}".format(rc=rc)) | ||
sys.exit(rc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters