Skip to content

Commit 3508291

Browse files
Merge pull request #273 from mapswipe/auth
Auth
2 parents aacb2fe + 33da93c commit 3508291

File tree

2 files changed

+44
-110
lines changed

2 files changed

+44
-110
lines changed

mapswipe_workers/mapswipe_workers/auth.py

Lines changed: 30 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,74 @@
1-
#!/usr/bin/python3
2-
#
3-
# Author: B. Herfort, M. Reinmuth, 2017
4-
############################################
5-
6-
import json
7-
8-
import psycopg2
91
import firebase_admin
10-
from firebase_admin import credentials
11-
from firebase_admin import db
12-
13-
from mapswipe_workers.definitions import CONFIG_PATH
14-
from mapswipe_workers.definitions import SERVICE_ACCOUNT_KEY_PATH
2+
import psycopg2
3+
from firebase_admin import credentials, db
154

5+
from mapswipe_workers.definitions import CONFIG, SERVICE_ACCOUNT_KEY_PATH
166

17-
def load_config():
18-
"""
19-
Loads the user configuration values.
207

21-
Returns
22-
-------
23-
dictonary
24-
"""
25-
with open(CONFIG_PATH) as f:
26-
CONFIG = json.load(f)
27-
return CONFIG
8+
def get_api_key(tileserver: str) -> str:
9+
if tileserver == "custom":
10+
return None
11+
else:
12+
return CONFIG["imagery"][tileserver]["api_key"]
2813

2914

30-
def get_api_key(tileserver):
31-
CONFIG = load_config()
32-
try:
33-
if tileserver == 'custom':
34-
return None
35-
else:
36-
return CONFIG['imagery'][tileserver]['api_key']
37-
except KeyError:
38-
print(
39-
f'Could not find the API key for imagery tileserver '
40-
f'{tileserver} in {CONFIG_PATH}.'
41-
)
42-
raise
43-
44-
45-
def get_tileserver_url(tileserver):
46-
CONFIG = load_config()
47-
try:
48-
if tileserver == 'custom':
49-
return None
50-
else:
51-
return CONFIG['imagery'][tileserver]['url']
52-
except KeyError:
53-
print('Could not find the url for imagery tileserver {} in {}.'.format(
54-
tileserver,
55-
CONFIG_PATH
56-
))
57-
raise
58-
59-
60-
def init_firebase():
61-
try:
62-
# Is an App instance already initialized?
63-
firebase_admin.get_app()
64-
except ValueError:
65-
cred = credentials.Certificate(SERVICE_ACCOUNT_KEY_PATH)
66-
# Initialize the app with a service account, granting admin privileges
67-
firebase_admin.initialize_app(cred)
15+
def get_tileserver_url(tileserver: str) -> str:
16+
if tileserver == "custom":
17+
return None
18+
else:
19+
return CONFIG["imagery"][tileserver]["url"]
6820

6921

70-
def firebaseDB():
22+
def firebaseDB() -> object:
7123
try:
7224
# Is an App instance already initialized?
7325
firebase_admin.get_app()
7426
# Return the imported Firebase Realtime Database module
7527
return db
7628
except ValueError:
7729
cred = credentials.Certificate(SERVICE_ACCOUNT_KEY_PATH)
78-
config = load_config()
79-
databaseName = config['firebase']['database_name']
80-
databaseURL = f'https://{databaseName}.firebaseio.com'
30+
databaseName = CONFIG["firebase"]["database_name"]
31+
databaseURL = f"https://{databaseName}.firebaseio.com"
8132

8233
# Initialize the app with a service account, granting admin privileges
83-
firebase_admin.initialize_app(cred, {
84-
'databaseURL': databaseURL
85-
})
34+
firebase_admin.initialize_app(cred, {"databaseURL": databaseURL})
8635

8736
# Return the imported Firebase Realtime Database module
8837
return db
8938

9039

9140
class postgresDB(object):
41+
"""Helper calss for Postgres interactions"""
42+
9243
_db_connection = None
9344
_db_cur = None
9445

9546
def __init__(self):
96-
CONFIG = load_config()
97-
try:
98-
host = CONFIG['postgres']['host']
99-
port = CONFIG['postgres']['port']
100-
dbname = CONFIG['postgres']['database']
101-
user = CONFIG['postgres']['username']
102-
password = CONFIG['postgres']['password']
103-
except KeyError:
104-
raise Exception(
105-
f'Could not load postgres credentials '
106-
f'from the configuration file'
107-
)
47+
host = CONFIG["postgres"]["host"]
48+
port = CONFIG["postgres"]["port"]
49+
dbname = CONFIG["postgres"]["database"]
50+
user = CONFIG["postgres"]["username"]
51+
password = CONFIG["postgres"]["password"]
10852

10953
self._db_connection = psycopg2.connect(
110-
database=dbname,
111-
user=user,
112-
password=password,
113-
host=host,
114-
port=port
115-
)
54+
database=dbname, user=user, password=password, host=host, port=port,
55+
)
11656

11757
def query(self, query, data=None):
11858
self._db_cur = self._db_connection.cursor()
11959
self._db_cur.execute(query, data)
12060
self._db_connection.commit()
12161
self._db_cur.close()
12262

123-
def copy_from(
124-
self,
125-
f,
126-
table,
127-
columns
128-
):
63+
def copy_from(self, f, table, columns):
12964
self._db_cur = self._db_connection.cursor()
130-
self._db_cur.copy_from(
131-
f,
132-
table,
133-
columns=columns
134-
)
65+
self._db_cur.copy_from(f, table, columns=columns)
13566
self._db_connection.commit()
13667
self._db_cur.close()
13768

138-
def copy_expert(
139-
self,
140-
sql,
141-
file,
142-
):
69+
def copy_expert(self, sql, file):
14370
self._db_cur = self._db_connection.cursor()
144-
self._db_cur.copy_expert(
145-
sql,
146-
file,
147-
)
71+
self._db_cur.copy_expert(sql, file)
14872
self._db_connection.commit()
14973
self._db_cur.close()
15074

mapswipe_workers/mapswipe_workers/definitions.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import logging
23
import logging.config
34
import os
@@ -10,12 +11,25 @@
1011
)
1112
from mapswipe_workers.project_types.footprint.footprint_project import FootprintProject
1213

14+
15+
class CustomError(Exception):
16+
pass
17+
18+
19+
def load_config(CONFIG_PATH) -> dict:
20+
"""Read the configuration file."""
21+
with open(CONFIG_PATH) as f:
22+
return json.load(f)
23+
24+
1325
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
1426

1527
CONFIG_DIR = os.path.abspath("/usr/share/config/mapswipe_workers/")
1628

1729
CONFIG_PATH = os.path.join(CONFIG_DIR, "configuration.json")
1830

31+
CONFIG = load_config(CONFIG_PATH)
32+
1933
SERVICE_ACCOUNT_KEY_PATH = os.path.join(CONFIG_DIR, "serviceAccountKey.json")
2034

2135
LOGGING_CONFIG_PATH = os.path.join(CONFIG_DIR, "logging.cfg")
@@ -36,7 +50,3 @@
3650

3751
logging.config.fileConfig(fname=LOGGING_CONFIG_PATH, disable_existing_loggers=True)
3852
logger = logging.getLogger("Mapswipe Workers")
39-
40-
41-
class CustomError(Exception):
42-
pass

0 commit comments

Comments
 (0)