diff --git a/halfapi/cli/cli.py b/halfapi/cli/cli.py index 28c4189..6c2239e 100644 --- a/halfapi/cli/cli.py +++ b/halfapi/cli/cli.py @@ -31,6 +31,7 @@ if IS_PROJECT: from . import config from . import domain from . import run - +elif IS_HOP_PROJECT: + from . import init_hop else: from . import init diff --git a/halfapi/cli/init_hop.py b/halfapi/cli/init_hop.py new file mode 100644 index 0000000..b33fb9c --- /dev/null +++ b/halfapi/cli/init_hop.py @@ -0,0 +1,33 @@ +import os +from configparser import ConfigParser +from half_orm.model import Model + +import click +from .cli import cli + +@cli.command() +def init(): + """ + The "halfapi init" command for hop projects + """ + hop_conf_path = os.path.join('.hop', 'config') + config = ConfigParser() + config.read([ hop_conf_path ]) + + assert os.path.isdir(config.get('halfORM', 'package_name')) + + model = Model(config.get('halfORM', 'package_name')) + + import halfapi + halfapi_path = list(halfapi.__path__)[0] + sql_path = os.path.join(halfapi_path, 'sql', 'api.sql') + + with open(sql_path, 'r') as sql_file: + for query in ''.join(sql_file.readlines()).split(';'): + if len(query.strip()) == 0: + continue + model.execute_query(query.strip()) + + click.echo('halfapi schema has been initialized') + click.echo('use halfapi route command to create your first route') + click.echo('example : halfapi route add') diff --git a/halfapi/conf.py b/halfapi/conf.py index a2a496f..c1fa999 100644 --- a/halfapi/conf.py +++ b/halfapi/conf.py @@ -60,6 +60,10 @@ is_project = lambda: os.path.isfile(CONF_FILE) + + + + config = ConfigParser(allow_no_value=True) CONF_DIR = environ.get('HALFAPI_CONF_DIR', '/etc/half_api') diff --git a/setup.py b/setup.py index cbef4ad..391b3a0 100755 --- a/setup.py +++ b/setup.py @@ -49,7 +49,8 @@ setup( "uvicorn>=0.13,<1", "orjson>=3.4.7,<4", "pyyaml>=5.3.1,<6", - "timing-asgi>=0.2.1,<1" + "timing-asgi>=0.2.1,<1", + "half_orm>=0.5.0" ], classifiers=[ "Development Status :: 3 - Alpha", @@ -67,6 +68,9 @@ setup( "pylint" ] }, + package_data={ + 'halfapi': ['sql/*.sql'] + }, entry_points={ "console_scripts":[ "halfapi=halfapi.cli.cli:cli"