Skip to content

Commit

Permalink
Lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
soapy1 committed Jan 9, 2025
1 parent 9df6602 commit f404d18
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 138 deletions.
84 changes: 40 additions & 44 deletions conda-store-server/conda_store_server/_internal/server/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1428,28 +1428,25 @@ async def api_get_settings(
namespace: str = None,
environment_name: str = None,
):
with conda_store.get_db() as db:
if namespace is None:
arn = ""
elif environment_name is None:
arn = namespace
else:
arn = f"{namespace}/{environment_name}"

auth.authorize_request(
request,
arn,
{Permissions.SETTING_READ},
require=True,
)
if namespace is None:
arn = ""
elif environment_name is None:
arn = namespace
else:
arn = f"{namespace}/{environment_name}"

auth.authorize_request(
request,
arn,
{Permissions.SETTING_READ},
require=True,
)

return {
"status": "ok",
"data": conda_store.get_settings(
namespace, environment_name
).model_dump(),
"message": None,
}
return {
"status": "ok",
"data": conda_store.get_settings(namespace, environment_name).model_dump(),
"message": None,
}


@router_api.put(
Expand All @@ -1472,28 +1469,27 @@ async def api_put_settings(
namespace: str = None,
environment_name: str = None,
):
with conda_store.get_db() as db:
if namespace is None:
arn = ""
elif environment_name is None:
arn = namespace
else:
arn = f"{namespace}/{environment_name}"
if namespace is None:
arn = ""
elif environment_name is None:
arn = namespace
else:
arn = f"{namespace}/{environment_name}"

auth.authorize_request(
request,
arn,
{Permissions.SETTING_UPDATE},
require=True,
)

auth.authorize_request(
request,
arn,
{Permissions.SETTING_UPDATE},
require=True,
)
try:
conda_store.set_settings(namespace, environment_name, data)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e.args[0]))

try:
conda_store.set_settings(namespace, environment_name, data)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e.args[0]))

return {
"status": "ok",
"data": None,
"message": f"global setting keys {list(data.keys())} updated",
}
return {
"status": "ok",
"data": None,
"message": f"global setting keys {list(data.keys())} updated",
}
59 changes: 29 additions & 30 deletions conda-store-server/conda_store_server/_internal/server/views/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,34 +293,33 @@ async def ui_get_setting(
namespace: str = None,
environment_name: str = None,
):
with conda_store.get_db() as db:
if namespace is None:
arn = ""
elif environment_name is None:
arn = namespace
else:
arn = f"{namespace}/{environment_name}"

auth.authorize_request(
request,
arn,
{Permissions.SETTING_READ},
require=True,
)

api_setting_url = str(request.url_for("api_put_settings"))
if namespace is not None:
api_setting_url += f"{namespace}/"
if environment_name is not None:
api_setting_url += f"{environment_name}/"

context = {
"request": request,
"namespace": namespace,
"environment_name": environment_name,
"api_settings_url": api_setting_url,
"settings": conda_store.get_settings(
namespace=namespace, environment_name=environment_name
),
}
if namespace is None:
arn = ""
elif environment_name is None:
arn = namespace
else:
arn = f"{namespace}/{environment_name}"

auth.authorize_request(
request,
arn,
{Permissions.SETTING_READ},
require=True,
)

api_setting_url = str(request.url_for("api_put_settings"))
if namespace is not None:
api_setting_url += f"{namespace}/"
if environment_name is not None:
api_setting_url += f"{environment_name}/"

context = {
"request": request,
"namespace": namespace,
"environment_name": environment_name,
"api_settings_url": api_setting_url,
"settings": conda_store.get_settings(
namespace=namespace, environment_name=environment_name
),
}
return templates.TemplateResponse(request, "setting.html", context)
22 changes: 14 additions & 8 deletions conda-store-server/conda_store_server/_internal/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

from typing import Any, Dict, Callable
import functools
from typing import Any, Callable, Dict

from sqlalchemy.orm import Session
import pydantic
from sqlalchemy.orm import Session

from conda_store_server import api
from conda_store_server._internal import schema
Expand All @@ -23,6 +23,7 @@ def wrapper(self, *args, **kwargs):
result = func(self, *args, **kwargs)
self.db.close()
return result

return wrapper

@_ensure_closed_session
Expand Down Expand Up @@ -110,20 +111,26 @@ def get_settings(
if len(prefixes) > 0:
# get the fields that scoped globally. These are the keys that will NOT be
# merged on for namespace and environment prefixes.
global_fields = [k for k, v in schema.Settings.model_fields.items() if v.json_schema_extra["metadata"]["global"]]
global_fields = [
k
for k, v in schema.Settings.model_fields.items()
if v.json_schema_extra["metadata"]["global"]
]
# start building settings with the least specific defaults
for prefix in prefixes:
new_settings = api.get_kvstore_key_values(self.db, prefix)
# remove any global fields
new_settings = {k: v for k, v in new_settings.items() if k not in global_fields}
new_settings = {
k: v for k, v in new_settings.items() if k not in global_fields
}
settings.update(new_settings)

return schema.Settings(**settings)

@_ensure_closed_session
def get_setting(
self, key: str, namespace: str = None, environment_name: str = None
) -> Any:
):
"""Get a given setting at the given level of specificity. Will short
cut and look up global setting directly even if a namespace/environment
is specified
Expand All @@ -142,11 +149,10 @@ def get_setting(
Any
setting value, merged for the given level of specificity
"""

field = schema.Settings.model_fields.get(key)
if field is None:
return
return None

prefixes = ["setting"]
if field.json_schema_extra["metadata"]["global"] is False:
if namespace is not None:
Expand Down
8 changes: 4 additions & 4 deletions conda-store-server/conda_store_server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,18 +796,18 @@ def get_kvstore_key_values(db, prefix: str):
.all()
}


def get_kvstore_key(db, prefix: str, key: str):
"""Get value for a particular prefix and key"""
row = (
db.query(orm.KeyValueStore)
.filter(orm.KeyValueStore.prefix == prefix)
.filter(orm.KeyValueStore.key == key)
.first()
.filter(orm.KeyValueStore.prefix == prefix)
.filter(orm.KeyValueStore.key == key)
.first()
)
if row is None:
return None
return row.value



def set_kvstore_key_values(db, prefix: str, d: Dict[str, Any], update: bool = True):
Expand Down
8 changes: 3 additions & 5 deletions conda-store-server/conda_store_server/conda_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
from contextlib import contextmanager
from typing import Any, Dict

import pydantic
from celery import Celery, group
from sqlalchemy.orm import Session, sessionmaker
from sqlalchemy.pool import QueuePool

from conda_store_server import CONDA_STORE_DIR, api, conda_store_config, storage
from conda_store_server._internal import conda_utils, orm, schema, settings, utils
from conda_store_server.exception import CondaStoreError
from conda_store_server._internal import conda_utils, orm, schema, utils, settings
from conda_store_server.plugins import hookspec, plugin_manager
from conda_store_server.plugins.types import lock

Expand Down Expand Up @@ -72,8 +71,7 @@ def settings(self):
# will release the connection, however, the connection may be restablished.
# ref: https://docs.sqlalchemy.org/en/20/orm/session_basics.html#closing
self._settings = settings.Settings(
db=db,
deployment_default=schema.Settings(**self.config.trait_values())
db=db, deployment_default=schema.Settings(**self.config.trait_values())
)
return self._settings

Expand Down Expand Up @@ -195,7 +193,7 @@ def get_settings(
return self.settings.get_settings(
namespace=namespace, environment_name=environment_name
)

def get_setting(
self, key: str, namespace: str = None, environment_name: str = None
) -> schema.Settings:
Expand Down
Loading

0 comments on commit f404d18

Please sign in to comment.