Skip to content

Commit

Permalink
Working version for validate command CLI (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximilien authored Jan 30, 2025
1 parent a666513 commit 4981b7b
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ ipython_config.py
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
bee-hive/poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
Expand Down
1 change: 1 addition & 0 deletions bee-hive/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12.8
3 changes: 3 additions & 0 deletions bee-hive/beeAI
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

python3 ./cli/beeAI.py $@
43 changes: 43 additions & 0 deletions bee-hive/cli/beeAI.py
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)
30 changes: 30 additions & 0 deletions bee-hive/cli/cli.py
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")
78 changes: 78 additions & 0 deletions bee-hive/cli/commands.py
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
62 changes: 62 additions & 0 deletions bee-hive/cli/common.py
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()
2 changes: 2 additions & 0 deletions bee-hive/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ python = ">= 3.11, < 3.13"
pyyaml = "^6.0.2"
openai = "^1.56.2"
python-dotenv = "^1.0.1"
jsonschema = "^4.23.0"
docopt-ng = "^0.9.0"


[tool.poetry.group.dev.dependencies]
Expand Down
6 changes: 3 additions & 3 deletions test/schema/funnier_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ spec:
- name: colleague
agent: colleague
condition:
- if: { input.find('funnier') != -1 }
then: expert
else: end
- if: (input.find('funnier') != -1)
then: expert
else: end
23 changes: 0 additions & 23 deletions test/schema/validator.py

This file was deleted.

50 changes: 50 additions & 0 deletions tools/cli/beeAI.py
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)
11 changes: 8 additions & 3 deletions tools/workflow_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
"description": "workflow labels, key: value pairs"
}
},
"required": ["name"]
"required": [
"name"
]
},
"spec": {
"type": "object",
Expand Down Expand Up @@ -110,10 +112,13 @@
}
}
},
"required": ["name", "agent"]
"required": [
"name",
"agent"
]
}
}
}
}
}
}
}

0 comments on commit 4981b7b

Please sign in to comment.