-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from citysciencelab/fix-linting-errors
fix: fix linting errors
- Loading branch information
Showing
19 changed files
with
1,147 additions
and
842 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,81 @@ | ||
import ump.config as config | ||
import logging | ||
|
||
import psycopg2 as db | ||
from psycopg2.extras import RealDictCursor | ||
|
||
import logging | ||
from ump import config | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
class DBHandler(): | ||
def __init__(self): | ||
self.connection = db.connect( | ||
database = config.postgres_db, | ||
host = config.postgres_host, | ||
user = config.postgres_user, | ||
password = config.postgres_password, | ||
port = config.postgres_port | ||
) | ||
self.sortable_columns = [] | ||
|
||
def set_sortable_columns(self, sortable_columns): | ||
self.sortable_columns = sortable_columns | ||
def __init__(self): | ||
self.connection = db.connect( | ||
database = config.postgres_db, | ||
host = config.postgres_host, | ||
user = config.postgres_user, | ||
password = config.postgres_password, | ||
port = config.postgres_port | ||
) | ||
self.sortable_columns = [] | ||
|
||
def run_query(self, query, conditions=[], query_params={}, order=[], limit=None, page=None): | ||
if conditions: | ||
query += " WHERE " + " AND ".join(conditions) | ||
def set_sortable_columns(self, sortable_columns): | ||
self.sortable_columns = sortable_columns | ||
|
||
if order and set(order).issubset(set(self.sortable_columns)): | ||
query += f" ORDER BY {', '.join(order)} DESC" | ||
elif order: | ||
logging.debug(f" --> Could not order by {order} since sortable_columns hasn't been set! Please call set_sortable_columns!") | ||
def run_query( | ||
self, | ||
query, | ||
conditions=None, | ||
query_params=None, | ||
order=None, | ||
limit=None, | ||
page=None | ||
): | ||
if query_params is None: | ||
query_params = {} | ||
if conditions: | ||
query += " WHERE " + " AND ".join(conditions) | ||
|
||
if limit: | ||
offset = 0 | ||
if page: | ||
offset = (page - 1) * limit | ||
if order and set(order).issubset(set(self.sortable_columns)): | ||
query += f" ORDER BY {', '.join(order)} DESC" | ||
elif order: | ||
logging.debug( | ||
" --> Could not order by %s since sortable_columns hasn't been set!" + | ||
" Please call set_sortable_columns!", | ||
order | ||
) | ||
|
||
query += " LIMIT %(limit)s OFFSET %(offset)s" | ||
query_params['limit'] = limit | ||
query_params['offset'] = offset | ||
if limit: | ||
offset = 0 | ||
if page: | ||
offset = (page - 1) * limit | ||
|
||
#logging.debug(f" --> SQL query = {query}") | ||
#logging.debug(f" --> query_params = {query_params}") | ||
query += " LIMIT %(limit)s OFFSET %(offset)s" | ||
query_params['limit'] = limit | ||
query_params['offset'] = offset | ||
|
||
with self.connection: | ||
with self.connection.cursor(cursor_factory = RealDictCursor) as cursor: | ||
cursor.execute(query, query_params) | ||
try: | ||
results = cursor.fetchall() | ||
except db.ProgrammingError as e: | ||
if str(e) == "no results to fetch": | ||
return | ||
else: | ||
raise e | ||
with self.connection: | ||
with self.connection.cursor(cursor_factory = RealDictCursor) as cursor: | ||
cursor.execute(query, query_params) | ||
try: | ||
results = cursor.fetchall() | ||
except db.ProgrammingError as e: | ||
if str(e) == "no results to fetch": | ||
return | ||
else: | ||
raise e | ||
|
||
return results | ||
return results | ||
|
||
# needed so that this class can be used as a context manager | ||
def __enter__(self): | ||
return self | ||
# needed so that this class can be used as a context manager | ||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, type, value, traceback): | ||
if (self.connection): | ||
self.connection.close() | ||
def __exit__(self, exc_type, value, traceback): | ||
if self.connection: | ||
self.connection.close() | ||
|
||
if type is None and value is None and traceback is None: | ||
return True | ||
if exc_type is None and value is None and traceback is None: | ||
return True | ||
|
||
logger.error(f"{type}: {value} - {traceback}") | ||
return False | ||
logger.error("%s: %s - %s", exc_type, value, traceback) | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
from enum import Enum | ||
|
||
|
||
class JobStatus(Enum): | ||
""" | ||
Enum for the job status options specified in the WPS 2.0 specification | ||
""" | ||
accepted = 'accepted' | ||
running = 'running' | ||
successful = 'successful' | ||
failed = 'failed' | ||
dismissed = 'dismissed' | ||
""" | ||
Enum for the job status options specified in the WPS 2.0 specification | ||
""" | ||
accepted = 'accepted' | ||
running = 'running' | ||
successful = 'successful' | ||
failed = 'failed' | ||
dismissed = 'dismissed' |
Oops, something went wrong.