Skip to content

Commit

Permalink
Merge pull request #186 from ral-facilities/feature/fix-code-linting-…
Browse files Browse the repository at this point in the history
…#184

Apply Fixes suggested by Linting Tools
  • Loading branch information
MRichards99 authored Jan 4, 2021
2 parents 821e8b9 + c4b63eb commit 9b4907c
Show file tree
Hide file tree
Showing 36 changed files with 1,177 additions and 1,449 deletions.
6 changes: 3 additions & 3 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
[flake8]
select = A,B,B9,BLK,C,E,F,I,N,S,W
ignore = E203,W503,E501
max-complexity = 10
max-complexity = 17
max-line-length = 80
application-import-names = common,src,test,util
application-import-names = datagateway_api,test,util
import-order-style = google
per-file-ignores = test/*:S101
per-file-ignores = test/*:S101,util/icat_db_generator.py:S311
enable-extensions=G
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ nox -s [SESSION/FUNCTION NAME]
```

Currently, the following Nox sessions have been created:
- `format` - this uses [Black](https://black.readthedocs.io/en/stable/) to format Python
- `black` - this uses [Black](https://black.readthedocs.io/en/stable/) to format Python
code to a pre-defined style.
- `lint` - this uses [flake8](https://flake8.pycqa.org/en/latest/) with a number of
additional plugins (see the included `noxfile.py` to see which plugins are used) to
Expand Down
1 change: 1 addition & 0 deletions config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"ICAT_URL": "https://localhost.localdomain:8181",
"icat_check_cert": false,
"log_level": "WARN",
"log_location": "/home/user1/datagateway-api/logs.log",
"debug_mode": false,
"generate_swagger": false,
"host": "127.0.0.1",
Expand Down
10 changes: 5 additions & 5 deletions datagateway_api/common/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def update_with_id(self, session_id, entity_type, id_, data):

@abstractmethod
def get_facility_cycles_for_instrument_with_filters(
self, session_id, instrument_id, filters
self, session_id, instrument_id, filters,
):
"""
Given an instrument_id get facility cycles where the instrument has
Expand All @@ -158,7 +158,7 @@ def get_facility_cycles_for_instrument_with_filters(

@abstractmethod
def get_facility_cycles_for_instrument_count_with_filters(
self, session_id, instrument_id, filters
self, session_id, instrument_id, filters,
):
"""
Given an instrument_id get the facility cycles count where the instrument has
Expand All @@ -173,7 +173,7 @@ def get_facility_cycles_for_instrument_count_with_filters(

@abstractmethod
def get_investigations_for_instrument_in_facility_cycle_with_filters(
self, session_id, instrument_id, facilitycycle_id, filters
self, session_id, instrument_id, facilitycycle_id, filters,
):
"""
Given an instrument id and facility cycle id, get investigations that use the
Expand All @@ -188,8 +188,8 @@ def get_investigations_for_instrument_in_facility_cycle_with_filters(
pass

@abstractmethod
def get_investigations_for_instrument_in_facility_cycle_count_with_filters(
self, session_id, instrument_id, facilitycycle_id, filters
def get_investigation_count_for_instrument_facility_cycle_with_filters(
self, session_id, instrument_id, facilitycycle_id, filters,
):
"""
Given an instrument id and facility cycle id, get the count of the
Expand Down
8 changes: 5 additions & 3 deletions datagateway_api/common/backends.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from datagateway_api.common.database.backend import DatabaseBackend
from datagateway_api.common.icat.backend import PythonICATBackend
import sys

from datagateway_api.common.backend import Backend
from datagateway_api.common.config import config
import sys
from datagateway_api.common.database.backend import DatabaseBackend
from datagateway_api.common.icat.backend import PythonICATBackend


backend_type = config.get_backend_type()

Expand Down
34 changes: 19 additions & 15 deletions datagateway_api/common/config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import json
import sys
import logging
from pathlib import Path
import sys

import requests
import logging


log = logging.getLogger()

Expand All @@ -11,65 +13,67 @@ class Config(object):
def __init__(self):
config_path = Path(__file__).parent.parent.parent / "config.json"
with open(config_path) as target:

self.config = json.load(target)
target.close()

def get_backend_type(self):
try:
return self.config["backend"]
except:
except KeyError:
sys.exit("Missing config value, backend")

def get_db_url(self):
try:
return self.config["DB_URL"]
except:
except KeyError:
sys.exit("Missing config value, DB_URL")

def get_icat_url(self):
try:
return self.config["ICAT_URL"]
except:
except KeyError:
sys.exit("Missing config value, ICAT_URL")

def get_icat_check_cert(self):
try:
return self.config["icat_check_cert"]
except:
# This could be set to true if there's no value, and log a warning
# that no value has been found from the config - save app from
# exiting
except KeyError:
sys.exit("Missing config value, icat_check_cert")

def get_log_level(self):
try:
return self.config["log_level"]
except:
except KeyError:
sys.exit("Missing config value, log_level")

def get_log_location(self):
try:
return self.config["log_location"]
except KeyError:
sys.exit("Missing config value, log_location")

def is_debug_mode(self):
try:
return self.config["debug_mode"]
except:
except KeyError:
sys.exit("Missing config value, debug_mode")

def is_generate_swagger(self):
try:
return self.config["generate_swagger"]
except:
except KeyError:
sys.exit("Missing config value, generate_swagger")

def get_host(self):
try:
return self.config["host"]
except:
except KeyError:
sys.exit("Missing config value, host")

def get_port(self):
try:
return self.config["port"]
except:
except KeyError:
sys.exit("Missing config value, port")

def get_icat_properties(self):
Expand Down
37 changes: 19 additions & 18 deletions datagateway_api/common/database/backend.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import datetime
import logging
import uuid

from datagateway_api.common.backend import Backend
from datagateway_api.common.database.helpers import (
create_rows_from_json,
delete_row_by_id,
get_facility_cycles_for_instrument,
get_facility_cycles_for_instrument_count,
get_filtered_row_count,
get_first_filtered_row,
get_investigations_for_instrument_in_facility_cycle,
get_investigations_for_instrument_in_facility_cycle_count,
get_rows_by_filter,
create_rows_from_json,
patch_entities,
get_row_by_id,
get_rows_by_filter,
insert_row_into_table,
delete_row_by_id,
update_row_from_id,
get_filtered_row_count,
get_first_filtered_row,
patch_entities,
requires_session_id,
update_row_from_id,
)
from datagateway_api.common.helpers import queries_records
from datagateway_api.common.database.models import EntityHelper, SESSION
import uuid
from datagateway_api.common.exceptions import AuthenticationError
import datetime
from datagateway_api.common.helpers import queries_records

import logging

log = logging.getLogger()

Expand Down Expand Up @@ -110,31 +111,31 @@ def update_with_id(self, session_id, entity_type, id_, data):
@requires_session_id
@queries_records
def get_facility_cycles_for_instrument_with_filters(
self, session_id, instrument_id, filters
self, session_id, instrument_id, filters,
):
return get_facility_cycles_for_instrument(instrument_id, filters)

@requires_session_id
@queries_records
def get_facility_cycles_for_instrument_count_with_filters(
self, session_id, instrument_id, filters
self, session_id, instrument_id, filters,
):
return get_facility_cycles_for_instrument_count(instrument_id, filters)

@requires_session_id
@queries_records
def get_investigations_for_instrument_in_facility_cycle_with_filters(
self, session_id, instrument_id, facilitycycle_id, filters
self, session_id, instrument_id, facilitycycle_id, filters,
):
return get_investigations_for_instrument_in_facility_cycle(
instrument_id, facilitycycle_id, filters
instrument_id, facilitycycle_id, filters,
)

@requires_session_id
@queries_records
def get_investigations_for_instrument_in_facility_cycle_count_with_filters(
self, session_id, instrument_id, facilitycycle_id, filters
def get_investigation_count_for_instrument_facility_cycle_with_filters(
self, session_id, instrument_id, facilitycycle_id, filters,
):
return get_investigations_for_instrument_in_facility_cycle_count(
instrument_id, facilitycycle_id, filters
instrument_id, facilitycycle_id, filters,
)
22 changes: 12 additions & 10 deletions datagateway_api/common/database/filters.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import logging

from sqlalchemy import asc, desc

from datagateway_api.common.database import models
from datagateway_api.common.exceptions import FilterError, MultipleIncludeError
from datagateway_api.common.filters import (
WhereFilter,
DistinctFieldFilter,
IncludeFilter,
LimitFilter,
OrderFilter,
SkipFilter,
LimitFilter,
IncludeFilter,
WhereFilter,
)
from datagateway_api.common.exceptions import FilterError, MultipleIncludeError
from datagateway_api.common.database import models

from sqlalchemy import asc, desc
import logging

log = logging.getLogger()

Expand Down Expand Up @@ -53,14 +55,14 @@ def apply_filter(self, query):
field = getattr(query.table, self.field)
except AttributeError:
raise FilterError(
f"Unknown attribute {self.field} on table {query.table.__name__}"
f"Unknown attribute {self.field} on table {query.table.__name__}",
)

if self.included_included_field:
included_table = getattr(models, self.field)
included_included_table = getattr(models, self.included_field)
query.base_query = query.base_query.join(included_table).join(
included_included_table
included_included_table,
)
field = getattr(included_included_table, self.included_included_field)

Expand All @@ -87,7 +89,7 @@ def apply_filter(self, query):
query.base_query = query.base_query.filter(field.in_(self.value))
else:
raise FilterError(
f" Bad operation given to where filter. operation: {self.operation}"
f" Bad operation given to where filter. operation: {self.operation}",
)


Expand Down
Loading

0 comments on commit 9b4907c

Please sign in to comment.