Skip to content

Commit

Permalink
Use built-in 'open' instead of 'io.open'.
Browse files Browse the repository at this point in the history
The 'io.open' was previously used for py2/py3 compatibility. In
Python3, 'io.open' is simply an alias for the 'open' built-in, so
there is no reason not to use the more standard 'open' instead.
  • Loading branch information
Dominik Gresch committed Dec 9, 2019
1 parent 50e24bb commit 85686be
Show file tree
Hide file tree
Showing 46 changed files with 111 additions and 152 deletions.
7 changes: 3 additions & 4 deletions aiida/backends/general/migrations/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import datetime
import errno
import io
import os
import re

Expand Down Expand Up @@ -47,7 +46,7 @@ def put_object_from_string(uuid, name, content):
ensure_repository_folder_created(uuid)
filepath = os.path.join(get_node_repository_sub_folder(uuid), name)

with io.open(filepath, 'w', encoding='utf-8') as handle:
with open(filepath, 'w', encoding='utf-8') as handle:
handle.write(content)


Expand All @@ -59,7 +58,7 @@ def get_object_from_repository(uuid, name):
"""
filepath = os.path.join(get_node_repository_sub_folder(uuid), name)

with io.open(filepath) as handle:
with open(filepath) as handle:
return handle.read()


Expand Down Expand Up @@ -99,7 +98,7 @@ def store_numpy_array_in_repository(uuid, name, array):
ensure_repository_folder_created(uuid)
filepath = get_numpy_array_absolute_path(uuid, name)

with io.open(filepath, 'wb') as handle:
with open(filepath, 'wb') as handle:
numpy.save(handle, array)


Expand Down
2 changes: 1 addition & 1 deletion aiida/backends/tests/cmdline/commands/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def setUp(self):
self.r = RemoteData()
p = tempfile.mkdtemp()
self.r.set_remote_path(p)
with io.open(p + '/file.txt', 'w', encoding='utf8') as fhandle:
with open(p + '/file.txt', 'w', encoding='utf8') as fhandle:
fhandle.write('test string')
self.r.computer = comp
self.r.store()
Expand Down
4 changes: 2 additions & 2 deletions aiida/backends/tests/common/test_folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def test_unicode(cls):
"""Check that there are no exceptions raised when using unicode folders."""
tmpsource = tempfile.mkdtemp()
tmpdest = tempfile.mkdtemp()
with io.open(os.path.join(tmpsource, 'sąžininga'), 'w', encoding='utf8') as fhandle:
with open(os.path.join(tmpsource, 'sąžininga'), 'w', encoding='utf8') as fhandle:
fhandle.write('test')
with io.open(os.path.join(tmpsource, 'žąsis'), 'w', encoding='utf8') as fhandle:
with open(os.path.join(tmpsource, 'žąsis'), 'w', encoding='utf8') as fhandle:
fhandle.write('test')
folder = Folder(tmpdest)
folder.insert_path(tmpsource, 'destination')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
###########################################################################
"""Tests for the backup setup script."""

import io
import os
import shutil
import tempfile
Expand Down Expand Up @@ -117,7 +116,7 @@ def test_full_backup_setup_script(self):
)

# Check the content of the main backup configuration file
with io.open(os.path.join(temp_aiida_folder, 'backup_info.json'), encoding='utf8') as conf_jfile:
with open(os.path.join(temp_aiida_folder, 'backup_info.json'), encoding='utf8') as conf_jfile:
conf_cont = json.load(conf_jfile)
self.assertEqual(conf_cont[AbstractBackup.OLDEST_OBJECT_BK_KEY], '2014-07-18 13:54:53.688484+00:00')
self.assertEqual(conf_cont[AbstractBackup.DAYS_TO_BACKUP_KEY], None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# For further information please visit http://www.aiida.net #
###########################################################################
"""Tests for the configuration migration functionality."""
import io
import os
import uuid
from unittest import TestCase
Expand All @@ -28,7 +27,7 @@ def load_config_sample(filename):
"""Load a configuration file from a fixture."""
currdir = os.path.dirname(os.path.abspath(__file__))
filepath = os.path.join(currdir, 'test_samples', filename)
with io.open(filepath, 'r', encoding='utf8') as handle:
with open(filepath, 'r', encoding='utf8') as handle:
return json.load(handle)

def setUp(self):
Expand Down
3 changes: 1 addition & 2 deletions aiida/backends/tests/orm/data/test_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
###########################################################################
"""Tests for the `FolderData` class."""

import io
import os
import shutil
import tempfile
Expand All @@ -31,7 +30,7 @@ def setUpClass(cls, *args, **kwargs):
}

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

@classmethod
Expand Down
3 changes: 1 addition & 2 deletions aiida/backends/tests/orm/data/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
###########################################################################

import errno
import io
import os
import shutil
import tempfile
Expand All @@ -34,7 +33,7 @@ def setUp(self):
self.remote = RemoteData(computer=self.computer)
self.remote.set_remote_path(self.tmp_path)

with io.open(os.path.join(self.tmp_path, 'file.txt'), 'w', encoding='utf8') as fhandle:
with open(os.path.join(self.tmp_path, 'file.txt'), 'w', encoding='utf8') as fhandle:
fhandle.write('test string')

self.remote.computer = self.computer
Expand Down
7 changes: 3 additions & 4 deletions aiida/backends/tests/orm/utils/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
###########################################################################
"""Tests for the `Repository` utility class."""

import io
import os
import shutil
import tempfile
Expand Down Expand Up @@ -53,7 +52,7 @@ def create_file_tree(self, directory, tree):
os.makedirs(subdir)
self.create_file_tree(subdir, value)
else:
with io.open(os.path.join(directory, key), 'w', encoding='utf8') as handle:
with open(os.path.join(directory, key), 'w', encoding='utf8') as handle:
handle.write(value)

def get_file_content(self, key):
Expand Down Expand Up @@ -99,7 +98,7 @@ def test_put_object_from_filelike(self):
filepath = os.path.join(self.tempdir, key)
content = self.get_file_content(key)

with io.open(filepath, 'r') as handle:
with open(filepath, 'r') as handle:
node = Node()
node.put_object_from_filelike(handle, key)
self.assertEqual(node.get_object_content(key), content)
Expand All @@ -108,7 +107,7 @@ def test_put_object_from_filelike(self):
filepath = os.path.join(self.tempdir, key)
content = self.get_file_content(key)

with io.open(filepath, 'r') as handle:
with open(filepath, 'r') as handle:
node = Node()
node.put_object_from_filelike(handle, key)
self.assertEqual(node.get_object_content(key), content)
Expand Down
4 changes: 2 additions & 2 deletions aiida/backends/tests/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3112,7 +3112,7 @@ def test_export_to_file(self):
files_created = [] # In case there is an exception
try:
files_created = n.export(filename, fileformat=format)
with io.open(filename, encoding='utf8') as fhandle:
with open(filename, encoding='utf8') as fhandle:
filedata = fhandle.read()
finally:
for file in files_created:
Expand Down Expand Up @@ -3703,7 +3703,7 @@ def test_export_to_file(self):
files_created = [] # In case there is an exception
try:
files_created = b.export(filename, fileformat=format)
with io.open(filename, encoding='utf8') as fhandle:
with open(filename, encoding='utf8') as fhandle:
filedata = fhandle.read()
finally:
for file in files_created:
Expand Down
3 changes: 1 addition & 2 deletions aiida/backends/tests/test_dbimporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"""
Tests for subclasses of DbImporter, DbSearchResults and DbEntry
"""
import io
import unittest


Expand Down Expand Up @@ -264,7 +263,7 @@ def test_upfentry_creation(self):

path_root = os.path.split(aiida.__file__)[0]
path_pseudos = os.path.join(path_root, 'backends', 'tests', 'fixtures', 'pseudos')
with io.open(os.path.join(path_pseudos, '{}.UPF'.format(upf)), 'r', encoding='utf8') as f:
with open(os.path.join(path_pseudos, '{}.UPF'.format(upf)), 'r', encoding='utf8') as f:
entry._contents = f.read()

upfnode = entry.get_upf_node()
Expand Down
5 changes: 2 additions & 3 deletions aiida/backends/tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,11 +577,11 @@ def test_folders(self):
os.makedirs(tree_1)
file_content = 'some text ABCDE'
file_content_different = 'other values 12345'
with io.open(os.path.join(tree_1, 'file1.txt'), 'w', encoding='utf8') as fhandle:
with open(os.path.join(tree_1, 'file1.txt'), 'w', encoding='utf8') as fhandle:
fhandle.write(file_content)
os.mkdir(os.path.join(tree_1, 'dir1'))
os.mkdir(os.path.join(tree_1, 'dir1', 'dir2'))
with io.open(os.path.join(tree_1, 'dir1', 'file2.txt'), 'w', encoding='utf8') as fhandle:
with open(os.path.join(tree_1, 'dir1', 'file2.txt'), 'w', encoding='utf8') as fhandle:
fhandle.write(file_content)
os.mkdir(os.path.join(tree_1, 'dir1', 'dir2', 'dir3'))

Expand Down Expand Up @@ -2085,4 +2085,3 @@ def test_long_case(self):
with Capturing():
delete_nodes((node_list[3].pk,), force=True, create_forward=True)
self._check_existence(uuids_check_existence, uuids_check_deleted)

Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def setUp(self):

def test_migrate_recursively(self):
"""Test function 'migrate_recursively'"""
import io
import tarfile
import zipfile

Expand All @@ -88,9 +87,9 @@ def test_migrate_recursively(self):
raise ValueError('invalid file format, expected either a zip archive or gzipped tarball')

try:
with io.open(folder.get_abs_path('data.json'), 'r', encoding='utf8') as fhandle:
with open(folder.get_abs_path('data.json'), 'r', encoding='utf8') as fhandle:
data = jsonload(fhandle)
with io.open(folder.get_abs_path('metadata.json'), 'r', encoding='utf8') as fhandle:
with open(folder.get_abs_path('metadata.json'), 'r', encoding='utf8') as fhandle:
metadata = jsonload(fhandle)
except IOError:
raise NotExistent('export archive does not contain the required file {}'.format(fhandle.filename))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"""Test export file migration from export version 0.3 to 0.4"""
# pylint: disable=too-many-locals,too-many-branches,too-many-statements

import io
import tarfile
import zipfile

Expand Down Expand Up @@ -57,9 +56,9 @@ def test_migrate_v3_to_v4(self):
raise ValueError('invalid file format, expected either a zip archive or gzipped tarball')

try:
with io.open(folder.get_abs_path('data.json'), 'r', encoding='utf8') as fhandle:
with open(folder.get_abs_path('data.json'), 'r', encoding='utf8') as fhandle:
data_v3 = jsonload(fhandle)
with io.open(folder.get_abs_path('metadata.json'), 'r', encoding='utf8') as fhandle:
with open(folder.get_abs_path('metadata.json'), 'r', encoding='utf8') as fhandle:
metadata_v3 = jsonload(fhandle)
except IOError:
raise NotExistent('export archive does not contain the required file {}'.format(fhandle.filename))
Expand Down Expand Up @@ -110,9 +109,9 @@ def test_migrate_v3_to_v4_complete(self):
raise ValueError('invalid file format, expected either a zip archive or gzipped tarball')

try:
with io.open(folder.get_abs_path('data.json'), 'r', encoding='utf8') as fhandle:
with open(folder.get_abs_path('data.json'), 'r', encoding='utf8') as fhandle:
data = jsonload(fhandle)
with io.open(folder.get_abs_path('metadata.json'), 'r', encoding='utf8') as fhandle:
with open(folder.get_abs_path('metadata.json'), 'r', encoding='utf8') as fhandle:
metadata = jsonload(fhandle)
except IOError:
raise NotExistent('export archive does not contain the required file {}'.format(fhandle.filename))
Expand Down Expand Up @@ -317,9 +316,9 @@ def test_compare_migration_with_aiida_made(self):
raise ValueError('invalid file format, expected either a zip archive or gzipped tarball')

try:
with io.open(folder.get_abs_path('data.json'), 'r', encoding='utf8') as fhandle:
with open(folder.get_abs_path('data.json'), 'r', encoding='utf8') as fhandle:
data_v3 = jsonload(fhandle)
with io.open(folder.get_abs_path('metadata.json'), 'r', encoding='utf8') as fhandle:
with open(folder.get_abs_path('metadata.json'), 'r', encoding='utf8') as fhandle:
metadata_v3 = jsonload(fhandle)
except IOError:
raise NotExistent('export archive does not contain the required file {}'.format(fhandle.filename))
Expand Down Expand Up @@ -455,9 +454,9 @@ def test_illegal_create_links(self):
raise ValueError('invalid file format, expected either a zip archive or gzipped tarball')

try:
with io.open(folder.get_abs_path('data.json'), 'r', encoding='utf8') as fhandle:
with open(folder.get_abs_path('data.json'), 'r', encoding='utf8') as fhandle:
data = jsonload(fhandle)
with io.open(folder.get_abs_path('metadata.json'), 'r', encoding='utf8') as fhandle:
with open(folder.get_abs_path('metadata.json'), 'r', encoding='utf8') as fhandle:
metadata = jsonload(fhandle)
except IOError:
raise NotExistent('export archive does not contain the required file {}'.format(fhandle.filename))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
###########################################################################
"""Test export file migration from export version 0.4 to 0.5"""

import io
import tarfile
import zipfile

Expand Down Expand Up @@ -56,9 +55,9 @@ def test_migrate_v4_to_v5(self):
raise ValueError('invalid file format, expected either a zip archive or gzipped tarball')

try:
with io.open(folder.get_abs_path('data.json'), 'r', encoding='utf8') as fhandle:
with open(folder.get_abs_path('data.json'), 'r', encoding='utf8') as fhandle:
data_v4 = jsonload(fhandle)
with io.open(folder.get_abs_path('metadata.json'), 'r', encoding='utf8') as fhandle:
with open(folder.get_abs_path('metadata.json'), 'r', encoding='utf8') as fhandle:
metadata_v4 = jsonload(fhandle)
except IOError:
raise NotExistent('export archive does not contain the required file {}'.format(fhandle.filename))
Expand Down
9 changes: 4 additions & 5 deletions aiida/backends/tests/tools/importexport/orm/test_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
###########################################################################
"""orm links tests for the export and import routines"""

import io
import os
import tarfile

Expand Down Expand Up @@ -52,7 +51,7 @@ def test_links_to_unknown_nodes(self, temp_dir):
with tarfile.open(filename, 'r:gz', format=tarfile.PAX_FORMAT) as tar:
tar.extractall(unpack.abspath)

with io.open(unpack.get_abs_path('data.json'), 'r', encoding='utf8') as fhandle:
with open(unpack.get_abs_path('data.json'), 'r', encoding='utf8') as fhandle:
data = json.load(fhandle)
data['links_uuid'].append({
'output': struct.uuid,
Expand All @@ -62,7 +61,7 @@ def test_links_to_unknown_nodes(self, temp_dir):
'type': LinkType.CREATE.value
})

with io.open(unpack.get_abs_path('data.json'), 'wb') as fhandle:
with open(unpack.get_abs_path('data.json'), 'wb') as fhandle:
json.dump(data, fhandle)

with tarfile.open(filename, 'w:gz', format=tarfile.PAX_FORMAT) as tar:
Expand Down Expand Up @@ -634,7 +633,7 @@ def test_dangling_link_to_existing_db_node(self, temp_dir):
with tarfile.open(filename, 'r:gz', format=tarfile.PAX_FORMAT) as tar:
tar.extractall(unpack.abspath)

with io.open(unpack.get_abs_path('data.json'), 'r', encoding='utf8') as fhandle:
with open(unpack.get_abs_path('data.json'), 'r', encoding='utf8') as fhandle:
data = json.load(fhandle)
data['links_uuid'].append({
'output': calc.uuid,
Expand All @@ -643,7 +642,7 @@ def test_dangling_link_to_existing_db_node(self, temp_dir):
'type': LinkType.INPUT_CALC.value
})

with io.open(unpack.get_abs_path('data.json'), 'wb') as fhandle:
with open(unpack.get_abs_path('data.json'), 'wb') as fhandle:
json.dump(data, fhandle)

with tarfile.open(filename, 'w:gz', format=tarfile.PAX_FORMAT) as tar:
Expand Down
5 changes: 2 additions & 3 deletions aiida/backends/tests/tools/importexport/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
###########################################################################
"""Simple tests for the export and import routines"""

import io
import os
import shutil
import tarfile
Expand Down Expand Up @@ -111,11 +110,11 @@ def test_check_for_export_format_version(self):
with tarfile.open(filename, 'r:gz', format=tarfile.PAX_FORMAT) as tar:
tar.extractall(unpack_tmp_folder)

with io.open(os.path.join(unpack_tmp_folder, 'metadata.json'), 'r', encoding='utf8') as fhandle:
with open(os.path.join(unpack_tmp_folder, 'metadata.json'), 'r', encoding='utf8') as fhandle:
metadata = json.load(fhandle)
metadata['export_version'] = 0.0

with io.open(os.path.join(unpack_tmp_folder, 'metadata.json'), 'wb') as fhandle:
with open(os.path.join(unpack_tmp_folder, 'metadata.json'), 'wb') as fhandle:
json.dump(metadata, fhandle)

with tarfile.open(filename, 'w:gz', format=tarfile.PAX_FORMAT) as tar:
Expand Down
Loading

0 comments on commit 85686be

Please sign in to comment.