How yinkaisheng/python-uiautomation-for-windows works
Main Components
• Core Library (uiautomation package):
• Contains the heart of the functionality (for example, uiautomation/uiautomation.py and uiautomation/version.py).
• Implements the abstraction of Windows COM objects (IUIAutomationElement) and the search/traversal logic for UI controls.
• Designed for both “direct use” from code and use via the CLI.
• Command Line Interface (CLI) & Scripts:
• The top-level automation.py (and scripts/automation.py) which calls into the core library.
• Provides a user interface in the command prompt whereby a user can execute automation commands (e.g., print controls, send keys).
• Demo Applications:
• Located in the demos directory, these sample scripts (automation_calculator.py, automation_notepad.py, etc.) show how to use the library in real-world scenarios.
• Auxiliary/Support Components:
• Additional scripts (e.g., automation.py under “scripts” folder, rename_wheel.py) which support functionality or serve as utilities.
• Documentation files (README, API changes.txt) and packaging files (setup.py, setup.cfg, MANIFEST.in) used for distribution and configuration.
• External Dependencies & OS Integration:
• The project is built around the Microsoft Windows UI Automation API (a COM-based framework).
• Uses comtypes (for Python versions 3.5+ in uiautomation2.0+ and later) for COM interfacing.
• Relies on Windows-specific services and requires running as administrator on some Windows versions to operate properly.
Relationships and Interactions
• The automation.py CLI (and the scripts/automation.py) act as entry points for end users; they invoke functions within the core library.
• Within the uiautomation package, various classes (e.g., WindowControl, EditControl) encapsulate the logic for locating and interacting with UI elements.
• The demos folder demonstrates how these classes and methods are used to automate real applications (e.g., notepad.exe) by making sequential searches (using searchDepth and other parameters) and directing UI operations (SendKeys, Click, etc.).
• The core library internally calls the Windows COM API (via comtypes) to enumerate, inspect, and manipulate UI controls.
Architectural Patterns and Principles
• Modular Design: Separation into distinct packages/modules (uiautomation, demos, scripts) that isolate core functionality from examples and distribution logic.
• Layered Architecture:
• Presentation Layer: CLI (automation.py) and demos.
• Abstraction Layer: The API wrapping Windows UI Automation; this abstracts COM calls behind Python classes.
• External Dependency Layer: Windows UI Automation Provider/COM APIs.
• Error Handling and Lazy Evaluation:
• The library defers searching for controls until a property or method is called (lazy evaluation) and triggers re-searches via methods like Refind or Exists.
• Specific exceptions (e.g., LookupError, COMError) are used to indicate problems when automating the UI.