Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
Quality of life improvements. (#40)
Browse files Browse the repository at this point in the history
* When using no contract, use the first available and warn user. (Before, when no contract specified, the app crashed.)
* Why not use only one dockerfile ? I've added the mqtt output in the same dockerfile and moved to the root. In the long run, I think having only one docker will be easier for users.
* Added a way to use environment variables instead of cli. This is the way to go for docker/k8s as environment are the principal way to configure an app.
  • Loading branch information
ncareau authored and titilambert committed Oct 27, 2019
1 parent 4c40b97 commit c7953bd
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ dist/
htmlcov/
.pytest_cache/
.tox/
.coverage*
.coverage*
2 changes: 1 addition & 1 deletion docker/cli/Dockerfile → Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install -r requirements.txt --force-reinstall --no-cache-dir

COPY docker/cli/entrypoint.sh .
COPY ./entrypoint.sh .

COPY . .

Expand Down
18 changes: 0 additions & 18 deletions docker/mqtt/Dockerfile

This file was deleted.

10 changes: 0 additions & 10 deletions docker/mqtt/entrypoint.sh

This file was deleted.

12 changes: 11 additions & 1 deletion docker/cli/entrypoint.sh → entrypoint.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,14 @@ then
PYHQ_CMD_CONTRACT="-c $PYHQ_CONTRACT"
fi

pyhydroquebec -u $PYHQ_USER -p $PYHQ_PASSWORD $PYHQ_CMD_OUTPUT $PYHQ_CMD_CONTRACT
# Config
if [ -z "$CONFIG" ]
then
export CONFIG="/etc/pyhydroquebec/pyhydroquebec.yaml"
fi

if [ "$PYHQ_OUTPUT" = "MQTT"]
mqtt_pyhydroquebec
else
pyhydroquebec -u $PYHQ_USER -p $PYHQ_PASSWORD $PYHQ_CMD_OUTPUT $PYHQ_CMD_CONTRACT
fi
29 changes: 24 additions & 5 deletions pyhydroquebec/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import datetime, timedelta
from pprint import pprint
import sys
import os

from pyhydroquebec.client import HydroQuebecClient
from pyhydroquebec.consts import REQUESTS_TIMEOUT, HQ_TIMEZONE
Expand All @@ -17,8 +18,11 @@ async def fetch_data(client, contract_id, fetch_hourly=False):
"""Fetch data for basic report."""
await client.login()
for customer in client.customers:
if customer.contract_id != contract_id:
if customer.contract_id != contract_id and contract_id is not None:
continue
if contract_id is None:
client.logger.warn("Contract id not specified, using first available.")

await customer.fetch_current_period()
await customer.fetch_annual_data()
await customer.fetch_monthly_data()
Expand Down Expand Up @@ -100,23 +104,38 @@ def main():
print(VERSION)
return 0

if not args.username or not args.password:
# Check input for Username, Password and Contract - CLI overwrite ENV variable

# Check Env
hydro_user = os.environ.get("PYHQ_USER")
hydro_pass = os.environ.get("PYHQ_PASSWORD")
hydro_contract = os.environ.get("PYHQ_CONTRACT")

# Check Cli
if args.username:
hydro_user = args.username
if args.password:
hydro_pass = args.password
if args.contract:
hydro_contract = args.contract

if not hydro_user or not hydro_pass:
parser.print_usage()
print("pyhydroquebec: error: the following arguments are required: "
"-u/--username, -p/--password")
return 3

client = HydroQuebecClient(args.username, args.password,
client = HydroQuebecClient(hydro_user, hydro_pass,
args.timeout, log_level=args.log_level)
loop = asyncio.get_event_loop()

# Get the async_func
if args.list_contracts:
async_func = list_contracts(client)
elif args.dump_data:
async_func = dump_data(client, args.contract)
async_func = dump_data(client, hydro_contract)
elif args.detailled_energy is False:
async_func = fetch_data(client, args.contract, args.hourly)
async_func = fetch_data(client, hydro_contract, args.hourly)
else:
start_date = datetime.strptime(args.start_date, '%Y-%m-%d')
end_date = datetime.strptime(args.end_date, '%Y-%m-%d')
Expand Down

0 comments on commit c7953bd

Please sign in to comment.