Skip to content

Commit

Permalink
CLI as own package (#205)
Browse files Browse the repository at this point in the history
* flexmeasures.cli as own top-level package; fix a few mypy complaints (we didn't check before as the code was under /scripts ...)

Co-authored-by: Nicolas Höning <nicolas@seita.nl>
  • Loading branch information
create-issue-branch[bot] and nhoening committed Oct 7, 2021
1 parent 5814940 commit 7bcde8d
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 24 deletions.
6 changes: 6 additions & 0 deletions flexmeasures/app.py
Expand Up @@ -97,6 +97,12 @@ def create(env: Optional[str] = None, path_to_config: Optional[str] = None) -> F

register_db_at(app)

# Register the CLI

from flexmeasures.cli import register_at as register_cli_at

register_cli_at(app)

# Register the API

from flexmeasures.api import register_at as register_api_at
Expand Down
Expand Up @@ -10,4 +10,4 @@ To view the available commands, run:
For help on individual commands, type `flexmesaures <command> --help`.
Structural data refers to database tables which do not contain time series data.

To create new commands, be sure to register any new file (containing the corresponding script) with the flask cli in `flexmeasures/data/__init__.py`.
To create new commands, be sure to register any new file (containing the corresponding script) with the flexmeasures CLI in `flexmeasures/cli/__init__.py`.
13 changes: 13 additions & 0 deletions flexmeasures/cli/__init__.py
@@ -0,0 +1,13 @@
from flask import Flask


def register_at(app: Flask):

if app.cli:
with app.app_context():
import flexmeasures.cli.jobs
import flexmeasures.cli.monitor
import flexmeasures.cli.data_add
import flexmeasures.cli.data_delete
import flexmeasures.cli.db_ops
import flexmeasures.cli.testing # noqa: F401
Expand Up @@ -68,7 +68,7 @@ def new_account_role(name: str, description: str):
@with_appcontext
@click.option("--name", required=True)
@click.option("--roles", help="e.g. anonymous,Prosumer,CPO")
def new_account(name: str, roles: List[str]):
def new_account(name: str, roles: str):
"""
Create an account for a tenant in the FlexMeasures platform.
"""
Expand Down Expand Up @@ -99,26 +99,29 @@ def new_account(name: str, roles: List[str]):
@click.option("--roles", help="e.g. anonymous,Prosumer,CPO")
@click.option(
"--timezone",
"timezone_optional",
help="timezone as string, e.g. 'UTC' or 'Europe/Amsterdam' (defaults to FLEXMEASURES_TIMEZONE config setting)",
)
def new_user(
username: str,
email: str,
account_id: int,
roles: List[str],
timezone: Optional[str],
timezone_optional: Optional[str],
):
"""
Create a FlexMeasures user.
The `users create` task from Flask Security Too is too simple for us.
Use this to add email, timezone and roles.
"""
if timezone is None:
timezone = app.config.get("FLEXMEASURES_TIMEZONE")
if timezone_optional is None:
timezone = app.config.get("FLEXMEASURES_TIMEZONE", "UTC")
print(
f"Setting user timezone to {timezone} (taken from FLEXMEASURES_TIMEZONE config setting)..."
)
else:
timezone = timezone_optional
try:
pytz.timezone(timezone)
except pytz.UnknownTimeZoneError:
Expand Down Expand Up @@ -536,17 +539,19 @@ def add_beliefs(
)
@click.option(
"--from-date",
"from_date_str",
default="2015-02-08",
help="Forecast from date (inclusive). Follow up with a date in the form yyyy-mm-dd.",
)
@click.option(
"--to-date",
"to_date_str",
default="2015-12-31",
help="Forecast to date (inclusive). Follow up with a date in the form yyyy-mm-dd.",
)
@click.option(
"--horizon",
"horizons",
"horizons_as_hours",
multiple=True,
type=click.Choice(["1", "6", "24", "48"]),
default=["1", "6", "24", "48"],
Expand All @@ -562,9 +567,9 @@ def add_beliefs(
def create_forecasts(
asset_type: str = None,
asset_id: int = None,
from_date: str = "2015-02-08",
to_date: str = "2015-12-31",
horizons: List[str] = ["1"],
from_date_str: str = "2015-02-08",
to_date_str: str = "2015-12-31",
horizons_as_hours: List[str] = ["1"],
as_job: bool = False,
):
"""
Expand All @@ -579,12 +584,12 @@ def create_forecasts(
"""
# make horizons
horizons = [timedelta(hours=int(h)) for h in horizons]
horizons = [timedelta(hours=int(h)) for h in horizons_as_hours]

# apply timezone:
timezone = app.config.get("FLEXMEASURES_TIMEZONE")
from_date = pd.Timestamp(from_date).tz_localize(timezone)
to_date = pd.Timestamp(to_date).tz_localize(timezone)
from_date = pd.Timestamp(from_date_str).tz_localize(timezone)
to_date = pd.Timestamp(to_date_str).tz_localize(timezone)

if as_job:
if asset_type == "Asset":
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Expand Up @@ -95,7 +95,7 @@ def test_generic_model(
asset: Optional[str] = None,
from_date: str = "2015-03-10",
period: int = 3,
horizon: int = 1,
horizon_hours: int = 1,
training: int = 30,
):
"""Manually test integration of timetomodel for our generic model."""
Expand All @@ -108,7 +108,7 @@ def test_generic_model(
start = as_server_time(datetime.strptime(from_date, "%Y-%m-%d"))
end = start + timedelta(days=period)
training_and_testing_period = timedelta(days=training)
horizon = timedelta(hours=horizon)
horizon = timedelta(hours=horizon_hours)

with app.app_context():
asset = (
Expand Down
10 changes: 0 additions & 10 deletions flexmeasures/data/__init__.py
Expand Up @@ -22,14 +22,4 @@ def register_at(app: Flask):

configure_auth(app, db)

if app.cli:
# Register some useful custom scripts with the flask cli
with app.app_context():
import flexmeasures.data.scripts.cli_tasks.jobs
import flexmeasures.data.scripts.cli_tasks.monitor
import flexmeasures.data.scripts.cli_tasks.data_add
import flexmeasures.data.scripts.cli_tasks.data_delete
import flexmeasures.data.scripts.cli_tasks.db_ops
import flexmeasures.data.scripts.cli_tasks.testing # noqa: F401

app.teardown_request(after_request_exception_rollback_session)
Empty file.

0 comments on commit 7bcde8d

Please sign in to comment.