cloudmesh-ai-common API Reference
This page provides the API reference for the cloudmesh-ai-common library.
API Documentation
Unified entry point for cloudmesh-ai-common. Exports the most commonly used utilities for easier access.
Attributes
__all__
module-attribute
__all__ = ['console', 'Console', 'readfile', 'writefile', 'path_expand', 'banner', 'flatten', 'backup_name', 'yn_choice', 'HEADING', 'DotDict', 'SystemInfo', 'DateTime', 'time', 'Shell', 'VERBOSE']
console
module-attribute
console = Console()
time
module-attribute
time = DateTime
Classes
Console
Bases: BaseIO, Console
Unified Console for cloudmesh-ai providing styled output, I/O, and table printing.
Source code in src/cloudmesh/ai/common/io.py
103 104 105 106 107 108 109 110 111 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 211 212 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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | |
Functions
ai_response
ai_response(text, title='AI Response', style='cyan')
Displays a standardized AI response box.
Source code in src/cloudmesh/ai/common/io.py
309 310 311 312 313 314 315 316 317 318 319 | |
banner
banner(label=None, txt=None, c='-', prefix='#', debug=True, color='blue', padding=False, figlet=False, font='big')
Prints a banner of the form with a frame of # around the txt
Source code in src/cloudmesh/ai/common/io.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 175 176 177 178 179 180 181 182 183 184 185 | |
bold
bold(message)
Prints a message in bold.
Source code in src/cloudmesh/ai/common/io.py
130 131 132 | |
cprint
cprint(text, color, style='')
Helper to print with color.
Source code in src/cloudmesh/ai/common/io.py
187 188 189 | |
create_banner
create_banner(title, content=None)
Creates a banner Panel without printing it.
Source code in src/cloudmesh/ai/common/io.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
error
error(message)
Prints an error message in red.
Source code in src/cloudmesh/ai/common/io.py
106 107 108 | |
info
info(message)
Prints an info message in magenta.
Source code in src/cloudmesh/ai/common/io.py
118 119 120 | |
left
left()
Moves the cursor to the beginning of the current line.
Source code in src/cloudmesh/ai/common/io.py
304 305 306 307 | |
msg
msg(message)
Prints a message in blue.
Source code in src/cloudmesh/ai/common/io.py
114 115 116 | |
note
note(message)
Prints a note in cyan.
Source code in src/cloudmesh/ai/common/io.py
122 123 124 | |
ok
ok(message)
Prints a success message in green.
Source code in src/cloudmesh/ai/common/io.py
126 127 128 | |
print_attributes
print_attributes(d, header=None, order=None, sort_keys=True, humanize=False, output='table')
Prints a dictionary of attributes in various formats.
Source code in src/cloudmesh/ai/common/io.py
204 205 206 207 208 209 210 211 212 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 245 246 247 248 249 250 251 | |
print_csv
print_csv(d, order=None)
Prints a dictionary as CSV.
Source code in src/cloudmesh/ai/common/io.py
273 274 275 276 277 278 279 280 | |
print_error
print_error(message)
Prints a standardized error message.
Source code in src/cloudmesh/ai/common/io.py
347 348 349 | |
print_json
print_json(data)
Prints data as formatted JSON.
Source code in src/cloudmesh/ai/common/io.py
265 266 267 | |
print_markdown
print_markdown(text)
Renders and prints markdown text.
Source code in src/cloudmesh/ai/common/io.py
282 283 284 | |
print_status
print_status(message, style='yellow')
Prints a simple status message.
Source code in src/cloudmesh/ai/common/io.py
343 344 345 | |
print_success
print_success(message)
Prints a standardized success message.
Source code in src/cloudmesh/ai/common/io.py
351 352 353 | |
print_table
print_table(headers, data, title=None, expand=False)
Prints a formatted table. By default, it is compact (expand=False).
Source code in src/cloudmesh/ai/common/io.py
253 254 255 256 257 258 259 260 261 | |
print_yaml
print_yaml(data)
Prints data as formatted YAML.
Source code in src/cloudmesh/ai/common/io.py
269 270 271 | |
status
status(message)
Returns a status spinner context manager.
Source code in src/cloudmesh/ai/common/io.py
294 295 296 | |
telemetry_table
telemetry_table(records, title='Telemetry Records')
Displays a standardized telemetry records table.
Source code in src/cloudmesh/ai/common/io.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | |
top
top(lines)
Moves the cursor up by the specified number of lines.
Source code in src/cloudmesh/ai/common/io.py
298 299 300 301 302 | |
warning
warning(message)
Prints a warning message in yellow.
Source code in src/cloudmesh/ai/common/io.py
110 111 112 | |
ynchoice
ynchoice(message, default=True)
Asks a yes/no question and returns a boolean.
Source code in src/cloudmesh/ai/common/io.py
191 192 193 194 195 196 197 198 199 200 201 202 | |
DotDict
Bases: OrderedDict
A dictionary subclass that supports dot-notation for nested access and assignment.
Source code in src/cloudmesh/ai/common/dotdict.py
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 211 212 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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | |
Attributes
dict
property
dict
Returns the DotDict as a plain Python dictionary.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
A plain Python dictionary representation of the DotDict. |
yaml
property
yaml
Returns a YAML dump of the dictionary using literal block style for multi-line strings.
Returns:
| Name | Type | Description |
|---|---|---|
str |
The YAML representation of the DotDict. |
Functions
__delattr__
__delattr__(key)
Deletes an attribute, which removes the corresponding dictionary item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The attribute name to delete. |
required |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If the attribute is not found. |
Source code in src/cloudmesh/ai/common/dotdict.py
282 283 284 285 286 287 288 289 290 291 292 293 294 | |
__delitem__
__delitem__(key)
Deletes a value from the dictionary, supporting dot-notation for nested deletion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to delete. If it contains dots, it is treated as a path. |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the key or any part of the path is not found. |
Source code in src/cloudmesh/ai/common/dotdict.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
__getattr__
__getattr__(attr)
Returns an element using attribute access.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attr
|
str
|
The attribute name to look up. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
The value associated with the attribute. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If the attribute is not found. |
Source code in src/cloudmesh/ai/common/dotdict.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | |
__getitem__
__getitem__(key)
Retrieves a value from the dictionary, supporting dot-notation for nested access.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to look up. If it contains dots, it is treated as a path. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
The value associated with the key or path. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the key or any part of the path is not found. |
Source code in src/cloudmesh/ai/common/dotdict.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
__init__
__init__(data=None, **kwargs)
Initializes the DotDict with optional data and keyword arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
Initial dictionary data. Defaults to None. |
None
|
**kwargs
|
Additional key-value pairs to initialize the dictionary. |
{}
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If the provided data is not a dictionary. |
Source code in src/cloudmesh/ai/common/dotdict.py
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 | |
__repr__
__repr__()
Returns a string representation of the DotDict as YAML.
Source code in src/cloudmesh/ai/common/dotdict.py
90 91 92 | |
__setattr__
__setattr__(key, value)
Sets an attribute value, which is stored as a dictionary item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The attribute name. |
required |
value
|
Any
|
The value to set. |
required |
Source code in src/cloudmesh/ai/common/dotdict.py
273 274 275 276 277 278 279 280 | |
__setitem__
__setitem__(key, value)
Sets a value in the dictionary, supporting dot-notation for nested assignment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to set. If it contains dots, it is treated as a path. |
required |
value
|
Any
|
The value to assign. Dictionaries are automatically converted to DotDict. |
required |
Source code in src/cloudmesh/ai/common/dotdict.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
expand
expand(d=None)
Expands placeholders in a dictionary using this DotDict's values.
If a value in the target dictionary is a string containing {attribute}, it is replaced by the value of the corresponding attribute found in this DotDict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
dict
|
The dictionary to expand. If None, this DotDict itself is expanded. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
DotDict |
A new DotDict with expanded values. |
Source code in src/cloudmesh/ai/common/dotdict.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 | |
get
get(key, default=None)
Retrieves a value from the dictionary, supporting dot-notation for nested access.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to look up. If it contains dots, it is treated as a path. |
required |
default
|
Any
|
The value to return if the key or path is not found. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
The value associated with the key or path, or the default value. |
Source code in src/cloudmesh/ai/common/dotdict.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
merge
merge(data)
Deep merges the provided data into this DotDict.
If a key exists in both and both values are dictionaries, they are merged recursively. Standard dictionaries are automatically converted to DotDict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict | DotDict
|
The data to merge into this object. |
required |
Source code in src/cloudmesh/ai/common/dotdict.py
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | |
smart_get
smart_get(key, default=None)
Retrieves a value using a smart lookup.
First attempts a direct lookup (supporting dot-notation). If not found, performs a recursive search for the key in the nested structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to look up. |
required |
default
|
Any
|
The value to return if the key is not found. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
The value found in the configuration, or the default value. |
Source code in src/cloudmesh/ai/common/dotdict.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | |
to_dict
to_dict()
Recursively converts the DotDict and all nested DotDicts to standard dictionaries.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
A plain Python dictionary representation of the DotDict. |
Source code in src/cloudmesh/ai/common/dotdict.py
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | |
to_json
to_json(indent=None)
Returns a JSON string representation of the DotDict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indent
|
int
|
Number of spaces for indentation. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
The JSON representation of the DotDict. |
Source code in src/cloudmesh/ai/common/dotdict.py
377 378 379 380 381 382 383 384 385 386 | |
Functions
HEADING
HEADING(txt=None, c='#', color='magenta')
Prints a message to stdout with #### surrounding it.
Source code in src/cloudmesh/ai/common/util.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
SystemInfo
SystemInfo(info=None, user=None, node=None, realtime=False)
Collects comprehensive system metadata into a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
info
|
Optional[Dict[str, Any]]
|
Optional dictionary of additional information to merge into the result. |
None
|
user
|
Optional[str]
|
Optional override for the current system user. |
None
|
node
|
Optional[str]
|
Optional override for the system node name. |
None
|
realtime
|
bool
|
If True, includes real-time CPU, memory, and disk utilization metrics. |
False
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dictionary containing system hardware, OS, and (optionally) real-time performance data. |
Source code in src/cloudmesh/ai/common/sys.py
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 | |
VERBOSE
VERBOSE(arguments)
Prints the arguments if debug is enabled.
Maintains backward compatibility with the original VERBOSE function.
Source code in src/cloudmesh/ai/common/debug.py
64 65 66 67 68 69 70 71 | |
backup_name
backup_name(filename)
creates a backup name of the form filename.bak.1
Source code in src/cloudmesh/ai/common/util.py
250 251 252 253 254 255 256 257 258 | |
banner
banner(label=None, txt=None, c='-', prefix='#', debug=True, color='blue', padding=False, figlet=False, font='big')
Standalone wrapper for console.banner.
Source code in src/cloudmesh/ai/common/io.py
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | |
flatten
flatten(d, parent_key='', sep='.')
Flattens a multidimensional dict into a one-dimensional dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
Any
|
The multidimensional dictionary or list to flatten. |
required |
parent_key
|
str
|
The prefix to use for the keys in the flattened dictionary. Defaults to "". |
''
|
sep
|
str
|
The separation character used to join nested keys. Defaults to "__". |
'.'
|
Returns:
| Type | Description |
|---|---|
Union[Dict, List]
|
A flattened dictionary if the input was a dictionary, a list of flattened |
Union[Dict, List]
|
dictionaries if the input was a list, or the original object if it was |
Union[Dict, List]
|
neither. |
Source code in src/cloudmesh/ai/common/flatdict.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
path_expand
path_expand(text, slashreplace=True)
Standalone wrapper for console.expand_path.
Source code in src/cloudmesh/ai/common/io.py
421 422 423 | |
readfile
readfile(path)
Standalone wrapper for console.readfile.
Source code in src/cloudmesh/ai/common/io.py
409 410 411 | |
writefile
writefile(path, content)
Standalone wrapper for console.writefile.
Source code in src/cloudmesh/ai/common/io.py
413 414 415 | |
yn_choice
yn_choice(message, default='y', tries=None)
asks for a yes/no question.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
the message containing the question |
required |
default
|
str
|
the default answer |
'y'
|
tries
|
Optional[int]
|
the number of tries |
None
|
Source code in src/cloudmesh/ai/common/util.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
Modules
DateTime
Classes
DateTime
Bases: object
This class provides some simple date time functions so we can use all the same format. Here is a simple example
start = DateTime.now()
stop = DateTime.now() + DateTime.delta(1)
print ("START", start)
print ("STOP", stop)
print("HUMANIZE STOP", DateTime.humanize(stop - start))
print ("LOCAL", DateTime.local(start))
print("UTC", DateTime.utc(start))
print("NATURAL", DateTime.natural(start))
print("WORDS", DateTime.words(start))
print("TIMEZONE", DateTime.timezone)
This will result in
START 2019-08-03 21:34:14.019147
STOP 2019-08-03 21:34:15.019150
HUMANIZE STOP a second ago
LOCAL 2019-08-03 17:34:14 EST
UTC 2019-08-03 21:34:14.019147 UTC
NATURAL 2019-08-03 21:34:14.019147 UTC
WORDS Sat 6 Aug 2019, 21:34:14 UTC
TIMEZONE EST
Source code in src/cloudmesh/ai/common/DateTime.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
Functions
staticmethod
delta(n)
Returns a timedelta object representing a time duration of n seconds.
Parameters: n (int): The number of seconds for the time duration.
Returns: timedelta: A timedelta object representing the time duration in datetime and not string format.
Source code in src/cloudmesh/ai/common/DateTime.py
88 89 90 91 92 93 94 95 96 97 98 99 | |
staticmethod
locale_name()
Detects system locale (e.g., 'en_us').
Source code in src/cloudmesh/ai/common/DateTime.py
138 139 140 141 142 143 144 145 | |
staticmethod
timezone(default='America/New_York')
Returns the local timezone name.
Source code in src/cloudmesh/ai/common/DateTime.py
130 131 132 133 134 135 136 | |
Shell
A convenient method to execute shell commands and return their output.
Classes
Shell
Bases: object
The shell class allowing us to conveniently access many operating system commands.
Source code in src/cloudmesh/ai/common/Shell.py
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
Functions
staticmethod
browser(filename=None)
Opens a file or URL in the browser
Source code in src/cloudmesh/ai/common/Shell.py
169 170 171 172 173 174 175 | |
classmethod
execute(cmd, arguments='', shell=False, cwd=None, traceflag=True, witherror=True)
Run Shell command
Source code in src/cloudmesh/ai/common/Shell.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
staticmethod
install_brew()
Installs Homebrew on macOS
Source code in src/cloudmesh/ai/common/Shell.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
staticmethod
install_chocolatey()
Install chocolatey windows package manager
Source code in src/cloudmesh/ai/common/Shell.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
staticmethod
live(command)
Executes a command and prints output in real time
Source code in src/cloudmesh/ai/common/Shell.py
156 157 158 159 160 161 162 163 164 165 166 167 | |
staticmethod
rm(top)
Removes a directory tree
Source code in src/cloudmesh/ai/common/Shell.py
177 178 179 180 181 182 183 184 185 186 | |
staticmethod
run(command, exitcode='', encoding='utf-8', replace=True, timeout=None)
executes the command and returns the output as string
Source code in src/cloudmesh/ai/common/Shell.py
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 119 | |
SubprocessError
Bases: Exception
Manages the formatting of the error and stdout.
Source code in src/cloudmesh/ai/common/Shell.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
Functions
windows_not_supported
windows_not_supported(f)
This is a decorator function that checks if the current platform is Windows. If it is, it prints an error message and returns an empty string.
Source code in src/cloudmesh/ai/common/Shell.py
26 27 28 29 30 31 32 33 34 35 36 37 38 | |
aggregation
Telemetry aggregation utility for cloudmesh-ai. Provides tools to analyze and summarize telemetry data from various backends.
Classes
TelemetryAggregator
Analyzes telemetry records to provide summaries and statistics. Supports loading data from both JSONL files and SQLite databases.
Source code in src/cloudmesh/ai/common/aggregation.py
12 13 14 15 16 17 18 19 20 21 22 23 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 119 120 121 | |
Functions
__init__(source)
Initialize the TelemetryAggregator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Union[str, Path]
|
Path to the telemetry source (JSONL file or SQLite .db file). |
required |
Source code in src/cloudmesh/ai/common/aggregation.py
18 19 20 21 22 23 24 25 26 27 | |
aggregate_metric(metric_name)
Calculates average, min, and max for a specific metric across all records.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric_name
|
str
|
The key of the metric to aggregate from the 'metrics' dictionary. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dictionary containing the count, average, minimum, and maximum values. |
Source code in src/cloudmesh/ai/common/aggregation.py
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 119 120 121 | |
get_summary()
Calculates a high-level summary of the telemetry data.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dictionary containing total records, success rate, |
Dict[str, Any]
|
status distribution, and command distribution. |
Source code in src/cloudmesh/ai/common/aggregation.py
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 | |
config
Classes
Config
Handles configuration for AI packages from a YAML file.
Source code in src/cloudmesh/ai/common/config.py
19 20 21 22 23 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 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
Functions
__init__(config_path=None)
Initialize the Config object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
Optional[Path]
|
Optional path to the configuration file. Defaults to DEFAULT_CONFIG_PATH. |
None
|
Source code in src/cloudmesh/ai/common/config.py
28 29 30 31 32 33 34 35 36 37 | |
get(key_path, default=None)
Get a value from the config using a dot-separated path (e.g., 'telemetry.enabled').
Environment variables can override config values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_path
|
str
|
Dot-separated path to the configuration value. |
required |
default
|
Any
|
Value to return if the key is not found. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The configuration value if found, otherwise the default value. |
Source code in src/cloudmesh/ai/common/config.py
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 | |
save()
Saves the current configuration to the YAML file.
Raises:
| Type | Description |
|---|---|
OSError
|
If the configuration file cannot be written. |
Source code in src/cloudmesh/ai/common/config.py
119 120 121 122 123 124 125 126 127 128 129 130 131 | |
set(key_path, value)
Set a value in the config using a dot-separated path (e.g., 'telemetry.enabled').
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_path
|
str
|
Dot-separated path to the configuration value. |
required |
value
|
Any
|
The value to set. |
required |
Source code in src/cloudmesh/ai/common/config.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
validate(key_path, value)
Validates a configuration value against the schema if it exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_path
|
str
|
Dot-separated path to the configuration value. |
required |
value
|
Any
|
The value to validate. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the value does not match the expected type in the schema. |
Source code in src/cloudmesh/ai/common/config.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 | |
Modules
debug
Classes
Debug
Utility class for enhanced debugging and tracing in cloudmesh-ai.
Source code in src/cloudmesh/ai/common/debug.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
Functions
classmethod
enable(value=True)
Enables or disables debug output.
Source code in src/cloudmesh/ai/common/debug.py
11 12 13 14 | |
classmethod
is_enabled()
Returns whether debug output is enabled.
Source code in src/cloudmesh/ai/common/debug.py
16 17 18 19 | |
classmethod
log(message, level='debug')
Logs a message using the unified console if debug is enabled.
Source code in src/cloudmesh/ai/common/debug.py
21 22 23 24 25 26 27 28 29 30 31 32 | |
Functions
VERBOSE
VERBOSE(arguments)
Prints the arguments if debug is enabled.
Maintains backward compatibility with the original VERBOSE function.
Source code in src/cloudmesh/ai/common/debug.py
64 65 66 67 68 69 70 71 | |
trace
trace(fn)
Decorator that logs the entry and exit of a function, including arguments and return value.
Only logs if Debug.is_enabled() is True.
Source code in src/cloudmesh/ai/common/debug.py
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 | |
dotdict
DotDict provides a dictionary-like object that allows accessing and setting nested values using both attribute notation and dot-separated string keys.
Examples:
>>> data = {"cloudmesh": {"ai": {"server": "uva"}}}
>>> config = DotDict(data)
1. Attribute access (chaining)
>>> config.cloudmesh.ai.server
'uva'
2. Dot-notation bracket access
>>> config["cloudmesh.ai.server"]
'uva'
3. Dot-notation bracket assignment
>>> config["cloudmesh.ai.port"] = 8000
>>> config.cloudmesh.ai.port
8000
4. Nested attribute assignment
>>> config.new_section = DotDict()
>>> config.new_section.key = "value"
>>> config["new_section.key"]
'value'
5. Dot-notation deletion
>>> del config["cloudmesh.ai.server"]
>>> "server" in config.cloudmesh.ai
False
6. Expanding placeholders
>>> config = DotDict({"name": "gemma", "path": "/models/{name}"})
>>> expanded = config.expand()
>>> expanded.path
'/models/gemma'
>>> data = {"path": "/models/{name}"}
>>> expanded_external = config.expand(d=data)
>>> expanded_external["path"]
'/models/gemma'
7. YAML dump with literal blocks
>>> config = DotDict({"script": "line1
line2"}) >>> print(config.yaml) script: | line1 line2
Classes
DotDict
Bases: OrderedDict
A dictionary subclass that supports dot-notation for nested access and assignment.
Source code in src/cloudmesh/ai/common/dotdict.py
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 211 212 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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | |
Attributes
property
dict
Returns the DotDict as a plain Python dictionary.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
A plain Python dictionary representation of the DotDict. |
property
yaml
Returns a YAML dump of the dictionary using literal block style for multi-line strings.
Returns:
| Name | Type | Description |
|---|---|---|
str |
The YAML representation of the DotDict. |
Functions
__delattr__(key)
Deletes an attribute, which removes the corresponding dictionary item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The attribute name to delete. |
required |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If the attribute is not found. |
Source code in src/cloudmesh/ai/common/dotdict.py
282 283 284 285 286 287 288 289 290 291 292 293 294 | |
__delitem__(key)
Deletes a value from the dictionary, supporting dot-notation for nested deletion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to delete. If it contains dots, it is treated as a path. |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the key or any part of the path is not found. |
Source code in src/cloudmesh/ai/common/dotdict.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
__getattr__(attr)
Returns an element using attribute access.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attr
|
str
|
The attribute name to look up. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
The value associated with the attribute. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If the attribute is not found. |
Source code in src/cloudmesh/ai/common/dotdict.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | |
__getitem__(key)
Retrieves a value from the dictionary, supporting dot-notation for nested access.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to look up. If it contains dots, it is treated as a path. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
The value associated with the key or path. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the key or any part of the path is not found. |
Source code in src/cloudmesh/ai/common/dotdict.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
__init__(data=None, **kwargs)
Initializes the DotDict with optional data and keyword arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
Initial dictionary data. Defaults to None. |
None
|
**kwargs
|
Additional key-value pairs to initialize the dictionary. |
{}
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If the provided data is not a dictionary. |
Source code in src/cloudmesh/ai/common/dotdict.py
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 | |
__repr__()
Returns a string representation of the DotDict as YAML.
Source code in src/cloudmesh/ai/common/dotdict.py
90 91 92 | |
__setattr__(key, value)
Sets an attribute value, which is stored as a dictionary item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The attribute name. |
required |
value
|
Any
|
The value to set. |
required |
Source code in src/cloudmesh/ai/common/dotdict.py
273 274 275 276 277 278 279 280 | |
__setitem__(key, value)
Sets a value in the dictionary, supporting dot-notation for nested assignment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to set. If it contains dots, it is treated as a path. |
required |
value
|
Any
|
The value to assign. Dictionaries are automatically converted to DotDict. |
required |
Source code in src/cloudmesh/ai/common/dotdict.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
expand(d=None)
Expands placeholders in a dictionary using this DotDict's values.
If a value in the target dictionary is a string containing {attribute}, it is replaced by the value of the corresponding attribute found in this DotDict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
dict
|
The dictionary to expand. If None, this DotDict itself is expanded. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
DotDict |
A new DotDict with expanded values. |
Source code in src/cloudmesh/ai/common/dotdict.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 | |
get(key, default=None)
Retrieves a value from the dictionary, supporting dot-notation for nested access.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to look up. If it contains dots, it is treated as a path. |
required |
default
|
Any
|
The value to return if the key or path is not found. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
The value associated with the key or path, or the default value. |
Source code in src/cloudmesh/ai/common/dotdict.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
merge(data)
Deep merges the provided data into this DotDict.
If a key exists in both and both values are dictionaries, they are merged recursively. Standard dictionaries are automatically converted to DotDict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict | DotDict
|
The data to merge into this object. |
required |
Source code in src/cloudmesh/ai/common/dotdict.py
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | |
smart_get(key, default=None)
Retrieves a value using a smart lookup.
First attempts a direct lookup (supporting dot-notation). If not found, performs a recursive search for the key in the nested structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key to look up. |
required |
default
|
Any
|
The value to return if the key is not found. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
The value found in the configuration, or the default value. |
Source code in src/cloudmesh/ai/common/dotdict.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | |
to_dict()
Recursively converts the DotDict and all nested DotDicts to standard dictionaries.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
A plain Python dictionary representation of the DotDict. |
Source code in src/cloudmesh/ai/common/dotdict.py
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | |
to_json(indent=None)
Returns a JSON string representation of the DotDict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indent
|
int
|
Number of spaces for indentation. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
The JSON representation of the DotDict. |
Source code in src/cloudmesh/ai/common/dotdict.py
377 378 379 380 381 382 383 384 385 386 | |
Functions
str_presenter
str_presenter(dumper, data)
Custom YAML representer to use literal block style for multi-line strings.
Source code in src/cloudmesh/ai/common/dotdict.py
58 59 60 61 62 | |
exceptions
Classes
CommonAIError
Bases: Exception
Base exception for all cloudmesh-ai-common errors.
Source code in src/cloudmesh/ai/common/exceptions.py
9 10 11 | |
IOErrorBase
Bases: CommonAIError
Base exception for IO related errors.
Source code in src/cloudmesh/ai/common/exceptions.py
25 26 27 | |
IOReadError
Bases: IOErrorBase
Raised when reading a file or stream fails.
Source code in src/cloudmesh/ai/common/exceptions.py
29 30 31 | |
IOWriteError
Bases: IOErrorBase
Raised when writing to a file or stream fails.
Source code in src/cloudmesh/ai/common/exceptions.py
33 34 35 | |
SSHAuthenticationError
Bases: SSHError
Raised when authentication fails.
Source code in src/cloudmesh/ai/common/exceptions.py
21 22 23 | |
SSHConnectionError
Bases: SSHError
Raised when a connection to a remote host fails.
Source code in src/cloudmesh/ai/common/exceptions.py
17 18 19 | |
SSHError
Bases: CommonAIError
Base exception for SSH related errors.
Source code in src/cloudmesh/ai/common/exceptions.py
13 14 15 | |
SecurityAuthError
Bases: SecurityError
Raised when privilege escalation (sudo) fails.
Source code in src/cloudmesh/ai/common/exceptions.py
41 42 43 | |
SecurityError
Bases: CommonAIError
Base exception for security and privilege escalation errors.
Source code in src/cloudmesh/ai/common/exceptions.py
37 38 39 | |
flatdict
FlatDict provides utilities for flattening nested dictionaries and managing them as a single-level map.
This module is useful for configuration management where nested structures need to be represented as flat key-value pairs (e.g., for environment variables or simple lookups).
Variable Expansion
The expand_config_parameters function (used by FlatDict.load) supports four types of expansion:
1. Internal YAML Expansion: Replaces {key} with the value of another key in the same dict.
This is recursive, allowing chained references.
2. OS Environment Expansion: Replaces {os.VARIABLE} with the value of the system environment variable.
3. Cloudmesh Variable Expansion: Replaces {cloudmesh.VAR} or {cm.VAR} using the Variables registry.
4. Math Evaluation: Replaces eval(expression) with the result of a restricted Python evaluation
(e.g., eval(1 + 1) becomes 2).
Examples:
>>> # 1. Basic Flattening
>>> data = {"cloudmesh": {"ai": {"server": "uva"}}}
>>> flat = FlatDict(data)
>>> print(flat["cloudmesh.ai.server"])
'uva'
>>> # 2. Unflattening
>>> nested = flat.unflatten()
>>> print(nested["cloudmesh"]["ai"]["server"])
'uva'
>>> # 3. Attribute-style access
>>> print(flat.cloudmesh.ai.server)
'uva'
>>> # 4. Creating from a Python Object
>>> class Server:
... def __init__(self):
... self.name = "uva"
... self.port = 8000
>>> s = Server()
>>> flat_obj = FlatDict.from_object(s)
>>> print(flat_obj.name)
'uva'
>>> # 5. Variable Expansion
>>> data = {"user": "grey", "path": "/home/{user}/models"}
>>> flat_exp = FlatDict(data)
>>> flat_exp.load(content=data, expand=True)
>>> print(flat_exp.path)
'/home/grey/models'
>>> # 6. Applying to strings
>>> template = "Connecting to {cloudmesh.ai.server}..."
>>> print(flat.apply_in_string(template))
'Connecting to uva...'
Classes
FlatDict
Bases: dict
A data structure to manage a flattened dictionary.
This class provides a way to handle nested dictionaries as a single-level dictionary with keys joined by a separator.
Source code in src/cloudmesh/ai/common/flatdict.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | |
Functions
__getattr__(attr)
Allow attribute-style access to keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attr
|
str
|
The attribute name (key) to retrieve. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
The value associated with the key, or None if not found. |
Source code in src/cloudmesh/ai/common/flatdict.py
278 279 280 281 282 283 284 285 286 287 | |
__init__(d=None, expand=['os.', 'cm.', 'cloudmesh.'], sep='.')
Initializes the FlatDict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
Optional[Dict]
|
The dictionary data to flatten. Defaults to None. |
None
|
expand
|
List[str]
|
List of prefixes to expand. Defaults to ["os.", "cm.", "cloudmesh."]. |
['os.', 'cm.', 'cloudmesh.']
|
sep
|
str
|
The character used to indicate a hierarchy. Defaults to "__". |
'.'
|
Source code in src/cloudmesh/ai/common/flatdict.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
apply(content, write=True)
Converts a string or the contents of a file with the values of the flatdict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
Any
|
The string or file path to process. |
required |
write
|
bool
|
Whether to write the result back to the file. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Optional[str]: The processed string, or None if content was invalid. |
Source code in src/cloudmesh/ai/common/flatdict.py
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | |
apply_in_string(content)
Replaces placeholders in a string with values from the flat dict.
Placeholders should be in the format {key}.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
The string containing placeholders. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The string with placeholders replaced by their corresponding values. |
Source code in src/cloudmesh/ai/common/flatdict.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | |
classmethod
from_object(obj, **kwargs)
Creates a FlatDict from a Python object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to convert and flatten. |
required |
**kwargs
|
Additional arguments passed to the FlatDict constructor (e.g., sep, expand). |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
FlatDict |
FlatDict
|
A FlatDict instance created from the object. |
Source code in src/cloudmesh/ai/common/flatdict.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |
load(content=None, expand=True, sep='.')
Reads in the dict based on the values and types provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
Any
|
The content to load (file path, string, or dict). |
None
|
expand
|
bool
|
Whether to expand variables. Defaults to True. |
True
|
sep
|
str
|
The separation character to use. Defaults to ".". |
'.'
|
Source code in src/cloudmesh/ai/common/flatdict.py
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | |
loadd(content=None, sep=None)
Loads configuration from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
Any
|
The dictionary to load. |
None
|
sep
|
str
|
The separation character to use for flattening. Defaults to self.sep. |
None
|
Source code in src/cloudmesh/ai/common/flatdict.py
358 359 360 361 362 363 364 365 366 367 368 | |
loadf(filename=None, sep=None)
Loads configuration from a YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the YAML configuration file. |
None
|
sep
|
str
|
The separation character to use for flattening. Defaults to self.sep. |
None
|
Source code in src/cloudmesh/ai/common/flatdict.py
334 335 336 337 338 339 340 341 342 343 344 | |
loads(content=None, sep=None)
Loads configuration from a YAML string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
Any
|
The YAML string to load. |
None
|
sep
|
str
|
The separation character to use for flattening. Defaults to self.sep. |
None
|
Source code in src/cloudmesh/ai/common/flatdict.py
346 347 348 349 350 351 352 353 354 355 356 | |
search(key, value=None)
Returns keys that match the given regex pattern and value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The regex pattern to search for in keys. |
required |
value
|
Any
|
The value to match against. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: A list of keys that match the pattern and value. |
Source code in src/cloudmesh/ai/common/flatdict.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | |
unflatten()
Unflattens the flat dict back to a regular nested dict.
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict
|
The unflattened nested dictionary. |
Source code in src/cloudmesh/ai/common/flatdict.py
308 309 310 311 312 313 314 315 316 317 | |
Variables
A lightweight class for managing variables used in configuration expansion.
Attributes:
| Name | Type | Description |
|---|---|---|
_vars |
Dict
|
Internal storage for variables. |
Source code in src/cloudmesh/ai/common/flatdict.py
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 119 120 121 122 123 124 125 126 127 | |
Functions
__contains__(key)
Checks if a variable exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The variable name to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
True if the variable exists, False otherwise. |
Source code in src/cloudmesh/ai/common/flatdict.py
101 102 103 104 105 106 107 108 109 110 | |
__getitem__(key)
Retrieves a variable value by key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The variable name. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
The value of the variable, or an empty string if not found. |
Source code in src/cloudmesh/ai/common/flatdict.py
82 83 84 85 86 87 88 89 90 91 | |
__init__()
Initializes the Variables object.
Source code in src/cloudmesh/ai/common/flatdict.py
78 79 80 | |
__iter__()
Iterates over the variable keys.
Returns:
| Name | Type | Description |
|---|---|---|
Iterator |
An iterator over the keys of the variables dictionary. |
Source code in src/cloudmesh/ai/common/flatdict.py
93 94 95 96 97 98 99 | |
add(key, value)
Adds or updates a variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The variable name. |
required |
value
|
Any
|
The value to assign to the variable. |
required |
Source code in src/cloudmesh/ai/common/flatdict.py
112 113 114 115 116 117 118 119 | |
items()
Returns the variables as key-value pairs.
Returns:
| Name | Type | Description |
|---|---|---|
ItemsView |
A view of the variables dictionary items. |
Source code in src/cloudmesh/ai/common/flatdict.py
121 122 123 124 125 126 127 | |
Functions
expand_config_parameters
expand_config_parameters(flat=None, expand_yaml=True, expand_os=True, expand_cloudmesh=True, debug=False, depth=100)
Expands all variables in the flat dict if they are specified in the values.
Supports expansion of YAML variables, OS environment variables, and cloudmesh-specific variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flat
|
Dict
|
The flattened dictionary to expand. Defaults to None. |
None
|
expand_yaml
|
bool
|
Whether to expand variables defined within the dict itself. Defaults to True. |
True
|
expand_os
|
bool
|
Whether to expand OS environment variables (e.g., {os.HOME}). Defaults to True. |
True
|
expand_cloudmesh
|
bool
|
Whether to expand cloudmesh variables (e.g., {cm.USER}). Defaults to True. |
True
|
debug
|
bool
|
Whether to enable debug logging. Defaults to False. |
False
|
depth
|
int
|
Maximum recursion depth for YAML expansion. Defaults to 100. |
100
|
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict
|
A new dictionary with all variables expanded. |
Source code in src/cloudmesh/ai/common/flatdict.py
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 | |
flatme
flatme(d)
Flattens all values in a dictionary if they are dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
Dict
|
The dictionary whose values should be flattened. |
required |
Returns:
| Type | Description |
|---|---|
Dict
|
A new dictionary where all dictionary values have been flattened. |
Source code in src/cloudmesh/ai/common/flatdict.py
179 180 181 182 183 184 185 186 187 188 189 190 191 | |
flatten
flatten(d, parent_key='', sep='.')
Flattens a multidimensional dict into a one-dimensional dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
Any
|
The multidimensional dictionary or list to flatten. |
required |
parent_key
|
str
|
The prefix to use for the keys in the flattened dictionary. Defaults to "". |
''
|
sep
|
str
|
The separation character used to join nested keys. Defaults to "__". |
'.'
|
Returns:
| Type | Description |
|---|---|
Union[Dict, List]
|
A flattened dictionary if the input was a dictionary, a list of flattened |
Union[Dict, List]
|
dictionaries if the input was a list, or the original object if it was |
Union[Dict, List]
|
neither. |
Source code in src/cloudmesh/ai/common/flatdict.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
key_prefix_replace
key_prefix_replace(d, prefix, new_prefix='')
Replaces the list of prefixes in keys of a flattened dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
Dict
|
The flattened dict. |
required |
prefix
|
List[str]
|
A list of prefixes that are replaced with a new prefix. |
required |
new_prefix
|
str
|
The new prefix. |
''
|
Returns:
| Type | Description |
|---|---|
Dict
|
The dict with the keys replaced as specified. |
Source code in src/cloudmesh/ai/common/flatdict.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
read_config_parameters
read_config_parameters(filename=None, d=None, sep='.')
Reads configuration parameters from a YAML file and produces a flattened dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the YAML configuration file. |
None
|
d
|
str
|
Optional YAML string to merge into the configuration. |
None
|
sep
|
str
|
The separation character used for flattening. Defaults to ".". |
'.'
|
Returns:
| Type | Description |
|---|---|
Dict
|
A flattened dictionary of the configuration parameters. |
Source code in src/cloudmesh/ai/common/flatdict.py
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | |
read_config_parameters_from_dict
read_config_parameters_from_dict(content=None, d=None, sep='.')
Reads configuration parameters from a dictionary and produces a flattened dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
Dict
|
The dictionary to parse. |
None
|
d
|
str
|
Optional YAML string to merge into the configuration. |
None
|
sep
|
str
|
The separation character used for flattening. Defaults to ".". |
'.'
|
Returns:
| Type | Description |
|---|---|
Dict
|
A flattened dictionary of the configuration parameters. |
Source code in src/cloudmesh/ai/common/flatdict.py
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 | |
read_config_parameters_from_string
read_config_parameters_from_string(content=None, d=None, sep='.')
Reads configuration parameters from a YAML string and produces a flattened dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
The YAML string to parse. |
None
|
d
|
str
|
Optional YAML string to merge into the configuration. |
None
|
sep
|
str
|
The separation character used for flattening. Defaults to ".". |
'.'
|
Returns:
| Type | Description |
|---|---|
Dict
|
A flattened dictionary of the configuration parameters. |
Source code in src/cloudmesh/ai/common/flatdict.py
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 | |
github
Classes
GitHub
Bases: GitHubBase
Main entry point for GitHub CLI operations.
Source code in src/cloudmesh/ai/common/github.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
Functions
get_authenticated_user()
Returns the login of the currently authenticated user.
Source code in src/cloudmesh/ai/common/github.py
203 204 205 | |
org(org_name)
Returns a GitHubOrg object for the given organization name.
Source code in src/cloudmesh/ai/common/github.py
199 200 201 | |
repo(name)
Returns a GitHubRepo object for the given repository name.
Source code in src/cloudmesh/ai/common/github.py
191 192 193 | |
user(username)
Returns a GitHubUser object for the given username.
Source code in src/cloudmesh/ai/common/github.py
195 196 197 | |
GitHubBase
Base class for GitHub CLI operations.
Source code in src/cloudmesh/ai/common/github.py
13 14 15 16 17 18 19 20 21 22 23 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 | |
GitHubError
Bases: Exception
Exception raised for errors in GitHub CLI operations.
Source code in src/cloudmesh/ai/common/github.py
9 10 11 | |
GitHubOrg
Bases: GitHubBase
Operations for a GitHub organization.
Source code in src/cloudmesh/ai/common/github.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
Functions
get_info()
Gets organization information.
Source code in src/cloudmesh/ai/common/github.py
179 180 181 | |
get_public_repos_count()
Returns the number of public repositories in the organization.
Source code in src/cloudmesh/ai/common/github.py
183 184 185 186 | |
list_repos(limit=1000, json_fields=None)
Lists repositories for the organization.
Source code in src/cloudmesh/ai/common/github.py
170 171 172 173 174 175 176 177 | |
GitHubRepo
Bases: GitHubBase
Operations for a specific GitHub repository.
Source code in src/cloudmesh/ai/common/github.py
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 | |
Functions
get()
Gets general repository information.
Source code in src/cloudmesh/ai/common/github.py
65 66 67 | |
get_branches_count()
Returns the number of branches.
Source code in src/cloudmesh/ai/common/github.py
79 80 81 82 | |
get_contributors_count()
Returns the number of contributors.
Source code in src/cloudmesh/ai/common/github.py
94 95 96 97 | |
get_latest_commit_date()
Returns the date of the latest commit.
Source code in src/cloudmesh/ai/common/github.py
89 90 91 92 | |
get_latest_release()
Returns the latest release tag name.
Source code in src/cloudmesh/ai/common/github.py
99 100 101 102 103 104 | |
get_pull_requests_count()
Returns the number of open pull requests.
Source code in src/cloudmesh/ai/common/github.py
74 75 76 77 | |
get_size()
Returns the repository size in KB.
Source code in src/cloudmesh/ai/common/github.py
106 107 108 109 110 111 | |
get_tags_count()
Returns the number of tags.
Source code in src/cloudmesh/ai/common/github.py
84 85 86 87 | |
list_repos(limit=1000, json_fields=None)
Lists repositories for the owner of this repo (not typically used this way).
Source code in src/cloudmesh/ai/common/github.py
69 70 71 72 | |
GitHubUser
Bases: GitHubBase
Operations for a GitHub user.
Source code in src/cloudmesh/ai/common/github.py
113 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
Functions
get_orgs()
Lists organizations the user belongs to with pagination.
Source code in src/cloudmesh/ai/common/github.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
list_repos(limit=1000, json_fields=None, include_username=True)
Lists repositories for the user.
Source code in src/cloudmesh/ai/common/github.py
120 121 122 123 124 125 126 127 128 129 130 | |
io
Classes
BaseIO
Base class for I/O operations providing path expansion and file utilities.
Source code in src/cloudmesh/ai/common/io.py
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 | |
Functions
appendfile(path, content)
Appends content to a file.
Source code in src/cloudmesh/ai/common/io.py
66 67 68 69 70 71 72 73 74 75 76 | |
dump_yaml(path, data)
Safely writes a dictionary to a YAML file.
Source code in src/cloudmesh/ai/common/io.py
91 92 93 94 95 96 97 98 99 100 101 | |
expand_path(text, slashreplace=True)
Expands a path string by resolving '~', environment variables, and relative links.
Source code in src/cloudmesh/ai/common/io.py
35 36 37 38 39 40 41 42 43 | |
load_yaml(path)
Safely loads a YAML file from the given path.
Source code in src/cloudmesh/ai/common/io.py
78 79 80 81 82 83 84 85 86 87 88 89 | |
readfile(path)
Reads the content of a file.
Source code in src/cloudmesh/ai/common/io.py
45 46 47 48 49 50 51 52 53 | |
writefile(path, content)
Writes content to a file.
Source code in src/cloudmesh/ai/common/io.py
55 56 57 58 59 60 61 62 63 64 | |
Console
Bases: BaseIO, Console
Unified Console for cloudmesh-ai providing styled output, I/O, and table printing.
Source code in src/cloudmesh/ai/common/io.py
103 104 105 106 107 108 109 110 111 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 211 212 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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | |
Functions
ai_response(text, title='AI Response', style='cyan')
Displays a standardized AI response box.
Source code in src/cloudmesh/ai/common/io.py
309 310 311 312 313 314 315 316 317 318 319 | |
banner(label=None, txt=None, c='-', prefix='#', debug=True, color='blue', padding=False, figlet=False, font='big')
Prints a banner of the form with a frame of # around the txt
Source code in src/cloudmesh/ai/common/io.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 175 176 177 178 179 180 181 182 183 184 185 | |
bold(message)
Prints a message in bold.
Source code in src/cloudmesh/ai/common/io.py
130 131 132 | |
cprint(text, color, style='')
Helper to print with color.
Source code in src/cloudmesh/ai/common/io.py
187 188 189 | |
create_banner(title, content=None)
Creates a banner Panel without printing it.
Source code in src/cloudmesh/ai/common/io.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
error(message)
Prints an error message in red.
Source code in src/cloudmesh/ai/common/io.py
106 107 108 | |
info(message)
Prints an info message in magenta.
Source code in src/cloudmesh/ai/common/io.py
118 119 120 | |
left()
Moves the cursor to the beginning of the current line.
Source code in src/cloudmesh/ai/common/io.py
304 305 306 307 | |
msg(message)
Prints a message in blue.
Source code in src/cloudmesh/ai/common/io.py
114 115 116 | |
note(message)
Prints a note in cyan.
Source code in src/cloudmesh/ai/common/io.py
122 123 124 | |
ok(message)
Prints a success message in green.
Source code in src/cloudmesh/ai/common/io.py
126 127 128 | |
print_attributes(d, header=None, order=None, sort_keys=True, humanize=False, output='table')
Prints a dictionary of attributes in various formats.
Source code in src/cloudmesh/ai/common/io.py
204 205 206 207 208 209 210 211 212 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 245 246 247 248 249 250 251 | |
print_csv(d, order=None)
Prints a dictionary as CSV.
Source code in src/cloudmesh/ai/common/io.py
273 274 275 276 277 278 279 280 | |
print_error(message)
Prints a standardized error message.
Source code in src/cloudmesh/ai/common/io.py
347 348 349 | |
print_json(data)
Prints data as formatted JSON.
Source code in src/cloudmesh/ai/common/io.py
265 266 267 | |
print_markdown(text)
Renders and prints markdown text.
Source code in src/cloudmesh/ai/common/io.py
282 283 284 | |
print_status(message, style='yellow')
Prints a simple status message.
Source code in src/cloudmesh/ai/common/io.py
343 344 345 | |
print_success(message)
Prints a standardized success message.
Source code in src/cloudmesh/ai/common/io.py
351 352 353 | |
print_table(headers, data, title=None, expand=False)
Prints a formatted table. By default, it is compact (expand=False).
Source code in src/cloudmesh/ai/common/io.py
253 254 255 256 257 258 259 260 261 | |
print_yaml(data)
Prints data as formatted YAML.
Source code in src/cloudmesh/ai/common/io.py
269 270 271 | |
status(message)
Returns a status spinner context manager.
Source code in src/cloudmesh/ai/common/io.py
294 295 296 | |
telemetry_table(records, title='Telemetry Records')
Displays a standardized telemetry records table.
Source code in src/cloudmesh/ai/common/io.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | |
top(lines)
Moves the cursor up by the specified number of lines.
Source code in src/cloudmesh/ai/common/io.py
298 299 300 301 302 | |
warning(message)
Prints a warning message in yellow.
Source code in src/cloudmesh/ai/common/io.py
110 111 112 | |
ynchoice(message, default=True)
Asks a yes/no question and returns a boolean.
Source code in src/cloudmesh/ai/common/io.py
191 192 193 194 195 196 197 198 199 200 201 202 | |
Editor
Bases: BaseIO
Utility to open files in the default system editor.
Source code in src/cloudmesh/ai/common/io.py
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | |
Functions
appendfile
appendfile(path, content)
Standalone wrapper for console.appendfile.
Source code in src/cloudmesh/ai/common/io.py
417 418 419 | |
async_readfile
async
async_readfile(path)
Asynchronously reads the content of a file.
Source code in src/cloudmesh/ai/common/io.py
448 449 450 451 452 | |
async_writefile
async
async_writefile(path, content)
Asynchronously writes content to a file.
Source code in src/cloudmesh/ai/common/io.py
454 455 456 457 458 459 460 461 | |
banner
banner(label=None, txt=None, c='-', prefix='#', debug=True, color='blue', padding=False, figlet=False, font='big')
Standalone wrapper for console.banner.
Source code in src/cloudmesh/ai/common/io.py
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | |
create_benchmark_file
create_benchmark_file(path, n)
Creates a file of a given size in binary megabytes.
Source code in src/cloudmesh/ai/common/io.py
440 441 442 443 444 445 446 | |
create_benchmark_yaml
create_benchmark_yaml(path, n)
Creates a Cloudmesh service YAML test file.
Source code in src/cloudmesh/ai/common/io.py
433 434 435 436 437 438 | |
dump_yaml
dump_yaml(path, data)
Standalone wrapper for console.dump_yaml.
Source code in src/cloudmesh/ai/common/io.py
429 430 431 | |
load_yaml
load_yaml(path)
Standalone wrapper for console.load_yaml.
Source code in src/cloudmesh/ai/common/io.py
425 426 427 | |
path_expand
path_expand(text, slashreplace=True)
Standalone wrapper for console.expand_path.
Source code in src/cloudmesh/ai/common/io.py
421 422 423 | |
readfile
readfile(path)
Standalone wrapper for console.readfile.
Source code in src/cloudmesh/ai/common/io.py
409 410 411 | |
writefile
writefile(path, content)
Standalone wrapper for console.writefile.
Source code in src/cloudmesh/ai/common/io.py
413 414 415 | |
logging
Logging utility for cloudmesh-ai components. Provides centralized management of log directories, file naming, and logger configuration. Includes support for JSON logging, log rotation, and request tracing.
Classes
ContextFilter
Bases: Filter
Filter that injects the current context_id into the log record.
Source code in src/cloudmesh/ai/common/logging.py
36 37 38 39 40 41 42 43 44 45 46 47 48 | |
Functions
filter(record)
Injects the current context_id into the log record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
The log record to be filtered. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True to indicate the record should be logged. |
Source code in src/cloudmesh/ai/common/logging.py
38 39 40 41 42 43 44 45 46 47 48 | |
JsonFormatter
Bases: Formatter
Formatter that outputs log records in JSON format.
Source code in src/cloudmesh/ai/common/logging.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
Functions
format(record)
Formats the log record as a JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
LogRecord
|
The log record to format. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A JSON string representation of the log record. |
Source code in src/cloudmesh/ai/common/logging.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
Functions
ensure_log_dir
ensure_log_dir()
Ensures the log directory exists and returns it.
Returns:
| Type | Description |
|---|---|
Path
|
The Path object pointing to the ensured logs directory. |
Source code in src/cloudmesh/ai/common/logging.py
109 110 111 112 113 114 115 116 117 | |
get_context_id
get_context_id()
Retrieves the context ID for the current thread.
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The current context ID if set, otherwise None. |
Source code in src/cloudmesh/ai/common/logging.py
28 29 30 31 32 33 34 | |
get_log_dir
get_log_dir()
Returns the expanded path to the AI logs directory, using config if available.
Returns:
| Type | Description |
|---|---|
Path
|
The Path object pointing to the logs directory. |
Source code in src/cloudmesh/ai/common/logging.py
100 101 102 103 104 105 106 107 | |
get_log_file_path
get_log_file_path(script_name)
Generates a timestamped log file path for a given script.
Example: ~/.config/cloudmesh/ai/logs/test_20260412_124500.log
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
script_name
|
str
|
The name of the script for which to generate the log path. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
The Path object pointing to the generated log file. |
Source code in src/cloudmesh/ai/common/logging.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
get_logger
get_logger(script_name, level=None, json_format=None, max_bytes=None, backup_count=None)
Returns a configured logger instance for the given script.
Configures both a rotating file handler and a stream handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
script_name
|
str
|
Name of the logger/script. |
required |
level
|
Optional[int]
|
Logging level. |
None
|
json_format
|
Optional[bool]
|
If True, uses JSON formatting for logs. |
None
|
max_bytes
|
Optional[int]
|
Max size of a log file before rotation. |
None
|
backup_count
|
Optional[int]
|
Number of backup log files to keep. |
None
|
Returns:
| Type | Description |
|---|---|
Logger
|
A configured logging.Logger instance. |
Source code in src/cloudmesh/ai/common/logging.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 | |
load_logging_config
load_logging_config(config_path)
Loads logging configuration from a JSON file.
Example config: {"log_dir": "/var/log/cloudmesh", "level": "DEBUG", "json_format": true}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
Union[str, Path]
|
Path to the JSON configuration file. |
required |
Source code in src/cloudmesh/ai/common/logging.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
progress
progress(filename=None, status='ready', progress=0, pid=None, timestamp=False, stdout=True, stderr=True, append=None, **kwargs)
Creates a printed line of the form "# cloudmesh status=ready progress=0 pid=$$ time='2022-08-05 16:29:40.228901'".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
Optional[str]
|
Optional file to append the progress line to. |
None
|
status
|
str
|
The current status string. |
'ready'
|
progress
|
Union[int, str, float]
|
The current progress value (int, float, or string). |
0
|
pid
|
Optional[Union[int, str]]
|
Process ID. If None, it will be automatically detected. |
None
|
timestamp
|
bool
|
If True, includes a timestamp in the output. |
False
|
stdout
|
bool
|
If True, prints to stdout. |
True
|
stderr
|
bool
|
If True, prints to stderr. |
True
|
append
|
Optional[str]
|
Optional string to append to the end of the line. |
None
|
**kwargs
|
Additional key-value pairs to include in the progress line. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The formatted progress string. |
Source code in src/cloudmesh/ai/common/logging.py
202 203 204 205 206 207 208 209 210 211 212 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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | |
set_context_id
set_context_id(context_id)
Sets the context ID for the current thread to enable request tracing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context_id
|
str
|
The unique identifier for the current request or context. |
required |
Source code in src/cloudmesh/ai/common/logging.py
20 21 22 23 24 25 26 | |
logging_utils
Classes
ContextualLogger
Bases: LoggerAdapter
Logger adapter that adds contextual information to log messages.
This is particularly useful for parallel operations where you need to distinguish logs by host, user, or operation ID.
Source code in src/cloudmesh/ai/common/logging_utils.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
Functions
process(msg, kwargs=None)
Inject context into the log message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
Any
|
The log message. |
required |
kwargs
|
Optional[Dict[str, Any]]
|
The log arguments. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple
|
(msg, kwargs) |
Source code in src/cloudmesh/ai/common/logging_utils.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
Functions
get_contextual_logger
get_contextual_logger(name, initial_context=None)
Factory function to create a ContextualLogger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the logger. |
required |
initial_context
|
Optional[Dict[str, Any]]
|
Initial context to associate with the logger. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
ContextualLogger |
ContextualLogger
|
A logger adapter with the specified context. |
Source code in src/cloudmesh/ai/common/logging_utils.py
40 41 42 43 44 45 46 47 48 49 50 51 | |
remote
Remote execution utilities for cloudmesh-ai. Provides a unified interface for SSH command execution and file transfers.
Classes
RemoteExecutor
A unified executor for remote operations via SSH. Supports command execution, file uploads, and direct remote file writing.
Source code in src/cloudmesh/ai/common/remote.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
Functions
__enter__()
Establishes the SSH connection.
Returns:
| Type | Description |
|---|---|
|
The RemoteExecutor instance. |
Raises:
| Type | Description |
|---|---|
SSHException
|
If the connection fails. |
Source code in src/cloudmesh/ai/common/remote.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
__exit__(exc_type, exc_val, exc_tb)
Closes the SSH connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc_type
|
The type of the exception that occurred. |
required | |
exc_val
|
The instance of the exception that occurred. |
required | |
exc_tb
|
The traceback of the exception that occurred. |
required |
Source code in src/cloudmesh/ai/common/remote.py
51 52 53 54 55 56 57 58 59 60 | |
__init__(host, username=None, key_filename=None)
Initialize the RemoteExecutor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
The hostname or IP address of the remote host. |
required |
username
|
Optional[str]
|
The SSH username. Defaults to None. |
None
|
key_filename
|
Optional[str]
|
Path to the private key file. Defaults to None. |
None
|
Source code in src/cloudmesh/ai/common/remote.py
16 17 18 19 20 21 22 23 24 25 26 27 | |
download(remote_path, local_path)
Downloads a remote file to the local host using SFTP.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
remote_path
|
str
|
Path to the file on the remote host. |
required |
local_path
|
str
|
Path on the local host where the file should be saved. |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the executor is not used as a context manager. |
IOError
|
If the download fails. |
Source code in src/cloudmesh/ai/common/remote.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
execute(command, timeout=60)
Executes a command on the remote host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
The shell command to execute. |
required |
timeout
|
int
|
Command timeout in seconds. |
60
|
Returns:
| Type | Description |
|---|---|
Tuple[int, str, str]
|
A tuple of (exit_status, stdout, stderr). |
Source code in src/cloudmesh/ai/common/remote.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
upload(local_path, remote_path)
Uploads a local file to the remote host using SFTP.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_path
|
str
|
Path to the local file to upload. |
required |
remote_path
|
str
|
Path on the remote host where the file should be saved. |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the executor is not used as a context manager. |
IOError
|
If the upload fails. |
Source code in src/cloudmesh/ai/common/remote.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
write_remote_file(content, remote_path)
Writes a string directly to a remote file.
Useful for creating scripts or config files on the fly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
The string content to write. |
required |
remote_path
|
str
|
Path on the remote host where the file should be created. |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the executor is not used as a context manager. |
IOError
|
If the write operation fails. |
Source code in src/cloudmesh/ai/common/remote.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
security
Classes
BaseSecurity
Base class for security and privilege escalation utilities.
Source code in src/cloudmesh/ai/common/security.py
19 20 21 22 23 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 | |
Functions
is_root()
Check if the current process is running as root.
Source code in src/cloudmesh/ai/common/security.py
25 26 27 | |
secure_write(path, content, mode=384)
Write content to a file with restricted permissions (e.g., for private keys).
Source code in src/cloudmesh/ai/common/security.py
81 82 83 84 85 86 87 88 89 90 | |
sudo_execute_local(command, input_data=None)
Execute a command locally with sudo.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
Union[str, List[str]]
|
The command to execute. |
required |
input_data
|
Optional[str]
|
Optional data to pass to stdin (e.g., password). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The output of the command. |
Raises:
| Type | Description |
|---|---|
SecurityAuthError
|
If sudo authentication fails. |
Source code in src/cloudmesh/ai/common/security.py
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 | |
verify_file_permissions(path, readable=True, writable=False)
Verify if the current user has the required permissions for a file.
Source code in src/cloudmesh/ai/common/security.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
Functions
ssh
Classes
SSHConfig
Bases: SSHBase
Managing the SSH config file (usually ~/.ssh/config).
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
22 23 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 211 212 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 245 246 247 248 249 250 251 | |
Functions
__str__()
The string representation of the config as JSON.
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
61 62 63 64 65 66 67 68 69 70 | |
delete(name)
Removes a host entry from the SSH config file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
the name of the host to remove. |
required |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
execute(name, command, use_pty=False)
Execute the command on the named host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
the name of the host in config. |
required |
command
|
str
|
the command to be executed. |
required |
use_pty
|
bool
|
whether to allocate a pseudo-terminal. |
False
|
Returns:
| Type | Description |
|---|---|
Union[CommandResult, str]
|
Union[CommandResult, str]: CommandResult for remote, stdout for local. |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
execute_parallel(hosts, command)
Execute the same command on multiple hosts in parallel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hosts
|
List[str]
|
list of host names. |
required |
command
|
str
|
the command to execute. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, CommandResult]
|
Dict[str, CommandResult]: mapping of host to its result. |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
generate(host='uva', hostname='login.hpc.virginia.edu', identity='~/.ssh/id_rsa.pub', user=None, verbose=False)
Adds a host to the config file with given parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
the alias for the host. |
'uva'
|
hostname
|
str
|
the actual hostname or IP. |
'login.hpc.virginia.edu'
|
identity
|
str
|
the path to the identity file. |
'~/.ssh/id_rsa.pub'
|
user
|
Optional[str]
|
the username for the host. |
None
|
verbose
|
bool
|
prints debug messages. |
False
|
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
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 245 246 247 248 249 250 251 | |
hostname(host)
Returns the actual HostName for the given host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
the host identifier to look up. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The actual hostname or IP address associated with the host identifier. |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
list()
List the hosts defined in the config file.
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: list of host names. |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
51 52 53 54 55 56 57 58 59 | |
load()
Parse the SSH config file using sshconf.
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
43 44 45 46 47 48 49 | |
local(command)
Execute the command on the localhost.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
the command to execute. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
the output of the command. |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
142 143 144 145 146 147 148 149 150 151 | |
login(name)
Login to the host defined in .ssh/config by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
the name of the host as defined in the config file. |
required |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
72 73 74 75 76 77 78 79 80 81 82 | |
names()
The names defined in the SSH config.
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: the host names. |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
35 36 37 38 39 40 41 | |
sudo_execute(name, command, use_pty=False)
Execute the command on the named host with sudo.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
the name of the host in config. |
required |
command
|
str
|
the command to be executed. |
required |
use_pty
|
bool
|
whether to allocate a pseudo-terminal. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
CommandResult |
CommandResult
|
structured result of the execution. |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
104 105 106 107 108 109 110 111 112 113 114 115 116 | |
username(host)
Returns the username for a given host, falling back to global config or local user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
the hostname. |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Optional[str]: the username associated with the host, the global user, |
Optional[str]
|
or the local system user. |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
yaml()
Returns the parsed SSH configuration in YAML format.
Returns:
| Type | Description |
|---|---|
str
|
A YAML string representation of the parsed hosts dictionary. |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
188 189 190 191 192 193 194 195 196 197 198 199 200 | |
Tunnel
Manages an SSH tunnel for port forwarding.
Source code in src/cloudmesh/ai/common/ssh/tunnel.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 | |
Functions
is_active()
Checks if the tunnel process is still running.
Source code in src/cloudmesh/ai/common/ssh/tunnel.py
60 61 62 | |
start()
Starts the SSH tunnel in the background.
Source code in src/cloudmesh/ai/common/ssh/tunnel.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
stop()
Stops the SSH tunnel process.
Source code in src/cloudmesh/ai/common/ssh/tunnel.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
Modules
authorized_keys
Classes
Bases: SSHBase
Class to manage authorized keys.
Source code in src/cloudmesh/ai/common/ssh/authorized_keys.py
19 20 21 22 23 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 | |
add(pubkey)
Add a public key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pubkey
|
str
|
the public key string. |
required |
Source code in src/cloudmesh/ai/common/ssh/authorized_keys.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
get_fingerprint_from_public_key(pubkey)
Generate the fingerprint of a public key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pubkey
|
str
|
the value of the public key |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
fingerprint |
Source code in src/cloudmesh/ai/common/ssh/authorized_keys.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
load(path)
Load the keys from a path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Path]
|
the filename (path) in which we find the keys |
required |
Source code in src/cloudmesh/ai/common/ssh/authorized_keys.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
remove(fingerprint)
Removes the public key by its fingerprint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fingerprint
|
str
|
the fingerprint of the public key to remove. |
required |
Source code in src/cloudmesh/ai/common/ssh/authorized_keys.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
Modules
base
Classes
dataclass
Structured result of a remote command execution.
Source code in src/cloudmesh/ai/common/ssh/base.py
18 19 20 21 22 23 24 25 | |
Base class for SSH utilities providing shared execution and path logic.
Source code in src/cloudmesh/ai/common/ssh/base.py
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
get(remote_path, local_path, host, user=None)
Download a remote file to a local path.
Source code in src/cloudmesh/ai/common/ssh/base.py
139 140 141 142 143 144 145 146 147 148 | |
put(local_path, remote_path, host, user=None)
Upload a local file to a remote host.
Source code in src/cloudmesh/ai/common/ssh/base.py
128 129 130 131 132 133 134 135 136 137 | |
resolve_path(path)
Expand and resolve a path.
Source code in src/cloudmesh/ai/common/ssh/base.py
60 61 62 | |
Modules
encryption
Classes
Bases: SSHBase
Utility to encrypt and decrypt files using RSA keys via OpenSSL.
Source code in src/cloudmesh/ai/common/ssh/encryption.py
16 17 18 19 20 21 22 23 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 | |
decrypt(filename=None)
Decrypt the secret file using the private key.
Source code in src/cloudmesh/ai/common/ssh/encryption.py
61 62 63 64 65 66 67 68 69 70 71 72 | |
encrypt()
Encrypt the input file using the public PEM key.
Source code in src/cloudmesh/ai/common/ssh/encryption.py
48 49 50 51 52 53 54 55 56 57 58 59 | |
pem_cat()
Print the PEM public key to stdout.
Source code in src/cloudmesh/ai/common/ssh/encryption.py
43 44 45 46 | |
pem_create()
Create a PEM public key from the private key.
Source code in src/cloudmesh/ai/common/ssh/encryption.py
33 34 35 36 37 38 39 40 41 | |
Modules
ssh_config
Classes
Bases: SSHBase
Managing the SSH config file (usually ~/.ssh/config).
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
22 23 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 211 212 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 245 246 247 248 249 250 251 | |
__str__()
The string representation of the config as JSON.
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
61 62 63 64 65 66 67 68 69 70 | |
delete(name)
Removes a host entry from the SSH config file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
the name of the host to remove. |
required |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
execute(name, command, use_pty=False)
Execute the command on the named host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
the name of the host in config. |
required |
command
|
str
|
the command to be executed. |
required |
use_pty
|
bool
|
whether to allocate a pseudo-terminal. |
False
|
Returns:
| Type | Description |
|---|---|
Union[CommandResult, str]
|
Union[CommandResult, str]: CommandResult for remote, stdout for local. |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
execute_parallel(hosts, command)
Execute the same command on multiple hosts in parallel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hosts
|
List[str]
|
list of host names. |
required |
command
|
str
|
the command to execute. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, CommandResult]
|
Dict[str, CommandResult]: mapping of host to its result. |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
generate(host='uva', hostname='login.hpc.virginia.edu', identity='~/.ssh/id_rsa.pub', user=None, verbose=False)
Adds a host to the config file with given parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
the alias for the host. |
'uva'
|
hostname
|
str
|
the actual hostname or IP. |
'login.hpc.virginia.edu'
|
identity
|
str
|
the path to the identity file. |
'~/.ssh/id_rsa.pub'
|
user
|
Optional[str]
|
the username for the host. |
None
|
verbose
|
bool
|
prints debug messages. |
False
|
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
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 245 246 247 248 249 250 251 | |
hostname(host)
Returns the actual HostName for the given host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
the host identifier to look up. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The actual hostname or IP address associated with the host identifier. |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
list()
List the hosts defined in the config file.
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: list of host names. |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
51 52 53 54 55 56 57 58 59 | |
load()
Parse the SSH config file using sshconf.
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
43 44 45 46 47 48 49 | |
local(command)
Execute the command on the localhost.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
the command to execute. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
the output of the command. |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
142 143 144 145 146 147 148 149 150 151 | |
login(name)
Login to the host defined in .ssh/config by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
the name of the host as defined in the config file. |
required |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
72 73 74 75 76 77 78 79 80 81 82 | |
names()
The names defined in the SSH config.
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: the host names. |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
35 36 37 38 39 40 41 | |
sudo_execute(name, command, use_pty=False)
Execute the command on the named host with sudo.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
the name of the host in config. |
required |
command
|
str
|
the command to be executed. |
required |
use_pty
|
bool
|
whether to allocate a pseudo-terminal. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
CommandResult |
CommandResult
|
structured result of the execution. |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
104 105 106 107 108 109 110 111 112 113 114 115 116 | |
username(host)
Returns the username for a given host, falling back to global config or local user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
the hostname. |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Optional[str]: the username associated with the host, the global user, |
Optional[str]
|
or the local system user. |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
yaml()
Returns the parsed SSH configuration in YAML format.
Returns:
| Type | Description |
|---|---|
str
|
A YAML string representation of the parsed hosts dictionary. |
Source code in src/cloudmesh/ai/common/ssh/ssh_config.py
188 189 190 191 192 193 194 195 196 197 198 199 200 | |
Modules
transfer
Classes
Bases: SSHBase
Utility to transfer files to and from remote hosts using Fabric/SFTP.
Source code in src/cloudmesh/ai/common/ssh/transfer.py
16 17 18 19 20 21 22 23 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 | |
download(remote_path, local_path, host, user=None)
Download a file from a remote host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
remote_path
|
Union[str, Path]
|
path to the file on the remote host. |
required |
local_path
|
Union[str, Path]
|
path to the destination on the local system. |
required |
host
|
str
|
the remote host. |
required |
user
|
Optional[str]
|
optional username. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if successful, False otherwise. |
Source code in src/cloudmesh/ai/common/ssh/transfer.py
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 | |
upload(local_path, remote_path, host, user=None)
Upload a file to a remote host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_path
|
Union[str, Path]
|
path to the local file. |
required |
remote_path
|
Union[str, Path]
|
path to the destination on the remote host. |
required |
host
|
str
|
the remote host. |
required |
user
|
Optional[str]
|
optional username. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if successful, False otherwise. |
Source code in src/cloudmesh/ai/common/ssh/transfer.py
19 20 21 22 23 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 | |
Modules
tunnel
Classes
Manages an SSH tunnel for port forwarding.
Source code in src/cloudmesh/ai/common/ssh/tunnel.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 | |
is_active()
Checks if the tunnel process is still running.
Source code in src/cloudmesh/ai/common/ssh/tunnel.py
60 61 62 | |
start()
Starts the SSH tunnel in the background.
Source code in src/cloudmesh/ai/common/ssh/tunnel.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
stop()
Stops the SSH tunnel process.
Source code in src/cloudmesh/ai/common/ssh/tunnel.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
stopwatch
Class for starting and stopping named timers. Provides a simple way to benchmark code execution and track events.
Classes
StopWatch
A class to measure times between events.
Source code in src/cloudmesh/ai/common/stopwatch.py
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 211 212 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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |
Functions
classmethod
benchmark(sysinfo=True, tag=None, filename=None)
Prints out all timers in a convenient benchmark format for the current thread.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sysinfo
|
bool
|
If True, includes system information in the output. Defaults to True. |
True
|
tag
|
Optional[str]
|
Optional tag to include in the benchmark. |
None
|
filename
|
Optional[str]
|
Optional file path to write the benchmark results to. |
None
|
Source code in src/cloudmesh/ai/common/stopwatch.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |
classmethod
clear()
Clears all timers in the current thread.
Source code in src/cloudmesh/ai/common/stopwatch.py
289 290 291 292 293 294 295 296 297 298 299 | |
classmethod
debug_mode(value=True)
Enables or disables debug mode for timers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
bool
|
True to enable debug mode, False to disable it. Defaults to True. |
True
|
Source code in src/cloudmesh/ai/common/stopwatch.py
71 72 73 74 75 76 77 78 79 80 81 82 | |
classmethod
event(name, msg=None, values=None)
Adds an event with a given name, where start and stop is the same time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the event. |
required |
msg
|
Optional[str]
|
Optional message to associate with the event. |
None
|
values
|
Any
|
Optional values to associate with the event. |
None
|
Source code in src/cloudmesh/ai/common/stopwatch.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
classmethod
get(name, digits=4)
Returns the elapsed time of the timer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the timer. |
required |
digits
|
int
|
Number of decimal places to round to. Defaults to 4. |
4
|
Returns:
| Type | Description |
|---|---|
Union[float, str, None]
|
The elapsed time as a float, "undefined" if not found, or None on error. |
Source code in src/cloudmesh/ai/common/stopwatch.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
classmethod
get_message(name)
Returns the message of the timer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the timer. |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The message associated with the timer, or None if not set. |
Source code in src/cloudmesh/ai/common/stopwatch.py
144 145 146 147 148 149 150 151 152 153 154 | |
classmethod
get_status(name)
Returns the status of the timer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the timer. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The status value associated with the timer. |
Source code in src/cloudmesh/ai/common/stopwatch.py
236 237 238 239 240 241 242 243 244 245 246 | |
classmethod
keys()
Returns the names of the timers.
Returns:
| Type | Description |
|---|---|
List[str]
|
A list of timer names. |
Source code in src/cloudmesh/ai/common/stopwatch.py
123 124 125 126 127 128 129 130 | |
classmethod
message(name, value)
Records a message for the timer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the timer. |
required |
value
|
str
|
The message string to record. |
required |
Source code in src/cloudmesh/ai/common/stopwatch.py
156 157 158 159 160 161 162 163 164 | |
classmethod
print(label, name)
Prints a timer with a label.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label to print before the timer value. |
required |
name
|
str
|
The name of the timer to print. |
required |
Source code in src/cloudmesh/ai/common/stopwatch.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
classmethod
start(name=None, values=None)
Starts a timer with the given name.
If name is None, it is automatically detected from the caller.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
Optional[str]
|
The name of the timer. |
None
|
values
|
Any
|
Optional values to associate with the timer. |
None
|
Source code in src/cloudmesh/ai/common/stopwatch.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
classmethod
status(name, value)
Records a status for the timer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the timer. |
required |
value
|
Any
|
The status value to record. |
required |
Source code in src/cloudmesh/ai/common/stopwatch.py
132 133 134 135 136 137 138 139 140 141 142 | |
classmethod
stop(name=None, state=True, values=None)
Stops the timer with a given name.
If name is None, it is automatically detected from the caller.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
Optional[str]
|
The name of the timer. |
None
|
state
|
Any
|
The final state of the timer (e.g., True for success). |
True
|
values
|
Any
|
Optional values to associate with the timer. |
None
|
Source code in src/cloudmesh/ai/common/stopwatch.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
classmethod
sum(name, digits=4)
Returns the sum of the timer if used multiple times.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the timer. |
required |
digits
|
int
|
Number of decimal places to round to. Defaults to 4. |
4
|
Returns:
| Type | Description |
|---|---|
Union[float, str, None]
|
The total elapsed time as a float, "undefined" if not found, or None on error. |
Source code in src/cloudmesh/ai/common/stopwatch.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | |
classmethod
timer(name=None, values=None)
Context manager to time a block of code.
Example
with StopWatch.timer("my_operation"): do_work()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
Optional[str]
|
The name of the timer. If None, it is automatically detected. |
None
|
values
|
Any
|
Optional values to associate with the timer. |
None
|
Source code in src/cloudmesh/ai/common/stopwatch.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
StopWatchBlock
Context manager for StopWatch.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
The name of the timer. |
|
data |
Optional data to associate with the timer. |
|
log |
The log destination (file path or stream). |
|
is_file |
Boolean indicating if the log is a file. |
|
start_time |
The time when the block was entered. |
Source code in src/cloudmesh/ai/common/stopwatch.py
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | |
Functions
__enter__()
Starts the timer and returns the current elapsed time.
Returns:
| Type | Description |
|---|---|
|
The current elapsed time of the timer. |
Source code in src/cloudmesh/ai/common/stopwatch.py
379 380 381 382 383 384 385 386 387 | |
__exit__(exc_type, exc_val, exc_tb)
Stops the timer and logs the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc_type
|
The type of the exception that occurred. |
required | |
exc_val
|
The instance of the exception that occurred. |
required | |
exc_tb
|
The traceback of the exception that occurred. |
required |
Source code in src/cloudmesh/ai/common/stopwatch.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | |
__init__(name, data=None, log=sys.stdout, mode='w')
Initializes the StopWatchBlock.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the timer. |
required |
data
|
Any
|
Optional data to associate with the timer. |
None
|
log
|
The log destination. Defaults to sys.stdout. |
stdout
|
|
mode
|
str
|
The mode to open the log file in. Defaults to "w". |
'w'
|
Source code in src/cloudmesh/ai/common/stopwatch.py
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | |
Functions
benchmark
benchmark(func)
Decorator to benchmark a function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
The function to be benchmarked. |
required |
Returns:
| Type | Description |
|---|---|
|
A wrapper function that starts and stops a StopWatch timer. |
Source code in src/cloudmesh/ai/common/stopwatch.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | |
rename
rename(newname)
Decorator to rename a function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
newname
|
The new name to assign to the function. |
required |
Returns:
| Type | Description |
|---|---|
|
A decorator function. |
Source code in src/cloudmesh/ai/common/stopwatch.py
16 17 18 19 20 21 22 23 24 25 26 27 28 | |
Modules
sudo
Classes
Sudo
A utility class for executing commands with sudo privileges and performing file operations.
Methods:
| Name | Description |
|---|---|
password |
"): Prompt the user for the sudo password. |
execute |
Execute the specified command with sudo. Args: command (list or str): The command to run. decode (bool, optional): If True, decode the output from bytes to ASCII. debug (bool, optional): If True, print command execution details. msg (str, optional): Message to print before executing the command. Returns: subprocess.CompletedProcess: The result of the command execution. |
readfile |
Read the content of the file with sudo privileges and return the result. Args: filename (str): The filename. split (bool, optional): If True, return a list of lines. trim (bool, optional): If True, trim trailing whitespace. decode (bool, optional): If True, decode the output from bytes to ASCII. Returns: str or list: The content of the file. |
writefile |
Write the content to the specified file with sudo privileges. Args: filename (str): The filename. content (str): The content to write. append (bool, optional): If True, append the content at the end; otherwise, overwrite the file. Returns: str: The output created by the write process. |
Source code in src/cloudmesh/ai/common/sudo.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
Functions
staticmethod
execute(command, decode='True', debug=False, msg=None)
Execute the specified command with sudo.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
list or str
|
The command to run. |
required |
decode
|
bool
|
If True, decode the output from bytes to ASCII. |
'True'
|
debug
|
bool
|
If True, print command execution details. |
False
|
msg
|
str
|
Message to print before executing the command. |
None
|
Returns:
| Type | Description |
|---|---|
|
subprocess.CompletedProcess: The result of the command execution. |
Source code in src/cloudmesh/ai/common/sudo.py
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 | |
staticmethod
expire()
Expires the password
Source code in src/cloudmesh/ai/common/sudo.py
64 65 66 67 | |
staticmethod
password(msg='sudo password: ')
Prompt the user for the sudo password.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
str
|
The message to display when prompting for the password. |
'sudo password: '
|
Source code in src/cloudmesh/ai/common/sudo.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
staticmethod
readfile(filename, split=False, trim=False, decode=True)
Read the content of the file with sudo privileges and return the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The filename. |
required |
split
|
bool
|
If True, return a list of lines. |
False
|
trim
|
bool
|
If True, trim trailing whitespace. |
False
|
decode
|
bool
|
If True, decode the output from bytes to ASCII. |
True
|
Returns:
| Type | Description |
|---|---|
|
str or list: The content of the file. |
Source code in src/cloudmesh/ai/common/sudo.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
staticmethod
writefile(filename, content, append=False)
Write the content to the specified file with sudo privileges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The filename. |
required |
content
|
str
|
The content to write. |
required |
append
|
bool
|
If True, append the content at the end; otherwise, overwrite the file. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
The output created by the write process. |
Source code in src/cloudmesh/ai/common/sudo.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
Functions
sys
System utility functions for cloudmesh-ai. Provides tools for OS detection, hardware information retrieval, and real-time system metrics collection.
Functions
get_container_info
get_container_info()
Detects if the system is running inside a container (Docker, Kubernetes).
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dictionary with container type and metadata. |
Source code in src/cloudmesh/ai/common/sys.py
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | |
get_cpu_description
get_cpu_description()
Safely retrieves the CPU model name across platforms.
Returns:
| Type | Description |
|---|---|
str
|
The CPU model description as a string. |
Source code in src/cloudmesh/ai/common/sys.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
get_cpu_metrics
get_cpu_metrics()
Retrieves real-time CPU utilization and load averages.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dictionary containing 'cpu.utilization_overall', 'cpu.utilization_per_core', |
Dict[str, Any]
|
and 'cpu.load_avg'. |
Source code in src/cloudmesh/ai/common/sys.py
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | |
get_disk_metrics
get_disk_metrics()
Retrieves real-time disk usage and I/O statistics for the root partition.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dictionary containing total, used, and free space, as well as |
Dict[str, Any]
|
read/write counts and bytes. |
Source code in src/cloudmesh/ai/common/sys.py
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 | |
get_disk_read_speed
get_disk_read_speed(path, size_mb=100)
Measures the read speed of a file to profile disk throughput.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the file to read. |
required |
size_mb
|
int
|
Amount of data to read in megabytes. Defaults to 100. |
100
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The read speed as a string (e.g., "150.25 MB/s"), or None if an error occurs. |
Source code in src/cloudmesh/ai/common/sys.py
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | |
get_gpu_info
get_gpu_info()
Attempts to retrieve GPU information from multiple vendors.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dictionary with VRAM, temperature, and power draw if available, |
Dict[str, Any]
|
otherwise {'gpu.present': False}. |
Source code in src/cloudmesh/ai/common/sys.py
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
get_memory_metrics
get_memory_metrics()
Retrieves detailed real-time memory and swap usage.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dictionary containing utilization percentages and human-readable |
Dict[str, Any]
|
sizes for available, used, and swap memory. |
Source code in src/cloudmesh/ai/common/sys.py
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | |
get_network_info
get_network_info()
Detects high-speed network interfaces (InfiniBand, RoCE) on Linux.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dictionary indicating if high-speed networking is present and |
Dict[str, Any]
|
listing the interfaces. |
Source code in src/cloudmesh/ai/common/sys.py
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | |
get_numa_info
get_numa_info()
Detects NUMA nodes on Linux systems.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dictionary containing NUMA presence and node details. |
Source code in src/cloudmesh/ai/common/sys.py
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | |
get_platform
get_platform()
Returns a simplified string representing the OS platform.
Returns:
| Type | Description |
|---|---|
str
|
A string representing the platform (e.g., 'macos', 'windows', 'raspberry'). |
Source code in src/cloudmesh/ai/common/sys.py
102 103 104 105 106 107 108 109 110 111 112 113 114 | |
get_thermal_info
get_thermal_info()
Retrieves system thermal information across platforms.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dictionary with CPU and GPU temperatures. |
Source code in src/cloudmesh/ai/common/sys.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | |
has_window_manager
has_window_manager()
Checks if a GUI environment is likely available.
Returns:
| Type | Description |
|---|---|
bool
|
True if a window manager is likely present, False otherwise. |
Source code in src/cloudmesh/ai/common/sys.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
os_is_linux
os_is_linux()
Checks if the os is linux (excluding Raspberry Pi).
Returns:
| Type | Description |
|---|---|
bool
|
True if the OS is Linux and not Raspbian, False otherwise. |
Source code in src/cloudmesh/ai/common/sys.py
53 54 55 56 57 58 59 | |
os_is_mac
os_is_mac()
Checks if the operating system is macOS.
Returns:
| Type | Description |
|---|---|
bool
|
True if the OS is macOS, False otherwise. |
Source code in src/cloudmesh/ai/common/sys.py
45 46 47 48 49 50 51 | |
os_is_pi
os_is_pi()
Checks if the os is Raspberry OS.
Returns:
| Type | Description |
|---|---|
bool
|
True if the OS is Raspbian, False otherwise. |
Source code in src/cloudmesh/ai/common/sys.py
61 62 63 64 65 66 67 | |
os_is_windows
os_is_windows()
Checks if the operating system is Windows.
Returns:
| Type | Description |
|---|---|
bool
|
True if the OS is Windows, False otherwise. |
Source code in src/cloudmesh/ai/common/sys.py
37 38 39 40 41 42 43 | |
resolve_package_path
resolve_package_path(anchor, relative_path)
Resolves a path relative to a given anchor file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
anchor
|
str
|
The anchor file path. |
required |
relative_path
|
str
|
The relative path to resolve. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
The resolved absolute Path. |
Source code in src/cloudmesh/ai/common/sys.py
488 489 490 491 492 493 494 495 496 497 498 | |
sys_user
sys_user()
Returns the current username with Colab and Root detection.
Returns:
| Type | Description |
|---|---|
str
|
The detected username as a string. |
Source code in src/cloudmesh/ai/common/sys.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
systeminfo
systeminfo(info=None, user=None, node=None, realtime=False)
Collects comprehensive system metadata into a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
info
|
Optional[Dict[str, Any]]
|
Optional dictionary of additional information to merge into the result. |
None
|
user
|
Optional[str]
|
Optional override for the current system user. |
None
|
node
|
Optional[str]
|
Optional override for the system node name. |
None
|
realtime
|
bool
|
If True, includes real-time CPU, memory, and disk utilization metrics. |
False
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dictionary containing system hardware, OS, and (optionally) real-time performance data. |
Source code in src/cloudmesh/ai/common/sys.py
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 | |
telemetry
Telemetry utility for cloudmesh-ai components. Provides a standardized way to emit structured performance and status data.
Classes
AsyncTelemetry
Bases: Telemetry
Asynchronous version of Telemetry. Prevents telemetry I/O from blocking the main execution thread by offloading backend emission to a thread pool.
Source code in src/cloudmesh/ai/common/telemetry.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | |
Functions
async
complete(metrics=None, message='Command completed successfully', **kwargs)
Async helper to emit a 'completed' status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metrics
|
Optional[Dict[str, Any]]
|
A dictionary of final KPIs and measurements. |
None
|
message
|
Optional[str]
|
An optional human-readable message. |
'Command completed successfully'
|
**kwargs
|
Any
|
Additional metrics to include in the record. |
{}
|
Source code in src/cloudmesh/ai/common/telemetry.py
364 365 366 367 368 369 370 371 372 373 | |
async
emit(status, metrics=None, message=None, stdout=False, **kwargs)
Asynchronously emits a structured telemetry record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status
|
str
|
The current status of the command. |
required |
metrics
|
Optional[Dict[str, Any]]
|
A dictionary of KPIs. |
None
|
message
|
Optional[str]
|
An optional human-readable message. |
None
|
stdout
|
bool
|
If True, prints the JSON record to stdout. |
False
|
Source code in src/cloudmesh/ai/common/telemetry.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
async
fail(error, metrics=None, **kwargs)
Async helper to emit a 'failed' status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error
|
str
|
The error message to record. |
required |
metrics
|
Optional[Dict[str, Any]]
|
A dictionary of KPIs at the time of failure. |
None
|
**kwargs
|
Any
|
Additional metrics to include in the record. |
{}
|
Source code in src/cloudmesh/ai/common/telemetry.py
375 376 377 378 379 380 381 382 383 384 | |
async
start(message=None, **kwargs)
Async helper to emit a 'started' status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
Optional[str]
|
Optional message for the start event. |
None
|
**kwargs
|
Any
|
Additional metrics to include in the record. |
{}
|
Source code in src/cloudmesh/ai/common/telemetry.py
354 355 356 357 358 359 360 361 362 | |
async
track(message=None, metrics=None)
Async context manager to automatically track the start and completion of a task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
Optional[str]
|
Optional message for the start event. |
None
|
metrics
|
Optional[Dict[str, Any]]
|
Initial metrics to include. |
None
|
Source code in src/cloudmesh/ai/common/telemetry.py
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | |
JSONFileBackend
Writes telemetry records to a JSON Lines (JSONL) file. Each record is stored as a single JSON object per line.
Source code in src/cloudmesh/ai/common/telemetry.py
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 | |
Functions
__init__(path)
Initialize the JSONL backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Path]
|
Path to the output file. |
required |
Source code in src/cloudmesh/ai/common/telemetry.py
38 39 40 41 42 43 44 45 | |
emit(record)
Append a telemetry record to the JSONL file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
Dict[str, Any]
|
The telemetry data dictionary to export. |
required |
Source code in src/cloudmesh/ai/common/telemetry.py
47 48 49 50 51 52 53 54 55 56 57 58 | |
SQLiteBackend
Writes telemetry records to a SQLite database. Provides structured storage for easier querying and analysis.
Source code in src/cloudmesh/ai/common/telemetry.py
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 | |
Functions
__init__(db_path='telemetry.db')
Initialize the SQLite backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_path
|
Union[str, Path]
|
Path to the SQLite database file. |
'telemetry.db'
|
Source code in src/cloudmesh/ai/common/telemetry.py
65 66 67 68 69 70 71 72 73 | |
emit(record)
Insert a telemetry record into the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
Dict[str, Any]
|
The telemetry data dictionary to export. |
required |
Source code in src/cloudmesh/ai/common/telemetry.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
Telemetry
Handles structured telemetry emission for AI commands. Supports multiple backends for flexible data export.
Source code in src/cloudmesh/ai/common/telemetry.py
159 160 161 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 211 212 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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
Functions
__init__(command_name, telemetry_file=None, backends=None)
Initialize the Telemetry collector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command_name
|
str
|
Name of the command emitting telemetry. |
required |
telemetry_file
|
Optional[Union[str, Path]]
|
Backward compatibility: path to a JSONL file. |
None
|
backends
|
Optional[List[TelemetryBackend]]
|
List of TelemetryBackend implementations to use. |
None
|
Source code in src/cloudmesh/ai/common/telemetry.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
complete(metrics=None, message='Command completed successfully', **kwargs)
Helper to emit a 'completed' status with final metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metrics
|
Optional[Dict[str, Any]]
|
A dictionary of final KPIs and measurements. |
None
|
message
|
Optional[str]
|
An optional human-readable message. |
'Command completed successfully'
|
**kwargs
|
Any
|
Additional metrics to include in the record. |
{}
|
Source code in src/cloudmesh/ai/common/telemetry.py
254 255 256 257 258 259 260 261 262 263 | |
emit(status, metrics=None, message=None, stdout=False, **kwargs)
Emits a structured telemetry record to all configured backends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status
|
str
|
The current status of the command (e.g., 'started', 'completed', 'failed'). |
required |
metrics
|
Optional[Dict[str, Any]]
|
A dictionary of KPIs and measurements. |
None
|
message
|
Optional[str]
|
An optional human-readable message. |
None
|
stdout
|
bool
|
If True, prints the JSON record to stdout. |
False
|
Source code in src/cloudmesh/ai/common/telemetry.py
201 202 203 204 205 206 207 208 209 210 211 212 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 | |
fail(error, metrics=None, **kwargs)
Helper to emit a 'failed' status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error
|
str
|
The error message to record. |
required |
metrics
|
Optional[Dict[str, Any]]
|
A dictionary of KPIs at the time of failure. |
None
|
**kwargs
|
Any
|
Additional metrics to include in the record. |
{}
|
Source code in src/cloudmesh/ai/common/telemetry.py
265 266 267 268 269 270 271 272 273 274 | |
start(message=None, **kwargs)
Helper to emit a 'started' status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
Optional[str]
|
Optional message for the start event. |
None
|
**kwargs
|
Any
|
Additional metrics to include in the record. |
{}
|
Source code in src/cloudmesh/ai/common/telemetry.py
244 245 246 247 248 249 250 251 252 | |
track(message=None, metrics=None)
Context manager to automatically track the start and completion of a task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
Optional[str]
|
Optional message for the start event. |
None
|
metrics
|
Optional[Dict[str, Any]]
|
Initial metrics to include. |
None
|
Source code in src/cloudmesh/ai/common/telemetry.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
TelemetryBackend
Bases: Protocol
Interface for telemetry export backends. Any class implementing this protocol can be used as a telemetry sink.
Source code in src/cloudmesh/ai/common/telemetry.py
19 20 21 22 23 24 25 26 27 28 29 30 31 | |
Functions
emit(record)
Emit a single telemetry record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
Dict[str, Any]
|
The telemetry data dictionary to export. |
required |
Source code in src/cloudmesh/ai/common/telemetry.py
24 25 26 27 28 29 30 31 | |
TextBackend
Presents telemetry records in a human-readable text format. Can output to a file or directly to the console.
Source code in src/cloudmesh/ai/common/telemetry.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
Functions
__init__(path=None)
Initialize the Text backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Optional[Union[str, Path]]
|
Optional path to a text file. If None, outputs to stdout. |
None
|
Source code in src/cloudmesh/ai/common/telemetry.py
120 121 122 123 124 125 126 127 | |
emit(record)
Format and emit a telemetry record as a human-readable string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
Dict[str, Any]
|
The telemetry data dictionary to export. |
required |
Source code in src/cloudmesh/ai/common/telemetry.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
Modules
user
Utility module for retrieving user-related system information and permissions.
This module provides cross-platform support for checking root/admin status, identifying the current user, and verifying user existence on the system.
Functions
exists
exists(username)
Verifies if a specific user exists on the local system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
username
|
str
|
The login name of the user to verify. |
required |
Returns:
| Type | Description |
|---|---|
|
True if the user exists, False otherwise. |
Source code in src/cloudmesh/ai/common/user.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
get
get()
Retrieves the login name of the current user.
This function looks at environment variables (LOGNAME, USER, LNAME, USERNAME) to provide a reliable username across different platforms.
Returns:
| Type | Description |
|---|---|
|
The username of the current user. |
Source code in src/cloudmesh/ai/common/user.py
29 30 31 32 33 34 35 36 37 38 | |
groups
groups()
Retrieves a list of group names that the current user belongs to.
Note
This function is currently only fully supported on Unix-like systems. On Windows, this will return an empty list.
Returns:
| Type | Description |
|---|---|
|
A list of strings representing the group names. |
Source code in src/cloudmesh/ai/common/user.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
home
home()
Retrieves the path to the current user's home directory.
Returns:
| Type | Description |
|---|---|
|
A Path object representing the user's home directory |
|
|
(e.g., /home/user or C:\Users\user). |
Source code in src/cloudmesh/ai/common/user.py
40 41 42 43 44 45 46 47 | |
is_root
is_root()
Checks if the current process has administrative or root privileges.
On Unix-like systems (Linux, macOS), it checks if the Effective User ID is 0. On Windows, it utilizes shell32 to check for administrative elevation.
Returns:
| Type | Description |
|---|---|
|
True if the process is running with root/admin rights, False otherwise. |
Source code in src/cloudmesh/ai/common/user.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
util
Functions
FUNCTIONNAME
FUNCTIONNAME()
Returns the name of a function.
Source code in src/cloudmesh/ai/common/util.py
244 245 246 247 | |
HEADING
HEADING(txt=None, c='#', color='magenta')
Prints a message to stdout with #### surrounding it.
Source code in src/cloudmesh/ai/common/util.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
auto_create_requirements
auto_create_requirements(requirements)
creates a requirement.txt file from the requirements in the list.
Source code in src/cloudmesh/ai/common/util.py
271 272 273 274 275 276 277 278 279 | |
auto_create_version
auto_create_version(class_name, version, filename='__init__.py')
creates a version number in the init.py file.
Source code in src/cloudmesh/ai/common/util.py
261 262 263 264 265 266 267 268 | |
backup_name
backup_name(filename)
creates a backup name of the form filename.bak.1
Source code in src/cloudmesh/ai/common/util.py
250 251 252 253 254 255 256 257 258 | |
check_root
check_root(dryrun=False, terminate=True)
check if I am the root user. If not, simply exits the program.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dryrun
|
bool
|
if set to true, does not terminate if not root user |
False
|
terminate
|
bool
|
terminates if not root user and dryrun is False |
True
|
Source code in src/cloudmesh/ai/common/util.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
convert_from_unicode
convert_from_unicode(data)
Converts unicode data to a string
Source code in src/cloudmesh/ai/common/util.py
189 190 191 192 193 194 195 196 197 198 | |
copy_files
copy_files(files_glob, source_dir, dest_dir)
copies the files to the destination
Source code in src/cloudmesh/ai/common/util.py
282 283 284 285 286 287 288 | |
csv_to_list
csv_to_list(csv_string, sep=',')
Converts a CSV table from a string to a list of lists
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
csv_string
|
string
|
The CSV table |
required |
sep
|
string
|
The separator |
','
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
List[List[str]]
|
list of lists |
Source code in src/cloudmesh/ai/common/util.py
98 99 100 101 102 103 104 105 106 107 108 109 | |
download
download(source, destination, force=False)
Downloads the file from source to destination
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str
|
The http source |
required |
destination
|
Union[str, Path]
|
The destination in the file system |
required |
force
|
bool
|
If True the file will be downloaded even if it already exists |
False
|
Source code in src/cloudmesh/ai/common/util.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
exponential_backoff
exponential_backoff(fn, sleeptime_s_max=30 * 60)
Calls fn until it returns True, with an exponentially increasing wait time between calls
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
callable
|
the function to be called that returns True or False |
required |
sleeptime_s_max
|
int
|
the maximum sleep time in seconds |
30 * 60
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if fn() returned True, False if max sleep time was reached |
Source code in src/cloudmesh/ai/common/util.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
generate_password
generate_password(length=8, lower=True, upper=True, number=True)
generates a simple password.
Source code in src/cloudmesh/ai/common/util.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
get_password
get_password(prompt)
gets a password from the user securely
Source code in src/cloudmesh/ai/common/util.py
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | |
grep
grep(pattern, filename)
Very simple grep that returns the first matching line in a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
the pattern to search for |
required |
filename
|
Union[str, Path]
|
the file to search in |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
the first matching line or empty string |
Source code in src/cloudmesh/ai/common/util.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
is_cmd_exe
is_cmd_exe()
return True if you run in a Windows CMD
Source code in src/cloudmesh/ai/common/util.py
180 181 182 183 184 | |
is_gitbash
is_gitbash()
returns True if you run in a Windows gitbash
Source code in src/cloudmesh/ai/common/util.py
161 162 163 164 165 166 | |
is_local
is_local(host)
Checks if the host is the localhost
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
The hostname or ip |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if local, False otherwise |
Source code in src/cloudmesh/ai/common/util.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
is_powershell
is_powershell()
True if you run in powershell
Source code in src/cloudmesh/ai/common/util.py
169 170 171 172 173 174 175 176 177 | |
search
search(lines, pattern)
return all lines that match the pattern
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lines
|
List[str]
|
list of strings to search |
required |
pattern
|
str
|
the pattern to search for (supports * as wildcard) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
List[str]
|
matching lines |
Source code in src/cloudmesh/ai/common/util.py
112 113 114 115 116 117 118 119 120 121 122 123 124 | |
tempdir
tempdir(*args, **kwargs)
A contextmanager to work in an auto-removed temporary directory
Arguments are passed through to tempfile.mkdtemp
Source code in src/cloudmesh/ai/common/util.py
25 26 27 28 29 30 31 32 33 34 35 | |
writefd
writefd(filename, content, mode='w', flags=os.O_RDWR | os.O_CREAT, mask=384)
writes the content into the file and control permissions
Source code in src/cloudmesh/ai/common/util.py
293 294 295 296 297 298 299 300 | |
yn_choice
yn_choice(message, default='y', tries=None)
asks for a yes/no question.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
the message containing the question |
required |
default
|
str
|
the default answer |
'y'
|
tries
|
Optional[int]
|
the number of tries |
None
|
Source code in src/cloudmesh/ai/common/util.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |