Skip to content

Commit e2b4eb4

Browse files
authored
Merge pull request #19 from faevourite/auth-sample-cfg-trailing-comma
Fix trailing comma in config
2 parents f99ef5b + 4995992 commit e2b4eb4

File tree

2 files changed

+44
-45
lines changed

2 files changed

+44
-45
lines changed

cfg/auth.py

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,30 @@
88
import json
99
import sys
1010

11+
CONFIG_PATH = '../cfg/config.cfg'
12+
13+
try:
14+
with open(CONFIG_PATH) as f:
15+
config_data = f.read()
16+
except IOError:
17+
print('Unable to load configuration file at {}. Exiting.'.format(CONFIG_PATH))
18+
raise
19+
20+
try:
21+
CONFIG = json.loads(config_data)
22+
except ValueError:
23+
print("Unable to parse configuration file at {}, likely because of malformed JSON. Exiting.".format(CONFIG_PATH))
24+
raise
1125

1226
# Configuration of the firebase database
1327
def firebase_admin_auth():
1428
try:
15-
with open('../cfg/config.cfg') as json_data_file:
16-
data = json.load(json_data_file)
17-
api_key = data['firebase']['api_key']
18-
auth_domain = data['firebase']['auth_domain']
19-
database_url = data['firebase']['database_url']
20-
storage_bucket = data['firebase']['storage_bucket']
21-
service_account = data['firebase']['service_account']
22-
# print('use configuration for psql as provided by config.json')
29+
api_key = CONFIG['firebase']['api_key']
30+
auth_domain = CONFIG['firebase']['auth_domain']
31+
database_url = CONFIG['firebase']['database_url']
32+
storage_bucket = CONFIG['firebase']['storage_bucket']
33+
service_account = CONFIG['firebase']['service_account']
34+
# print('use configuration for psql as provided by config.json')
2335
except:
2436
# Default Configuration
2537
print('could not get firebase informtaion from config file')
@@ -40,13 +52,11 @@ def firebase_admin_auth():
4052

4153
def dev_firebase_admin_auth():
4254
try:
43-
with open('../cfg/config.cfg') as json_data_file:
44-
data = json.load(json_data_file)
45-
api_key = data['dev_firebase']['api_key']
46-
auth_domain = data['dev_firebase']['auth_domain']
47-
database_url = data['dev_firebase']['database_url']
48-
storage_bucket = data['dev_firebase']['storage_bucket']
49-
service_account = data['dev_firebase']['service_account']
55+
api_key = CONFIG['dev_firebase']['api_key']
56+
auth_domain = CONFIG['dev_firebase']['auth_domain']
57+
database_url = CONFIG['dev_firebase']['database_url']
58+
storage_bucket = CONFIG['dev_firebase']['storage_bucket']
59+
service_account = CONFIG['dev_firebase']['service_account']
5060
except:
5161
# Default Configuration
5262
print('could not get firebase dev information from config file')
@@ -71,26 +81,19 @@ def dev_firebase_admin_auth():
7181
# get the api_key
7282
def get_api_key(tileserver):
7383
try:
74-
with open('../cfg/config.cfg') as json_data_file:
75-
data = json.load(json_data_file)
76-
api_key = data['imagery'][tileserver]
77-
return api_key
78-
except:
79-
print("Something is wrong with your API key."
80-
"Do you even have an API key?")
81-
sys.exit()
84+
return CONFIG['imagery'][tileserver]
85+
except KeyError:
86+
print("Couldn't find the API key for imagery tileserver {} in {}".format(tileserver, CONFIG_PATH))
87+
raise
8288

8389

8490
# get the import submission_key
8591
def get_submission_key():
8692
try:
87-
with open('../cfg/config.cfg') as json_data_file:
88-
data = json.load(json_data_file)
89-
submission_key = data['import']['submission_key']
90-
return submission_key
91-
except:
92-
print('we could not load submission key info the config file')
93-
sys.exit()
93+
return CONFIG['import']['submission_key']
94+
except KeyError:
95+
print("Couldn't find the submission key in {}".format(CONFIG_PATH))
96+
raise
9497

9598

9699
class mysqlDB(object):
@@ -100,13 +103,11 @@ class mysqlDB(object):
100103
def __init__(self):
101104
# try to load configuration from config file
102105
try:
103-
with open('../cfg/config.cfg') as json_data_file:
104-
data = json.load(json_data_file)
105-
dbname = data['mysql']['database']
106-
user = data['mysql']['username']
107-
password = data['mysql']['password']
108-
host = data['mysql']['host']
109-
# print('use configuration for mysql as provided by config.json')
106+
dbname = CONFIG['mysql']['database']
107+
user = CONFIG['mysql']['username']
108+
password = CONFIG['mysql']['password']
109+
host = CONFIG['mysql']['host']
110+
# print('use configuration for mysql as provided by config.json')
110111
except:
111112
print('we could not load mysql info the config file')
112113
sys.exit()
@@ -146,13 +147,11 @@ class dev_mysqlDB(object):
146147
def __init__(self):
147148
# try to load configuration from config file
148149
try:
149-
with open('../cfg/config.cfg') as json_data_file:
150-
data = json.load(json_data_file)
151-
dbname = data['dev_mysql']['database']
152-
user = data['dev_mysql']['username']
153-
password = data['dev_mysql']['password']
154-
host = data['dev_mysql']['host']
155-
# print('use configuration for mysql as provided by config.json')
150+
dbname = CONFIG['dev_mysql']['database']
151+
user = CONFIG['dev_mysql']['username']
152+
password = CONFIG['dev_mysql']['password']
153+
host = CONFIG['dev_mysql']['host']
154+
# print('use configuration for mysql as provided by config.json')
156155
except:
157156
# Default configuration
158157
print('we could not load mysql dev info from the config file')

cfg/your_config_file.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"imagery":{
1616
"bing": "your_bing_api_key",
1717
"digital_globe": "your_digital_globe_api_key",
18-
"google": "your_google_api_key",
18+
"google": "your_google_api_key"
1919
},
2020
"slack":{
2121
"token": "your_slack_token",

0 commit comments

Comments
 (0)