|
1 |
| -#!/usr/bin/python3 |
2 |
| -# |
3 |
| -# Author: B. Herfort, M. Reinmuth, 2017 |
4 |
| -############################################ |
5 |
| - |
6 |
| -import json |
7 |
| - |
8 |
| -import psycopg2 |
9 | 1 | 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 |
15 | 4 |
|
| 5 | +from mapswipe_workers.definitions import CONFIG, SERVICE_ACCOUNT_KEY_PATH |
16 | 6 |
|
17 |
| -def load_config(): |
18 |
| - """ |
19 |
| - Loads the user configuration values. |
20 | 7 |
|
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"] |
28 | 13 |
|
29 | 14 |
|
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"] |
68 | 20 |
|
69 | 21 |
|
70 |
| -def firebaseDB(): |
| 22 | +def firebaseDB() -> object: |
71 | 23 | try:
|
72 | 24 | # Is an App instance already initialized?
|
73 | 25 | firebase_admin.get_app()
|
74 | 26 | # Return the imported Firebase Realtime Database module
|
75 | 27 | return db
|
76 | 28 | except ValueError:
|
77 | 29 | 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" |
81 | 32 |
|
82 | 33 | # 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}) |
86 | 35 |
|
87 | 36 | # Return the imported Firebase Realtime Database module
|
88 | 37 | return db
|
89 | 38 |
|
90 | 39 |
|
91 | 40 | class postgresDB(object):
|
| 41 | + """Helper calss for Postgres interactions""" |
| 42 | + |
92 | 43 | _db_connection = None
|
93 | 44 | _db_cur = None
|
94 | 45 |
|
95 | 46 | 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"] |
108 | 52 |
|
109 | 53 | 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 | + ) |
116 | 56 |
|
117 | 57 | def query(self, query, data=None):
|
118 | 58 | self._db_cur = self._db_connection.cursor()
|
119 | 59 | self._db_cur.execute(query, data)
|
120 | 60 | self._db_connection.commit()
|
121 | 61 | self._db_cur.close()
|
122 | 62 |
|
123 |
| - def copy_from( |
124 |
| - self, |
125 |
| - f, |
126 |
| - table, |
127 |
| - columns |
128 |
| - ): |
| 63 | + def copy_from(self, f, table, columns): |
129 | 64 | 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) |
135 | 66 | self._db_connection.commit()
|
136 | 67 | self._db_cur.close()
|
137 | 68 |
|
138 |
| - def copy_expert( |
139 |
| - self, |
140 |
| - sql, |
141 |
| - file, |
142 |
| - ): |
| 69 | + def copy_expert(self, sql, file): |
143 | 70 | 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) |
148 | 72 | self._db_connection.commit()
|
149 | 73 | self._db_cur.close()
|
150 | 74 |
|
|
0 commit comments