@@ -288,34 +288,54 @@ def _make_absolute_href_path(
288
288
parsed_source : URLParseResult ,
289
289
parsed_start : URLParseResult ,
290
290
start_is_dir : bool = False ,
291
+ must_include_scheme : bool = False ,
291
292
) -> str :
292
- # If the source is already absolute, just return it
293
+ # If the source is already absolute and doesn't need a scheme just return it
293
294
if os .path .isabs (parsed_source .path ):
294
- return urlunparse (parsed_source )
295
-
296
- # If the start path is not a directory, get the parent directory
297
- start_dir = (
298
- parsed_start .path if start_is_dir else os .path .dirname (parsed_start .path )
299
- )
295
+ if must_include_scheme :
296
+ abs_path = parsed_source .path
297
+ else :
298
+ return urlunparse (parsed_source )
299
+ else :
300
+ # If the start path is not a directory, get the parent directory
301
+ start_dir = (
302
+ parsed_start .path if start_is_dir else os .path .dirname (parsed_start .path )
303
+ )
300
304
301
- # Join the start directory to the relative path and find the absolute path
302
- abs_path = make_posix_style (
303
- os .path .abspath (os .path .join (start_dir , parsed_source .path ))
304
- )
305
+ # Join the start directory to the relative path and find the absolute path
306
+ abs_path = make_posix_style (
307
+ os .path .abspath (os .path .join (start_dir , parsed_source .path ))
308
+ )
305
309
306
- # Account for the normalization of abspath for
307
- # things like /vsitar// prefixes by replacing the
308
- # original start_dir text when abspath modifies the start_dir.
309
- if not start_dir == make_posix_style (os .path .abspath (start_dir )):
310
- abs_path = abs_path .replace (
311
- make_posix_style (os .path .abspath (start_dir )), start_dir
310
+ # Account for the normalization of abspath for
311
+ # things like /vsitar// prefixes by replacing the
312
+ # original start_dir text when abspath modifies the start_dir.
313
+ if not start_dir == make_posix_style (os .path .abspath (start_dir )):
314
+ abs_path = abs_path .replace (
315
+ make_posix_style (os .path .abspath (start_dir )), start_dir
316
+ )
317
+
318
+ # add a scheme if there isn't one already
319
+ if must_include_scheme :
320
+ return urlunparse (
321
+ (
322
+ "file" ,
323
+ parsed_start .netloc ,
324
+ abs_path ,
325
+ parsed_source .params ,
326
+ parsed_source .query ,
327
+ parsed_source .fragment ,
328
+ )
312
329
)
313
330
314
331
return abs_path
315
332
316
333
317
334
def make_absolute_href (
318
- source_href : str , start_href : str | None = None , start_is_dir : bool = False
335
+ source_href : str ,
336
+ start_href : str | None = None ,
337
+ start_is_dir : bool = False ,
338
+ must_include_scheme : bool = False ,
319
339
) -> str :
320
340
"""Returns a new string that represents ``source_href`` as an absolute path. If
321
341
``source_href`` is already absolute it is returned unchanged. If ``source_href``
@@ -332,6 +352,8 @@ def make_absolute_href(
332
352
start_is_dir : If ``True``, ``start_href`` is treated as a directory.
333
353
Otherwise, ``start_href`` is considered to be a path to a file. Defaults to
334
354
``False``.
355
+ must_include_scheme : If ``True``, every output will have a scheme. This means
356
+ that local filepaths will be prefixed with `file://`. Defaults to ``False``.
335
357
336
358
Returns:
337
359
str: The absolute HREF.
@@ -349,7 +371,9 @@ def make_absolute_href(
349
371
if parsed_source .scheme != "" or parsed_start .scheme != "" :
350
372
return _make_absolute_href_url (parsed_source , parsed_start , start_is_dir )
351
373
else :
352
- return _make_absolute_href_path (parsed_source , parsed_start , start_is_dir )
374
+ return _make_absolute_href_path (
375
+ parsed_source , parsed_start , start_is_dir , must_include_scheme
376
+ )
353
377
354
378
355
379
def is_absolute_href (href : str ) -> bool :
0 commit comments