Skip to content

Commit

Permalink
style: use Ruff and Black (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsstevenson authored Aug 30, 2023
1 parent 16ceed4 commit 95b95a7
Show file tree
Hide file tree
Showing 31 changed files with 217 additions and 177 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/backend_checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: backend_checks
on: [push, pull_request]
jobs:
build:
name: build
runs-on: ubuntu-latest
strategy:
fail-fast: false
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: 3.11

- name: Install dependencies
run: pip install server/
lint:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: black
uses: psf/black@stable
with:
src: "./server"

- name: ruff
uses: chartboost/ruff-action@v1
with:
src: "./server"
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ dynamodb_local_latest/*

# Build files
Pipfile.lock
pyproject.toml

# client-side things
curation/client/node_modules
Expand Down
14 changes: 9 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v1.4.0
hooks:
- id: flake8
additional_dependencies: [flake8-docstrings]
args: ["--config=server/.flake8"]
- id: check-added-large-files
args: ["--maxkb=2000"]
- id: detect-private-key
- id: trailing-whitespace
- id: end-of-file-fixer
- repo: https://github.com/psf/black
rev: 22.6.0
rev: 23.7.0
hooks:
- id: black
args: [--diff, --check]
language_version: python3.11
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.286
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- repo: https://github.com/pre-commit/mirrors-eslint
rev: "v8.20.0"
hooks:
Expand Down
20 changes: 0 additions & 20 deletions server/.flake8

This file was deleted.

11 changes: 3 additions & 8 deletions server/curfu/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"""Fusion curation interface."""
from pathlib import Path
from os import environ
import logging
from os import environ
from pathlib import Path

from .version import __version__


# provide consistent paths
APP_ROOT = Path(__file__).resolve().parents[0]

Expand Down Expand Up @@ -51,13 +50,9 @@
SEQREPO_DATA_PATH = environ["SEQREPO_DATA_PATH"]


class ServiceWarning(Exception):
class LookupServiceError(Exception):
"""Custom Exception to use when lookups fail in curation services."""

def __init__(self, *args, **kwargs):
"""Initialize exception."""
super().__init__(*args, **kwargs)


# define max acceptable matches for autocomplete suggestions
MAX_SUGGESTIONS = 50
9 changes: 4 additions & 5 deletions server/curfu/cli.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"""Provide command-line interface to application and associated utilities."""
import os
from typing import Optional
from pathlib import Path
from typing import Optional

import click
from curfu import APP_ROOT

from curfu import APP_ROOT
from curfu.devtools import DEFAULT_INTERPRO_TYPES
from curfu.devtools.build_client_types import build_client_types
from curfu.devtools.interpro import build_gene_domain_maps
from curfu.devtools.gene import GeneSuggestionBuilder
from curfu.devtools.interpro import build_gene_domain_maps


@click.command()
Expand All @@ -27,9 +27,8 @@ def serve(port: int) -> None:


@click.group()
def devtools():
def devtools() -> None:
"""Provide setup utilities for constructing data for Fusion Curation app."""
pass


types_help = """
Expand Down
3 changes: 2 additions & 1 deletion server/curfu/devtools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Utility functions for application setup."""
import ftplib
from typing import Callable

from curfu import logger


def ftp_download(domain: str, path: str, fname: str, callback) -> None:
def ftp_download(domain: str, path: str, fname: str, callback: Callable) -> None:
"""Acquire file via FTP.
:param str domain: domain name for remote file host
:param str path: path within host to desired file
Expand Down
3 changes: 2 additions & 1 deletion server/curfu/devtools/build_client_types.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Provide client type generation tooling."""
from pathlib import Path

from pydantic2ts.cli.script import generate_typescript_defs


def build_client_types():
def build_client_types() -> None:
"""Construct type definitions for front-end client."""
client_dir = Path(__file__).resolve().parents[3] / "client"
generate_typescript_defs(
Expand Down
11 changes: 5 additions & 6 deletions server/curfu/devtools/gene.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
"""Provide tools to build backend data relating to gene identification."""
from typing import Dict, Tuple
from pathlib import Path
from datetime import datetime as dt
from pathlib import Path
from timeit import default_timer as timer
from typing import Dict, Tuple

from gene.query import QueryHandler
import click
from gene.query import QueryHandler

from curfu import APP_ROOT, logger


# type stub
Map = Dict[str, Tuple[str, str, str]]

Expand All @@ -27,7 +26,7 @@ class GeneSuggestionBuilder:
alias_map = {}
assoc_with_map = {}

def __init__(self):
def __init__(self) -> None:
"""Initialize class.
TODO: think about how best to force prod environment
Expand Down Expand Up @@ -108,7 +107,7 @@ def build_gene_suggest_maps(self, output_dir: Path = APP_ROOT / "data") -> None:
break

today = dt.strftime(dt.today(), "%Y%m%d")
for (map, name) in (
for map, name in (
(self.xrefs_map, "xrefs"),
(self.symbol_map, "symbols"),
(self.label_map, "labels"),
Expand Down
18 changes: 9 additions & 9 deletions server/curfu/devtools/interpro.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""Provide utilities relating to data fetched from InterPro service."""
import gzip
from typing import Tuple, Dict, Optional, Set
from pathlib import Path
import csv
from datetime import datetime
from timeit import default_timer as timer
import gzip
import os
import shutil
import xml.etree.ElementTree as ET
import xml.etree.ElementTree as ET # noqa: N817
from datetime import datetime
from pathlib import Path
from timeit import default_timer as timer
from typing import Dict, Optional, Set, Tuple

from gene.query import QueryHandler
import click
from gene.query import QueryHandler

from curfu import APP_ROOT, logger
from curfu.devtools import ftp_download
Expand All @@ -34,7 +34,7 @@ def download_protein2ipr(output_dir: Path) -> None:
gz_file_path = output_dir / "protein2ipr.dat.gz"
with open(gz_file_path, "w") as fp:

def writefile(data):
def writefile(data): # noqa
fp.write(data)

ftp_download(
Expand Down Expand Up @@ -283,7 +283,7 @@ def build_gene_domain_maps(
# get relevant Interpro IDs
interpro_data_bin = []

def get_interpro_data(data):
def get_interpro_data(data): # noqa
interpro_data_bin.append(data)

ftp_download(
Expand Down
10 changes: 6 additions & 4 deletions server/curfu/domain_services.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
"""Provide lookup services for functional domains.
TODO
Todo:
----
* domains file should be a JSON and pre-pruned to unique pairs
* get_possible_domains shouldn't have to force uniqueness
"""
from typing import List, Dict
import csv
from typing import Dict, List

from curfu import logger, ServiceWarning
from curfu import LookupServiceError, logger
from curfu.utils import get_data_file


Expand Down Expand Up @@ -56,5 +58,5 @@ def get_possible_domains(self, gene_id: str) -> List[Dict]:
domains = self.domains[gene_id.lower()]
except KeyError:
logger.warning(f"Unable to retrieve associated domains for {gene_id}")
raise ServiceWarning
raise LookupServiceError
return domains
15 changes: 7 additions & 8 deletions server/curfu/gene_services.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
"""Wrapper for required Gene Normalization services."""
from typing import List, Tuple, Dict, Union
import csv
from typing import Dict, List, Tuple, Union

from ga4gh.vrsatile.pydantic.vrsatile_models import CURIE
from gene.query import QueryHandler
from gene.schemas import MatchType
from ga4gh.vrsatile.pydantic.vrsatile_models import CURIE

from curfu import logger, ServiceWarning, MAX_SUGGESTIONS
from curfu import MAX_SUGGESTIONS, LookupServiceError, logger
from curfu.utils import get_data_file


# term -> (normalized ID, normalized label)
Map = Dict[str, Tuple[str, str, str]]

Expand Down Expand Up @@ -51,13 +50,13 @@ def get_normalized_gene(
if not gd or not gd.gene_id:
msg = f"Unexpected null property in normalized response for `{term}`"
logger.error(msg)
raise ServiceWarning(msg)
raise LookupServiceError(msg)
concept_id = gd.gene_id
symbol = gd.label
if not symbol:
msg = f"Unable to retrieve symbol for gene {concept_id}"
logger.error(msg)
raise ServiceWarning(msg)
raise LookupServiceError(msg)
term_lower = term.lower()
term_cased = None
if response.match_type == 100:
Expand Down Expand Up @@ -100,7 +99,7 @@ def get_normalized_gene(
else:
warn = f"Lookup of gene term {term} failed."
logger.warning(warn)
raise ServiceWarning(warn)
raise LookupServiceError(warn)

def suggest_genes(self, query: str) -> Dict[str, List[Tuple[str, str, str]]]:
"""Provide autocomplete suggestions based on submitted term.
Expand Down Expand Up @@ -153,6 +152,6 @@ def suggest_genes(self, query: str) -> Dict[str, List[Tuple[str, str, str]]]:
if n > MAX_SUGGESTIONS:
warn = f"Exceeds max matches: Got {n} possible matches for {query} (limit: {MAX_SUGGESTIONS})" # noqa: E501
logger.warning(warn)
raise ServiceWarning(warn)
raise LookupServiceError(warn)
else:
return suggestions
27 changes: 12 additions & 15 deletions server/curfu/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from starlette.templating import _TemplateResponse as TemplateResponse
from fusor import FUSOR
from starlette.templating import _TemplateResponse as TemplateResponse

from curfu import APP_ROOT
from curfu.version import __version__ as curfu_version
from curfu.gene_services import GeneService
from curfu.domain_services import DomainService
from curfu.gene_services import GeneService
from curfu.routers import (
nomenclature,
utilities,
constructors,
lookup,
complete,
validate,
constructors,
demo,
lookup,
meta,
nomenclature,
utilities,
validate,
)

from curfu.version import __version__ as curfu_version

fastapi_app = FastAPI(
version=curfu_version,
Expand Down Expand Up @@ -52,8 +51,7 @@


def serve_react_app(app: FastAPI) -> FastAPI:
"""
Wrap application initialization in Starlette route param converter.
"""Wrap application initialization in Starlette route param converter.
:param app: FastAPI application instance
:return: application with React frontend mounted
Expand All @@ -67,8 +65,7 @@ def serve_react_app(app: FastAPI) -> FastAPI:

@app.get("/{full_path:path}")
async def serve_react_app(request: Request, full_path: str) -> TemplateResponse:
"""
Add arbitrary path support to FastAPI service.
"""Add arbitrary path support to FastAPI service.
React-router provides something akin to client-side routing based out
of the Javascript embedded in index.html. However, FastAPI will intercede
Expand Down Expand Up @@ -118,14 +115,14 @@ def get_domain_services() -> DomainService:


@app.on_event("startup")
async def startup():
async def startup() -> None:
"""Get FUSOR reference"""
app.state.fusor = await start_fusor()
app.state.genes = get_gene_services()
app.state.domains = get_domain_services()


@app.on_event("shutdown")
async def shutdown():
async def shutdown() -> None:
"""Clean up thread pool."""
await app.state.fusor.cool_seq_tool.uta_db._connection_pool.close()
Loading

0 comments on commit 95b95a7

Please sign in to comment.