Skip to content

Commit

Permalink
fix pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalebi committed Mar 4, 2024
1 parent ab03749 commit ee66f26
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 27 deletions.
9 changes: 4 additions & 5 deletions common/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
See the License for the specific language governing permissions and
limitations under the License.
"""
import graphql.error.graphql_error
import logging
import pymongo
import mongomock
import grpc
import logging
from ensembl.production.metadata.grpc import ensembl_metadata_pb2_grpc

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -44,7 +43,7 @@ def get_database_conn(self, grpc_model, uuid):
# chosen_db value will fall back to the default value, which is 'mongo_default_db' that is in the config
# TODO: check why "except graphql.error.graphql_error.GraphQLError as grpc_exp:" didn't catch the error
logger.debug(
f"[get_database_conn] Couldn't connected to gRPC Host: {grpc_exp}"
"[get_database_conn] Couldn't connect to gRPC Host: %s", grpc_exp
)

if grpc_response:
Expand All @@ -53,7 +52,7 @@ def get_database_conn(self, grpc_model, uuid):
release_version = str(grpc_response.release_version).replace(".", "_")
chosen_db = "release_" + release_version

logger.debug(f"[get_database_conn] Connected to '{chosen_db}' MongoDB")
logger.debug("[get_database_conn] Connected to '%s' MongoDB", chosen_db)
data_database_connection = self.mongo_client[chosen_db]
return data_database_connection

Expand All @@ -78,7 +77,7 @@ def connect_mongo(config):
client.server_info()
print(f"Connected to MongoDB, Host: {host}")
except Exception as exc:
raise "Connection to mongo Failed" from exc
raise Exception("Connection to MongoDB failed") from exc

return client

Expand Down
5 changes: 4 additions & 1 deletion graphql_service/resolver/data_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ async def query_mongo(self, query: Dict, doc_type) -> List[Dict]:
"""
# the collection name is the doc_type
logger.info(
f"Getting '{doc_type}' from DB: '{self.database_conn.name}', collection '{doc_type}'"
"Getting '%s' from DB: '%s', collection '%s'",
doc_type,
self.database_conn.name,
doc_type,
)
return list(self.database_conn[doc_type].find(query))
13 changes: 13 additions & 0 deletions graphql_service/resolver/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
"""
.. See the NOTICE file distributed with this work for additional information
regarding copyright ownership.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from typing import Optional, Dict

from graphql import GraphQLError
Expand Down
34 changes: 19 additions & 15 deletions graphql_service/resolver/gene_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ def resolve_gene(
connection_db = get_db_conn(info)
gene_collection = connection_db["gene"]

logger.info(f"[resolve_gene] Getting Gene from DB: '{connection_db.name}'")
logger.info("[resolve_gene] Getting Gene from DB: '%s'", connection_db.name)
try:
result = gene_collection.find_one(query)
except Exception as e:
logging.error(f"Exception: {e}")
logging.error("Exception: %s", e)
raise DatabaseNotFoundError(db_name=connection_db.name)

if not result:
Expand All @@ -120,12 +120,12 @@ def resolve_genes(_, info: GraphQLResolveInfo, by_symbol: Dict[str, str]) -> Lis
set_db_conn_for_uuid(info, by_symbol["genome_id"])
connection_db = get_db_conn(info)
gene_collection = connection_db["gene"]
logger.info(f"[resolve_genes] Getting Gene from DB: '{connection_db.name}'")
logger.info("[resolve_genes] Getting Gene from DB: '%s'", connection_db.name)

try:
result = gene_collection.find(query)
except Exception as e:
logging.error(f"Exception: {e}")
logging.error("Exception: %s", e)
raise DatabaseNotFoundError(db_name=connection_db.name)

# unpack cursor into a list. We're guaranteed relatively small results
Expand Down Expand Up @@ -240,13 +240,13 @@ def resolve_transcript(
connection_db = get_db_conn(info)
transcript_collection = connection_db["transcript"]
logger.info(
f"[resolve_transcript] Getting Transcript from DB: '{connection_db.name}'"
"[resolve_transcript] Getting Transcript from DB: '%s'", connection_db.name
)

try:
transcript = transcript_collection.find_one(query)
except Exception as e:
logging.error(f"Exception: {e}")
logging.error("Exception: %s", e)
raise DatabaseNotFoundError(db_name=connection_db.name)

if not transcript:
Expand All @@ -267,7 +267,7 @@ def resolve_api(
version_details = get_version_details()
return {"api": version_details}
except Exception as exp:
logging.error(f"Error resolving API version: {exp}")
logging.error("Error resolving API version: %s", exp)
raise


Expand Down Expand Up @@ -312,7 +312,8 @@ async def resolve_transcripts_page_transcripts(
connection_db = get_db_conn(info)
transcript_collection = connection_db["transcript"]
logger.info(
f"[resolve_transcripts_page_transcripts] Getting Transcript from DB: '{connection_db.name}'"
"[resolve_transcripts_page_transcripts] Getting Transcript from DB: '%s'",
connection_db.name,
)

results = (
Expand All @@ -336,7 +337,8 @@ async def resolve_transcripts_page_metadata(
connection_db = get_db_conn(info)
transcript_collection = connection_db["transcript"]
logger.info(
f"[resolve_transcripts_page_metadata] Getting Transcript from DB: '{connection_db.name}'"
"[resolve_transcripts_page_metadata] Getting Transcript from DB: '%s'",
connection_db.name,
)

return {
Expand Down Expand Up @@ -366,7 +368,7 @@ async def resolve_transcript_gene(transcript: Dict, info: GraphQLResolveInfo) ->
connection_db = get_db_conn(info)
gene_collection = connection_db["gene"]
logger.info(
f"[resolve_transcript_gene] Getting Gene from DB: '{connection_db.name}'"
"[resolve_transcript_gene] Getting Gene from DB: '%s'", connection_db.name
)

gene = gene_collection.find_one(query)
Expand Down Expand Up @@ -417,7 +419,8 @@ def resolve_overlap(
set_db_conn_for_uuid(info, genome_id)
connection_db = get_db_conn(info)
logger.info(
f"[resolve_overlap] Getting Gene and Transcript Overlap from DB: '{connection_db.name}'"
"[resolve_overlap] Getting Gene and Transcript Overlap from DB: '%s'",
connection_db.name,
)

return {
Expand Down Expand Up @@ -511,7 +514,7 @@ def resolve_product_by_id(
connection_db = get_db_conn(info)
protein_collection = connection_db["protein"]
logger.info(
f"[resolve_product_by_id] Getting Protein from DB: '{connection_db.name}'"
"[resolve_product_by_id] Getting Protein from DB: '%s'", connection_db.name
)
# NB: 'MatureRNA' will be added in the future, with collection per type approach
# with have two options (TBD)
Expand Down Expand Up @@ -581,7 +584,8 @@ async def resolve_assembly_from_region(
connection_db = get_db_conn(info)
assembly_collection = connection_db["assembly"]
logger.info(
f"[resolve_assembly_from_region] Getting Assembly from DB: '{connection_db.name}'"
"[resolve_assembly_from_region] Getting Assembly from DB: '%s'",
connection_db.name,
)
assembly = assembly_collection.find_one(query)

Expand Down Expand Up @@ -672,7 +676,7 @@ async def resolve_region(_, info: GraphQLResolveInfo, by_name: Dict[str, str]) -
set_db_conn_for_uuid(info, by_name["genome_id"])
connection_db = get_db_conn(info)
region_collection = connection_db["region"]
logger.info(f"[resolve_region] Getting Region from DB: '{connection_db.name}'")
logger.info("[resolve_region] Getting Region from DB: '%s'", connection_db.name)

result = region_collection.find_one(query)
if not result:
Expand Down Expand Up @@ -777,7 +781,7 @@ def get_version_details() -> Dict[str, str]:
"Version section or keys not found in INI file. Using default values."
)
except Exception as exp:
logging.error(f"Error reading INI file: {exp}. Using default values.")
logging.error("Error reading INI file: %s. Using default values.", exp)

return {"major": "0", "minor": "1", "patch": "0-beta"}

Expand Down
1 change: 0 additions & 1 deletion graphql_service/resolver/tests/test_data_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
limitations under the License.
"""

import mongomock
import pytest

from common.db import FakeMongoDbClient
Expand Down
2 changes: 0 additions & 2 deletions graphql_service/resolver/tests/test_resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import graphql_service.resolver.gene_model as model
from common.crossrefs import XrefResolver
from graphql_service.resolver.data_loaders import BatchLoaders
from common.db import FakeMongoDbClient
from graphql_service.tests.snapshot_utils import prepare_mongo_instance


Expand Down
5 changes: 2 additions & 3 deletions graphql_service/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@
from starlette import applications, middleware
from starlette.middleware.cors import CORSMiddleware

from common import crossrefs, db, extensions, utils
from common import crossrefs, db, extensions, utils, logger
from grpc_service import grpc_model
from graphql_service.ariadne_app import (
prepare_executable_schema,
prepare_context_provider,
)
from dotenv import load_dotenv
from common.logger import CommandLogger


load_dotenv("connections.conf")
Expand All @@ -50,7 +49,7 @@
log.setLevel(logging.DEBUG)
logging.basicConfig(level=logging.DEBUG)

monitoring.register(CommandLogger(log))
monitoring.register(logger.CommandLogger(log))

# Apollo Tracing extension will display information about which resolvers are used and their duration
# https://ariadnegraphql.org/docs/apollo-tracing
Expand Down

0 comments on commit ee66f26

Please sign in to comment.