Skip to content

Commit

Permalink
Merge pull request #4 from mgorsk1/improvement/new_config_approach
Browse files Browse the repository at this point in the history
♻️ existing code
  • Loading branch information
mgorsk1 authored Nov 9, 2019
2 parents 0e40f15 + 3179e2e commit f33ae38
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 51 deletions.
4 changes: 3 additions & 1 deletion app/analyzers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def __init__(self, *args, **kwargs):
# in seconds
self.grace_period = kwargs.get('grace_period', 60)

self.tdb = TemporaryDatabase(config.get('TDB_HOST'), config.get('TDB_PORT'), **dict(db_pass=getenv('TDB_PASS')))
self.tdb = TemporaryDatabase(config.temporaryDb.connection.host,
config.temporaryDb.connection.port,
**dict(db_pass=config.temporaryDb.connection.auth.password))

self.lock = Lock()

Expand Down
22 changes: 15 additions & 7 deletions app/config.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
from json import loads
from os import path, getenv
from app.logger import prepare
from os import path
from piny import YamlLoader
from types import SimpleNamespace

from .logger import prepare

env = getenv('ENV', 'local')
class NestedNamespace(SimpleNamespace):
def __init__(self, dictionary, **kwargs):
super().__init__(**kwargs)
for key, value in dictionary.items():
if isinstance(value, dict):
self.__setattr__(key, NestedNamespace(value))
else:
self.__setattr__(key, value)


BASE_PATH = path.dirname(__file__) + '/..'

with open('{}/config/app/{}.json'.format(BASE_PATH, env), 'r') as f:
config = loads(f.read())
config = NestedNamespace(YamlLoader(path='{}/config/application/application.yaml'.format(BASE_PATH)).load())

log = prepare('DEBUG', '/tmp', 'VideoAnalysis')
log = prepare(config.log.level.upper(), '/tmp', 'VideoAnalysis')
30 changes: 10 additions & 20 deletions app/executors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ def __init__(self, *args, **kwargs):

self.index = 'video-analysis-{}'.format(self.bucket_folder_name)

self.rdb = ResultDatabase(config.get('RDB_HOST'),
config.get('RDB_PORT'), self.index,
**dict(db_user=config.get('RDB_USER'),
db_pass=getenv('RDB_PASS'),
db_scheme=config.get('RDB_SCHEME')))
self.rdb = ResultDatabase(config.resultDb.connection.host,
config.resultDb.connection.port, self.index,
**dict(db_user=config.resultDb.connection.auth.user,
db_pass=config.resultDb.connection.auth.password,
db_scheme=config.resultDb.connection.scheme))

self.tdb = TemporaryDatabase(config.get('TDB_HOST'), config.get('TDB_PORT'), **dict(db_pass=getenv('TDB_PASS')))
self.tdb = TemporaryDatabase(config.temporaryDb.connection.host,
config.temporaryDb.connection.port,
**dict(db_pass=config.temporaryDb.connection.auth.password))

self.save_file = None

self.bucket = None

self.pushover_config = None

def take_action(self, value, confidence, image, **kwargs):
run_uuid = uuid4()

Expand Down Expand Up @@ -118,14 +118,11 @@ def notify(self, title, message, url):
:param url: optional url to pass
:return: -
"""
if self.pushover_config is None:
self._setup_pushover()

result = None
device = "video-analysis"

request_data = dict(token=self.pushover_config.get("APP_TOKEN", ""),
user=self.pushover_config.get("USER_KEY", ""),
request_data = dict(token=config.pushover.appToken,
user=config.pushover.userKey,
message=message,
device=device,
title=title,
Expand Down Expand Up @@ -174,13 +171,6 @@ def _setup_gcp(self):

log.info("#finished #gcp setup")

def _setup_pushover(self):
log.info("#starting #pushover #setup")

self.pushover_config = dict(APP_TOKEN=getenv("PUSHOVER_APP_TOKEN"), USER_KEY=getenv("PUSHOVER_USER_KEY"))

log.info("#finished #pushover #setup")

@staticmethod
def save_image_locally(value, image, uuid, folder, ext):
filename = "{}_{}.{}".format(value, uuid, ext)
Expand Down
Empty file added app/video/prospector.py
Empty file.
9 changes: 0 additions & 9 deletions config/app/cloud.json

This file was deleted.

7 changes: 0 additions & 7 deletions config/app/docker.json

This file was deleted.

7 changes: 0 additions & 7 deletions config/app/local.json

This file was deleted.

24 changes: 24 additions & 0 deletions config/application/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
resultDb:
connection:
scheme: ${RESULT_DB_CONN_SCHEME:-http}
host: ${RESULT_DB_CONN_HOST:-localhost}
port: ${RESULT_DB_CONN_PORT:-9200}
auth:
user: ${RESULT_DB_CONN_AUTH_USER}
password: ${RESULT_DB_CONN_AUTH_PASSWORD}

temporaryDb:
connection:
scheme: ${RESULT_DB_CONN_SCHEME:-http}
host: ${TEMPORARY_DB_CONN_HOST:-localhost}
port: ${TEMPORARY_DB_CONN_PORT:-6379}
auth:
user: ${TEMPORARY_DB_CONN_AUTH_USER}
password: ${TEMPORARY_DB_CONN_AUTH_PASSWORD}

pushover:
appToken: ${PUSHOVER_APP_TOKEN}
userKey: ${PUSHOVER_USER_KEY}

log:
level: ${LOG_LEVEL:-info}
File renamed without changes.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ rsa==4.0
six==1.12.0
soupsieve==1.7.3
urllib3==1.24.2
piny==0.6.0

0 comments on commit f33ae38

Please sign in to comment.