@@ -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
0 commit comments