Skip to content

Commit

Permalink
Clean up #25
Browse files Browse the repository at this point in the history
  • Loading branch information
heming-h committed Jun 18, 2024
1 parent b6f1a51 commit 766400e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 40 deletions.
2 changes: 2 additions & 0 deletions vision-ai-service/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import time

import click
from dotenv import load_dotenv
from events_adapter import EventsAdapter
from exceptions import VideoStreamNotFoundException
from vision_ai_service_v2 import VisionAIService2

# get base settings
load_dotenv()
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
photos_file_path = os.getenv("PHOTOS_FILE_PATH", ".")
video_stream_url = os.getenv("VIDEO_URL")
Expand Down
44 changes: 4 additions & 40 deletions vision-ai-service/events_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,9 @@
class EventsAdapter:
"""Class representing events."""

def get_env(self, param_name: str) -> str:
"""Get environment variable. First check getenv(), then read from .env file.
Args:
param_name: The name of the environment variable to retrieve.
Returns:
The value of the environment variable, or None if the variable is not found.
Raises:
FileNotFoundError: If the .env file is not found.
Exception: If an error occurs while reading the .env file.
"""
env_param = os.getenv(param_name)
if env_param:
return env_param

env_file_path = os.path.join(os.getcwd(), ".env")
try:
with open(env_file_path, "r") as file:
lines = file.read()
env_list = lines.split("\n")
for line in env_list:
if line.startswith(param_name):
key, value = line.split("=")
env_param = value
except FileNotFoundError as e:
err_info = f"The .env file was not found at {env_file_path}"
logging.error(err_info)
raise FileNotFoundError(err_info) from e
except Exception as e:
err_info = f"Error loading env {param_name}. {e}"
logging.error(err_info)
raise Exception(err_info) from e
return env_param

def get_global_setting(self, param_name: str) -> str:
"""Get global settings from global_settings.json file."""
config_file = self.get_env("GLOBAL_SETTINGS_FILE")
config_file = os.getenv("GLOBAL_SETTINGS_FILE")
try:
with open(config_file, "r") as json_file:
settings = json.load(json_file)
Expand All @@ -72,7 +36,7 @@ def get_global_setting_bool(self, param_name: str) -> bool:
def get_video_service_status_messages(self) -> list:
"""Get video service status."""
video_status = []
config_file = self.get_env("VIDEO_STATUS_FILE")
config_file = os.getenv("VIDEO_STATUS_FILE")
try:
with open(config_file, "r") as json_file:
video_status = json.load(json_file)
Expand All @@ -89,7 +53,7 @@ def add_video_service_message(self, message: str) -> None:
current_time = datetime.now()
time_text = current_time.strftime("%H:%M:%S")
video_status = []
config_file = self.get_env("VIDEO_STATUS_FILE")
config_file = os.getenv("VIDEO_STATUS_FILE")
try:
with open(config_file, "r") as json_file:
old_status = json.load(json_file)
Expand All @@ -115,7 +79,7 @@ def add_video_service_message(self, message: str) -> None:

def update_global_setting(self, param_name: str, new_value: str) -> None:
"""Update global_settings file."""
config_file = self.get_env("GLOBAL_SETTINGS_FILE")
config_file = os.getenv("GLOBAL_SETTINGS_FILE")
try:
# Open the global settings file in read-only mode.
with open(config_file, "r") as json_file:
Expand Down

0 comments on commit 766400e

Please sign in to comment.