Skip to content

Commit

Permalink
Allow passing directory path in FolderData constructor (#3359)
Browse files Browse the repository at this point in the history
This will initialize the node and copy over the tree pointed to by the
`tree` keyword argument into the node's repository.
  • Loading branch information
sphuber authored Sep 27, 2019
1 parent 0c2a8da commit 954511f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions aiida/backends/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
'orm.comments': ['aiida.backends.tests.orm.test_comments'],
'orm.computers': ['aiida.backends.tests.orm.test_computers'],
'orm.data.dict': ['aiida.backends.tests.orm.data.test_dict'],
'orm.data.folder': ['aiida.backends.tests.orm.data.test_folder'],
'orm.data.kpoints': ['aiida.backends.tests.orm.data.test_kpoints'],
'orm.data.orbital': ['aiida.backends.tests.orm.data.test_orbital'],
'orm.data.remote': ['aiida.backends.tests.orm.data.test_remote'],
Expand Down
47 changes: 47 additions & 0 deletions aiida/backends/tests/orm/data/test_folder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
###########################################################################
# Copyright (c), The AiiDA team. All rights reserved. #
# This file is part of the AiiDA code. #
# #
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core #
# For further information on the license, see the LICENSE.txt file #
# For further information please visit http://www.aiida.net #
###########################################################################
"""Tests for the `FolderData` class."""
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import

import io
import os
import shutil
import tempfile

from aiida.backends.testbase import AiidaTestCase
from aiida.orm import FolderData


class TestFolderData(AiidaTestCase):
"""Test for the `FolderData` class."""

@classmethod
def setUpClass(cls, *args, **kwargs):
super(TestFolderData, cls).setUpClass(*args, **kwargs)
cls.tempdir = tempfile.mkdtemp()
cls.tree = {
'a.txt': u'Content of file A\nWith some newlines',
'b.txt': u'Content of file B without newline',
}

for filename, content in cls.tree.items():
with io.open(os.path.join(cls.tempdir, filename), 'w', encoding='utf8') as handle:
handle.write(content)

@classmethod
def tearDownClass(cls, *args, **kwargs):
shutil.rmtree(cls.tempdir)

def test_constructor_tree(self):
"""Test the `tree` constructor keyword."""
node = FolderData(tree=self.tempdir)
self.assertEqual(sorted(node.list_object_names()), sorted(self.tree.keys()))
22 changes: 22 additions & 0 deletions aiida/orm/nodes/data/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,25 @@

class FolderData(Data):
"""`Data` sub class to represent a folder on a file system."""

def __init__(self, **kwargs):
"""Construct a new `FolderData` to which any files and folders can be added.
Use the `tree` keyword to simply wrap a directory:
folder = FolderData(tree='/absolute/path/to/directory')
Alternatively, one can construct the node first and then use the various repository methods to add objects:
folder = FolderData()
folder.put_object_from_tree('/absolute/path/to/directory')
folder.put_object_from_filepath('/absolute/path/to/file.txt')
folder.put_object_from_filelike(filelike_object)
:param tree: absolute path to a folder to wrap
:type tree: str
"""
tree = kwargs.pop('tree', None)
super(FolderData, self).__init__(**kwargs)
if tree:
self.put_object_from_tree(tree)

0 comments on commit 954511f

Please sign in to comment.