Skip to content

Commit 5e5ec01

Browse files
Pathlib for nixpy (#550)
* [pathlib] adding pathlibsupport * [multitag] removing docs, and check, should not be in this branch * [file_test] adding test for pathlib and unicode * [file] adding pahtlib support, removing check for utf-8 encoding * [multi_tag] removing embed * [file] removing embed * [test] moving test class to test_file.py * [pathlib] adding pathlibsupport * [multitag] removing docs, and check, should not be in this branch * [file_test] adding test for pathlib and unicode * [file] adding pahtlib support, removing check for utf-8 encoding * [multi_tag] removing embed * [file] removing embed * [test] moving test class to test_file.py * [file] fixing imports
1 parent 80d2df2 commit 5e5ec01

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

nixio/file.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@
66
# Redistribution and use in source and binary forms, with or without
77
# modification, are permitted under the terms of the BSD License. See
88
# LICENSE file in the root of the Project.
9-
import os
9+
1010
import gc
11-
import numpy as np
11+
import pathlib
1212
from sys import maxsize
13+
from typing import Union
1314
from warnings import warn
1415

1516
import h5py
17+
import numpy as np
1618

17-
from .hdf5.h5group import H5Group
19+
from . import util, validator
1820
from .block import Block
19-
from .section import Section
21+
from .compression import Compression
2022
from .container import Container, SectionContainer
21-
from . import util
22-
from .exceptions import InvalidFile, DuplicateName
23+
from .exceptions import DuplicateName, InvalidFile
24+
from .hdf5.h5group import H5Group
25+
from .section import Section
2326
from .util import find as finders
24-
from . import validator
25-
from .compression import Compression
26-
2727

2828
FILE_FORMAT = "nix"
2929
HDF_FF_VERSION = (1, 2, 1)
@@ -81,7 +81,7 @@ def make_fcpl():
8181

8282
class File:
8383

84-
def __init__(self, path, mode=FileMode.ReadWrite,
84+
def __init__(self, path: Union[str, pathlib.Path], mode=FileMode.ReadWrite,
8585
compression=Compression.Auto,
8686
auto_update_timestamps=True):
8787
"""
@@ -96,27 +96,24 @@ def __init__(self, path, mode=FileMode.ReadWrite,
9696
9797
:return: nixio.File object
9898
"""
99-
try:
100-
path = path.encode("utf-8")
101-
except (UnicodeError, LookupError):
102-
pass
99+
path = pathlib.Path(path)
103100

104-
if not os.path.exists(path) and mode == FileMode.ReadOnly:
101+
if not path.exists() and mode == FileMode.ReadOnly:
105102
raise RuntimeError(
106103
"Cannot open non-existent file in ReadOnly mode!"
107104
)
108105

109-
if not os.path.exists(path) or mode == FileMode.Overwrite:
106+
if not path.exists or mode == FileMode.Overwrite:
110107
mode = FileMode.Overwrite
111108
h5mode = map_file_mode(mode)
112-
fid = h5py.h5f.create(path, flags=h5mode, fapl=make_fapl(),
109+
fid = h5py.h5f.create(str(path).encode("utf-8"), flags=h5mode, fapl=make_fapl(),
113110
fcpl=make_fcpl())
114111
self._h5file = h5py.File(fid)
115112
self._root = H5Group(self._h5file, "/", create=True)
116113
self._create_header()
117114
else:
118115
h5mode = map_file_mode(mode)
119-
fid = h5py.h5f.open(path, flags=h5mode, fapl=make_fapl())
116+
fid = h5py.h5f.open(str(path).encode("utf-8"), flags=h5mode, fapl=make_fapl())
120117
self._h5file = h5py.File(fid)
121118
self._root = H5Group(self._h5file, "/")
122119

@@ -144,6 +141,7 @@ def open(cls, path, mode=FileMode.ReadWrite, compression=Compression.Auto,
144141
warn("Backend selection is deprecated. Ignoring value.")
145142
return cls(path, mode, compression, auto_update_timestamps)
146143

144+
147145
def _create_header(self):
148146
self._set_format()
149147
self._set_version()

nixio/test/test_file.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
import time
1111
import h5py
12+
import pathlib
1213
import unittest
1314
import numpy as np
1415

@@ -327,3 +328,22 @@ def test_bad_id(self):
327328

328329
self.set_header(version=(1, 0, 0), fileid="")
329330
self.try_open(nix.FileMode.ReadOnly)
331+
332+
333+
class TestFilePathlib(unittest.TestCase):
334+
335+
def setUp(self):
336+
self.tmpdir = TempDir("filetest")
337+
self.testfilename = pathlib.Path(self.tmpdir.path) / "filetest.nix"
338+
339+
def tearDown(self):
340+
self.tmpdir.cleanup()
341+
342+
def test_with_pathlib(self):
343+
file = nix.File.open(self.testfilename, nix.FileMode.Overwrite)
344+
file.close()
345+
346+
def test_with_unicode(self):
347+
file_test = self.testfilename.parent /"👍_test.nix"
348+
file = nix.File.open(file_test, nix.FileMode.Overwrite)
349+
file.close()

0 commit comments

Comments
 (0)