API Reference
Complete API documentation for pychrony, auto-generated from source code docstrings.
Connection
The main entry point for interacting with chronyd.
ChronyConnection
Context manager for chrony connections.
Provides connection reuse for multiple queries to chronyd within a single context, properly managing socket and session lifecycle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
str | None
|
Connection address. Supports:
|
None
|
Methods:
| Name | Description |
|---|---|
get_tracking |
Get current NTP tracking status (returns |
get_sources |
Get configured time sources (returns |
get_source_stats |
Get source statistics (returns |
get_rtc_data |
Get RTC tracking data (returns |
Choosing a transport
The Unix socket is a control channel; the command port serves only
monitoring commands, which covers every report this class reads. A
read-only consumer should prefer it and can assert on transport.
Thread Safety
NOT thread-safe. Each thread needs its own connection.
See Also
Transport: The transport a connection resolved to, and why it matters.
TrackingStatus: Tracking data model.
Source: Time source data model.
SourceStats: Source statistics data model.
RTCData: RTC tracking data model.
chronyc man page (access methods and the monitoring command set): https://chrony-project.org/doc/4.9/chronyc.html
Examples:
>>> with ChronyConnection() as conn:
... tracking = conn.get_tracking()
... sources = conn.get_sources()
... stats = conn.get_source_stats()
... rtc = conn.get_rtc_data()
Source code in src/pychrony/_core/_bindings.py
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 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 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 492 493 494 495 496 497 498 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 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 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 674 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 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 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 809 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 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 | |
address
property
Address this connection is actually using, or None if not connected.
For an explicit address this is that address; for auto-detect it is whichever candidate connected. Cleared when the connection closes.
transport
property
Transport in use, or None if not connected.
Transport covers why the two differ in privilege. A caller that must
not hold a control channel can assert on this.
__init__(address=None)
Initialize ChronyConnection with optional address.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
str | None
|
Connection address (see class docstring for formats) |
None
|
Source code in src/pychrony/_core/_bindings.py
276 277 278 279 280 281 282 283 284 285 286 287 288 | |
__enter__()
Enter context manager, opening connection to chronyd.
Source code in src/pychrony/_core/_bindings.py
290 291 292 293 294 295 | |
__exit__(exc_type, exc_val, exc_tb)
Exit context manager, closing connection to chronyd.
Source code in src/pychrony/_core/_bindings.py
297 298 299 300 301 302 303 304 305 | |
get_tracking()
Get current tracking status from chronyd.
Returns:
| Name | Type | Description |
|---|---|---|
TrackingStatus |
TrackingStatus
|
Current tracking information from chronyd. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If called outside context manager |
ChronyConnectionError
|
If chronyd did not respond. |
ChronyDataError
|
If tracking data is invalid or incomplete. |
Examples:
>>> with ChronyConnection() as conn:
... status = conn.get_tracking()
... print(f"Offset: {status.offset:.6f} seconds")
Source code in src/pychrony/_core/_bindings.py
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 587 588 589 590 591 592 593 594 595 596 597 598 599 | |
get_sources()
Get all configured time sources from chronyd.
Returns:
| Type | Description |
|---|---|
list[Source]
|
list[Source]: List of Source objects for each configured source. Empty list if no sources are configured. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If called outside context manager |
ChronyConnectionError
|
If chronyd did not respond. |
ChronyDataError
|
If source data is invalid or incomplete. |
Examples:
>>> with ChronyConnection() as conn:
... sources = conn.get_sources()
... for src in sources:
... print(f"{src.address}: stratum {src.stratum}")
Source code in src/pychrony/_core/_bindings.py
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 | |
get_source_stats()
Get statistical data for all time sources from chronyd.
Returns:
| Type | Description |
|---|---|
list[SourceStats]
|
list[SourceStats]: List of SourceStats objects for each source. Empty list if no sources are configured. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If called outside context manager |
ChronyConnectionError
|
If chronyd did not respond. |
ChronyDataError
|
If statistics data is invalid or incomplete. |
Examples:
>>> with ChronyConnection() as conn:
... stats = conn.get_source_stats()
... for s in stats:
... print(f"{s.address}: {s.samples} samples")
Source code in src/pychrony/_core/_bindings.py
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 | |
get_rtc_data()
Get Real-Time Clock tracking data from chronyd.
Returns:
| Type | Description |
|---|---|
RTCData | None
|
RTCData if RTC tracking is enabled, None otherwise. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If called outside context manager |
ChronyConnectionError
|
If chronyd did not respond. |
ChronyDataError
|
If RTC data is invalid or malformed. |
Examples:
>>> with ChronyConnection() as conn:
... rtc = conn.get_rtc_data()
... if rtc:
... print(f"RTC offset: {rtc.offset:.6f}s")
Source code in src/pychrony/_core/_bindings.py
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 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 835 836 837 | |
Data Models
Frozen dataclasses representing chronyd report data.
TrackingStatus
dataclass
Chrony tracking status information.
Represents the current time synchronization state from chronyd, including offset, frequency, and accuracy metrics.
Attributes:
| Name | Type | Description |
|---|---|---|
reference_id |
int
|
NTP reference identifier (uint32 as hex IP or name). |
reference_id_name |
str
|
Human-readable reference source name. |
reference_ip |
str
|
IP address of reference source (IPv4, IPv6, or ID#). |
stratum |
int
|
NTP stratum level (0=reference clock, 1-15=downstream). |
leap_status |
LeapStatus
|
Leap second status (see |
ref_time |
float
|
Timestamp of last measurement (seconds since epoch). |
offset |
float
|
Current offset from reference (seconds, can be negative). |
last_offset |
float
|
Offset at last measurement (seconds). |
rms_offset |
float
|
Root mean square of recent offsets (seconds). |
frequency |
float
|
Clock frequency error (parts per million). |
residual_freq |
float
|
Residual frequency for current source (ppm). |
skew |
float
|
Estimated error bound on frequency (ppm). |
root_delay |
float
|
Total roundtrip delay to stratum-1 source (seconds). |
root_dispersion |
float
|
Total dispersion to reference (seconds). |
update_interval |
float
|
Seconds since last successful update. |
See Also
LeapStatus: Enum for leap second status values.
ChronyConnection.get_tracking: Method to retrieve this data.
Source code in src/pychrony/models.py
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 | |
is_synchronized()
Check if chronyd is synchronized to a source.
Returns:
| Type | Description |
|---|---|
bool
|
True if synchronized (reference_id != 0 and stratum < 16), |
bool
|
False otherwise. |
Examples:
>>> with ChronyConnection() as conn:
... status = conn.get_tracking()
... if status.is_synchronized():
... print(f"Synced to {status.reference_ip}")
Source code in src/pychrony/models.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
is_leap_pending()
Check if a leap second adjustment is pending.
Returns:
| Type | Description |
|---|---|
bool
|
True if leap_status is INSERT or DELETE, |
bool
|
False otherwise. |
Examples:
>>> with ChronyConnection() as conn:
... status = conn.get_tracking()
... if status.is_leap_pending():
... print(f"Leap second pending: {status.leap_status.name}")
Source code in src/pychrony/models.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
Source
dataclass
Chrony source information.
Represents an NTP server, peer, or reference clock being used as a time source by chronyd.
Attributes:
| Name | Type | Description |
|---|---|---|
address |
str
|
IP address or reference ID of the source (IPv4, IPv6, or refclock ID). |
poll |
int
|
Polling interval as log2 seconds (e.g., 6 means 64 seconds). |
stratum |
int
|
NTP stratum level of the source (0-15). |
state |
SourceState
|
Selection state (see |
mode |
SourceMode
|
Source mode (see |
flags |
int
|
Source flags (bitfield). |
reachability |
int
|
Reachability register (8-bit, 377 octal = all recent polls succeeded). |
last_sample_ago |
int
|
Seconds since last valid sample was received. |
orig_latest_meas |
float
|
Original last sample offset (seconds). |
latest_meas |
float
|
Adjusted last sample offset (seconds). |
latest_meas_err |
float
|
Last sample error bound (seconds). |
See Also
SourceState: Enum for source selection states.
SourceMode: Enum for source operational modes.
ChronyConnection.get_sources: Method to retrieve source list.
Source code in src/pychrony/models.py
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 | |
is_reachable()
Check if the source has been reachable recently.
Returns:
| Type | Description |
|---|---|
bool
|
True if reachability register is non-zero (at least one successful poll). |
Examples:
>>> with ChronyConnection() as conn:
... for src in conn.get_sources():
... if not src.is_reachable():
... print(f"Source {src.address} is unreachable")
Source code in src/pychrony/models.py
256 257 258 259 260 261 262 263 264 265 266 267 268 | |
is_selected()
Check if this source is currently selected for synchronization.
Returns:
| Type | Description |
|---|---|
bool
|
True if state is SELECTED. |
Examples:
>>> with ChronyConnection() as conn:
... for src in conn.get_sources():
... if src.is_selected():
... print(f"Currently using {src.address}")
Source code in src/pychrony/models.py
270 271 272 273 274 275 276 277 278 279 280 281 282 | |
SourceStats
dataclass
Chrony source statistics.
Represents statistical data about measurements from an NTP source, used for drift and offset estimation.
Attributes:
| Name | Type | Description |
|---|---|---|
reference_id |
int
|
32-bit NTP reference identifier. |
address |
str
|
IP address of the source (empty for reference clocks). |
samples |
int
|
Number of sample points currently retained. |
runs |
int
|
Number of runs of residuals with same sign. |
span |
int
|
Time interval between oldest and newest samples (seconds). |
std_dev |
float
|
Estimated sample standard deviation (seconds). |
resid_freq |
float
|
Residual frequency (parts per million). |
skew |
float
|
Frequency skew (error bound) in ppm. |
offset |
float
|
Estimated offset of the source (seconds). |
offset_err |
float
|
Offset error bound (seconds). |
See Also
ChronyConnection.get_source_stats: Method to retrieve statistics.
Source code in src/pychrony/models.py
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 | |
has_sufficient_samples(minimum=4)
Check if enough samples exist for reliable statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
minimum
|
int
|
Minimum number of samples required (default 4). |
4
|
Returns:
| Type | Description |
|---|---|
bool
|
True if samples >= minimum. |
Examples:
>>> with ChronyConnection() as conn:
... for stats in conn.get_source_stats():
... if stats.has_sufficient_samples(8):
... print(f"{stats.address}: offset={stats.offset:.6f}s")
Source code in src/pychrony/models.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
RTCData
dataclass
Chrony RTC (Real-Time Clock) data.
Represents information about the hardware RTC and its relationship to system time, as tracked by chronyd.
Note: RTC tracking must be enabled in chronyd configuration.
If not enabled, get_rtc_data() returns None.
Attributes:
| Name | Type | Description |
|---|---|---|
ref_time |
float
|
RTC reading at last error measurement (seconds since epoch). |
samples |
int
|
Number of previous measurements used for calibration. |
runs |
int
|
Number of runs of residuals (indicates linear model fit quality). |
span |
int
|
Time period covered by measurements (seconds). |
offset |
float
|
Estimated RTC offset (fast by) in seconds. |
freq_offset |
float
|
RTC frequency offset (drift rate) in parts per million. |
See Also
ChronyConnection.get_rtc_data: Method to retrieve RTC data.
Source code in src/pychrony/models.py
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 | |
is_calibrated()
Check if RTC has enough calibration data.
Returns:
| Type | Description |
|---|---|
bool
|
True if samples > 0 (some calibration exists). |
Examples:
>>> with ChronyConnection() as conn:
... rtc = conn.get_rtc_data()
... if rtc and rtc.is_calibrated():
... print(f"RTC drift: {rtc.freq_offset:.2f} ppm")
Source code in src/pychrony/models.py
366 367 368 369 370 371 372 373 374 375 376 377 378 | |
Enums
Categorical values for status fields.
LeapStatus
Bases: Enum
Leap second status for NTP synchronization.
Indicates whether time is normal or if a leap second adjustment is scheduled at the next midnight UTC.
Attributes:
| Name | Type | Description |
|---|---|---|
NORMAL |
No leap second pending. |
|
INSERT |
Leap second will be inserted at midnight (23:59:60). |
|
DELETE |
Leap second will be deleted at midnight (skip 23:59:59). |
|
UNSYNC |
Clock is unsynchronized. |
Examples:
>>> from pychrony import ChronyConnection, LeapStatus
>>> with ChronyConnection() as conn:
... status = conn.get_tracking()
... if status.leap_status == LeapStatus.INSERT:
... print("Leap second insertion scheduled")
... elif status.leap_status == LeapStatus.UNSYNC:
... print("Clock not synchronized")
Source code in src/pychrony/models.py
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 | |
SourceState
Bases: Enum
Selection state of a chrony time source.
Indicates whether chrony has selected, rejected, or is considering this source for time synchronization.
Attributes:
| Name | Type | Description |
|---|---|---|
SELECTED |
Currently selected for synchronization. |
|
NONSELECTABLE |
Cannot be selected (bad measurements). |
|
FALSETICKER |
Detected as providing incorrect time. |
|
JITTERY |
Measurements have excessive jitter. |
|
UNSELECTED |
Valid but not currently selected. |
|
SELECTABLE |
Candidate for selection. |
Examples:
>>> from pychrony import ChronyConnection, SourceState
>>> with ChronyConnection() as conn:
... for src in conn.get_sources():
... if src.state == SourceState.FALSETICKER:
... print(f"Warning: {src.address} detected as falseticker")
... elif src.state == SourceState.SELECTED:
... print(f"Active source: {src.address}")
Source code in src/pychrony/models.py
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 | |
SourceMode
Bases: Enum
Operational mode of a chrony time source.
Distinguishes between NTP client connections, peer relationships, and local reference clocks.
Attributes:
| Name | Type | Description |
|---|---|---|
CLIENT |
NTP client polling a server. |
|
PEER |
NTP peer relationship (bidirectional). |
|
REFCLOCK |
Local reference clock (GPS, PPS, etc.). |
Examples:
>>> from pychrony import ChronyConnection, SourceMode
>>> with ChronyConnection() as conn:
... for src in conn.get_sources():
... if src.mode == SourceMode.REFCLOCK:
... print(f"Reference clock: {src.address}")
... elif src.mode == SourceMode.CLIENT:
... print(f"NTP server: {src.address}")
Source code in src/pychrony/models.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
Transport
Bases: Enum
Transport a ChronyConnection is using to reach chronyd.
The two do not carry the same privileges: chrony describes full access
through the Unix socket as "more or less equivalent to being able to modify
the chronyd's configuration file and restart it". Every report pychrony
reads is in the monitoring set the command port serves, so a read-only
consumer loses nothing by requiring COMMAND_PORT.
Attributes:
| Name | Type | Description |
|---|---|---|
UNIX_SOCKET |
chronyd's Unix domain socket, its read-write control channel. Accessible locally by the root or chrony user. |
|
COMMAND_PORT |
chronyd's UDP command port (default 323, bound to 127.0.0.1 and ::1). Serves monitoring commands only. |
See Also
https://chrony-project.org/doc/4.9/chronyc.html
Examples:
>>> from pychrony import ChronyConnection, Transport
>>> with ChronyConnection() as conn:
... if conn.transport is not Transport.COMMAND_PORT:
... raise RuntimeError("refusing to hold a control channel")
... tracking = conn.get_tracking()
Source code in src/pychrony/models.py
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 | |
Exceptions
Exception hierarchy for error handling.
ChronyError
Bases: Exception
Base exception for all chrony-related errors.
Attributes:
| Name | Type | Description |
|---|---|---|
message |
Human-readable error description |
|
error_code |
Optional numeric error code from libchrony |
Source code in src/pychrony/exceptions.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
ChronyConnectionError
Bases: ChronyError
Raised when chronyd cannot be reached.
Covers a connection that could not be opened and one that opened but was never answered. The second is specific to the command port: it is UDP, so the socket opens whether or not chronyd is listening.
Common causes:
- chronyd is not running
- No candidate address could be connected to during auto-detect
- The command port is disabled (
cmdport 0), bound elsewhere (bindcmdaddress), or blocked by a firewall - The client is not permitted by
cmdallow(chronyd accepts monitoring commands only from localhost by default) chrony_init_session()returns error
Examples:
>>> from pychrony import ChronyConnection, ChronyConnectionError
>>> try:
... with ChronyConnection() as conn:
... status = conn.get_tracking()
... except ChronyConnectionError as e:
... print(f"Connection failed: {e}")
Source code in src/pychrony/exceptions.py
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 | |
ChronyPermissionError
Bases: ChronyError
Raised when chronyd's Unix socket exists but refuses the connection.
Connecting to a Unix socket needs write permission, and chronyd creates the socket owned by the chrony user without group write, so joining chrony's group does not grant access - only root does. chrony also requires the socket's directory to be accessible only by the root or chrony user, so widening these permissions works against its design.
Prefer the command port for read-only monitoring: chronyd enables it on localhost by default and serves every report pychrony reads, while refusing the control commands the Unix socket would expose.
Common causes:
- Running as an unprivileged user against chronyd's Unix socket
- SELinux/AppArmor restrictions
See Also
https://chrony-project.org/doc/4.9/chronyc.html
Examples:
>>> from pychrony import ChronyConnection, ChronyPermissionError
>>> try:
... with ChronyConnection() as conn:
... status = conn.get_tracking()
... except ChronyPermissionError as e:
... print(f"Permission denied: {e}")
... # Read-only fallback; needs chronyd without "cmdport 0"
... with ChronyConnection("127.0.0.1") as conn:
... status = conn.get_tracking()
Source code in src/pychrony/exceptions.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 | |
ChronyDataError
Bases: ChronyError
Raised when a report is retrieved but its data is invalid or incomplete.
Reserved for problems with the report itself. A failure to reach chronyd,
including a request sent but never answered, raises ChronyConnectionError
instead, so "chronyd is unreachable" is one exception to catch, not two.
Common causes:
chrony_get_field_index()returns < 0 (field not found)- chronyd rejected the report request (unknown report, unauthorized)
- Field validation fails (NaN, out of range)
- Protocol version mismatch
Examples:
>>> from pychrony import ChronyConnection, ChronyDataError
>>> with ChronyConnection() as conn:
... try:
... status = conn.get_tracking()
... except ChronyDataError as e:
... print(f"Invalid data: {e}")
Source code in src/pychrony/exceptions.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
ChronyLibraryError
Bases: ChronyError
Raised when libchrony is not available.
Common causes:
- libchrony not installed at runtime
- CFFI bindings not compiled (missing libchrony-devel at build time)
- Library version incompatible
Examples:
>>> from pychrony import ChronyConnection, ChronyLibraryError
>>> try:
... with ChronyConnection() as conn:
... status = conn.get_tracking()
... except ChronyLibraryError as e:
... print(f"Library not available: {e}")
... print("Install libchrony-devel and rebuild")
Source code in src/pychrony/exceptions.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
Testing Utilities
Factory functions and pytest fixtures for testing code that uses pychrony.
testing
Testing utilities for pychrony.
This module provides factory functions and pytest fixtures for creating test instances of pychrony dataclasses with sensible defaults.
Factory Functions (for any test framework): from pychrony.testing import make_tracking, make_source status = make_tracking(stratum=3, offset=-0.001)
Pytest Fixtures (auto-discovered via plugin): def test_something(tracking_status, source): assert tracking_status.is_synchronized()
make_tracking(**overrides)
Create a TrackingStatus instance with sensible defaults.
Default state is synchronized (reference_id != 0, stratum=2).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**overrides
|
Any
|
Field values to override defaults |
{}
|
Returns:
| Type | Description |
|---|---|
TrackingStatus
|
TrackingStatus instance |
Examples:
>>> make_tracking() # Synchronized status
>>> make_tracking(stratum=16, reference_id=0) # Unsynchronized
>>> make_tracking(leap_status=LeapStatus.INSERT) # Leap pending
Source code in src/pychrony/testing.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
make_source(**overrides)
Create a Source instance with sensible defaults.
Default state is selected and reachable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**overrides
|
Any
|
Field values to override defaults. |
{}
|
Returns:
| Type | Description |
|---|---|
Source
|
Source instance. |
Examples:
>>> make_source() # Selected, reachable source
>>> make_source(state=SourceState.FALSETICKER) # Falseticker
>>> make_source(reachability=0) # Unreachable source
Source code in src/pychrony/testing.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
make_source_stats(**overrides)
Create a SourceStats instance with sensible defaults.
Default has 8 samples (sufficient for statistics).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**overrides
|
Any
|
Field values to override defaults. |
{}
|
Returns:
| Type | Description |
|---|---|
SourceStats
|
SourceStats instance. |
Examples:
>>> make_source_stats() # Stats with 8 samples
>>> make_source_stats(samples=2) # Insufficient samples
>>> make_source_stats(offset=0.001) # Custom offset
Source code in src/pychrony/testing.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
make_rtc_data(**overrides)
Create an RTCData instance with sensible defaults.
Default is calibrated (samples > 0).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**overrides
|
Any
|
Field values to override defaults. |
{}
|
Returns:
| Type | Description |
|---|---|
RTCData
|
RTCData instance. |
Examples:
>>> make_rtc_data() # Calibrated RTC
>>> make_rtc_data(samples=0) # Uncalibrated RTC
>>> make_rtc_data(freq_offset=-5.0) # Custom drift rate
Source code in src/pychrony/testing.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
tracking_status()
Fixture providing a synchronized TrackingStatus with defaults.
Source code in src/pychrony/testing.py
173 174 175 176 | |
source()
Fixture providing a selected, reachable Source with defaults.
Source code in src/pychrony/testing.py
179 180 181 182 | |
source_stats()
Fixture providing a SourceStats with sufficient samples.
Source code in src/pychrony/testing.py
185 186 187 188 | |
rtc_data()
Fixture providing a calibrated RTCData with defaults.
Source code in src/pychrony/testing.py
191 192 193 194 | |