@@ -22,9 +22,9 @@ def read(self, uri):
2222 usock = urllib .request .urlopen (uri )
2323 self .feed (usock .read ().decode (usock .headers .get_content_charset ()))
2424 usock .close ()
25-
25+
2626 return self .urls
27-
27+
2828 def reset (self ):
2929 """Reset state of URLLister"""
3030 HTMLParser .reset (self )
@@ -58,7 +58,7 @@ def read(self, uri):
5858 print (e )
5959
6060 return self .urls
61-
61+
6262 def reset (self ):
6363 """Reset state of URLLister"""
6464 SGMLParser .reset (self )
@@ -73,25 +73,29 @@ class HTTPDataBrowser(BaseDataBrowser):
7373 def __init__ (self , server ):
7474 BaseDataBrowser .__init__ (self , server )
7575 socket .setdefaulttimeout (60 )
76-
76+
7777 def get_directories (self , start_date , end_date ):
7878 """Generates a list of remote directories which may be queried
7979 for files corresponding to the requested range. Note that these
8080 directories do not necessarily exist on the remote server."""
8181 # filter(lambda url: url.endswith("/"), self._query(location))
8282 return self .server .compute_directories (start_date , end_date )
8383
84- def get_files (self , location , extension ):
84+ def get_files (self , location , extension , filter_func : callable | None = None ):
8585 """Get all the files that end with specified extension at the uri"""
8686 files = None
8787 num_retries = 0
88-
88+
8989 # Get a list of the files at the remote location, if it exists
9090 # To avoid spending too much time, we will timeout after a short time
9191 # and retry up to 10 times.
9292 while files is None and num_retries <= 10 :
9393 try :
94+ # Only grab files with the matching file extension
9495 files = filter (lambda url : url .endswith ("." + extension ), self ._query (location ))
96+ # If there is a user-defined filter function, use that to only get those specific files.
97+ if filter_func is not None :
98+ files = filter (filter_func , files )
9599 except IOError as e :
96100 if isinstance (e .strerror , socket .error ):
97101 # if server is unreachable, raise an exception
@@ -105,10 +109,10 @@ def get_files(self, location, extension):
105109 files = []
106110
107111 return files
108-
112+
109113 def _query (self , location ):
110114 """Get a list of files and folders at the specified remote location"""
111- # query the remote location for the list of files and subdirectories
115+ # query the remote location for the list of files and subdirectories.
112116
113117 if (sys .version_info >= (3 , 0 )):
114118 url_lister = URLLister ()
@@ -121,4 +125,4 @@ def _query(self, location):
121125 urls = filter (lambda url : url [0 ] != "/" and url [0 ] != "?" , result )
122126
123127 return [os .path .join (location , url ) for url in urls ]
124-
128+
0 commit comments