Skip to content

Commit

Permalink
refactor env settings
Browse files Browse the repository at this point in the history
  • Loading branch information
m4droid committed May 14, 2017
1 parent 2fffb7d commit cf19242
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 27 deletions.
File renamed without changes.
27 changes: 27 additions & 0 deletions configs/env_local.json.default
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"pymongo": {
"client": {
"host": "localhost",
"port": 27017
},
"database": "restriccion"
},
"moment": {
"timezone": "UTC-3"
},
"notifications": {
"gcm": {
"api_key": "dummy_gcm_key"
},
"email": {
"enabled": false,
"template": "template_email_test.html",
"server": {
"host": ["localhost"],
"login": ["user", "pass"]
},
"from": "dummy@email.com",
"subject": "Dummy email"
}
}
}
File renamed without changes.
17 changes: 10 additions & 7 deletions scripts/get_data_uoct.py → get_data_uoct.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
#!/usr/bin/env python3
import os
import sys

import pymongo

os.environ.setdefault('ENV', 'local')

from restriccion import CONFIG
from restriccion.crawlers.uoct import UOCT_Crawler
from restriccion.models.device import Device
from restriccion.models.air_quality import AirQuality
from restriccion.models.restriction import Restriction
from restriccion.models.air_quality import AirQualityReport
from restriccion.models.restriction import RestrictionReport


def main(argv):
mongo_client = pymongo.MongoClient(**CONFIG['pymongo']['client'])
mongo_db = mongo_client[CONFIG['pymongo']['database']]

current_restrictions = Restriction.get(mongo_db)
current_restrictions = RestrictionReport.get(mongo_db)

crawler = UOCT_Crawler()
new_reports = crawler.parse()

AirQuality.insert_many(mongo_db, new_reports['air_quality'])
Restriction.insert_many(mongo_db, new_reports['restrictions'])
AirQualityReport.insert_many(mongo_db, new_reports['air_quality'])
RestrictionReport.insert_many(mongo_db, new_reports['restriction'])

if '--notify' in argv[1:]:
_notify_to_devices(mongo_db, new_reports['restrictions'][0])
elif '--notify-new' in argv[1:]:
del current_restrictions[0]['actualizacion']
if new_reports['restrictions'][0] != current_restrictions[0]:
_notify_to_devices(mongo_db, new_reports['restrictions'][0])
if new_reports['restriction'][0] != current_restrictions[0]:
_notify_to_devices(mongo_db, new_reports['restriction'][0])

mongo_client.close()

Expand Down
7 changes: 0 additions & 7 deletions nosetests.sh

This file was deleted.

14 changes: 2 additions & 12 deletions restriccion/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
import json
import os
from restriccion.libs.env import load_env_params


script_path = os.path.dirname(os.path.realpath(__file__))
config_file_path = os.environ.get(
'RESTRICCION_CONFIG',
os.path.join(script_path, '..', 'configs/localhost.json')
)


with open(config_file_path, 'r') as config_file:
CONFIG = json.load(config_file)
CONFIG = load_env_params()
33 changes: 33 additions & 0 deletions restriccion/libs/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import json
import os
import sys


BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))


def _get_config_path(filename):
config_paths = [
os.path.join(BASE_DIR, 'configs'),
os.path.join(os.path.expanduser('~'), 'configs'),
]
for config_path in config_paths:
p = os.path.join(config_path, filename)
if os.path.exists(p):
return p
return None


def load_env_params():
env = os.environ.get('ENV')

config_path = _get_config_path('env_{0:s}.json'.format(env))

if config_path is None:
print('ERROR: {0:s} environment config file not found'.format(env), file=sys.stderr)
return

with open(config_path) as f:
data = json.load(f)

return data
2 changes: 1 addition & 1 deletion runtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
COVERAGE_DIR="$DIR/coverage"

RESTRICCION_CONFIG="$DIR/configs/tests.json" nosetests --with-coverage --cover-package=restriccion --cover-html --cover-html-dir=$COVERAGE_DIR $@
ENV=test nosetests --with-coverage --cover-package=restriccion --cover-html --cover-html-dir=$COVERAGE_DIR $@
4 changes: 4 additions & 0 deletions scripts/serve.py → serve.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/usr/bin/env python3
import os

os.environ.setdefault('ENV', 'local')

from restriccion.wsgi import app


Expand Down

0 comments on commit cf19242

Please sign in to comment.