Skip to content
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ extend-ignore =
E203,
E402,
E741,
E742,
E743,
F403,
F405,
W503
9 changes: 3 additions & 6 deletions allensdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ def one(x):
except TypeError:
return x
if xlen != 1:
raise OneResultExpectedError("Expected length one result, received: "
f"{x} results from query")
raise OneResultExpectedError(f"Expected length one result, received: {x} results from query")
if isinstance(x, set):
return list(x)[0]
else:
Expand All @@ -65,11 +64,9 @@ def one(x):
logging.getLogger(__name__).addHandler(logging.NullHandler())

if True:
file_download_log = logging.getLogger(
'allensdk.api.api.retrieve_file_over_http')
file_download_log = logging.getLogger("allensdk.api.api.retrieve_file_over_http")
file_download_log.setLevel(logging.INFO)
console = logging.StreamHandler()
formatter = logging.Formatter("%(asctime)s %(name)-12s "
"%(levelname)-8s %(message)s")
formatter = logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s %(message)s")
console.setFormatter(formatter)
file_download_log.addHandler(console)
4 changes: 2 additions & 2 deletions allensdk/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
''' Subclasses of allensdk.api.api.Api to implement specific queries to the
"""Subclasses of allensdk.api.api.Api to implement specific queries to the
`Allen Brain Atlas Data Portal <http://www.brain-map.org/api/index.html>`_.
'''
"""
162 changes: 75 additions & 87 deletions allensdk/api/api.py

Large diffs are not rendered by default.

334 changes: 133 additions & 201 deletions allensdk/api/cloud_cache/cloud_cache.py

Large diffs are not rendered by default.

25 changes: 10 additions & 15 deletions allensdk/api/cloud_cache/file_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,15 @@ class CacheFileAttributes(object):
(probably computed by the Manifest class)
"""

def __init__(self,
url: str,
version_id: str,
file_hash: str,
local_path: pathlib.Path):

def __init__(self, url: str, version_id: str, file_hash: str, local_path: pathlib.Path):
if not isinstance(url, str):
raise ValueError(f"url must be str; got {type(url)}")
if not isinstance(version_id, str):
raise ValueError(f"version_id must be str; got {type(version_id)}")
if not isinstance(file_hash, str):
raise ValueError(f"file_hash must be str; "
f"got {type(file_hash)}")
raise ValueError(f"file_hash must be str; got {type(file_hash)}")
if not isinstance(local_path, pathlib.Path):
raise ValueError(f"local_path must be pathlib.Path; "
f"got {type(local_path)}")
raise ValueError(f"local_path must be pathlib.Path; got {type(local_path)}")

self._url = url
self._version_id = version_id
Expand All @@ -61,9 +54,11 @@ def local_path(self) -> pathlib.Path:
return self._local_path

def __str__(self):
output = {'url': self.url,
'version_id': self.version_id,
'file_hash': self.file_hash,
'local_path': str(self.local_path)}
output = {
"url": self.url,
"version_id": self.version_id,
"file_hash": self.file_hash,
"local_path": str(self.local_path),
}
output = json.dumps(output, indent=2, sort_keys=True)
return f'CacheFileParameters{output}'
return f"CacheFileParameters{output}"
79 changes: 25 additions & 54 deletions allensdk/api/cloud_cache/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,28 @@ class Manifest(object):
local paths for remote resources. Defaults to False.
"""

def __init__(
self,
cache_dir: Union[str, pathlib.Path],
json_input,
use_static_project_dir: bool = False
):
def __init__(self, cache_dir: Union[str, pathlib.Path], json_input, use_static_project_dir: bool = False):
if isinstance(cache_dir, str):
self._cache_dir = pathlib.Path(cache_dir).resolve()
elif isinstance(cache_dir, pathlib.Path):
self._cache_dir = cache_dir.resolve()
else:
raise ValueError("cache_dir must be either a str "
"or a pathlib.Path; "
f"got {type(cache_dir)}")
raise ValueError(f"cache_dir must be either a str or a pathlib.Path; got {type(cache_dir)}")

self._use_static_project_dir = use_static_project_dir

self._data: Dict[str, Any] = json.load(json_input)
if not isinstance(self._data, dict):
raise ValueError("Expected to deserialize manifest into a dict; "
f"instead got {type(self._data)}")
raise ValueError(f"Expected to deserialize manifest into a dict; instead got {type(self._data)}")
self._project_name: str = self._data["project_name"]
self._version: str = self._data['manifest_version']
self._file_id_column: str = self._data['metadata_file_id_column_name']
self._version: str = self._data["manifest_version"]
self._file_id_column: str = self._data["metadata_file_id_column_name"]
self._data_pipeline: str = self._data["data_pipeline"]

self._metadata_file_names: List[str] = [
file_name for file_name in self._data['metadata_files']
]
self._metadata_file_names: List[str] = [file_name for file_name in self._data["metadata_files"]]
self._metadata_file_names.sort()

self._file_id_values: List[Any] = [ii for ii in
self._data['data_files'].keys()]
self._file_id_values: List[Any] = [ii for ii in self._data["data_files"].keys()]
self._file_id_values.sort()

@property
Expand Down Expand Up @@ -107,10 +96,7 @@ def file_id_values(self):
"""
return self._file_id_values

def _create_file_attributes(self,
remote_path: str,
version_id: str,
file_hash: str) -> CacheFileAttributes:
def _create_file_attributes(self, remote_path: str, version_id: str, file_hash: str) -> CacheFileAttributes:
"""
Create the cache_file_attributes describing a file.
This method does the work of assigning a local_path for a remote file.
Expand Down Expand Up @@ -151,19 +137,11 @@ def _create_file_attributes(self,

local_path = project_dir / shaved_rel_path

obj = CacheFileAttributes(
remote_path,
version_id,
file_hash,
local_path
)
obj = CacheFileAttributes(remote_path, version_id, file_hash, local_path)

return obj

def metadata_file_attributes(
self,
metadata_file_name: str
) -> CacheFileAttributes:
def metadata_file_attributes(self, metadata_file_name: str) -> CacheFileAttributes:
"""
Return the CacheFileAttributes associated with a metadata file

Expand All @@ -186,19 +164,15 @@ def metadata_file_attributes(
If the metadata_file_name is not a valid option
"""
if self._data is None:
raise RuntimeError("You cannot retrieve "
"metadata_file_attributes;\n"
"you have not yet loaded a manifest.json file")
raise RuntimeError(
"You cannot retrieve metadata_file_attributes;\nyou have not yet loaded a manifest.json file"
)

if metadata_file_name not in self._metadata_file_names:
raise ValueError(f"{metadata_file_name}\n"
"is not in self.metadata_file_names:\n"
f"{self._metadata_file_names}")
raise ValueError(f"{metadata_file_name}\nis not in self.metadata_file_names:\n{self._metadata_file_names}")

file_data = self._data['metadata_files'][metadata_file_name]
return self._create_file_attributes(file_data['url'],
file_data['version_id'],
file_data['file_hash'])
file_data = self._data["metadata_files"][metadata_file_name]
return self._create_file_attributes(file_data["url"], file_data["version_id"], file_data["file_hash"])

def data_file_attributes(self, file_id) -> CacheFileAttributes:
"""
Expand All @@ -224,17 +198,14 @@ def data_file_attributes(self, file_id) -> CacheFileAttributes:
If the file_id is not a valid option
"""
if self._data is None:
raise RuntimeError("You cannot retrieve data_file_attributes;\n"
"you have not yet loaded a manifest.json file")
raise RuntimeError(
"You cannot retrieve data_file_attributes;\nyou have not yet loaded a manifest.json file"
)

if file_id not in self._data['data_files']:
valid_keys = list(self._data['data_files'].keys())
if file_id not in self._data["data_files"]:
valid_keys = list(self._data["data_files"].keys())
valid_keys.sort()
raise ValueError(f"file_id: {file_id}\n"
"Is not a data file listed in manifest:\n"
f"{valid_keys}")

file_data = self._data['data_files'][file_id]
return self._create_file_attributes(file_data['url'],
file_data['version_id'],
file_data['file_hash'])
raise ValueError(f"file_id: {file_id}\nIs not a data file listed in manifest:\n{valid_keys}")

file_data = self._data["data_files"][file_id]
return self._create_file_attributes(file_data["url"], file_data["version_id"], file_data["file_hash"])
8 changes: 4 additions & 4 deletions allensdk/api/cloud_cache/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def bucket_name_from_url(url: str) -> Optional[str]:
here
https://aws.amazon.com/blogs/aws/amazon-s3-path-deprecation-plan-the-rest-of-the-story/
"""
s3_pattern = re.compile('\.s3[\.,a-z,0-9,\-]*\.amazonaws.com') # noqa: W605, E501
s3_pattern = re.compile("\.s3[\.,a-z,0-9,\-]*\.amazonaws.com") # noqa: W605, E501
url_params = url_parse.urlparse(url)
raw_location = url_params.netloc
s3_match = s3_pattern.search(raw_location)
Expand All @@ -37,8 +37,8 @@ def bucket_name_from_url(url: str) -> Optional[str]:
warnings.warn(f"{s3_pattern} does not occur in url {url}")
return None

s3_match = raw_location[s3_match.start():s3_match.end()]
return url_params.netloc.replace(s3_match, '')
s3_match = raw_location[s3_match.start() : s3_match.end()]
return url_params.netloc.replace(s3_match, "")


def relative_path_from_url(url: str) -> str:
Expand Down Expand Up @@ -81,7 +81,7 @@ def file_hash_from_path(file_path: Union[str, Path]) -> str:
The file hash (Blake2b; hexadecimal) of the file
"""
hasher = hashlib.blake2b()
with open(file_path, 'rb') as in_file:
with open(file_path, "rb") as in_file:
chunk = in_file.read(1000000)
while len(chunk) > 0:
hasher.update(chunk)
Expand Down
2 changes: 1 addition & 1 deletion allensdk/api/queries/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
#
Loading
Loading