Skip to content

Commit

Permalink
Fix the path for finding the herd zip file. (#1046)
Browse files Browse the repository at this point in the history
* fix

* test

* test

* Update CHANGELOG.md
  • Loading branch information
mavaylon1 committed Jan 29, 2024
1 parent d5d6383 commit 825e31f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## HDMF 3.12.1 (Upcoming)

### Bug fixes
- Fixed retrieving the correct path for a `HERD` zip file on read. [#1046](https://github.com/hdmf-dev/hdmf/pull/1046)
- Fixed internal links in docstrings and tutorials. @stephprince [#1031](https://github.com/hdmf-dev/hdmf/pull/1031)
- Fixed issue with creating documentation links to classes in docval arguments. @rly [#1036](https://github.com/hdmf-dev/hdmf/pull/1036)

Expand Down
11 changes: 10 additions & 1 deletion src/hdmf/common/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,14 +989,23 @@ def to_zip(self, **kwargs):
for file in files:
os.remove(file)

@classmethod
@docval({'name': 'path', 'type': str, 'doc': 'The path to the zip file.'})
def get_zip_directory(cls, path):
"""
Return the directory of the file given.
"""
directory = os.path.dirname(os.path.realpath(path))
return directory

@classmethod
@docval({'name': 'path', 'type': str, 'doc': 'The path to the zip file.'})
def from_zip(cls, **kwargs):
"""
Method to read in zipped tsv files to populate HERD.
"""
zip_file = kwargs['path']
directory = os.path.dirname(zip_file)
directory = cls.get_zip_directory(zip_file)

with zipfile.ZipFile(zip_file, 'r') as zip:
zip.extractall(directory)
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/common/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def remove_er_files(self):
remove_test_file('./keys.tsv')
remove_test_file('./files.tsv')
remove_test_file('./HERD.zip')
remove_test_file('./HERD2.zip')

def child_tsv(self, external_resources):
for child in external_resources.children:
Expand Down Expand Up @@ -737,6 +738,25 @@ def test_to_and_from_zip(self):

self.remove_er_files()

def test_get_zip_directory(self):
er = HERD()
data = Data(name="species", data=['Homo sapiens', 'Mus musculus'])
er.add_ref(file=HERDManagerContainer(name='file'),
container=data,
key='key1',
entity_id='entity_id1',
entity_uri='entity1')

er.to_zip(path='./HERD.zip')
er.to_zip(path='HERD2.zip')

d1 = er.get_zip_directory('./HERD.zip')
d2 = er.get_zip_directory('HERD2.zip')

self.assertEqual(d1,d2)

self.remove_er_files()

def test_to_and_from_zip_entity_value_error(self):
er = HERD()
data = Data(name="species", data=['Homo sapiens', 'Mus musculus'])
Expand Down

0 comments on commit 825e31f

Please sign in to comment.