[cli] add "halfapi init" for hop projects, that creates an "halfapi" schema in the db

Read halfapi/sql/api.sql to see the structure
This commit is contained in:
Maxime Alves LIRMM@home 2021-06-13 14:58:56 +02:00
parent f27b68e350
commit 6503601c60
4 changed files with 44 additions and 2 deletions

View File

@ -31,6 +31,7 @@ if IS_PROJECT:
from . import config from . import config
from . import domain from . import domain
from . import run from . import run
elif IS_HOP_PROJECT:
from . import init_hop
else: else:
from . import init from . import init

33
halfapi/cli/init_hop.py Normal file
View File

@ -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')

View File

@ -60,6 +60,10 @@ is_project = lambda: os.path.isfile(CONF_FILE)
config = ConfigParser(allow_no_value=True) config = ConfigParser(allow_no_value=True)
CONF_DIR = environ.get('HALFAPI_CONF_DIR', '/etc/half_api') CONF_DIR = environ.get('HALFAPI_CONF_DIR', '/etc/half_api')

View File

@ -49,7 +49,8 @@ setup(
"uvicorn>=0.13,<1", "uvicorn>=0.13,<1",
"orjson>=3.4.7,<4", "orjson>=3.4.7,<4",
"pyyaml>=5.3.1,<6", "pyyaml>=5.3.1,<6",
"timing-asgi>=0.2.1,<1" "timing-asgi>=0.2.1,<1",
"half_orm>=0.5.0"
], ],
classifiers=[ classifiers=[
"Development Status :: 3 - Alpha", "Development Status :: 3 - Alpha",
@ -67,6 +68,9 @@ setup(
"pylint" "pylint"
] ]
}, },
package_data={
'halfapi': ['sql/*.sql']
},
entry_points={ entry_points={
"console_scripts":[ "console_scripts":[
"halfapi=halfapi.cli.cli:cli" "halfapi=halfapi.cli.cli:cli"