Skip to content

Commit

Permalink
Merge pull request #604 from dandi/code-cleanup
Browse files Browse the repository at this point in the history
Remove unused functions (and other cleanups)
  • Loading branch information
yarikoptic authored Apr 29, 2021
2 parents 6803fa1 + c6d8689 commit 914a9cf
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 114 deletions.
26 changes: 1 addition & 25 deletions dandi/cli/base.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
from functools import wraps
import os
from os import path as op

import click

from ..consts import dandiset_metadata_file, known_instances
from ..consts import known_instances
from .. import get_logger, set_logger_level # noqa: F401

lgr = get_logger()

# Aux common functionality


def get_files(paths, recursive=True, recursion_limit=None):
"""Given a list of paths, return a list of paths"""
# For now we support only individual files
dirs = list(filter(op.isdir, paths))
if dirs:
raise NotImplementedError(
"ATM supporting only listing of individual files, no recursive "
"operation. Was provided following directories: {}".format(", ".join(dirs))
)
return paths


# ???: could make them always available but hidden
# via hidden=True.
def devel_option(*args, **kwargs):
Expand All @@ -50,17 +37,6 @@ def _updated_option(*args, **kwargs):
return click.option(*args, **kwargs)


def dandiset_id_option(**kwargs):
return _updated_option(
"--dandiset-id",
kwargs,
help=f"ID of the dandiset on DANDI archive. Necessary to populate "
f"{dandiset_metadata_file}. Please use 'register' command to first "
f"register a new dandiset.",
# TODO: could add a check for correct regexp
)


def dandiset_path_option(**kwargs):
return _updated_option(
"-d",
Expand Down
59 changes: 28 additions & 31 deletions dandi/cli/cmd_ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,37 +320,34 @@ def get_metadata_ls(path, keys, errors, flatten=False, schema=None):
ignore_benign_pynwb_warnings()

def fn():
try:
rec = {}
# No need for calling get_metadata if no keys are needed from it
if keys is None or list(keys) != ["nwb_version"]:
try:
if schema is not None:
if op.isdir(path):
dandiset = APIDandiset(path, schema_version=schema)
rec = dandiset.metadata
else:
rec = nwb2asset(path, schema_version=schema).json_dict()
rec = {}
# No need for calling get_metadata if no keys are needed from it
if keys is None or list(keys) != ["nwb_version"]:
try:
if schema is not None:
if op.isdir(path):
dandiset = APIDandiset(path, schema_version=schema)
rec = dandiset.metadata
else:
rec = get_metadata(path)
except Exception as exc:
_add_exc_error(path, rec, errors, exc)
if flatten:
rec = flatten_meta_to_pyout(rec)
if keys is not None:
rec = {k: v for k, v in rec.items() if k in keys}
if (
not op.isdir(path)
and "nwb_version" not in rec
and (keys and "nwb_version" in keys)
):
# Let's at least get that one
try:
rec["nwb_version"] = get_nwb_version(path)
except Exception as exc:
_add_exc_error(path, rec, errors, exc)
return rec
finally:
pass
rec = nwb2asset(path, schema_version=schema).json_dict()
else:
rec = get_metadata(path)
except Exception as exc:
_add_exc_error(path, rec, errors, exc)
if flatten:
rec = flatten_meta_to_pyout(rec)
if keys is not None:
rec = {k: v for k, v in rec.items() if k in keys}
if (
not op.isdir(path)
and "nwb_version" not in rec
and (keys and "nwb_version" in keys)
):
# Let's at least get that one
try:
rec["nwb_version"] = get_nwb_version(path)
except Exception as exc:
_add_exc_error(path, rec, errors, exc)
return rec

return fn
8 changes: 0 additions & 8 deletions dandi/cli/cmd_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ def get_metavar(self, param):
#
# TODO: should always go to dandi for now
@instance_option()
@devel_option(
"--fake-data",
help="For development: fake file content (filename will be stored instead of actual load)",
default=False,
is_flag=True,
)
@devel_option(
"--allow-any-path",
help="For development: allow DANDI 'unsupported' file types/paths",
Expand All @@ -91,7 +85,6 @@ def upload(
dandiset_path=None,
# Development options should come as kwargs
dandi_instance="dandi",
fake_data=False, # TODO: not implemented, prune?
allow_any_path=False,
upload_dandiset_metadata=False,
devel_debug=False,
Expand Down Expand Up @@ -123,7 +116,6 @@ def upload(
validation=validation,
dandiset_path=dandiset_path,
dandi_instance=dandi_instance,
fake_data=fake_data,
allow_any_path=allow_any_path,
upload_dandiset_metadata=upload_dandiset_metadata,
devel_debug=devel_debug,
Expand Down
11 changes: 0 additions & 11 deletions dandi/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,4 @@
# ATM used only in download
MAX_CHUNK_SIZE = int(os.environ.get("DANDI_MAX_CHUNK_SIZE", 1024 * 1024 * 8)) # 64

#
# Some routes
# TODO: possibly centralize in dandi-common from our redirection service
#


# just a structure, better than dict for RFing etc
class routes(object):
dandiset_draft = "{dandi_instance.redirector}/dandiset/{dandiset[identifier]}/draft"


DANDI_SCHEMA_VERSION = "0.3.0"
4 changes: 0 additions & 4 deletions dandi/dandiarchive.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ class _dandi_url_parser:
# Defining as a class with all the attributes to not leak all the variables
# etc into module space, and later we might end up with classes for those
# anyways
id_regex = "[a-f0-9]{24}"
id_grp = f"(?P<id>{id_regex})"
dandiset_id_grp = "(?P<dandiset_id>[0-9]{6})"
# Should absorb port and "api/":
server_grp = "(?P<server>(?P<protocol>https?)://(?P<hostname>[^/]+)/(api/)?)"
Expand Down Expand Up @@ -184,8 +182,6 @@ class _dandi_url_parser:
known_patterns = "Patterns for known setups:" + "\n - ".join(
[""] + [display for _, _, display in known_urls]
)
# We might need to remap some assert_types
map_asset_types = {"dandiset": "folder"}
map_to = {}
for (gui, redirector, api) in known_instances.values():
for h in (gui, redirector):
Expand Down
6 changes: 2 additions & 4 deletions dandi/dandiset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Classes/utilities for support of a dandiset"""
import os
from pathlib import Path

from .consts import dandiset_metadata_file
Expand Down Expand Up @@ -51,9 +50,8 @@ def get_dandiset_record(cls, meta):
obtain_msg = ""
else:
obtain_msg = (
" edited online at https://dandiarchive.org/dandiset/{}{}# and".format(
dandiset_identifier, os.linesep
)
" edited online at https://dandiarchive.org/dandiset/"
f"{dandiset_identifier}\n# and"
)
header = f"""\
# DO NOT EDIT THIS FILE LOCALLY. ALL LOCAL UPDATES WILL BE LOST.
Expand Down
1 change: 0 additions & 1 deletion dandi/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def upload(
validation="require",
dandiset_path=None,
dandi_instance="dandi",
fake_data=False, # TODO: not implemented, prune?
allow_any_path=False,
upload_dandiset_metadata=False,
devel_debug=False,
Expand Down
30 changes: 0 additions & 30 deletions dandi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,6 @@ def flattened(it):
return list(flatten(it))


def updated(d, update):
"""Return a copy of the input with the 'update'
Primarily for updating dictionaries
"""
d = d.copy()
d.update(update)
return d


def remap_dict(rec, revmapping):
"""Remap nested dicts according to mapping
Expand Down Expand Up @@ -472,18 +462,6 @@ def _get_normalized_paths(path, prefix):
return path, prefix


def path_startswith(path, prefix):
"""Return True if path starts with prefix path
Parameters
----------
path: str
prefix: str
"""
path, prefix = _get_normalized_paths(path, prefix)
return path.startswith(prefix)


def path_is_subpath(path, prefix):
"""Return True if path is a subpath of prefix
Expand All @@ -498,14 +476,6 @@ def path_is_subpath(path, prefix):
return (len(prefix) < len(path)) and path.startswith(prefix)


def safe_call(func, path, default=None):
try:
return func(path)
except Exception as exc:
lgr.debug("Call to %s on %s failed: %s", func.__name__, path, exc)
return default


def shortened_repr(value, length=30):
try:
if hasattr(value, "__repr__") and (value.__repr__ is not object.__repr__):
Expand Down

0 comments on commit 914a9cf

Please sign in to comment.