Skip to content

Commit 36153ef

Browse files
committed
fixes #111, pathlib compatibility
1 parent 99ede9a commit 36153ef

9 files changed

Lines changed: 107 additions & 20 deletions

File tree

gnss_lib_py/parsers/android.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def __init__(self, input_path, remove_timing_outliers=True):
3030
3131
Parameters
3232
----------
33-
input_path : string
33+
input_path : string or path-like
3434
Path to measurement csv file
3535
remove_timing_outliers : bool
3636
Flag for whether to remove measures that are too close or
@@ -157,7 +157,7 @@ def __init__(self, input_path):
157157
158158
Parameters
159159
----------
160-
input_path : string
160+
input_path : string or path-like
161161
Path to measurement csv file
162162
"""
163163
super().__init__(csv_path=input_path)
@@ -255,7 +255,7 @@ def __init__(self, input_path):
255255
256256
Parameters
257257
----------
258-
input_path : string
258+
input_path : string or path-like
259259
Path to measurement csv file
260260
"""
261261

@@ -369,7 +369,7 @@ def preprocess(self, input_path):
369369
370370
Parameters
371371
----------
372-
input_path : string
372+
input_path : string or path-like
373373
File location of data file to read.
374374
375375
Returns
@@ -380,6 +380,12 @@ def preprocess(self, input_path):
380380
Dataframe that contains the gyro measurements from the log.
381381
382382
"""
383+
384+
if not isinstance(input_path, (str, os.PathLike)):
385+
raise TypeError("input_path must be string or path-like")
386+
if not os.path.exists(input_path):
387+
raise FileNotFoundError("file not found")
388+
383389
with open(input_path, encoding="utf8") as csvfile:
384390
reader = csv.reader(csvfile)
385391
for row in reader:
@@ -435,7 +441,7 @@ def preprocess(self, input_path):
435441
436442
Parameters
437443
----------
438-
input_path : string
444+
input_path : string or path-like
439445
File location of data file to read.
440446
441447
Returns
@@ -444,6 +450,12 @@ def preprocess(self, input_path):
444450
Dataframe that contains the location fixes from the log.
445451
446452
"""
453+
454+
if not isinstance(input_path, (str, os.PathLike)):
455+
raise TypeError("input_path must be string or path-like")
456+
if not os.path.exists(input_path):
457+
raise FileNotFoundError("file not found")
458+
447459
with open(input_path, encoding="utf8") as csvfile:
448460
reader = csv.reader(csvfile)
449461
for row in reader:
@@ -463,7 +475,7 @@ def make_csv(input_path, output_directory, field, show_path=False):
463475
464476
Parameters
465477
----------
466-
input_path : string
478+
input_path : string or path-like
467479
File location of data file to read.
468480
output_directory : string
469481
Directory where new csv file should be created
@@ -488,6 +500,12 @@ def make_csv(input_path, output_directory, field, show_path=False):
488500
output_path = os.path.join(output_directory, field + ".csv")
489501
with open(output_path, 'w', encoding="utf8") as out_csv:
490502
writer = csv.writer(out_csv)
503+
504+
if not isinstance(input_path, (str, os.PathLike)):
505+
raise TypeError("input_path must be string or path-like")
506+
if not os.path.exists(input_path):
507+
raise FileNotFoundError("file not found")
508+
491509
with open(input_path, 'r', encoding="utf8") as in_txt:
492510
for line in in_txt:
493511
# Comments in the log file

gnss_lib_py/parsers/navdata.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class NavData():
2424
2525
Parameters
2626
----------
27-
csv_path : string
27+
csv_path : string or path-like
2828
Path to csv file containing data
2929
pandas_df : pd.DataFrame
3030
Data used to initialize NavData instance.
@@ -93,7 +93,7 @@ def from_csv_path(self, csv_path, **kwargs):
9393
9494
Parameters
9595
----------
96-
csv_path : string
96+
csv_path : string or path-like
9797
Path to csv file containing data
9898
header : string, int, or None
9999
"infer" uses the first row as column names, setting to
@@ -102,8 +102,8 @@ def from_csv_path(self, csv_path, **kwargs):
102102
Delimiter to use when reading in csv file.
103103
104104
"""
105-
if not isinstance(csv_path, str):
106-
raise TypeError("csv_path must be string")
105+
if not isinstance(csv_path, (str, os.PathLike)):
106+
raise TypeError("csv_path must be string or path-like")
107107
if not os.path.exists(csv_path):
108108
raise FileNotFoundError("file not found")
109109

gnss_lib_py/parsers/nmea.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
__authors__ = "Ashwin Kanhere, Dalton Vega"
66
__date__ = "24 Jun, 2023"
77

8+
import os
89
import datetime
910

1011
import pynmea2
@@ -31,7 +32,7 @@ def __init__(self, filename, msg_types=None,
3132
3233
Parameters
3334
----------
34-
filename : str
35+
filename : str or path-like
3536
filepath to NMEA file to read.
3637
msg_types : list
3738
List of strings describing messages that can be parsed.
@@ -63,6 +64,12 @@ def __init__(self, filename, msg_types=None,
6364
pd_df = pd.DataFrame()
6465
field_dict = {}
6566
prev_timestamp = None
67+
68+
if not isinstance(filename, (str, os.PathLike)):
69+
raise TypeError("filename must be string or path-like")
70+
if not os.path.exists(filename):
71+
raise FileNotFoundError("file not found")
72+
6673
with open(filename, "r", encoding='UTF-8') as open_file:
6774
for line in open_file:
6875
check_ind = line.find('*')

gnss_lib_py/parsers/precise_ephemerides.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def parse_sp3(input_path, constellation = 'gps'):
6666
6767
Parameters
6868
----------
69-
input_path : string
69+
input_path : string or path-like
7070
Path to sp3 file
7171
constellation : string
7272
Key from among {gps, galileo, glonass, beidou, qzss, etc} that
@@ -99,8 +99,8 @@ def parse_sp3(input_path, constellation = 'gps'):
9999
Accessed as of August 20, 2022
100100
"""
101101
# Initial checks for loading sp3_path
102-
if not isinstance(input_path, str):
103-
raise TypeError("input_path must be string")
102+
if not isinstance(input_path, (str, os.PathLike)):
103+
raise TypeError("input_path must be string or path-like")
104104
if not os.path.exists(input_path):
105105
raise FileNotFoundError("file not found")
106106

@@ -191,7 +191,7 @@ def parse_clockfile(input_path, constellation = 'gps'):
191191
192192
Parameters
193193
----------
194-
input_path : string
194+
input_path : string or path-like
195195
Path to clk file
196196
constellation : string
197197
Key from among {gps, galileo, glonass, beidou, qzss, etc} that
@@ -225,8 +225,8 @@ def parse_clockfile(input_path, constellation = 'gps'):
225225
"""
226226

227227
# Initial checks for loading sp3_path
228-
if not isinstance(input_path, str):
229-
raise TypeError("input_path must be string")
228+
if not isinstance(input_path, (str, os.PathLike)):
229+
raise TypeError("input_path must be string or path-like")
230230
if not os.path.exists(input_path):
231231
raise FileNotFoundError("file not found")
232232

gnss_lib_py/parsers/smartloc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def __init__(self, input_path):
3636
3737
Parameters
3838
----------
39-
input_path : string
39+
input_path : string or path-like
4040
Path to measurement csv file
4141
4242
"""

tests/parsers/test_android.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
__date__ = "10 Nov 2021"
77

88
import os
9+
import pathlib
910

1011
import pytest
1112
import numpy as np
@@ -341,9 +342,22 @@ def test_imu_raw(android_raw_path):
341342
test_imu = android.AndroidRawImu(android_raw_path)
342343
isinstance(test_imu, NavData)
343344

345+
test_imu = android.AndroidRawImu(pathlib.Path(android_raw_path))
346+
isinstance(test_imu, NavData)
347+
348+
# raises exception if not a file path
349+
with pytest.raises(FileNotFoundError):
350+
android.AndroidRawImu("not_a_file.txt")
351+
with pytest.raises(FileNotFoundError):
352+
android.AndroidRawImu(pathlib.Path("not_a_file.txt"))
353+
354+
# raises exception if input not string or path-like
355+
with pytest.raises(TypeError):
356+
android.AndroidRawImu([])
357+
344358

345359
def test_fix_raw(android_raw_path):
346-
"""Test that AndroidRawImu initialization
360+
"""Test that AndroidRawFixes initialization
347361
348362
Parameters
349363
----------
@@ -353,6 +367,19 @@ def test_fix_raw(android_raw_path):
353367
test_fix = android.AndroidRawFixes(android_raw_path)
354368
isinstance(test_fix, NavData)
355369

370+
test_fix = android.AndroidRawFixes(pathlib.Path(android_raw_path))
371+
isinstance(test_fix, NavData)
372+
373+
# raises exception if not a file path
374+
with pytest.raises(FileNotFoundError):
375+
android.AndroidRawFixes("not_a_file.txt")
376+
with pytest.raises(FileNotFoundError):
377+
android.AndroidRawFixes(pathlib.Path("not_a_file.txt"))
378+
379+
# raises exception if input not string or path-like
380+
with pytest.raises(TypeError):
381+
android.AndroidRawFixes([])
382+
356383

357384
def test_navdata_type(derived):
358385
"""Test that all subclasses inherit from NavData
@@ -449,8 +476,18 @@ def test_csv_equivalence(android_raw_path, root_path, file_type):
449476
df_slice = test_df[col_name].values
450477
np.testing.assert_almost_equal(measure_slice, df_slice)
451478
os.remove(csv_loc)
479+
for file in os.listdir(output_directory):
480+
os.remove(os.path.join(output_directory, file))
452481
os.rmdir(output_directory)
453482

483+
# raises exception if not a file path
484+
with pytest.raises(FileNotFoundError):
485+
android.make_csv("", output_directory, file_type)
486+
487+
# raises exception if input not string or path-like
488+
with pytest.raises(TypeError):
489+
android.make_csv([], output_directory, file_type)
490+
454491
@pytest.fixture(name="android_gtruth_path")
455492
def fixture_gtruth_path(root_path):
456493
"""Filepath of Android Ground Truth data

tests/parsers/test_navdata.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88

99
import os
10+
import pathlib
1011
import itertools
1112

1213
import pytest
@@ -211,7 +212,11 @@ def test_init_csv(csv_path):
211212

212213
# should work when csv is passed
213214
data = NavData(csv_path=csv_path)
215+
# data should contain full csv
216+
assert data.shape == (4,6)
214217

218+
# should work when csv is passed as pathlib object
219+
data = NavData(csv_path=pathlib.Path(csv_path))
215220
# data should contain full csv
216221
assert data.shape == (4,6)
217222

tests/parsers/test_nmea.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
__date__ = "24 Jun, 2023"
77

88
import os
9+
import pathlib
910

1011
import numpy as np
1112
import pytest
@@ -52,7 +53,6 @@ def fixture_nmea_file_w_correct_checksum(root_path):
5253
nmea_checksum = os.path.join(root_path, 'nmea_w_correct_checksum.nmea')
5354
return nmea_checksum
5455

55-
5656
@pytest.fixture(name="nmea_wrong_checksum")
5757
def fixture_nmea_file_w_wrong_checksum(root_path):
5858
"""Location of NMEA file with wrong checksum values.
@@ -130,6 +130,18 @@ def test_nmea_loading(nmea_file, check, row_name, exp_value, eq_decimal):
130130
nmea_navdata = Nmea(nmea_file, check=check)
131131
compare_nmea_values(nmea_navdata, row_name, exp_value, eq_decimal)
132132

133+
nmea_navdata = Nmea(pathlib.Path(nmea_file), check=check)
134+
135+
# raises exception if not a file path
136+
with pytest.raises(FileNotFoundError):
137+
Nmea(pathlib.Path("not_a_file.txt"), check=check)
138+
with pytest.raises(FileNotFoundError):
139+
Nmea("not_a_file.txt", check=check)
140+
141+
# raises exception if input not string or path-like
142+
with pytest.raises(TypeError):
143+
Nmea([], check=check)
144+
133145
# Testing loading with raw latitude and longitude preserved
134146
nmea_raw = Nmea(nmea_file, check=check, keep_raw=True)
135147
compare_nmea_values(nmea_raw, row_name, exp_value, eq_decimal)

tests/parsers/test_precise_ephemerides.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ def fixture_load_sp3data_gps(sp3_path):
106106
"""
107107
sp3data_gps = parse_sp3(sp3_path, constellation = 'gps')
108108

109+
# raises exception if input not string or path-like
110+
with pytest.raises(TypeError):
111+
parse_sp3([])
112+
109113
return sp3data_gps
110114

111115
@pytest.fixture(name="clkdata_gps")
@@ -124,6 +128,10 @@ def fixture_load_clkdata_gps(clk_path):
124128
"""
125129
clkdata_gps = parse_clockfile(clk_path, constellation = 'gps')
126130

131+
# raises exception if input not string or path-like
132+
with pytest.raises(TypeError):
133+
parse_clockfile([])
134+
127135
return clkdata_gps
128136

129137
@pytest.fixture(name="sp3data_glonass")

0 commit comments

Comments
 (0)