Skip to content

Commit

Permalink
style: apply updated style configs from template (#141)
Browse files Browse the repository at this point in the history
* add flake8 config from template
* add pre-commit settings from template
* apply flake8/black/isort
* re-add linting/formatting check CI action
* update readme dev instructions
  • Loading branch information
jsstevenson committed Mar 28, 2024
1 parent c4db93b commit a35cf54
Show file tree
Hide file tree
Showing 19 changed files with 159 additions and 114 deletions.
13 changes: 13 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[flake8]
ignore = E129,E133,E203,E221,E241,E251,E303,E266,H106,H904,W291
max-line-length = 100
max-complexity = 15
hang-closing = true
exclude =
.eggs
.tox
build
dist
docs/conf.py
tests/*
**/_data/migrations/*
74 changes: 37 additions & 37 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,42 @@ on:
push:

jobs:
# cqa:
# runs-on: ubuntu-latest

# steps:
# - uses: actions/checkout@v3

# - name: Set up Python
# uses: actions/setup-python@v4
# with:
# python-version: "3.10"
# cache: pip
# cache-dependency-path: '**/setup.cfg'

# - name: Install test dependencies
# run: |
# python -m pip install --upgrade pip
# pip install --use-deprecated=legacy-resolver -e .[dev]

# - name: Lint with flake8
# run: |
# # stop the build if there are Python syntax errors or undefined names
# flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
# flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

# - name: Format check with isort
# run: |
# isort --check src

# - name: Format check with black
# run: |
# black --check src

# - name: Security check with bandit
# run: |
# bandit -ll -r src
cqa:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.10"
cache: pip
cache-dependency-path: '**/setup.cfg'

- name: Install test dependencies
run: |
python -m pip install --upgrade pip
pip install --use-deprecated=legacy-resolver -e .[dev]
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Format check with isort
run: |
isort --check src
- name: Format check with black
run: |
black --check src
- name: Security check with bandit
run: |
bandit -ll -r src
test:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -71,7 +71,7 @@ jobs:
- name: Test with pytest
run: |
make test
deploy:
needs:
# - cqa
Expand Down
23 changes: 23 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
repos:
- repo: local
hooks:
- id: flake8
name: flake8
entry: flake8
language: system
types: [python]
- id: pyright
name: pyright
entry: pyright
language: system
types: [python]
- id: isort
name: isort
entry: isort
language: system
types: [python]
- id: black
name: black
entry: black
language: system
types: [python]
21 changes: 17 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
.SUFFIXES:


SHELL:=bash -e -o pipefail -O globstar
SHELL:=bash -e -o pipefail # -O globstar

SELF:=$(firstword $(MAKEFILE_LIST))

Expand Down Expand Up @@ -47,6 +47,7 @@ ${VE_DIR}:
.PHONY: develop
develop:
pip install -e .[dev]
pre-commit install

#=> install: install package
.PHONY: install
Expand Down Expand Up @@ -91,6 +92,18 @@ test-%:
tox:
tox

#=> cqa: execute code quality tests
cqa:
flake8 src --show-source --statistics
pyright
isort --check src --profile black
black --check src
bandit -ll -r src

#=> reformat: reformat code
.PHONY: reformat
reformat:
pre-commit

############################################################################
#= UTILITY TARGETS
Expand Down Expand Up @@ -144,13 +157,13 @@ distclean: cleanest

## <LICENSE>
## Copyright 2023 Source Code Committers
##
##
## 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.
Expand Down
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ tested. Patches to get other systems working would be welcomed.
$ pip install seqrepo
$ sudo mkdir -p /usr/local/share/seqrepo
$ sudo chown $USER /usr/local/share/seqrepo
$ seqrepo pull -i 2018-11-26
$ seqrepo show-status -i 2018-11-26
$ seqrepo pull -i 2018-11-26
$ seqrepo show-status -i 2018-11-26
seqrepo 0.2.3.post3.dev8+nb8298bd62283
root directory: /usr/local/share/seqrepo/2018-11-26, 7.9 GB
backends: fastadir (schema 1), seqaliasdb (schema 1)
backends: fastadir (schema 1), seqaliasdb (schema 1)
sequences: 773587 sequences, 93051609959 residues, 192 files
aliases: 5579572 aliases, 5480085 current, 26 namespaces, 773587 sequences

Expand Down Expand Up @@ -187,6 +187,15 @@ Here's how to get started developing:
source venv/bin/activate
seqrepo --version

Code reformatting:

make reformat

Install pre-commit hook:

# included in `make devready`, not necessary for new installations
pre-commit install

## Building a docker image

Docker images are available at https://hub.docker.com/r/biocommons/seqrepo.
Expand Down
5 changes: 0 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,6 @@ line-length = 100
profile = "black"
src_paths = ["src", "tests"]

# [tool.flake8]
# flake8 does not support configuration in pyproject.toml
# https://github.com/PyCQA/flake8/issues/234#issuecomment-812800832
# The config in setup.cfg

[tool.pyright]
include = ["src", "tests"]

Expand Down
3 changes: 0 additions & 3 deletions src/biocommons/seqrepo/_internal/logging_support.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import logging


class DuplicateFilter:
"""
Filters away duplicate log messages.
Expand Down
13 changes: 3 additions & 10 deletions src/biocommons/seqrepo/_versionwarning.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
"""emits a warning when imported under Python < 3.6
This module may be used by other biocommons packages
"""
"""emits a warning when imported under Python < 3.9"""

import logging
import sys

__all__ = []

version_warning = (
"biocommons packages are tested and supported only on Python >= 3.6"
" (https://github.com/biocommons/org/wiki/Migrating-to-Python-3.6)"
)
version_warning = "This package is tested and supported only on Python >= 3.9."

_logger = logging.getLogger(__package__)

if sys.version_info < (3, 6):
if sys.version_info < (3, 9):
_logger.warning(version_warning)
34 changes: 16 additions & 18 deletions src/biocommons/seqrepo/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import itertools
import logging
import os
import pprint
import re
import shutil
import stat
Expand All @@ -30,7 +29,6 @@

import bioutils.assemblies
import bioutils.seqfetcher
import six
import tqdm

from . import SeqRepo, __version__
Expand Down Expand Up @@ -62,7 +60,7 @@ def _get_remote_instances(opts):
]
_logger.debug("Executing `" + " ".join(rsync_cmd) + "`")
lines = subprocess.check_output(rsync_cmd).decode().splitlines()[1:]
dirs = (m.group(1) for m in (line_re.match(l) for l in lines) if m)
dirs = (m.group(1) for m in (line_re.match(line) for line in lines) if m)
return sorted(list(filter(instance_name_new_re.match, dirs)))


Expand All @@ -81,12 +79,14 @@ def _latest_instance_path(opts):


def parse_arguments():
epilog = (
f"seqrepo {__version__}"
"See https://github.com/biocommons/biocommons.seqrepo for more information"
)
top_p = argparse.ArgumentParser(
description=__doc__.split("\n\n")[0],
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
epilog="seqrepo "
+ __version__
+ ". See https://github.com/biocommons/biocommons.seqrepo for more information",
epilog=epilog,
)
top_p.add_argument("--dry-run", "-n", default=False, action="store_true")
top_p.add_argument("--remote-host", default="dl.biocommons.org", help="rsync server host")
Expand Down Expand Up @@ -352,12 +352,12 @@ def add_assembly_names(opts):
]
if not_in_seqrepo:
_logger.warning(
"Assembly {an} references {n} accessions not in SeqRepo instance {opts.instance_name} (e.g., {acs})".format(
"Assembly {an} references {n} accessions not in SeqRepo instance "
"{opts.instance_name} (e.g., {acs})".format(
an=assy_name,
n=len(not_in_seqrepo),
opts=opts,
acs=", ".join(not_in_seqrepo[:5] + ["..."]),
seqrepo_dir=seqrepo_dir,
)
)
if not opts.partial_load:
Expand Down Expand Up @@ -432,8 +432,8 @@ def _rec_iterator():
"{ns}:{a}".format(ns=ns, a=a) for ns, aliases in sorted(nsad.items()) for a in aliases
]
print(">" + " ".join(aliases))
for l in _wrap_lines(srec["seq"], 100):
print(l)
for line in _wrap_lines(srec["seq"], 100):
print(line)


def export_aliases(opts):
Expand Down Expand Up @@ -477,7 +477,7 @@ def init(opts):
seqrepo_dir = os.path.join(opts.root_directory, opts.instance_name)
if os.path.exists(seqrepo_dir) and len(os.listdir(seqrepo_dir)) > 0:
raise IOError("{seqrepo_dir} exists and is not empty".format(seqrepo_dir=seqrepo_dir))
sr = SeqRepo(seqrepo_dir, writeable=True) # flake8: noqa
sr = SeqRepo(seqrepo_dir, writeable=True) # noqa: F841


def list_local_instances(opts):
Expand Down Expand Up @@ -586,14 +586,12 @@ def show_status(opts):
)
)
print(
"sequences: {ss[n_sequences]} sequences, {ss[tot_length]} residues, {ss[n_files]} files".format(
ss=sr.sequences.stats()
)
"sequences: {ss[n_sequences]} sequences, {ss[tot_length]} residues, "
"{ss[n_files]} files".format(ss=sr.sequences.stats())
)
print(
"aliases: {sa[n_aliases]} aliases, {sa[n_current]} current, {sa[n_namespaces]} namespaces, {sa[n_sequences]} sequences".format(
sa=sr.aliases.stats()
)
"aliases: {sa[n_aliases]} aliases, {sa[n_current]} current, "
"{sa[n_namespaces]} namespaces, {sa[n_sequences]} sequences".format(sa=sr.aliases.stats())
)
return sr

Expand Down Expand Up @@ -679,7 +677,7 @@ def _drop_write(p):

def start_shell(opts):
seqrepo_dir = os.path.join(opts.root_directory, opts.instance_name)
sr = SeqRepo(seqrepo_dir)
sr = SeqRepo(seqrepo_dir) # noqa: 682
import IPython

IPython.embed(
Expand Down
3 changes: 2 additions & 1 deletion src/biocommons/seqrepo/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ def parse_caching_env_var(env_name: str, env_default: str) -> Optional[int]:


SEQREPO_LRU_CACHE_MAXSIZE = parse_caching_env_var("SEQREPO_LRU_CACHE_MAXSIZE", "1000000")
# Using a default value here of -1 to differentiate not setting this env var and an explicit None (unbounded cache)
# Using a default value here of -1 to differentiate not setting this env var and an
# explicit None (unbounded cache)
SEQREPO_FD_CACHE_MAXSIZE = parse_caching_env_var("SEQREPO_FD_CACHE_MAXSIZE", "-1")
5 changes: 2 additions & 3 deletions src/biocommons/seqrepo/dataproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import logging
import os
from abc import ABC, abstractmethod
from collections.abc import Sequence
from urllib.parse import urlparse

import requests
Expand Down Expand Up @@ -149,7 +148,7 @@ def _get_sequence(self, identifier, start=None, end=None):
url = self.base_url + f"sequence/{identifier}"
params = {"start": start, "end": end}
_logger.info(f"Fetching {url} {params if (start or end) else ''}")
resp = requests.get(url, params=params)
resp = requests.get(url, params=params, timeout=60)
if resp.status_code == 404:
raise KeyError(identifier)
resp.raise_for_status()
Expand All @@ -158,7 +157,7 @@ def _get_sequence(self, identifier, start=None, end=None):
def _get_metadata(self, identifier):
url = self.base_url + f"metadata/{identifier}"
_logger.info("Fetching %s", url)
resp = requests.get(url)
resp = requests.get(url, timeout=60)
if resp.status_code == 404:
raise KeyError(identifier)
resp.raise_for_status()
Expand Down
2 changes: 1 addition & 1 deletion src/biocommons/seqrepo/fastadir/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .fastadir import FastaDir
from .fastadir import FastaDir # noqa: F401
Loading

0 comments on commit a35cf54

Please sign in to comment.