|
13 | 13 | """ |
14 | 14 |
|
15 | 15 | # Standard library imports. |
16 | | -import copy |
17 | | -import datetime |
18 | 16 | import keyword |
19 | | -import re |
20 | | -import unicodedata |
21 | | -import warnings |
22 | | - |
23 | | - |
24 | | -def clean_filename(name, replace_empty=""): |
25 | | - """ |
26 | | - Make a user-supplied string safe for filename use. |
27 | | -
|
28 | | - Returns an ASCII-encodable string based on the input string that's safe for |
29 | | - use as a component of a filename or URL. The returned value is a string |
30 | | - containing only lowercase ASCII letters, digits, and the characters '-' and |
31 | | - '_'. |
32 | | -
|
33 | | - This does not give a faithful representation of the original string: |
34 | | - different input strings can result in the same output string. |
35 | | -
|
36 | | - .. deprecated:: 6.3.0 |
37 | | - This function will be removed in a future version of Traits. |
38 | | -
|
39 | | - Parameters |
40 | | - ---------- |
41 | | - name : str |
42 | | - The string to be made safe. |
43 | | - replace_empty : str, optional |
44 | | - The return value to be used in the event that the sanitised |
45 | | - string ends up being empty. No validation is done on this |
46 | | - input - it's up to the user to ensure that the default is |
47 | | - itself safe. The default is to return the empty string. |
48 | | -
|
49 | | - Returns |
50 | | - ------- |
51 | | - safe_string : str |
52 | | - A filename-safe version of string. |
53 | | -
|
54 | | - """ |
55 | | - warnings.warn( |
56 | | - "clean_filename is deprecated and will eventually be removed", |
57 | | - DeprecationWarning, |
58 | | - stacklevel=2, |
59 | | - ) |
60 | | - |
61 | | - # Code is based on Django's slugify utility. |
62 | | - # https://docs.djangoproject.com/en/1.9/_modules/django/utils/text/#slugify |
63 | | - name = ( |
64 | | - unicodedata.normalize('NFKD', name) |
65 | | - .encode('ascii', 'ignore') |
66 | | - .decode('ascii') |
67 | | - ) |
68 | | - name = re.sub(r'[^\w\s-]', '', name).strip().lower() |
69 | | - safe_name = re.sub(r'[-\s]+', '-', name) |
70 | | - if safe_name == "": |
71 | | - return replace_empty |
72 | | - return safe_name |
73 | | - |
74 | | - |
75 | | -def clean_timestamp(dt=None, microseconds=False): |
76 | | - """ |
77 | | - Return a timestamp that has been cleansed of characters that might |
78 | | - cause problems in filenames, namely colons. If no datetime object |
79 | | - is provided, then uses the current time. |
80 | | -
|
81 | | - The timestamp is in ISO-8601 format with the following exceptions: |
82 | | -
|
83 | | - * Colons ':' are replaced by underscores '_'. |
84 | | - * Microseconds are not displayed if the 'microseconds' parameter is |
85 | | - False. |
86 | | -
|
87 | | - .. deprecated:: 6.3.0 |
88 | | - This function will be removed in a future version of Traits. |
89 | | -
|
90 | | - Parameters |
91 | | - ---------- |
92 | | - dt : None or datetime.datetime |
93 | | - If None, then the current time is used. |
94 | | - microseconds : bool |
95 | | - Display microseconds or not. |
96 | | -
|
97 | | - Returns |
98 | | - ------- |
99 | | - A string timestamp. |
100 | | - """ |
101 | | - warnings.warn( |
102 | | - "clean_timestamp is deprecated and will eventually be removed", |
103 | | - DeprecationWarning, |
104 | | - stacklevel=2, |
105 | | - ) |
106 | | - |
107 | | - if dt is None: |
108 | | - dt = datetime.datetime.now() |
109 | | - else: |
110 | | - # Operate on a copy. |
111 | | - dt = copy.copy(dt) |
112 | | - |
113 | | - if not microseconds: |
114 | | - # The microseconds are largely uninformative but annoying. |
115 | | - dt = dt.replace(microsecond=0) |
116 | | - |
117 | | - stamp = dt.isoformat().replace(":", "_") |
118 | | - |
119 | | - return stamp |
120 | 17 |
|
121 | 18 |
|
122 | 19 | def python_name(name): |
|
0 commit comments