1
1
#!/usr/bin/env python
2
- # -*- coding: utf-8; -*-
3
2
4
3
# Copyright (c) 2020, 2024 Oracle and/or its affiliates.
5
4
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
6
5
7
- from __future__ import absolute_import , print_function
8
6
9
7
import collections
10
8
import contextlib
23
21
from datetime import datetime
24
22
from enum import Enum
25
23
from io import DEFAULT_BUFFER_SIZE
26
- from pathlib import Path
27
24
from textwrap import fill
28
- from typing import Dict , Optional , Union
25
+ from typing import Dict , Optional , Tuple , Union
29
26
from urllib import request
30
27
from urllib .parse import urlparse
31
28
@@ -501,13 +498,13 @@ def print_user_message(
501
498
if is_documentation_mode () and is_notebook ():
502
499
if display_type .lower () == "tip" :
503
500
if "\n " in msg :
504
- t = "<b>{}:</b>" . format ( title .upper ().strip ()) if title else ""
501
+ t = f "<b>{ title .upper ().strip ()} :</b>" if title else ""
505
502
506
503
user_message = "{}{}" .format (
507
504
t ,
508
505
"" .join (
509
506
[
510
- "<br> + {}" . format ( x .strip ())
507
+ f "<br> + { x .strip ()} "
511
508
for x in msg .strip ().split ("\n " )
512
509
]
513
510
),
@@ -646,7 +643,7 @@ def ellipsis_strings(raw, n=24):
646
643
else :
647
644
n2 = int (n ) // 2 - 3
648
645
n1 = n - n2 - 3
649
- result .append ("{0 }...{1}" . format ( s [: n1 ], s [ - n2 :]) )
646
+ result .append (f" { s [: n1 ] } ...{ s [ - n2 :]} " )
650
647
651
648
return result
652
649
@@ -942,9 +939,9 @@ def generate_requirement_file(
942
939
with open (os .path .join (file_path , file_name ), "w" ) as req_file :
943
940
for lib in requirements :
944
941
if requirements [lib ]:
945
- req_file .write ("{ }=={} \n " . format ( lib , requirements [lib ]) )
942
+ req_file .write (f" { lib } =={ requirements [lib ]} \n " )
946
943
else :
947
- req_file .write ("{ }\n ". format ( lib ) )
944
+ req_file .write (f" { lib } \n " )
948
945
949
946
950
947
def _get_feature_type_and_dtype (column ):
@@ -966,7 +963,7 @@ def to_dataframe(
966
963
pd .Series ,
967
964
np .ndarray ,
968
965
pd .DataFrame ,
969
- ]
966
+ ],
970
967
):
971
968
"""
972
969
Convert to pandas DataFrame.
@@ -1391,7 +1388,7 @@ def remove_file(file_path: str, auth: Optional[Dict] = None) -> None:
1391
1388
fs = fsspec .filesystem (scheme , ** auth )
1392
1389
try :
1393
1390
fs .rm (file_path )
1394
- except FileNotFoundError as e :
1391
+ except FileNotFoundError :
1395
1392
raise FileNotFoundError (f"`{ file_path } ` not found." )
1396
1393
except Exception as e :
1397
1394
raise e
@@ -1786,3 +1783,36 @@ def get_log_links(
1786
1783
console_link_url = f"https://cloud.oracle.com/logging/log-groups/{ log_group_id } ?region={ region } "
1787
1784
1788
1785
return console_link_url
1786
+
1787
+
1788
+ def parse_content_disposition (header : str ) -> Tuple [str , Dict [str , str ]]:
1789
+ """
1790
+ Parses a Content-Disposition header into its main disposition and a dictionary of parameters.
1791
+
1792
+ For example:
1793
+ 'attachment; filename="example.txt"'
1794
+ will be parsed into:
1795
+ ('attachment', {'filename': 'example.txt'})
1796
+
1797
+ Parameters
1798
+ ----------
1799
+ header (str): The Content-Disposition header string.
1800
+
1801
+ Returns
1802
+ -------
1803
+ Tuple[str, Dict[str, str]]: A tuple containing the disposition and a dictionary of parameters.
1804
+ """
1805
+ if not header :
1806
+ return "" , {}
1807
+
1808
+ parts = header .split (";" )
1809
+ # The first part is the main disposition (e.g., "attachment").
1810
+ disposition = parts [0 ].strip ().lower ()
1811
+ params : Dict [str , str ] = {}
1812
+
1813
+ # Process each subsequent part to extract key-value pairs.
1814
+ for part in parts [1 :]:
1815
+ if "=" in part :
1816
+ key , value = part .split ("=" , 1 )
1817
+ params [key .strip ().lower ()] = value .strip ().strip ('"' )
1818
+ return disposition , params
0 commit comments