1- import pathlib
21import logging
32import os
43from typing import List , Tuple
@@ -91,6 +90,33 @@ def demo_to_buffer(
9190 return brain_params , demo_buffer
9291
9392
93+ def get_demo_files (path : str ) -> List [str ]:
94+ """
95+ Retrieves the demonstration file(s) from a path.
96+ :param path: Path of demonstration file or directory.
97+ :return: List of demonstration files
98+
99+ Raises errors if |path| is invalid.
100+ """
101+ if os .path .isfile (path ):
102+ if not path .endswith (".demo" ):
103+ raise ValueError ("The path provided is not a '.demo' file." )
104+ return [path ]
105+ elif os .path .isdir (path ):
106+ paths = [
107+ os .path .join (path , name )
108+ for name in os .listdir (path )
109+ if name .endswith (".demo" )
110+ ]
111+ if not paths :
112+ raise ValueError ("There are no '.demo' files in the provided directory." )
113+ return paths
114+ else :
115+ raise FileNotFoundError (
116+ f"The demonstration file or directory { path } does not exist."
117+ )
118+
119+
94120@timed
95121def load_demonstration (
96122 file_path : str
@@ -103,27 +129,7 @@ def load_demonstration(
103129
104130 # First 32 bytes of file dedicated to meta-data.
105131 INITIAL_POS = 33
106- file_paths = []
107- if os .path .isdir (file_path ):
108- all_files = os .listdir (file_path )
109- for _file in all_files :
110- if _file .endswith (".demo" ):
111- file_paths .append (os .path .join (file_path , _file ))
112- if not all_files :
113- raise ValueError ("There are no '.demo' files in the provided directory." )
114- elif os .path .isfile (file_path ):
115- file_paths .append (file_path )
116- file_extension = pathlib .Path (file_path ).suffix
117- if file_extension != ".demo" :
118- raise ValueError (
119- "The file is not a '.demo' file. Please provide a file with the "
120- "correct extension."
121- )
122- else :
123- raise FileNotFoundError (
124- "The demonstration file or directory {} does not exist." .format (file_path )
125- )
126-
132+ file_paths = get_demo_files (file_path )
127133 group_spec = None
128134 brain_param_proto = None
129135 info_action_pairs = []
0 commit comments