Python API (autogenerated)¶
This page is populated via mkdocstrings.
New formatted uptime helpers¶
The plugin exposes a few convenience formatting functions in the public API usable by other plugins or scripts:
format_uptime(seconds: float) -> str- full human-readable form (e.g. "1d 2h 3m 4s").format_uptime_dhm(seconds: float) -> str- days/hours/minutes (omits seconds).format_uptime_dh(seconds: float) -> str- days/hours (e.g. "1d 2h").format_uptime_d(seconds: float) -> str- whole days only (e.g. "3d").
Example usage from a Python context inside OctoPrint:
from octoprint_uptime.plugin import format_uptime_dhm
print(format_uptime_dhm(93784)) # -> "1d 2h 3m"
OctoPrint-Uptime plugin package.
Displays host system and OctoPrint process uptime in the navbar and exposes a small JSON API returning both raw seconds and formatted variants. This module avoids importing OctoPrint/Flask at import-time so it can be packaged and unit-tested without the OctoPrint runtime present.
__all__
module-attribute
¶
__all__ = [
"VERSION",
"OctoprintUptimePlugin",
"UptimePlugin",
"format_uptime",
"format_uptime_d",
"format_uptime_dh",
"format_uptime_dhm",
]
__plugin_description__
module-attribute
¶
__plugin_description__ = "Adds system uptime to the navbar and exposes a small uptime API."
__plugin_url__
module-attribute
¶
__plugin_url__ = (
"https://github.com/Ajimaru/OctoPrint-Uptime"
)
OctoprintUptimePlugin ¶
OctoprintUptimePlugin(*args, **kwargs)
Bases: SimpleApiPluginBase, AssetPluginBase, SettingsPluginBase, TemplatePluginBase
OctoPrint plugin implementation.
Uses lazy imports for OctoPrint/Flask integration points so the module can be imported in environments where OctoPrint is not installed.
Initialize internal state for debug, display format, and tracking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
object
|
Positional arguments passed to the parent class. |
()
|
**kwargs
|
object
|
Keyword arguments passed to the parent class. |
{}
|
Source code in octoprint_uptime/plugin.py
181 182 183 184 185 186 187 188 189 190 191 192 193 | |
get_update_information ¶
get_update_information()
Return metadata for OctoPrint's software update mechanism.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Update information dictionary keyed by plugin identifier. |
Source code in octoprint_uptime/plugin.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
is_api_protected ¶
is_api_protected()
Indicate that the plugin's API endpoint requires authentication.
Returns:
| Type | Description |
|---|---|
bool
|
Always True; OctoPrint enforces authentication for the endpoint. |
Source code in octoprint_uptime/plugin.py
216 217 218 219 220 221 222 | |
get_assets ¶
get_assets()
Return the plugin's static asset files.
Returns:
| Type | Description |
|---|---|
dict[str, list[str]]
|
Dictionary mapping asset types to file lists. |
Source code in octoprint_uptime/plugin.py
224 225 226 227 228 229 230 | |
get_template_configs ¶
get_template_configs()
Return template configurations for the navbar and settings panes.
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
A list of template configuration dictionaries. |
Source code in octoprint_uptime/plugin.py
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 | |
is_template_autoescaped ¶
is_template_autoescaped()
Indicate that the plugin's templates are autoescaped.
Returns:
| Type | Description |
|---|---|
bool
|
Always True. |
Source code in octoprint_uptime/plugin.py
265 266 267 268 269 270 271 | |
_get_uptime_seconds ¶
_get_uptime_seconds()
Retrieve system uptime, trying /proc first and psutil second.
Returns:
| Type | Description |
|---|---|
Optional[float]
|
Tuple |
str
|
|
Source code in octoprint_uptime/plugin.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |
_get_uptime_from_proc ¶
_get_uptime_from_proc()
Get system uptime from /proc/uptime if available.
Source code in octoprint_uptime/plugin.py
293 294 295 296 297 298 299 300 301 | |
_get_uptime_from_psutil ¶
_get_uptime_from_psutil()
Get system uptime from psutil's boot time if psutil is available.
Source code in octoprint_uptime/plugin.py
303 304 305 306 307 308 309 310 311 312 313 314 315 | |
_get_octoprint_uptime_from_proc ¶
_get_octoprint_uptime_from_proc()
Get process uptime from /proc on Linux in a clock-jump-safe way.
Source code in octoprint_uptime/plugin.py
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 | |
_get_octoprint_uptime ¶
_get_octoprint_uptime()
Get OctoPrint process uptime, preferring /proc and then psutil.
Source code in octoprint_uptime/plugin.py
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
on_settings_initialized ¶
on_settings_initialized()
Update internal state once OctoPrint has initialized settings.
Also calls a base implementation if OctoPrint provides one.
Source code in octoprint_uptime/plugin.py
378 379 380 381 382 383 384 385 386 387 388 389 390 | |
on_settings_save ¶
on_settings_save(data)
Sanitize, persist, and apply the plugin settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Settings data to save. |
required |
Source code in octoprint_uptime/plugin.py
392 393 394 395 396 397 398 399 400 401 402 | |
_safe_update_internal_state ¶
_safe_update_internal_state()
Update internal state, logging expected failures instead of raising.
Source code in octoprint_uptime/plugin.py
404 405 406 407 408 409 410 411 412 413 414 | |
_get_hook_positional_param_count ¶
_get_hook_positional_param_count(hook)
Return the number of positional params a callable accepts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hook
|
Any
|
The callable to inspect. |
required |
Returns:
| Type | Description |
|---|---|
Optional[int]
|
The positional parameter count, or None when the signature cannot |
Optional[int]
|
be inspected (logged as info). |
Source code in octoprint_uptime/plugin.py
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | |
_safe_invoke_hook ¶
_safe_invoke_hook(hook, param_count)
Invoke a hook with zero or one positional parameter, logging failures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hook
|
Any
|
The callable to invoke. |
required |
param_count
|
int
|
0 or 1; any exception raised by the hook is logged but not propagated. |
required |
Source code in octoprint_uptime/plugin.py
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | |
_invoke_settings_hook ¶
_invoke_settings_hook(hook)
Invoke a settings hook using signature inspection.
Skips the call (with a warning) when the hook expects an unexpected number of positional parameters or its signature cannot be inspected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hook
|
Any
|
The callable to invoke. |
required |
Source code in octoprint_uptime/plugin.py
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | |
_validate_and_sanitize_settings ¶
_validate_and_sanitize_settings(data)
Coerce and clamp integer settings in the provided save payload.
Missing or non-integer values are replaced with their defaults, valid
values are clamped to the allowed range (see _INT_SETTING_BOUNDS).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
The settings payload passed to |
required |
Source code in octoprint_uptime/plugin.py
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 | |
_log_settings_save_data ¶
_log_settings_save_data(data)
Log the payload passed to the settings save event for debugging.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
The data being saved to the settings. |
required |
Source code in octoprint_uptime/plugin.py
518 519 520 521 522 523 524 525 526 527 528 529 | |
_call_base_on_settings_save ¶
_call_base_on_settings_save(data)
Call the base class on_settings_save, if available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
The settings data to be saved. |
required |
Source code in octoprint_uptime/plugin.py
531 532 533 534 535 536 537 538 539 540 541 542 | |
get_settings_defaults ¶
get_settings_defaults()
Return default settings for the plugin.
OctoPrint populates settings.plugins.<identifier> from this mapping
so the frontend can safely bind to
settings.plugins.octoprint_uptime.*.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Mapping of setting names to default values. |
Source code in octoprint_uptime/plugin.py
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 | |
_update_internal_state ¶
_update_internal_state()
Refresh cached debug, display format, and throttle values.
Source code in octoprint_uptime/plugin.py
566 567 568 569 570 571 572 | |
_log_settings_after_save ¶
_log_settings_after_save()
Log the effective settings after they have been saved.
Source code in octoprint_uptime/plugin.py
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 | |
_log_debug ¶
_log_debug(message)
Log a debug message when enabled, throttled to avoid log spam.
Messages are dropped while _debug_throttle_seconds has not elapsed
since the last logged message. Logging errors are silently ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
The debug message to log. |
required |
Source code in octoprint_uptime/plugin.py
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 | |
_fallback_uptime_response ¶
_fallback_uptime_response()
Build a system-uptime-only response as a JSON or dict payload.
Defensive fallback used when the full API response cannot be built. Returns a Flask JSON response when Flask is available, a plain dict otherwise; on error the uptime is reported as unknown.
Source code in octoprint_uptime/plugin.py
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 | |
on_api_get ¶
on_api_get(_request=None)
Handle GET requests to the plugin's API endpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
_request
|
Any
|
The incoming request (unused). |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
A Flask JSON response with all uptime variants when Flask is |
Any
|
available, a reduced plain dict otherwise, or an error response |
Any
|
when the permission check fails. |
Source code in octoprint_uptime/plugin.py
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 | |
_handle_permission_check ¶
_handle_permission_check()
Check permissions for API GET requests, handling errors.
Returns:
| Type | Description |
|---|---|
Optional[Any]
|
A forbidden/fallback response when permission is denied or the |
Optional[Any]
|
check errors out, otherwise None when permission is granted. |
Source code in octoprint_uptime/plugin.py
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 | |
_check_permissions ¶
_check_permissions()
Check whether the current user may query the uptime API.
Returns:
| Type | Description |
|---|---|
bool
|
Always True for now: fine-grained permission enforcement is not |
bool
|
implemented; OctoPrint's API authentication already gates access. |
bool
|
Replace with real checks when needed. |
Source code in octoprint_uptime/plugin.py
756 757 758 759 760 761 762 763 764 | |
_abort_forbidden ¶
_abort_forbidden()
Abort with HTTP 403 when Flask is available.
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
An error dict when Flask is unavailable (otherwise Flask's abort |
dict[str, str]
|
raises before the return). |
Source code in octoprint_uptime/plugin.py
766 767 768 769 770 771 772 773 774 775 | |
_get_uptime_info ¶
_get_uptime_info()
Retrieve system uptime and its formatted display strings.
Honors a get_uptime_seconds attribute when one has been injected
(e.g. by tests or subclasses), otherwise uses the built-in sources.
Returns:
| Type | Description |
|---|---|
tuple[Optional[float], str, str, str, str]
|
Tuple |
Source code in octoprint_uptime/plugin.py
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 | |
_format_uptime_tuple ¶
_format_uptime_tuple(seconds)
Normalize and format an uptime value into display strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seconds
|
Optional[float]
|
The uptime in seconds, or None/invalid when unknown. |
required |
Returns:
| Type | Description |
|---|---|
Optional[float]
|
Tuple |
str
|
all strings are the localized "unknown" when seconds is invalid. |
Source code in octoprint_uptime/plugin.py
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 | |
_get_octoprint_uptime_info ¶
_get_octoprint_uptime_info()
Retrieve OctoPrint process uptime and its formatted display strings.
Returns:
| Type | Description |
|---|---|
tuple[Optional[float], str, str, str, str]
|
Tuple |
Source code in octoprint_uptime/plugin.py
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 | |
_get_api_settings ¶
_get_api_settings()
Read the display format and poll interval for API responses.
Returns:
| Type | Description |
|---|---|
str
|
Tuple |
int
|
|
tuple[str, int]
|
is defaulted and clamped to its allowed range when missing or |
tuple[str, int]
|
invalid. |
Source code in octoprint_uptime/plugin.py
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 | |
format_uptime ¶
format_uptime(seconds)
Format a duration as days, hours, minutes, and seconds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seconds
|
float
|
The total number of seconds to format. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The duration as e.g. |
str
|
omitted, so |
Source code in octoprint_uptime/plugin.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
format_uptime_d ¶
format_uptime_d(seconds)
Format a duration as whole days.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seconds
|
float
|
The total number of seconds to format. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The number of whole days followed by |
Source code in octoprint_uptime/plugin.py
156 157 158 159 160 161 162 163 164 165 166 | |
format_uptime_dh ¶
format_uptime_dh(seconds)
Format a duration as days and hours.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seconds
|
float
|
The total number of seconds to format. |
required |
Returns:
| Type | Description |
|---|---|
str
|
|
Source code in octoprint_uptime/plugin.py
141 142 143 144 145 146 147 148 149 150 151 152 153 | |
format_uptime_dhm ¶
format_uptime_dhm(seconds)
Format a duration as days, hours, and minutes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seconds
|
float
|
The total number of seconds to format. |
required |
Returns:
| Type | Description |
|---|---|
str
|
|
Source code in octoprint_uptime/plugin.py
126 127 128 129 130 131 132 133 134 135 136 137 138 | |
API changes: uptime availability¶
When uptime cannot be determined the plugin API now includes an uptime_available boolean and an optional uptime_note string that can contain a localized hint. Example response:
{
"uptime": "unknown",
"uptime_available": false,
"uptime_note": "Uptime could not be determined."
}
Frontend callers: when querying the plugin from JavaScript inside the OctoPrint UI prefer the OctoPrint helper and pass the plugin id only, for example:
OctoPrint.simpleApiGet("octoprint_uptime").done(function (data) {
/* ... */
});