Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT: Run ruff and address some issues #119

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions templateflow/__init__.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""TemplateFlow is the Zone of Templates."""
__packagename__ = "templateflow"
__copyright__ = "2020, The TemplateFlow developers"
__packagename__ = 'templateflow'
__copyright__ = '2020, The TemplateFlow developers'
try:
from ._version import __version__
except ModuleNotFoundError:
from importlib.metadata import version, PackageNotFoundError
from importlib.metadata import PackageNotFoundError, version
try:
__version__ = version(__packagename__)
except PackageNotFoundError:
__version__ = "0+unknown"
__version__ = '0+unknown'
del version
del PackageNotFoundError

import os
from . import api
from .conf import update, TF_USE_DATALAD

from . import api
from .conf import TF_USE_DATALAD, update

if not TF_USE_DATALAD and os.getenv("TEMPLATEFLOW_AUTOUPDATE", "1") not in (
"false",
"off",
"0",
"no",
"n",
if not TF_USE_DATALAD and os.getenv('TEMPLATEFLOW_AUTOUPDATE', '1') not in (
'false',
'off',
'0',
'no',
'n',
):
# trigger skeleton autoupdate
update(local=True, overwrite=False, silent=True)

__all__ = [
"__copyright__",
"__packagename__",
"__version__",
"api",
"update",
'__copyright__',
'__packagename__',
'__version__',
'api',
'update',
]
18 changes: 9 additions & 9 deletions templateflow/_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
except ImportError: # pragma: no cover
from importlib_resources.abc import Traversable

__all__ = ["Loader"]
__all__ = ['Loader']


class Loader:
Expand Down Expand Up @@ -119,19 +119,19 @@ def _doc(self):
directory.
"""
top_level = sorted(
os.path.relpath(p, self.files) + "/"[: p.is_dir()]
os.path.relpath(p, self.files) + '/'[: p.is_dir()]
for p in self.files.iterdir()
if p.name[0] not in (".", "_") and p.name != "tests"
if p.name[0] not in ('.', '_') and p.name != 'tests'
)
doclines = [
f"Load package files relative to ``{self._anchor}``.",
"",
"This package contains the following (top-level) files/directories:",
"",
*(f"* ``{path}``" for path in top_level),
f'Load package files relative to ``{self._anchor}``.',
'',
'This package contains the following (top-level) files/directories:',
'',
*(f'* ``{path}``' for path in top_level),
]

return "\n".join(doclines)
return '\n'.join(doclines)

def readable(self, *segments) -> Traversable:
"""Provide read access to a resource through a Path-like interface.
Expand Down
60 changes: 35 additions & 25 deletions templateflow/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
import sys
from json import loads
from pathlib import Path

from bids.layout import Query

from .conf import TF_LAYOUT, TF_S3_ROOT, TF_USE_DATALAD, requires_layout
from templateflow.conf import (
TF_GET_TIMEOUT,
TF_LAYOUT,
TF_S3_ROOT,
TF_USE_DATALAD,
requires_layout,
)

_layout_dir = tuple(
item for item in dir(TF_LAYOUT) if item.startswith("get_")
item for item in dir(TF_LAYOUT) if item.startswith('get_')
)


Expand Down Expand Up @@ -59,13 +66,13 @@

"""
# Normalize extensions to always have leading dot
if "extension" in kwargs:
kwargs["extension"] = _normalize_ext(kwargs["extension"])
if 'extension' in kwargs:
kwargs['extension'] = _normalize_ext(kwargs['extension'])

Check warning on line 70 in templateflow/api.py

View check run for this annotation

Codecov / codecov/patch

templateflow/api.py#L70

Added line #L70 was not covered by tests

return [
Path(p) for p in TF_LAYOUT.get(
template=Query.ANY if template is None else template,
return_type="file",
return_type='file',
**kwargs
)
]
Expand Down Expand Up @@ -130,7 +137,7 @@
out_file = ls(template, **kwargs)

if raise_empty and not out_file:
raise Exception("No results found")
raise Exception('No results found')

# Try DataLad first
dl_missing = [p for p in out_file if not p.is_file()]
Expand All @@ -147,7 +154,7 @@
not_fetched = [str(p) for p in out_file if not p.is_file() or p.stat().st_size == 0]

if not_fetched:
msg = "Could not fetch template files: %s." % ", ".join(not_fetched)
msg = 'Could not fetch template files: %s.' % ', '.join(not_fetched)

Check warning on line 157 in templateflow/api.py

View check run for this annotation

Codecov / codecov/patch

templateflow/api.py#L157

Added line #L157 was not covered by tests
if dl_missing and not TF_USE_DATALAD:
msg += (
"""\
Expand Down Expand Up @@ -222,7 +229,7 @@

"""
tf_home = Path(TF_LAYOUT.root)
filepath = tf_home / ("tpl-%s" % template) / "template_description.json"
filepath = tf_home / ('tpl-%s' % template) / 'template_description.json'

# Ensure that template is installed and file is available
if not filepath.is_file():
Expand All @@ -243,9 +250,9 @@

"""
data = get_metadata(template)
refs = data.get("ReferencesAndLinks", [])
refs = data.get('ReferencesAndLinks', [])
if isinstance(refs, dict):
refs = [x for x in refs.values()]
refs = list(refs.values())

Check warning on line 255 in templateflow/api.py

View check run for this annotation

Codecov / codecov/patch

templateflow/api.py#L255

Added line #L255 was not covered by tests

if not bibtex:
return refs
Expand All @@ -255,10 +262,10 @@

@requires_layout
def __getattr__(key: str):
key = key.replace("ls_", "get_")
key = key.replace('ls_', 'get_')
if (
key.startswith("get_")
and key not in ("get_metadata", "get_citations")
key.startswith('get_')
and key not in ('get_metadata', 'get_citations')
and key not in _layout_dir
):
return TF_LAYOUT.__getattr__(key)
Expand All @@ -277,7 +284,7 @@
try:
api.get(filepath, dataset=str(TF_LAYOUT.root))
except IncompleteResultsError as exc:
if exc.failed[0]["message"] == "path not associated with any dataset":
if exc.failed[0]['message'] == 'path not associated with any dataset':

Check warning on line 287 in templateflow/api.py

View check run for this annotation

Codecov / codecov/patch

templateflow/api.py#L287

Added line #L287 was not covered by tests
from .conf import TF_GITHUB_SOURCE

api.install(path=TF_LAYOUT.root, source=TF_GITHUB_SOURCE, recursive=True)
Expand All @@ -288,47 +295,50 @@

def _s3_get(filepath):
from sys import stderr
from tqdm import tqdm

import requests
from tqdm import tqdm

path = filepath.relative_to(TF_LAYOUT.root).as_posix()
url = f"{TF_S3_ROOT}/{path}"
url = f'{TF_S3_ROOT}/{path}'

print("Downloading %s" % url, file=stderr)
print('Downloading %s' % url, file=stderr)
# Streaming, so we can iterate over the response.
r = requests.get(url, stream=True)
r = requests.get(url, stream=True, timeout=TF_GET_TIMEOUT)

# Total size in bytes.
total_size = int(r.headers.get("content-length", 0))
total_size = int(r.headers.get('content-length', 0))
block_size = 1024
wrote = 0
if not filepath.is_file():
filepath.unlink()

with filepath.open("wb") as f:
with tqdm(total=total_size, unit="B", unit_scale=True) as t:
with filepath.open('wb') as f:
with tqdm(total=total_size, unit='B', unit_scale=True) as t:
for data in r.iter_content(block_size):
wrote = wrote + len(data)
f.write(data)
t.update(len(data))

if total_size != 0 and wrote != total_size:
raise RuntimeError("ERROR, something went wrong")
raise RuntimeError('ERROR, something went wrong')

Check warning on line 324 in templateflow/api.py

View check run for this annotation

Codecov / codecov/patch

templateflow/api.py#L324

Added line #L324 was not covered by tests


def _to_bibtex(doi, template, idx):
if "doi.org" not in doi:
if 'doi.org' not in doi:
return doi

# Is a DOI URL
import requests

response = requests.post(
doi, headers={"Accept": "application/x-bibtex; charset=utf-8"}
doi,
headers={'Accept': 'application/x-bibtex; charset=utf-8'},
timeout=TF_GET_TIMEOUT,
)
if not response.ok:
print(
f"Failed to convert DOI <{doi}> to bibtex, returning URL.",
f'Failed to convert DOI <{doi}> to bibtex, returning URL.',
file=sys.stderr,
)
return doi
Expand Down
58 changes: 34 additions & 24 deletions templateflow/conf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
"""Configuration and settings."""
from os import getenv
import re
from warnings import warn
from pathlib import Path
from contextlib import suppress
from functools import wraps
from os import getenv
from pathlib import Path
from warnings import warn

from .._loader import Loader

load_data = Loader(__package__)

TF_DEFAULT_HOME = Path.home() / ".cache" / "templateflow"
TF_HOME = Path(getenv("TEMPLATEFLOW_HOME", str(TF_DEFAULT_HOME)))
TF_GITHUB_SOURCE = "https://github.com/templateflow/templateflow.git"
TF_S3_ROOT = "https://templateflow.s3.amazonaws.com"
TF_USE_DATALAD = getenv("TEMPLATEFLOW_USE_DATALAD", "false").lower() in (
"true",
"on",
"1",
"yes",
"y",
TF_DEFAULT_HOME = Path.home() / '.cache' / 'templateflow'
TF_HOME = Path(getenv('TEMPLATEFLOW_HOME', str(TF_DEFAULT_HOME)))
TF_GITHUB_SOURCE = 'https://github.com/templateflow/templateflow.git'
TF_S3_ROOT = 'https://templateflow.s3.amazonaws.com'
TF_USE_DATALAD = getenv('TEMPLATEFLOW_USE_DATALAD', 'false').lower() in (
'true',
'on',
'1',
'yes',
'y',
)
TF_CACHED = True
TF_GET_TIMEOUT = 10


def _init_cache():
Expand All @@ -34,6 +36,7 @@
If the path reported above is not the desired location for TemplateFlow, \
please set the TEMPLATEFLOW_HOME environment variable.""",
ResourceWarning,
stacklevel=2,
)
if TF_USE_DATALAD:
try:
Expand Down Expand Up @@ -64,7 +67,7 @@
from bids import __version__

raise RuntimeError(
f"A layout with PyBIDS <{__version__}> could not be initiated"
f'A layout with PyBIDS <{__version__}> could not be initiated'
)
return func(*args, **kwargs)

Expand All @@ -85,6 +88,7 @@
init_layout()
# ensure the api uses the updated layout
import importlib

from .. import api

importlib.reload(api)
Expand All @@ -96,19 +100,20 @@
global TF_USE_DATALAD, TF_HOME

if TF_USE_DATALAD:
print("TemplateFlow is configured in DataLad mode, wipe() has no effect")
print('TemplateFlow is configured in DataLad mode, wipe() has no effect')

Check warning on line 103 in templateflow/conf/__init__.py

View check run for this annotation

Codecov / codecov/patch

templateflow/conf/__init__.py#L103

Added line #L103 was not covered by tests
return

import importlib
from shutil import rmtree

from templateflow import api

def _onerror(func, path, excinfo):
from pathlib import Path

if Path(path).exists():
print(
f"Warning: could not delete <{path}>, please clear the cache manually."
f'Warning: could not delete <{path}>, please clear the cache manually.'
)

rmtree(TF_HOME, onerror=_onerror)
Expand All @@ -132,32 +137,37 @@
def _update_datalad():
from datalad.api import update

print("Updating TEMPLATEFLOW_HOME using DataLad ...")
print('Updating TEMPLATEFLOW_HOME using DataLad ...')
try:
update(dataset=str(TF_HOME), recursive=True, merge=True)
except Exception as e:
warn(f"Error updating TemplateFlow's home directory (using DataLad): {e}")
except Exception as e: # noqa: BLE001
warn(

Check warning on line 144 in templateflow/conf/__init__.py

View check run for this annotation

Codecov / codecov/patch

templateflow/conf/__init__.py#L143-L144

Added lines #L143 - L144 were not covered by tests
f"Error updating TemplateFlow's home directory (using DataLad): {e}",
stacklevel=2,
)
return False

Check warning on line 148 in templateflow/conf/__init__.py

View check run for this annotation

Codecov / codecov/patch

templateflow/conf/__init__.py#L148

Added line #L148 was not covered by tests
return True


TF_LAYOUT = None


def init_layout():
from templateflow.conf.bids import Layout
from bids.layout.index import BIDSLayoutIndexer

from templateflow.conf.bids import Layout

global TF_LAYOUT
TF_LAYOUT = Layout(
TF_HOME,
validate=False,
config="templateflow",
config='templateflow',
indexer=BIDSLayoutIndexer(
validate=False,
ignore=(
re.compile(r"scripts/"),
re.compile(r"/\."),
re.compile(r"^\."),
re.compile(r'scripts/'),
re.compile(r'/\.'),
re.compile(r'^\.'),
),
),
)
Expand Down
Loading
Loading