Skip to content

Commit

Permalink
move log_error to registry
Browse files Browse the repository at this point in the history
  • Loading branch information
dmeliza committed Jan 16, 2024
1 parent 20b893d commit 1b13443
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
18 changes: 9 additions & 9 deletions nbank/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pathlib import Path
from typing import Any, Dict, Iterable, Iterator, List, Optional, Tuple, Union

import httpx
from httpx import NetRCAuth, Client

from nbank.util import ResourceLocation

Expand All @@ -24,7 +24,7 @@ def deposit(
dtype: Optional[str] = None,
hash: bool = False,
auto_id: bool = False,
auth: Optional[RegistryAuth] = None,
auth: RegistryAuth = NetRCAuth(None),
**metadata: Any,
) -> Iterator[Dict]:
"""Main entry point to deposit resources into an archive
Expand Down Expand Up @@ -65,7 +65,7 @@ def deposit(
auto_id_type = archive_cfg["policy"].get("auto_id_type", None)
allow_dirs = archive_cfg["policy"]["allow_directories"]

with httpx.Client() as session:
with Client() as session:
session.auth = auth
# check that archive exists for this path
url, params = find_archive_by_path(registry_url, archive_path)
Expand Down Expand Up @@ -119,7 +119,7 @@ def search(registry_url: str, **params) -> Iterator[Dict]:
from nbank.util import query_registry_paginated

url, _ = find_resource(registry_url)
with httpx.Client() as session:
with Client() as session:
for result in query_registry_paginated(session, url, params):
yield result

Expand All @@ -130,7 +130,7 @@ def describe(registry_url: str, id: str) -> Optional[Dict]:
from nbank.util import query_registry

url, params = get_resource(registry_url, id)
with httpx.Client() as session:
with Client() as session:
return query_registry(session, url, params)


Expand All @@ -144,7 +144,7 @@ def describe_many(registry_url: str, *ids: str) -> Iterator[Dict]:
from nbank.util import query_registry_bulk

url, query = get_resource_bulk(registry_url, ids)
with httpx.Client() as session:
with Client() as session:
for result in query_registry_bulk(session, url, query):
yield result

Expand All @@ -162,7 +162,7 @@ def find(
from nbank.util import parse_location, query_registry_paginated

url, params = get_locations(registry_url, id)
with httpx.Client() as session:
with Client() as session:
for loc in query_registry_paginated(session, url, params):
yield parse_location(loc, alt_base)

Expand Down Expand Up @@ -224,7 +224,7 @@ def fetch(

# query the database for the URL
url, _ = get_locations(base_url, id)
with httpx.Client(auth=auth) as session:
with Client(auth=auth) as session:
for loc in query_registry(session, url):
if loc["scheme"] in ("https", "http"):
res_url = parse_location(loc)
Expand All @@ -239,7 +239,7 @@ def update(
"""Update metadata for one or more resources. Set a key to None to delete."""
from nbank.registry import update_resource_metadata

with httpx.Client(headers={"Accept": "application/json"}, auth=auth) as session:
with Client(headers={"Accept": "application/json"}, auth=auth) as session:
for id in ids:
url, params = update_resource_metadata(base_url, id, **metadata)
r = session.patch(url, json=params)
Expand Down
13 changes: 13 additions & 0 deletions nbank/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,16 @@ def url_join(base: str, *path: str) -> str:
def strip_nulls(d: Dict) -> Dict:
"""Removes all keys from a dict that are equal to None"""
return {k: v for k, v in d.items() if v is not None}


def log_error(err):
"""Writes error message from server to log. Reraises errors where code != 400"""
if err.response.status_code == 400:
data = err.response.json()
for k, v in data.items():
for vv in v:
log.error(" registry error: %s: %s", k, vv)
elif err.response.status_code == 415:
log.error(" registry error: %s", err.response.reason)
else:
raise err
21 changes: 4 additions & 17 deletions nbank/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- mode: python -*-
"""Script entry points for neurobank
Copyright (C) 2013 Dan Meliza <[email protected]>
Copyright (C) 2013-2024 Dan Meliza <[email protected]>
Created Tue Nov 26 22:48:58 2013
"""
import argparse
Expand Down Expand Up @@ -30,19 +30,6 @@ def setup_log(log, debug=False):
log.addHandler(ch)


def log_error(err):
"""Writes error message from server to log. Reraises errors where code != 400"""
if err.response.status_code == 400:
data = err.response.json()
for k, v in data.items():
for vv in v:
log.error(" registry error: %s: %s", k, vv)
elif err.response.status_code == 415:
log.error(" registry error: %s", err.response.reason)
else:
raise err


def userpwd(arg):
"""If arg is of the form username:password, returns them as a tuple. Otherwise None."""
ret = arg.split(":")
Expand Down Expand Up @@ -313,7 +300,7 @@ def main(argv=None):
" Or, you may not have permission for this operation."
)
else:
log_error(e)
registry.log_error(e)
except KeyboardInterrupt:
pass

Expand Down Expand Up @@ -343,7 +330,7 @@ def init_archive(args):
r = httpx.post(url, json=params, auth=args.auth)
r.raise_for_status()
except httpx.HTTPStatusError as e:
log_error(e)
registry.log_error(e)
else:
log.info("registered '%s' as archive '%s'", args.directory, args.name)
archive.create(args.directory, args.registry_url, args.umask)
Expand Down Expand Up @@ -405,7 +392,7 @@ def locate_resources(args):
if e.response.status_code == 404:
log.error("%s: not found", id)
else:
log_error(e)
registry.log_error(e)


def search_resources(args):
Expand Down
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ setup_requires =
install_requires =
httpx>=0.24

[options.extras_require]
test =
pytest >= 7.0

[options.entry_points]
console_scripts =
nbank = nbank.script:main
Expand Down

0 comments on commit 1b13443

Please sign in to comment.