@@ -126,6 +126,34 @@ def download_json():
126126 return ret
127127
128128
129+ def _download_and_write (url , dest , err_prefix ):
130+ """
131+ Download a file from the given URL and write its content to the specified destination.
132+
133+ :param url: The URL to fetch.
134+ :param dest: The local filesystem path where the content will be saved.
135+ :param err_prefix: A prefix for error messages in case of failure.
136+
137+ :return: True if the download and write succeed, False otherwise.
138+ """
139+ try :
140+ resp = requests .get (url , allow_redirects = True , timeout = 10 )
141+ except requests .RequestException as e :
142+ print (f'{ err_prefix } Request Error: { e } ' )
143+ return False
144+ if resp .status_code != 200 :
145+ print (f'{ err_prefix } Return code: { resp .status_code } ' )
146+ return False
147+ os .makedirs (os .path .dirname (dest ), exist_ok = True )
148+ try :
149+ with open (dest , 'wb' ) as f :
150+ f .write (resp .content )
151+ except OSError :
152+ print (f'Failed to create file: { dest } ' )
153+ return False
154+ return True
155+
156+
129157def retrieve_custom_sidebar (root_dir ):
130158 """
131159 Generate the custom sidebar, downloading necessary custom files.
@@ -136,82 +164,57 @@ def retrieve_custom_sidebar(root_dir):
136164 :return: Custom sidebars if the file was downloaded and generated successfully.
137165 Readthedocs default ones if not.
138166 """
139- url = "https://raw.githubusercontent.com/eProsima/all-docs/master/source/_templates/sidebar/commercial-support.html"
140- url_img = "https://raw.githubusercontent.com/eProsima/all-docs/master/source/_static/eprosima-logo-white.png"
141- ret = {
142- "**" : [
143- "sidebar/brand.html" ,
144- "sidebar/search.html" ,
145- "sidebar/scroll-start.html" ,
146- "sidebar/navigation.html" ,
147- "sidebar/ethical-ads.html" ,
148- "sidebar/scroll-end.html" ,
149- "sidebar/variant-selector.html" ,
167+ html_url = (
168+ 'https://raw.githubusercontent.com/eProsima/all-docs/'
169+ 'master/source/_templates/sidebar/commercial-support.html'
170+ )
171+ img_url = (
172+ 'https://raw.githubusercontent.com/eProsima/all-docs/'
173+ 'master/source/_static/eprosima-logo-white.png'
174+ )
175+ default = {
176+ '**' : [
177+ 'sidebar/brand.html' ,
178+ 'sidebar/search.html' ,
179+ 'sidebar/scroll-start.html' ,
180+ 'sidebar/navigation.html' ,
181+ 'sidebar/ethical-ads.html' ,
182+ 'sidebar/scroll-end.html' ,
183+ 'sidebar/variant-selector.html' ,
150184 ]
151185 }
152- if not os .path .isfile (
153- "{}/_templates/sidebar/commercial-support.html" .format (root_dir )
154- ):
155- try :
156- req = requests .get (url , allow_redirects = True , timeout = 10 )
157- except requests .RequestException as e :
158- print (
159- "Failed to download the HTML with the eProsima commecial support button."
160- "Request Error: {}" .format (e )
161- )
162- return ret
163- if req .status_code != 200 :
164- print (
165- "Failed to download the HTML with the eProsima commercial support button."
166- "Return code: {}" .format (req .status_code )
167- )
168- return ret
169- os .makedirs (
170- os .path .dirname ("{}/_templates/sidebar/" .format (root_dir )), exist_ok = True
171- )
172- html_path = "{}/_templates/sidebar/commercial-support.html" .format (root_dir )
173- with open (html_path , "wb" ) as f :
174- try :
175- f .write (req .content )
176- except OSError :
177- print ("Failed to create the file: {}" .format (html_path ))
178- return ret
179-
180- if not os .path .isfile ("{}/_static/eprosima-logo-white.png" .format (root_dir )):
181- try :
182- req = requests .get (url_img , allow_redirects = True , timeout = 10 )
183- except requests .RequestException as e :
184- print (
185- "Failed to download the image for the eProsima commecial support button."
186- "Request Error: {}" .format (e )
187- )
188- return ret
189- if req .status_code != 200 :
190- print (
191- "Failed to download the image for the eProsima commercial support button."
192- "Return code: {}" .format (req .status_code )
193- )
194- return ret
195- img_path = "{}/_static/eprosima-logo-white.png" .format (root_dir )
196- with open (img_path , "wb" ) as f :
197- try :
198- f .write (req .content )
199- except OSError :
200- print ("Failed to create the file: {}" .format (img_path ))
201- return ret
202- ret = {
203- "**" : [
204- "sidebar/brand.html" ,
205- "sidebar/commercial-support.html" ,
206- "sidebar/search.html" ,
207- "sidebar/scroll-start.html" ,
208- "sidebar/navigation.html" ,
209- "sidebar/ethical-ads.html" ,
210- "sidebar/scroll-end.html" ,
211- "sidebar/variant-selector.html" ,
186+ html_path = f'{ root_dir } /_templates/sidebar/commercial-support.html'
187+ if not os .path .isfile (html_path ):
188+ if not _download_and_write (
189+ html_url ,
190+ html_path ,
191+ 'Failed to download the HTML with the eProsima commercial '
192+ 'support button.'
193+ ):
194+ return default
195+
196+ img_path = f'{ root_dir } /_static/eprosima-logo-white.png'
197+ if not os .path .isfile (img_path ):
198+ if not _download_and_write (
199+ img_url ,
200+ img_path ,
201+ 'Failed to download the image for the eProsima commercial '
202+ 'support button.'
203+ ):
204+ return default
205+
206+ return {
207+ '**' : [
208+ 'sidebar/brand.html' ,
209+ 'sidebar/commercial-support.html' ,
210+ 'sidebar/search.html' ,
211+ 'sidebar/scroll-start.html' ,
212+ 'sidebar/navigation.html' ,
213+ 'sidebar/ethical-ads.html' ,
214+ 'sidebar/scroll-end.html' ,
215+ 'sidebar/variant-selector.html' ,
212216 ]
213217 }
214- return ret
215218
216219
217220def download_css (html_css_dir ):
0 commit comments