1616 filename_here = fh.save(filename_here)
1717
1818they inherit from os.PathLike so can be used anywhere a conventional path can
19- be used:
19+ be used e.g.
2020
2121 with open(fh) as f:
2222 ...
@@ -44,6 +44,7 @@ def set_stage_point(stage_point):
4444class FileHandler :
4545 """
4646 Handle file operations
47+
4748 """
4849
4950 def __init__ (self , stage_point = None ):
@@ -55,13 +56,27 @@ def __init__(self, stage_point=None):
5556 def load (self , path ):
5657 """
5758 Method to load file.
59+
60+ args:
61+ path (str): file path or URL
62+
63+ returns:
64+ FileHandle: a FileHandle object
65+
5866 """
5967
6068 return FileHandle (path , self .stage_point , must_exist = True )
6169
6270 def create (self , path ):
6371 """
64- Method to load file.
72+ Method to create a new file.
73+
74+ args:
75+ path (str): file path
76+
77+ returns:
78+ FileHandle: a FileHandle object
79+
6580 """
6681
6782 return FileHandle (path , self .stage_point , must_exist = False )
@@ -70,14 +85,51 @@ def create(self, path):
7085class FileHandle :
7186 """
7287 A portable container for a file.
88+
89+ a FileHandle object is instantiated with the path of an existing file on
90+ an existing file system:
91+
92+ fh = FileHandle('/path/to/file')
93+ or:
94+ fh = FileHandle('http://example.com/file.txt')
95+
96+ and has a save() method that creates a local copy of that file:
97+
98+ filename_here = fh.save(filename_here)
99+
100+ FileHandle objects inherit from os.PathLike so can be used anywhere a
101+ conventional path can be used:
102+
103+ with open(fh) as f:
104+ ...
105+
106+ They can also be used to read and write binary and text data directly:
107+
108+ data = fh.read_binary()
109+ text = fh.read_text()
110+ fh.write_binary(data)
111+ fh.write_text(text)
112+
73113 """
74114
75115 def __init__ (self , path , stage_point , must_exist = True ):
76116 if not isinstance (path , (os .PathLike , str , bytes )):
77117 raise IOError (f"Error - illegal argument type { type (path )} for { path } " )
78118 if must_exist :
79- if not os .path .exists (path ):
80- raise IOError ("Error - no such file" )
119+ if isinstance (path , str ) and (
120+ path .startswith ("http://" )
121+ or path .startswith ("https://" )
122+ or path .startswith ("ftp://" )
123+ ):
124+ try :
125+ source = fsspec .open (path )
126+ with source as s :
127+ s .read (1 )
128+ except Exception as e :
129+ raise IOError ("Error - no such file" ) from e
130+ else :
131+ if not os .path .exists (path ):
132+ raise IOError ("Error - no such file" )
81133 source = fsspec .open (path )
82134 ext = os .path .splitext (path )[1 ]
83135 self .path = path
@@ -180,6 +232,10 @@ def read_text(self):
180232 def write_binary (self , data ):
181233 """
182234 A method for writing binary file formats
235+
236+ args:
237+ data (bytes): binary data to write
238+
183239 """
184240
185241 compressed_data = zlib .compress (data )
@@ -194,6 +250,10 @@ def write_binary(self, data):
194250 def write_text (self , text ):
195251 """
196252 A wrapper for writing binary formatted text.
253+
254+ args:
255+ text (str): text data to write
256+
197257 """
198258
199259 self .write_binary (text .encode ("utf-8" ))
0 commit comments