Skip to content

Commit

Permalink
terste
Browse files Browse the repository at this point in the history
  • Loading branch information
luc10921 committed Mar 27, 2023
1 parent 0d2febf commit c67054a
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 34 deletions.
50 changes: 16 additions & 34 deletions boa3/cli.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,23 @@
import argparse
import logging
import os
import sys
from argparse import ArgumentParser

from boa3.boa3 import Boa3
from boa3.internal.exception.NotLoadedException import NotLoadedException
from boa3.cli_commands import commands


def main():
parser = argparse.ArgumentParser()
parser.add_argument("input", help=".py smart contract to compile")
parser.add_argument("-db", "--debug", action='store_true', help="generates a .nefdbgnfo file")
parser.add_argument("--project-path", help="Project root path. Path of the contract by default.", type=str)
args = parser.parse_args()

if not args.input.endswith(".py") or not os.path.isfile(args.input):
logging.error("Input file is not .py")
sys.exit(1)

fullpath = os.path.realpath(args.input)
path, filename = os.path.split(fullpath)

if not args.project_path:
args.project_path = os.path.dirname(path)

try:
Boa3.compile_and_save(args.input, debug=args.debug, root_folder=args.project_path)
logging.info(f"Wrote {filename.replace('.py', '.nef')} to {path}")
except NotLoadedException as e:
error_message = e.message
log_error = 'Could not compile'
if len(error_message) > 0:
log_error += f': {error_message}'

logging.error(log_error)
except Exception as e:
logging.exception(e)
n3boa = ArgumentParser()

n3_subparser = n3boa.add_subparsers(title='Commands')

# Initialize all commands on cli_commands
for command in commands:
command(n3_subparser).add_arguments_and_callback()

# read command line and get the correct command requested
args = n3boa.parse_args()

# execute command
if hasattr(args, 'func'):
args.func(vars(args))


if __name__ == "__main__":
Expand Down
17 changes: 17 additions & 0 deletions boa3/cli_commands/ICommand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from abc import ABC, abstractmethod
from argparse import ArgumentParser, _SubParsersAction


class ICommand(ABC):

def __init__(self, main_parser: _SubParsersAction, command_name: str, help_text: str):
self.parser: ArgumentParser = main_parser.add_parser(command_name, help=help_text)

@abstractmethod
def add_arguments_and_callback(self):
pass

@staticmethod
@abstractmethod
def execute_command(args: dict):
pass
7 changes: 7 additions & 0 deletions boa3/cli_commands/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from boa3.cli_commands.build_command import BuildCommand
from boa3.cli_commands.test_command import TestCommand

commands = (
BuildCommand,
TestCommand
)
49 changes: 49 additions & 0 deletions boa3/cli_commands/build_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import logging
import os
import sys
from argparse import _SubParsersAction

from boa3.cli_commands.ICommand import ICommand
from boa3.not_boa3 import Boa3
from boa3.internal.exception.NotLoadedException import NotLoadedException


class BuildCommand(ICommand):

def __init__(self, main_parser: _SubParsersAction):
super().__init__(main_parser, 'build', 'Builds your smart contract')

def add_arguments_and_callback(self):
self.parser.add_argument("input", help=".py smart contract to compile", type=str)
self.parser.add_argument("-db", "--debug", action='store_true', help="generates a .nefdbgnfo file")
self.parser.add_argument("--project-path", help="Project root path. Path of the contract by default.", type=str)
self.parser.set_defaults(func=self.execute_command)

@staticmethod
def execute_command(args: dict):
sc_path: str = args['input']
project_path: str = args['project_path']
debug: bool = args['debug']

if not sc_path.endswith(".py") or not os.path.isfile(sc_path):
logging.error("Input file is not .py")
sys.exit(1)

fullpath = os.path.realpath(sc_path)
path, filename = os.path.split(fullpath)

if not project_path:
project_path = os.path.dirname(path)

try:
Boa3.compile_and_save(sc_path, debug=debug, root_folder=project_path)
logging.info(f"Wrote {filename.replace('.py', '.nef')} to {path}")
except NotLoadedException as e:
error_message = e.message
log_error = 'Could not compile'
if len(error_message) > 0:
log_error += f': {error_message}'

logging.error(log_error)
except Exception as e:
logging.exception(e)
27 changes: 27 additions & 0 deletions boa3/cli_commands/test_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import sys
from argparse import _SubParsersAction

from boa3.cli_commands.ICommand import ICommand


class TestCommand(ICommand):

def __init__(self, main_parser: _SubParsersAction):
super().__init__(main_parser, 'test', 'TESTETSTESTSETSETSETSE')

def add_arguments_and_callback(self):
self.parser.add_argument("num1", help=".py smart contract to compile", type=int)
self.parser.add_argument("num2", help=".py smart contract to compile", type=int)
self.parser.set_defaults(func=self.execute_command)

@staticmethod
def execute_command(args: dict):
num1: int = args['num1']
num2: int = args['num2']

print("~"*10)
print("num1 = {0}, num2 = {1}".format(num1, num2))
print("result = {0}".format(num1 + num2))
print("~"*10)

sys.exit(0)

0 comments on commit c67054a

Please sign in to comment.