Skip to content

Commit f385983

Browse files
committed
Allow samplesheet to accept a file handle or a Path like object
1 parent d6db10e commit f385983

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

sample_sheet/__init__.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ class SampleSheet(object):
401401
_section_header_re = re.compile(r'\[(.*)\]')
402402
_whitespace_re = re.compile(r'\s+')
403403

404-
def __init__(self, path: Optional[Union[Path, str]] = None) -> None:
404+
def __init__(self, path: Optional[Union[Path, str, TextIO]] = None) -> None:
405405
self.path = path
406406

407407
self._samples: List[Sample] = []
@@ -415,7 +415,12 @@ def __init__(self, path: Optional[Union[Path, str]] = None) -> None:
415415
self.Header: Section = Section()
416416
self.Settings: Section = Section()
417417

418-
if self.path:
418+
419+
if self.path is not None:
420+
if isinstance(self.path, (str, Path)):
421+
with open(self.path, 'r') as f:
422+
self._parse(f)
423+
else:
419424
self._parse(self.path)
420425

421426
def add_section(self, section_name: str) -> None:
@@ -476,12 +481,11 @@ def samples(self) -> List:
476481
"""Return the samples present in this :class:`SampleSheet`."""
477482
return self._samples
478483

479-
def _parse(self, path: Union[Path, str]) -> None:
484+
def _parse(self, handle: TextIO) -> None:
480485
section_name: str = ''
481486
sample_header: Optional[List[str]] = None
482487

483-
with open(path, encoding=self._encoding) as handle:
484-
lines = list(csv.reader(handle, skipinitialspace=True))
488+
lines = list(csv.reader(handle, skipinitialspace=True))
485489

486490
for i, line in enumerate(lines):
487491
# Skip to next line if this line is empty to support formats of

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
minversion = 3.0.0
33
skip_missing_interpreters = true
44
envlist =
5-
py36,py37
5+
py36,py37,py38
66
lint
77
type
88
docs

0 commit comments

Comments
 (0)