Skip to content

Commit

Permalink
Update pystow (#40)
Browse files Browse the repository at this point in the history
* Update pystow

* Update mock
  • Loading branch information
cthoyt committed Dec 7, 2020
1 parent 53e546c commit 34134ef
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 56 deletions.
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ignore =
E501
# ignore binary operator line breaks
W504
W503

# flake8-import-order
application-import-names =
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ install_requires =
pybel>=0.15.0<0.16.0
bel_resources>=0.0.3
pyobo>=0.2.2
pystow

# Random options
zip_safe = false
Expand Down
18 changes: 11 additions & 7 deletions src/bio2bel/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import click
from tqdm import tqdm

from .constants import config
from pybel import config
from .manager import AbstractManager, get_bio2bel_manager_classes
from .manager.bel_manager import BELManagerMixin
from .manager.namespace_manager import BELNamespaceManagerMixin
Expand All @@ -30,13 +30,17 @@
help='Database connection string.',
)

commands = {}
for name, manager_cls in MANAGERS.items():
commands[name] = manager_cls.get_cli()
# can not use single asterick, causes documentation build failure
commands[name].help = f'# Manage {name}'

main = click.Group(commands=commands)
def _get_commands():
rv = {}
for name, manager_cls in MANAGERS.items():
rv[name] = manager_cls.get_cli()
# can not use single asterick, causes documentation build failure
rv[name].help = f'# Manage {name}'
return rv


main = click.Group(commands=_get_commands())
main.help = f'Bio2BEL Command Line Utilities on {sys.executable}\nBio2BEL v{get_version()}'


Expand Down
5 changes: 4 additions & 1 deletion src/bio2bel/compath/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
unifying framework for integrating them.
"""

from .manager import CompathManager, get_compath_manager_classes, get_compath_modules # noqa: F401
from .manager import ( # noqa: F401
CompathManager, get_compath_manager_classes, get_compath_managers,
get_compath_modules, iter_compath_managers,
)
from .mixins import CompathPathwayMixin, CompathProteinMixin # noqa: F401
26 changes: 22 additions & 4 deletions src/bio2bel/compath/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import types
import typing
from collections import Counter
from functools import lru_cache
from typing import Iterable, List, Mapping, Optional, Set, Tuple, Type

import click
Expand All @@ -32,6 +33,8 @@
__all__ = [
'CompathManager',
'get_compath_manager_classes',
'iter_compath_managers',
'get_compath_managers',
]

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -133,7 +136,7 @@ def get_proteins_by_hgnc_ids(self, hgnc_ids: Iterable[str]) -> List[CompathProte
def _help_get_proteins(self, protein_column, queries: Iterable[str]) -> List[CompathProteinMixin]:
return self._query_protein().filter(protein_column.in_(queries)).all()

def search_genes(self, query: str, *, limit: Optional[int] = None) -> Optional[CompathPathwayMixin]:
def search_genes(self, query: str, *, limit: Optional[int] = None) -> List[CompathProteinMixin]:
"""Filter genes by HGNC gene symbol.
:param query: part of an HGNC identifier or gene symbol
Expand All @@ -156,7 +159,7 @@ def search_pathways(self, query: str, *, limit: Optional[int] = None) -> List[Co
:param limit: limit number of results
"""
_query = self._query_pathway().filter(or_(
self.pathway_model.name.contains(query),
self.pathway_model.name.ilike(f'%{query}%'),
self.pathway_model.identifier.contains(query),
))

Expand Down Expand Up @@ -395,8 +398,11 @@ def enrich_pathways(self, graph: BELGraph) -> None:
pathway_identifiers = {
node.identifier
for node in graph
if isinstance(node,
pybel.dsl.BiologicalProcess) and node.namespace.lower() == self.module_name and node.identifier
if (
isinstance(node, pybel.dsl.BiologicalProcess)
and node.namespace.lower() == self.module_name
and node.identifier
)
}
for pathway_identifier in pathway_identifiers:
pathway = self.get_pathway_by_id(pathway_identifier)
Expand All @@ -418,6 +424,18 @@ def get_compath_modules() -> Mapping[str, types.ModuleType]:
return dict(_get_modules('compath'))


@lru_cache()
def get_compath_manager_classes() -> Mapping[str, Type[CompathManager]]:
"""Get all ComPath manager classes."""
return dict(_get_managers('compath'))


def iter_compath_managers(*args, **kwargs) -> Iterable[Tuple[str, CompathManager]]:
"""Iterate over ComPath managers."""
for name, cls in _get_managers('compath'):
yield name, cls(*args, **kwargs)


def get_compath_managers(*args, **kwargs) -> Mapping[str, CompathManager]:
"""Get instantiated ComPath classes."""
return dict(iter_compath_managers(*args, **kwargs))
34 changes: 2 additions & 32 deletions src/bio2bel/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os

import click
from easy_config import EasyConfig
import pystow

_USER_CONFIG_DIRECTORY = os.path.abspath(os.path.join(os.path.expanduser('~'), '.config'))
DEFAULT_CONFIG_PATHS = [
Expand All @@ -25,37 +25,7 @@
os.path.join(_USER_CONFIG_DIRECTORY, 'pybel', 'pybel.ini'),
]


class Config(EasyConfig):
"""Configuration for Bio2BEL."""

NAME = 'bio2bel'
FILES = DEFAULT_CONFIG_PATHS

#: The directory in which Bio2BEL data is stored
directory: str = os.path.join(os.path.expanduser('~'), '.bio2bel')

#: The default name of the Bio2BEL database with SQLite
default_cache_name: str = 'bio2bel.db'

#: The SQLAlchemy connection string to the database
connection: str = None

def __post_init__(self):
os.makedirs(self.directory, exist_ok=True)
if self.connection is None:
self.connection = f'sqlite:///{os.path.join(self.directory, self.default_cache_name)}'


config = Config.load(_lookup_config_envvar='config')
BIO2BEL_DIR = config.directory
DEFAULT_CACHE_CONNECTION = config.connection


def get_global_connection() -> str:
"""Return the global connection string."""
return config.connection

BIO2BEL_HOME = pystow.get('bio2bel')

directory_option = click.option(
'-d', '--directory',
Expand Down
7 changes: 3 additions & 4 deletions src/bio2bel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session, sessionmaker

from .constants import get_global_connection
from .utils import get_connection

log = logging.getLogger(__name__)
logger = logging.getLogger(__name__)

Base = declarative_base()

Expand Down Expand Up @@ -146,8 +146,7 @@ def _store_helper(model: Action, session: Optional[Session] = None) -> None:
def _make_session(connection: Optional[str] = None) -> Session:
"""Make a session."""
if connection is None:
connection = get_global_connection()

connection = get_connection()
engine = create_engine(connection)

create_all(engine)
Expand Down
2 changes: 1 addition & 1 deletion src/bio2bel/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def mock_connection() -> str:
"""Get the connection enclosed by this class."""
return self.connection

self.mock_global_connection = mock.patch('bio2bel.models.get_global_connection', mock_connection)
self.mock_global_connection = mock.patch('bio2bel.models.get_connection', mock_connection)
self.mock_module_connection = mock.patch('bio2bel.utils.get_connection', mock_connection)


Expand Down
10 changes: 5 additions & 5 deletions src/bio2bel/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
from botocore.client import BaseClient
from pkg_resources import UnknownExtra, VersionConflict, iter_entry_points

from .constants import BIO2BEL_DIR, config
import pybel.config
from .constants import BIO2BEL_HOME

__all__ = [
'get_data_dir',
Expand All @@ -38,7 +39,7 @@ def get_data_dir(module_name: str) -> str:
:return: The module's data directory
"""
module_name = module_name.lower()
data_dir = os.path.join(BIO2BEL_DIR, module_name)
data_dir = os.path.join(BIO2BEL_HOME, module_name)
os.makedirs(data_dir, exist_ok=True)
return data_dir

Expand Down Expand Up @@ -126,7 +127,7 @@ def _ensure_s3_client(s3_client: Optional[BaseClient]) -> BaseClient:
return s3_client


def get_connection(*, connection: Optional[str] = None) -> str:
def get_connection(connection: Optional[str] = None) -> str:
"""Return the SQLAlchemy connection string if it is set.
Order of operations:
Expand All @@ -141,11 +142,10 @@ def get_connection(*, connection: Optional[str] = None) -> str:
6. Check the bio2bel config file for default
7. Fall back to standard default cache connection
:param module_name: The name of the module to get the configuration for
:param connection: get the SQLAlchemy connection string
:return: The SQLAlchemy connection string based on the configuration
"""
return connection or config.connection
return connection or pybel.config.connection


def get_bio2bel_modules() -> Mapping[str, types.ModuleType]:
Expand Down
4 changes: 2 additions & 2 deletions src/bio2bel/web/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from flask_admin import Admin
from pkg_resources import VersionConflict, iter_entry_points

from bio2bel.constants import DEFAULT_CACHE_CONNECTION
from bio2bel.manager.connection_manager import build_engine_session
from pybel import config

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -59,7 +59,7 @@ def create_application(connection: Optional[str] = None) -> Flask:
flask_bootstrap.Bootstrap(app)
Admin(app)

connection = connection or DEFAULT_CACHE_CONNECTION
connection = connection or config.connection
engine, session = build_engine_session(connection)

for name, add_admin in add_admins.items():
Expand Down

0 comments on commit 34134ef

Please sign in to comment.