tools.py
Some utility functions.
Style
¶
Common color styles.
Source code in copier/_tools.py
28 29 30 31 32 33 34 35 | |
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
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
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
98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
copier_version()
¶
Get closest match for the installed copier version.
Source code in copier/_tools.py
51 52 53 54 55 56 57 58 59 60 61 62 | |
escape_git_path(path)
¶
Escape paths that will be used as literal gitwildmatch patterns.
If the path was returned by a Git command, it should be unescaped completely.
normalize_git_path can be used for this purpose.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The Git path to escape. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The escaped Git path. |
Source code in copier/_tools.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
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
141 142 143 144 145 146 147 148 149 150 | |
get_git_objects_dir(path)
¶
Get the absolute path of a Git repository's objects directory.
Source code in copier/_tools.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
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[[str], None]
|
An OS-dependant function used to remove a file. |
required |
path
|
str
|
The path to the file to remove. |
required |
exc
|
BaseException | tuple[type[BaseException], BaseException, TracebackType]
|
An exception (Python >= 3.12) or |
required |
Source code in copier/_tools.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |
normalize_git_path(path)
¶
Convert weird characters returned by Git to normal UTF-8 path strings.
A filename like âñ will be reported by Git as "\303\242\303\261" (octal notation).
Similarly, a filename like "git config core.quotepath off.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The Git path to normalize. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The normalized Git path. |
Source code in copier/_tools.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
printf(action, msg='', style=None, indent=10, quiet=False, file_=sys.stdout)
¶
Print string with common format.
Source code in copier/_tools.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
printf_exception(e, action, msg='', indent=0, quiet=False)
¶
Print exception with common format.
Source code in copier/_tools.py
86 87 88 89 90 91 92 93 94 95 | |
scantree(path, follow_symlinks)
¶
A recursive extension of os.scandir.
Source code in copier/_tools.py
262 263 264 265 266 267 | |
set_git_alternates(*repos, path=Path())
¶
Set Git alternates to borrow Git objects from other repositories.
Alternates are paths of other repositories' object directories written to
$GIT_DIR/objects/info/alternates and delimited by the newline character.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*repos
|
Path
|
The paths of repositories from which to borrow Git objects. |
()
|
path
|
Path
|
The path of the repository where to set Git alternates. Defaults to the current working directory. |
Path()
|
Source code in copier/_tools.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |