Skip to content

Commit

Permalink
start to work toward using new bbconf
Browse files Browse the repository at this point in the history
  • Loading branch information
nsheff committed Sep 3, 2023
1 parent 1f05d15 commit 34863a6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
5 changes: 5 additions & 0 deletions bedhost/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import logging
from .const import PKG_NAME

global _LOGGER
_LOGGER = logging.getLogger(PKG_NAME)
8 changes: 5 additions & 3 deletions bedhost/helpers.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import enum
from logging import getLogger
from urllib import parse

from starlette.exceptions import HTTPException
from starlette.responses import FileResponse, RedirectResponse
from ubiquerg import VersionInHelpParser
from yacman import get_first_env_var

from . import _LOGGER
from ._version import __version__ as v
from .const import *

_LOGGER = getLogger(PKG_NAME)


def build_parser():
"""
Expand Down Expand Up @@ -374,7 +372,11 @@ def get_enum_map(bbc, table_name, file_type):
"""

enum_map = {}
_LOGGER.debug(f"Getting enum map for {file_type} in {table_name}")

# TO FIX: I think we need a different way to get the schema
schema = serve_schema_for_table(bbc=bbc, table_name=table_name)

for key, value in schema.sample_level_data.items():
if value["type"] == file_type:
enum_map[value["label"]] = value["label"]
Expand Down
17 changes: 13 additions & 4 deletions bedhost/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import bbconf
import logmuse
import logging

import sys
import uvicorn

Expand All @@ -10,12 +10,11 @@
from logging import DEBUG, INFO
from typing import Dict, List, Optional

from . import _LOGGER
from .const import *
from .data_models import DBResponse
from .helpers import *

global _LOGGER
_LOGGER = logging.getLogger(__name__)
app = FastAPI(
title=PKG_NAME,
description="BED file/sets statistics and image server API",
Expand Down Expand Up @@ -96,11 +95,17 @@ def main():
port=bbc.config[CFG_SERVER_KEY][CFG_PORT_KEY],
)


def register_globals(cfg):
import logging
_LOGGER.setLevel(logging.DEBUG)
stream = logging.StreamHandler(sys.stdout)
stream.setLevel(logging.DEBUG)
_LOGGER.addHandler(stream)
global bbc
bbc = BedBaseConf(bbconf.get_bedbase_cfg(cfg))

_LOGGER.debug("Registering uvicorn globals")

if __name__ != "__main__":
# TODO: streamline this to make it work easily with either CLI or uvicorn

Expand All @@ -109,9 +114,13 @@ def register_globals(cfg):
register_globals(os.environ.get("BEDBASE_CONFIG"))
from .routers import api, private_api

_LOGGER.debug("Mounting routers")
app.include_router(api.router)
app.include_router(private_api.router, prefix="/_private_api")




else:
# Warn
_LOGGER.warning("No BEDBASE_CONFIG found. Can't configure server.")
16 changes: 13 additions & 3 deletions bedhost/routers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,18 @@
else {"http": "http"},
)

# This is using Python's Functional API to create enumerations without the typical
# class syntax. This is useful for creating enumerations dynamically, which is
# what we're doing here. We're creating a new enumeration for each table in the
# database, and then creating a new enumeration for each column in each table.
# This is done by calling the `get_enum_map` function, which returns a dictionary
# of column names and values. The `enum.Enum` function then creates a new
# enumeration class with the given name and values.


FileColumnBedset = enum.Enum(
"FileColumnBedset", get_enum_map(bbc, BEDSET_TABLE, "file")
value="FileColumnBedset", # name of the enumeration
names=get_enum_map(bbc, BEDSET_TABLE, "file") # dictionary of names and values
)

FileColumnBed = enum.Enum("FileColumnBed", get_enum_map(bbc, BED_TABLE, "file"))
Expand Down Expand Up @@ -105,7 +115,7 @@ async def get_bed_genome_assemblies():
Returns available genome assemblies in the database
"""

return bbc.bed.select_distinct(table_name=BED_TABLE, columns=["genome"])
return bbc.bed.backend.select_distinct(table_name=BED_TABLE, columns=["genome"])


@router.get("/bed/count", response_model=int)
Expand All @@ -127,7 +137,7 @@ async def get_all_bed_metadata(
if ids:
assert_table_columns_match(bbc=bbc, table_name=BED_TABLE, columns=ids)

res = bbc.bed.select(columns=ids, limit=limit)
res = bbc.bed.backend.select(columns=ids, limit=limit)

if res:
if ids:
Expand Down

0 comments on commit 34863a6

Please sign in to comment.