Skip to content

Commit

Permalink
fix: use env vars from constants throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
nampereira committed Jan 5, 2024
1 parent 9bc081d commit adadfd1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 41 deletions.
29 changes: 20 additions & 9 deletions arena/arena_mqtt.py
Expand Up @@ -13,6 +13,14 @@
from .auth import ArenaAuth
from .event_loop import *
from .utils import ProgramStats

from .env_vars import (
MQTTH,
REALM,
ARENA_USERNAME,
ARENA_PASSWORD,
NAMESPACE
)
class ArenaMQTT(object):
"""
Wrapper around Paho MQTT client and EventLoop.
Expand All @@ -33,8 +41,8 @@ def __init__(
debug = False,
**kwargs
):
if os.environ.get("MQTTH"):
self.web_host = os.environ["MQTTH"]
if os.environ.get(MQTTH):
self.web_host = os.environ[MQTTH]
print(f"Using Host from 'MQTTH' env variable: {self.web_host}")
elif "host" in kwargs and kwargs["host"]:
self.web_host = kwargs["host"]
Expand All @@ -43,8 +51,8 @@ def __init__(
# Use default "web_host", helps avoid and web vs mqtt host and other user setup confusion
self.web_host = web_host

if os.environ.get("REALM"):
self.realm = os.environ["REALM"]
if os.environ.get(REALM):
self.realm = os.environ[REALM]
print(f"Using Realm from 'REALM' env variable: {self.realm}")
elif "realm" in kwargs and kwargs["realm"]:
self.realm = kwargs["realm"]
Expand All @@ -61,10 +69,10 @@ def __init__(
token = None
self.remote_auth_token = {} # provide reference for downloaded token
self.auth = ArenaAuth()
if os.environ.get("ARENA_USERNAME") and os.environ.get("ARENA_PASSWORD"):
if os.environ.get(ARENA_USERNAME) and os.environ.get(ARENA_PASSWORD):
# auth 1st: use passed in env var
self.username = os.environ["ARENA_USERNAME"]
token = os.environ["ARENA_PASSWORD"]
self.username = os.environ[ARENA_USERNAME]
token = os.environ[ARENA_PASSWORD]
self.auth.store_environment_auth(self.username, token)
else:
if self.scene:
Expand All @@ -80,8 +88,8 @@ def __init__(
# auth 3rd: use the user account online
self.username = self.auth.authenticate_user(self.web_host)

if os.environ.get("NAMESPACE"):
self.namespace = os.environ["NAMESPACE"]
if os.environ.get(NAMESPACE):
self.namespace = os.environ[NAMESPACE]
elif "namespace" not in kwargs or ("namespace" in kwargs and kwargs["namespace"] is None):
self.namespace = self.username
else:
Expand Down Expand Up @@ -336,3 +344,6 @@ def message_callback_remove(self, sub):
"""Unsubscribes to topic and removes callback"""
self.mqttc.unsubscribe(sub)
self.mqttc.message_callback_remove(sub)

def stats_update(self):
raise NotImplementedError("Must override stats_update")
7 changes: 5 additions & 2 deletions arena/device.py
Expand Up @@ -5,6 +5,9 @@

from .arena_mqtt import ArenaMQTT

from .env_vars import (
DEVICE,
)

class Device(ArenaMQTT):
"""
Expand Down Expand Up @@ -39,8 +42,8 @@ def __init__(
if self.args["debug"]:
debug = self.args["debug"]

if os.environ.get("DEVICE"):
self.device = os.environ["DEVICE"]
if os.environ.get(DEVICE):
self.device = os.environ[DEVICE]
print(f"Using Device from 'DEVICE' env variable: {self.device}")
elif "device" in kwargs and kwargs["device"]:
if re.search("/", kwargs["device"]):
Expand Down
28 changes: 1 addition & 27 deletions arena/env_vars.py
@@ -1,5 +1,5 @@
# Environment variables definitions
# Variable defaults defined by ENV_DEFAULTS (scroll down)
# When applicable, cariable defaults are defined by ENV_DEFAULTS
#

MQTTH = "MQTTH"
Expand All @@ -8,8 +8,6 @@
The :envvar:`MQTTH` defines the MQTT host used by the library.
This variable overrides arguments passed in the command line.
Default: None
"""

ARENA_USERNAME = "ARENA_USERNAME"
Expand All @@ -19,7 +17,6 @@
The :envvar:`ARENA_USERNAME` defines username used to authenticate.
This variable overrides arguments passed in the command line.
Default: None
"""

ARENA_PASSWORD = "ARENA_PASSWORD"
Expand All @@ -28,8 +25,6 @@
The :envvar:`ARENA_PASSWORD` defines password used to authenticate.
This variable overrides arguments passed in the command line.
Default: None
"""

REALM = "REALM"
Expand All @@ -40,8 +35,6 @@
After connecting, the library listens to a scene topic as follows:
`{REALM}/s/{NAMESPACE}/{SCENE}`.
This variable overrides arguments passed in the command line.
Default: None
"""

SCENE = "SCENE"
Expand All @@ -52,8 +45,6 @@
After connecting, the library listens to a scene topic as follows:
`{REALM}/s/{NAMESPACE}/{SCENE}`.
This variable overrides arguments passed in the command line.
Default: None
"""

NAMESPACE = "NAMESPACE"
Expand All @@ -64,8 +55,6 @@
After connecting, the library listens to a scene topic as follows:
`{REALM}/s/{NAMESPACE}/{SCENE}`.
This variable overrides arguments passed in the command line.
Default: None
"""

DEVICE = "DEVICE"
Expand All @@ -77,8 +66,6 @@
`{REALM}/d/{NAMESPACE}/{SCENE}`.
This variable overrides arguments passed in the command line.
Default: None
"""

PROGRAM_OBJECT_ID = "PROGRAM_OBJECT_ID"
Expand All @@ -87,8 +74,6 @@
The :envvar:`PROGRAM_OBJECT_ID` indicates the object id in ARENA persist for this program.
This is passed by the runtime and used to identify the program object that represents the currently running program.
Default: None
"""

ENABLE_INTERPRETER = "ENABLE_INTERPRETER"
Expand All @@ -109,8 +94,6 @@
The :envvar:`ARENA_TELEMETRY` environment variable enables the library's telemetry to generate
traces, metrics, and logs. Set this variable with a value of `otlp`, `mqtt` or `console` (case insensitive)
to enable telemetry using OpenTelemetry (OTEL) and its Protocol (OTLP), send JSON OTEL spans to MQTT, or to the console.
Default: None
"""

OTLP_ENDPOINT = "OTLP_ENDPOINT"
Expand All @@ -136,16 +119,7 @@

# env variables defaults
ENV_DEFAULTS = {
MQTTH: None,
ARENA_USERNAME: None,
ARENA_PASSWORD: None,
REALM: None,
SCENE: None,
NAMESPACE: None,
DEVICE: None,
PROGRAM_OBJECT_ID: None,
ENABLE_INTERPRETER: 'false',
ARENA_TELEMETRY: None,
OTLP_ENDPOINT: 'http://localhost:4317',
OTEL_LOG_LEVEL: 'info',
}
9 changes: 6 additions & 3 deletions arena/scene.py
Expand Up @@ -13,6 +13,9 @@
from .objects import *
from .utils import Utils, ArenaTelemetry, ArenaCmdInterpreter

from .env_vars import (
SCENE,
)
class Scene(ArenaMQTT):
"""
Gives access to an ARENA scene.
Expand Down Expand Up @@ -63,8 +66,8 @@ def __init__(
if self.args["debug"]:
debug = self.args["debug"]

if os.environ.get("SCENE"):
self.scene = os.environ["SCENE"]
if os.environ.get(SCENE):
self.scene = os.environ[SCENE]
print(f"Using Scene from 'SCENE' env variable: {self.scene}")
elif "scene" in kwargs and kwargs["scene"]:
if re.search("/", kwargs["scene"]):
Expand Down Expand Up @@ -555,7 +558,7 @@ def get_user_list(self):
"""Returns a list of users"""
return self.users.values()

def stats_update(self, running=True):
def stats_update(self):
"""Callbak when program stats are updated"""
obj = Program.running_instance_stats(self.stats.get_stats())
if obj:
Expand Down

0 comments on commit adadfd1

Please sign in to comment.