Utils
sereto.utils
¶
assert_file_size_within_range(file, max_bytes, min_bytes=0, interactive=False)
¶
Evaluates if the file size is within the specified range.
If interactive is True, the user is first prompted whether to continue if the file size is not within the range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
FilePath
|
The path to the file. |
required |
max_bytes
|
int
|
The maximum file size in bytes. |
required |
min_bytes
|
int
|
The minimum file size in bytes. Defaults to 0. |
0
|
interactive
|
bool
|
If True, the user is prompted whether to continue if the file size is not within the range. Defaults to False. |
False
|
Raises:
| Type | Description |
|---|---|
SeretoPathError
|
If the file does not exist. |
SeretoValueError
|
If the file size is not within the specified range. |
Source code in sereto/utils.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
copy_skel(templates, dst, overwrite=False)
¶
Copy the content of a templates skel directory to a destination directory.
A skel directory is a directory that contains a set of files and directories that can be used as a template
for creating new projects. This function copies the contents of the skel directory located at
the path specified by templates to the destination directory specified by dst.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
templates
|
DirectoryPath
|
The path to the directory containing the |
required |
dst
|
DirectoryPath
|
The destination directory to copy the |
required |
overwrite
|
bool
|
Whether to allow overwriting of existing files in the destination directory.
If |
False
|
Raises:
| Type | Description |
|---|---|
SeretoPathError
|
If the destination directory already exists and |
Source code in sereto/utils.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
lower_alphanum(text)
¶
Converts the input text to lowercase alphanumerical delimited by underscores.
Also all spaces are replaced with underscores.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The input text with the modifications applied. |
Source code in sereto/utils.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
replace_strings(text, replacements)
¶
replace_strings(text: str, replacements: dict[str, str]) -> str
replace_strings(text: list[str], replacements: dict[str, str]) -> list[str]
One-pass string replacement with values from dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str | list[str]
|
The input text. |
required |
replacements
|
dict[str, str]
|
Dictionary with replacements. Key-value in dictionary refers to pattern string and replacement string, respectively. |
required |
Returns:
| Type | Description |
|---|---|
str | list[str]
|
String (or list of strings, depending on the input value) obtained by applying the replacements from the
|
Source code in sereto/utils.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
write_if_different(file, content)
¶
Writes content to file only if the content is different from the existing file content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
Path
|
The path to the file. |
required |
content
|
str
|
The content to write to the file. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if new content was written to the file, False otherwise. |
Source code in sereto/utils.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |