1+ """File I/O and network utilities for brainrender."""
2+
3+ from collections .abc import Callable
14from pathlib import Path
5+ from typing import Any , ParamSpec , TypeVar
26
37import requests
4- from vedo import Mesh , load
8+ from vedo import Mesh , Volume , load
9+
10+ P = ParamSpec ("P" )
11+ R = TypeVar ("R" )
512
613
7- def connected_to_internet (url = "http://www.google.com/" , timeout = 5 ):
14+ def connected_to_internet (
15+ url : str = "http://www.google.com/" ,
16+ timeout : int = 5 ,
17+ ) -> bool :
818 """
9- Check that there is an internet connection
19+ Check that there is an internet connection.
20+
21+ Parameters
22+ ----------
23+ url
24+ URL to use for testing. Default ``"http://www.google.com/"``.
25+ timeout
26+ Timeout in seconds. Default 5.
1027
11- :param url: url to use for testing (Default value = 'http://www.google.com/')
12- :param timeout: timeout to wait for [in seconds] (Default value = 5)
28+ Returns
29+ -------
30+ bool
31+ ``True`` if an internet connection is available, otherwise ``False``.
1332 """
1433
1534 try :
@@ -20,27 +39,54 @@ def connected_to_internet(url="http://www.google.com/", timeout=5):
2039 return False
2140
2241
23- def fail_on_no_connection (func ) :
42+ def fail_on_no_connection (func : Callable [ P , R ]) -> Callable [ P , R ] :
2443 """
25- Decorator that throws an error if no internet connection is available
44+ Decorator that raises an error if no internet connection is available.
45+
46+ Parameters
47+ ----------
48+ func
49+ Function to wrap.
50+
51+ Returns
52+ -------
53+ collections.abc.Callable
54+
55+ Raises
56+ ------
57+ ConnectionError
58+ If no internet connection is found.
2659 """
2760 if not connected_to_internet (): # pragma: no cover
2861 raise ConnectionError (
2962 "No internet connection found."
3063 ) # pragma: no cover
3164
32- def inner (* args , ** kwargs ) :
65+ def inner (* args : Any , ** kwargs : Any ) -> Any :
3366 return func (* args , ** kwargs )
3467
3568 return inner
3669
3770
38- def request (url ) :
71+ def request (url : str ) -> requests . Response :
3972 """
40- Sends a request to a url
73+ Send a GET request to a URL.
4174
42- :param url:
75+ Parameters
76+ ----------
77+ url
78+ URL to request.
4379
80+ Returns
81+ -------
82+ requests.Response
83+
84+ Raises
85+ ------
86+ ConnectionError
87+ If no internet connection is found.
88+ ValueError
89+ If the request fails.
4490 """
4591 if not connected_to_internet (): # pragma: no cover
4692 raise ConnectionError (
@@ -57,13 +103,29 @@ def request(url):
57103 raise ValueError (exception_string )
58104
59105
60- def check_file_exists (func ): # pragma: no cover
106+ def check_file_exists (
107+ func : Callable [P , R ],
108+ ) -> Callable [P , R ]: # pragma: no cover
61109 """
62- Decorator that throws an error if a function; s first argument
110+ Decorator that raises an error if a function' s first argument
63111 is not a path to an existing file.
112+
113+ Parameters
114+ ----------
115+ func
116+ Function to wrap.
117+
118+ Returns
119+ -------
120+ collections.abc.Callable
121+
122+ Raises
123+ ------
124+ FileNotFoundError
125+ If the file does not exist.
64126 """
65127
66- def inner (* args , ** kwargs ) :
128+ def inner (* args : Any , ** kwargs : Any ) -> Any :
67129 if not Path (args [0 ]).exists ():
68130 raise FileNotFoundError (
69131 f"File { args [0 ]} not found"
@@ -74,13 +136,26 @@ def inner(*args, **kwargs):
74136
75137
76138@check_file_exists
77- def load_mesh_from_file (filepath , color = None , alpha = None ):
139+ def load_mesh_from_file (
140+ filepath : str | Path ,
141+ color : str | None = None ,
142+ alpha : float | None = None ,
143+ ) -> Mesh | Volume :
78144 """
79- Load a a mesh or volume from files like .obj, .stl , .. .
145+ Load a mesh or volume from a file (e.g. .obj , .stl) .
80146
81- :param filepath: path to file
82- :param **kwargs:
147+ Parameters
148+ ----------
149+ filepath
150+ Path to the mesh file.
151+ color
152+ Colour to apply to the mesh.
153+ alpha
154+ Transparency to apply to the mesh.
83155
156+ Returns
157+ -------
158+ vedo.Mesh or vedo.Volume
84159 """
85160 actor = load (str (filepath ))
86161 actor .c (color ).alpha (alpha )
0 commit comments