tools.py
Some utility functions.
Style
¶
Common color styles.
Source code in copier/tools.py
26 27 28 29 30 31 32 33 |
|
cast_to_bool(value)
¶
Parse anything to bool.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any
|
Anything to be casted to a bool. Tries to be as smart as possible.
|
required |
Source code in copier/tools.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
|
cast_to_str(value)
¶
Parse anything to str.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Any
|
Anything to be casted to a str. |
required |
Source code in copier/tools.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
copier_version()
¶
Get closest match for the installed copier version.
Source code in copier/tools.py
49 50 51 52 53 54 55 56 57 58 59 60 |
|
force_str_end(original_str, end='\n')
¶
Make sure a original_str
ends with end
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
original_str |
str
|
String that you want to ensure ending. |
required |
end |
str
|
String that must exist at the end of |
'\n'
|
Source code in copier/tools.py
139 140 141 142 143 144 145 146 147 148 |
|
handle_remove_readonly(func, path, exc)
¶
Handle errors when trying to remove read-only files through shutil.rmtree
.
On Windows, shutil.rmtree
does not handle read-only files very well. This handler
makes sure the given file is writable, then re-execute the given removal function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable
|
An OS-dependant function used to remove a file. |
required |
path |
str
|
The path to the file to remove. |
required |
exc |
Union[BaseException, Tuple[Type[BaseException], BaseException, TracebackType]]
|
An exception (Python >= 3.12) or |
required |
Source code in copier/tools.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
|
printf(action, msg='', style=None, indent=10, quiet=False, file_=sys.stdout)
¶
Print string with common format.
Source code in copier/tools.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
printf_exception(e, action, msg='', indent=0, quiet=False)
¶
Print exception with common format.
Source code in copier/tools.py
84 85 86 87 88 89 90 91 92 93 |
|
readlink(link)
¶
A custom version of os.readlink/pathlib.Path.readlink.
pathlib.Path.readlink is what we ideally would want to use, but it is only available on python>=3.9.
Source code in copier/tools.py
177 178 179 180 181 182 183 184 185 |
|