|
2 | 2 | import logging |
3 | 3 | import numbers |
4 | 4 | import os |
| 5 | +import platform |
5 | 6 | import sys |
6 | 7 | import warnings |
7 | 8 | from datetime import datetime, timedelta |
|
29 | 30 | MAX_DICT_SIZE = 50_000 |
30 | 31 |
|
31 | 32 |
|
| 33 | +def get_os_info(): |
| 34 | + """ |
| 35 | + Returns standardized OS name and version information. |
| 36 | + Similar to how user agent parsing works in JS. |
| 37 | + """ |
| 38 | + os_name = "" |
| 39 | + os_version = "" |
| 40 | + |
| 41 | + # Windows |
| 42 | + if sys.platform.startswith('win'): |
| 43 | + os_name = "Windows" |
| 44 | + if hasattr(platform, 'win32_ver'): |
| 45 | + win_version = platform.win32_ver()[0] |
| 46 | + if win_version: |
| 47 | + os_version = win_version |
| 48 | + |
| 49 | + # macOS/Mac OS X |
| 50 | + elif sys.platform == 'darwin': |
| 51 | + os_name = "Mac OS X" |
| 52 | + if hasattr(platform, 'mac_ver'): |
| 53 | + mac_version = platform.mac_ver()[0] |
| 54 | + if mac_version: |
| 55 | + os_version = mac_version |
| 56 | + |
| 57 | + # iOS (unlikely in standard Python but included for completeness) |
| 58 | + elif sys.platform == 'ios': |
| 59 | + os_name = "iOS" |
| 60 | + # iOS version would need a specific approach |
| 61 | + |
| 62 | + # Linux |
| 63 | + elif sys.platform.startswith('linux'): |
| 64 | + os_name = "Linux" |
| 65 | + if hasattr(platform, 'linux_distribution'): |
| 66 | + # Deprecated in Python 3.8+ |
| 67 | + try: |
| 68 | + linux_info = platform.linux_distribution() |
| 69 | + if linux_info[0] and linux_info[1]: |
| 70 | + os_version = linux_info[1] |
| 71 | + except: |
| 72 | + pass |
| 73 | + # For newer Python versions |
| 74 | + try: |
| 75 | + import distro |
| 76 | + linux_info = distro.info() |
| 77 | + if linux_info['version']: |
| 78 | + os_version = linux_info['version'] |
| 79 | + except ImportError: |
| 80 | + pass |
| 81 | + |
| 82 | + # FreeBSD |
| 83 | + elif sys.platform.startswith('freebsd'): |
| 84 | + os_name = "FreeBSD" |
| 85 | + if hasattr(platform, 'release'): |
| 86 | + os_version = platform.release() |
| 87 | + |
| 88 | + # Other platforms |
| 89 | + else: |
| 90 | + os_name = sys.platform |
| 91 | + if hasattr(platform, 'release'): |
| 92 | + os_version = platform.release() |
| 93 | + |
| 94 | + return os_name, os_version |
| 95 | + |
| 96 | + |
| 97 | +def system_context() -> dict[str, any]: |
| 98 | + os_name, os_version = get_os_info() |
| 99 | + |
| 100 | + return { |
| 101 | + "$python_runtime": platform.python_implementation(), |
| 102 | + "$python_version": "%s.%s.%s" % (sys.version_info[:3]), |
| 103 | + "$os": os_name, |
| 104 | + "$os_version": os_version, |
| 105 | + } |
| 106 | + |
| 107 | + |
32 | 108 | class Client(object): |
33 | 109 | """Create a new PostHog client.""" |
34 | 110 |
|
@@ -270,7 +346,7 @@ def capture( |
270 | 346 | extra_properties["$active_feature_flags"] = active_feature_flags |
271 | 347 |
|
272 | 348 | if extra_properties: |
273 | | - msg["properties"] = {**extra_properties, **msg["properties"]} |
| 349 | + msg["properties"] = {**extra_properties, **msg["properties"], **system_context()} |
274 | 350 |
|
275 | 351 | return self._enqueue(msg, disable_geoip) |
276 | 352 |
|
|
0 commit comments