././@PaxHeader 0000000 0000000 0000000 00000000034 00000000000 010212 x ustar 00 28 mtime=1740125723.9115314
python_dbusmock-0.34.3/ 0000755 0001751 0000166 00000000000 14756033034 014421 5 ustar 00runner docker ././@PaxHeader 0000000 0000000 0000000 00000000034 00000000000 010212 x ustar 00 28 mtime=1740125723.8995316
python_dbusmock-0.34.3/.fmf/ 0000755 0001751 0000166 00000000000 14756033034 015247 5 ustar 00runner docker ././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125649.0
python_dbusmock-0.34.3/.fmf/version 0000644 0001751 0000166 00000000002 14756032721 016651 0 ustar 00runner docker 1
././@PaxHeader 0000000 0000000 0000000 00000000034 00000000000 010212 x ustar 00 28 mtime=1740125723.8995316
python_dbusmock-0.34.3/.github/ 0000755 0001751 0000166 00000000000 14756033034 015761 5 ustar 00runner docker ././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125649.0
python_dbusmock-0.34.3/.github/release.eloquent.yml 0000644 0001751 0000166 00000000056 14756032721 021762 0 ustar 00runner docker assets:
- path: dist/python_dbusmock*.tar.*
././@PaxHeader 0000000 0000000 0000000 00000000034 00000000000 010212 x ustar 00 28 mtime=1740125723.9005315
python_dbusmock-0.34.3/.github/workflows/ 0000755 0001751 0000166 00000000000 14756033034 020016 5 ustar 00runner docker ././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125649.0
python_dbusmock-0.34.3/.github/workflows/pages.yml 0000644 0001751 0000166 00000002162 14756032721 021643 0 ustar 00runner docker name: Deploy to GitHub pages
on:
push:
branches: ["main"]
# Allow running this workflow manually from the Actions tab
workflow_dispatch:
permissions:
contents: read
pages: write
id-token: write
# Allow only one concurrent deployment
concurrency:
group: "pages"
cancel-in-progress: false
jobs:
deploy:
runs-on: ubuntu-22.04
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
- name: Install dependencies from pip
run: pip install sphinx sphinx-rtd-theme sphinx-autoapi myst-parser
- name: Build with sphinx
run: sphinx-build -M html doc/ doc/
- name: Move output to target directory
run: mv doc/html public_html/
- name: Setup Pages
uses: actions/configure-pages@v3
- name: Upload pages artifact
uses: actions/upload-pages-artifact@v1
with:
path: public_html/
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v2
././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125649.0
python_dbusmock-0.34.3/.github/workflows/release.yml 0000644 0001751 0000166 00000003027 14756032721 022165 0 ustar 00runner docker name: tag
on:
push:
tags:
# this is a glob, not a regexp
- '[0-9]*'
jobs:
release:
runs-on: ubuntu-24.04
environment: release
permissions:
# PyPI trusted publisher
id-token: write
# create release
contents: write
steps:
- name: Clone repository
uses: actions/checkout@v4
with:
# need this to also fetch tags
fetch-depth: 0
- name: Workaround for https://github.com/actions/checkout/pull/697
run: git fetch --force origin $(git describe --tags):refs/tags/$(git describe --tags)
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y python3-build python3-setuptools python3-setuptools-scm python3-venv
- name: Build release tarball
run: python3 -m build --sdist
- name: Sanity check
run: |
set -eux
mkdir tmp
cd tmp
tar xf ../dist/python_dbusmock-${{ github.ref_name }}.tar.gz
cd python_dbusmock-*
test "$(PYTHONPATH=. python3 -c 'import dbusmock; print(dbusmock.__version__)')" = "${{ github.ref_name }}"
PYTHONPATH=. python3 tests/test_api.py
cd ../..
rm -rf tmp
- name: Create GitHub release
uses: cockpit-project/action-release@7d2e2657382e8d34f88a24b5987f2b81ea165785
with:
filename: "dist/python_dbusmock-${{ github.ref_name }}.tar.gz"
- name: Create PyPy release
uses: pypa/gh-action-pypi-publish@release/v1
././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125649.0
python_dbusmock-0.34.3/.github/workflows/tests.yml 0000644 0001751 0000166 00000002274 14756032721 021712 0 ustar 00runner docker name: unit-tests
on:
push:
pull_request:
schedule:
- cron: 0 4 * * MON,FRI
jobs:
OS:
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
scenario:
- docker.io/debian:unstable
- docker.io/ubuntu:devel
- docker.io/ubuntu:rolling
- docker.io/ubuntu:latest
- registry.fedoraproject.org/fedora:latest
- registry.fedoraproject.org/fedora:rawhide
- quay.io/centos/centos:stream9
- quay.io/centos/centos:stream10-development
timeout-minutes: 30
steps:
- name: Clone repository
uses: actions/checkout@v4
with:
# need this to also fetch tags
fetch-depth: 0
- name: Workaround for https://github.com/actions/checkout/pull/697
run: |
set -ex
TAG=$(git describe --tags)
if echo "$TAG" | grep -q '^[0-9.]\+$'; then
git fetch --force origin $TAG:refs/tags/$TAG
fi
- name: Run unit tests
run: |
dpkg -s podman docker || true
cat /etc/apt/sources.list
cat /etc/apt/sources.list.d/*
${{ matrix.env }} tests/run ${{ matrix.scenario }}
././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125649.0
python_dbusmock-0.34.3/.gitignore 0000644 0001751 0000166 00000000133 14756032721 016410 0 ustar 00runner docker *.egg-info
/build
/dbusmock/_version.py
/dist
/doc/doctrees
/doc/html
MANIFEST
__pycache__
././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125649.0
python_dbusmock-0.34.3/.pre-commit-config.yaml 0000644 0001751 0000166 00000000732 14756032721 020706 0 ustar 00runner docker repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-merge-conflict
- id: check-toml
- id: check-symlinks
- id: no-commit-to-branch
args: ['--branch', 'main']
- repo: https://github.com/psf/black
rev: 23.11.0
hooks:
- id: black
args: ['--check', '--diff', '.']
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.1.6
hooks:
- id: ruff
././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125649.0
python_dbusmock-0.34.3/COPYING 0000644 0001751 0000166 00000016743 14756032721 015471 0 ustar 00runner docker GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc.
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125649.0
python_dbusmock-0.34.3/NEWS 0000644 0001751 0000166 00000051033 14756032721 015124 0 ustar 00runner docker OBSOLETE: Release news of newer versions are contained in the git tag descriptions.
## [0.28.1] - 2022-06-28
- Again works on RHEL/CentOS 8 (0.27 broke there), now in CI
- Avoid glib GI dependency for main dbusmock, for running in virtualenv (thanks Allison Karlitskaya)
## [0.28.0] - 2022-06-19
- Drop unmaintained and broken accountsservice template
- testcase: Throw an error when spawning a well-known name that exists (thanks Benjamin Berg)
- mockobject: Allow sending signals with extra details (thanks Peter Hutterer)
## [0.27.5] - 2022-04-05
- bluez and accountsservice templates: Drop default arguments from D-Bus methods (thanks Simon McVittie)
## [0.27.4] - 2022-04-04
- Fix D-Bus signature detection regression from 0.27.0 (thanks Peter Hutterer) (#118)
## [0.27.3] - 2022-03-22
- packit: Fix file name to sync
## [0.27.2] - 2022-03-22
- packit: Really fix Fedora dist-git syncing
## [0.27.1] - 2022-03-22
- packit: Fix Fedora dist-git syncing
## [0.27.0] - 2022-03-22
- Do not register standard session service directories, add API to enable selected services (thanks Benjamin Berg)
- Log static method calls from templates (thanks Peter Hutterer)
## [0.26.1] - 2022-02-25
- Fix README content type to Markdown, to fix releasing to PyPi
## [0.26.0] - 2022-02-25
- logind template: Add locking API (thanks Andy Holmes)
- bluez5 template: Add RemoveDevice() and RemoveAdapterWithDevices() methods,
fix properties (thanks Bastien Nocera)
- Documentation improvements, particularly wrt. raising errors
## [0.25.0] - 2021-12-25
- bluez template: Implement adapter discovery, connect, disconnect, and removal
(thanks Bastien Nocera)
- Fix changing array properties (thanks Jonas Ådahl)
- Fix CLI upower tests (thanks Marco Trevisan)
- Add testing and Fedora updating through packit
## [0.24.1] - 2021-10-27
- Do not register standard D-Bus service directories (Thanks Benjamin Berg)
- templates: Add IIO Sensors Proxy support (Thanks Marco Trevisan)
- Fix importlib module import (Thanks Marco Trevisan)
- Clean up code for most recent pylint version
## [0.24.0] - 2021-08-28
- Add Add power-profiles-daemon template (Thanks Bastien Nocera)
- logind: Implement Inhibit and ListInhibitors (Thanks to Benjamin Berg)
- Fix new complaints from pylint 2.9
## [0.23.1] - 2021-06-15
- Fix tests for Python 3.10
## [0.23.0] - 2021-03-24
- DBusMockObject: Add UpdateProperties() method (Thanks to Jonas Ådahl)
- DBusTestCase: Add bus override argument to spawn_server_template, to use
templates on either system or session bus; add corresponding --session CLI
option
- bluez template: Implement Pair() method on the Device interface and the
AgentManager1 interface (Thanks to Bastien Nocera)
- polkit template: Implement RegisterAuthenticationAgent() (Thanks to Jonas Ådahl)
- Add accountsservice template (Thanks to Marco Trevisan)
## [0.22.0] - 2021-01-02
- NetworkManager template: Fix connecting to wifi
- NetworkManager template: Add Reload() method
- tests: Replace nose test runner with standard unittest
- setup.py: Drop deprecated `test_suite`, run tests through
`python3 -m unittest` directly
## [0.21.0] - 2021-01-01
- Add type annotations to the whole API
- Drop obsolete ConsoleKit and bluez4 templates/tests
- upower template: Drop support for 0.9 API
## [0.20.0] - 2020-12-19
- NetworkManager template: Handle NetworkingEnable
- NetworkManager template: AddConnectionUnsaved
- Drop support for Python 2
## [0.19] - 2020-01-09
- NetworkManager template: Add "StateReason" property and active connection ID
- Add low-memory-monitor template
## [0.18.3] - 2019-09-08
- Fix timeouts for loaded machines and parallel tests
- Fix tests for Python 3.8
## [0.18.2] - 2019-02-23
- test: Adjust for changed version output format in systemd 241
## [0.18.1] - 2018-11-11
- test: Fix failures with systemd 239 in chroots
## [0.18] - 2018-07-01
- logind template: Add support for suspend-then-hibernate
- NetworkManager template: Add GetConnectionByUuid method
- bluez template: Add PairDevice() 'class_' parameter
- Fix logging of unicode strings in Python 2
- Adjust logind and timedated tests to systemd 239 (Debian #902602)
## [0.17.2] - 2018-03-01
- NetworkManager template: Add GetDeviceByIpIface().
- Move release tarballs from Launchpad to
.
remains as usual.
## [0.17.1] - 2018-02-22
- Fix NetworkManager tests due to name changes from 802-11-wireless to wifi.
## [0.17] - 2017-11-07
- Call dbus-daemon directly instead of dbus-launch. (Debian #836053)
- Fix DBusTestCase.stop_dbus() to actually enforce a timeout. Thanks to Renat
Galimov!
- Fix test_timedated for changed timedatectl output format in systemd 235.
## [0.16.9] - 2017-06-19
- NetworkManager template: Fix type of 'State' property in AddActiveConnection()
- Fix BlueZ tests to work with Python 2 again.
## [0.16.8] - 2017-06-12
- bluez5 template tests: Fix failure of test_no_adapters with BlueZ 5.45.
(LP: #1696480)
- tests: Move from pep8 to pycodestyle for static code tests.
- NetworkManager: Add ObjectManager, to work with NM ≥ 1.6.
## [0.16.7] - 2016-09-12
- bluez5 template tests: Fix failure on "Waiting to connect to bluetoothd"
messages.
## [0.16.6] - 2016-06-19
- upower template tests: Fix version comparison.
## [0.16.5] - 2016-06-19
- Ofono template: generate unique ICCIDs (CardIdentifier).
## [0.16.4] - 2016-06-07
- Ofono template: Add missing properties to SimManager interface.
## [0.16.3] - 2015-12-14
- NetworkManager template test: Make the test run standalone. Thanks Pete
Woods.
## [0.16.2] - 2015-12-09
- NetworkManager template: Fix connection settings Updated signal emitted by
wrong object.
- NetworkManager template: Handle empty device at connection activation.
- NetworkManager template: Implement secrets management in settings.
## [0.16.1] - 2015-10-22
- NetworkManager template: Fix indexing bug in SettingsAddConnection. Thanks
Pete Woods.
## [0.16] - 2015-10-08
- NetworkManager template: Generate a new unused name in connection activation
instead of just using the access point name. Thanks Pete Woods for the
original patch!
- Allow the passing of template parameters via the command-line as JSON
strings. Thanks Pete Woods.
## [0.15.3] - 2015-09-16
- NetworkManager template: Add missing properties to ethernet device and
active connection. Thanks Pete Woods.
- Quiesce irrelevant PEP-8 errors with pep8 1.6.
## [0.15.2] - 2015-06-11
- test_ofono: Test fields which don't get obfuscated with Ubuntu's latest
ofono (See LP #1459983). Thanks Iain Lane.
- timedated template: Add NTPSynchronized property and set it in SetNTP(), to
also work with systemd 220.
## [0.15.1] - 2015-05-12
- SECURITY FIX: When loading a template from an arbitrary file through the
AddTemplate() D-Bus method call or DBusTestCase.spawn_server_template()
Python method, don't create or use Python's *.pyc cached files. By tricking
a user into loading a template from a world-writable directory like /tmp, an
attacker could run arbitrary code with the user's privileges by putting a
crafted .pyc file into that directory. Note that this is highly unlikely to
actually appear in practice as custom dbusmock templates are usually shipped
in project directories, not directly in world-writable directories.
Thanks to Simon McVittie for discovering this!
(LP: #1453815, CVE-2015-1326)
## [0.15] - 2015-05-08
- NetworkManager template: Restore nm-specific PropertiesChanged signal
- NetworkManager template: Add DeactivateConnection(),
Settings.AddConnection(), Settings.Connection.Update(), and
Settings.Connection.Delete() methods. Also allow Connections with
autoconnect, added using AddConnection, to be automatically connected by the
first found device, roughly like NetworkManager itself does. Thanks Jonas
Grønås Drange!
- NetworkManager template: Fix broken exception in AddWiFiConnection.
- NetworkManager template: Set RsnFlags to have the same value as WpaFlags.
Thanks Pete Woods!
## [0.14] - 2015-03-30
- Move project hosting to github, update README.rst.
- urfkill template: Return boolean from block() method, as the original
urfkill does. Thanks Jonas Grønås Drange!
- Correctly instantiate DBusExceptions so that they get a proper name and
message (issue #3)
- ofono template: Fix SubscriberIdentity property type
- Emit PropertiesChanged signal when Set()ing properties.
- urfkill template: Return boolean from Block() and FlightMode() methods
- ofono template: Add ConnectionManager interface.
- NetworkManager template: Much more complete support for mocking access
points and connections.
## [0.13] - 2015-02-27
- Add urfkill template. Thanks Jussi Pakkanen!
## [0.12] - 2015-01-17
- upower template: Add SetDeviceProperties() convenience method for changing
upower device properties. Thanks Charles Kerr!
## [0.11.4] - 2014-09-22
- upower template: Go back to using type 's' for Device* signal arguments for
upower 0.9. The older library does not get along with 'o'. (Regression in
0.11.2)
## [0.11.3] - 2014-09-19
- Fix test suite crash if loginctl is not installed.
## [0.11.2] - 2014-09-19
- upower template: Fix type of Device* signal arguments to be 'o', not 's'.
Thanks Iain Lane.
- ofono template: Add org.ofono.SimManager interface. Thanks Jonas Grønås
Drange.
- bluez5 template: Fix the type of the 'Transferred' property. Thanks Philip
Withnall.
- networkmanager template: Fix the "FAILED" state value to be 120, not 12.
Thanks Jonas Grønås Drange.
- bluez5 tests: Accept devices in state "not available" (or other states) as
well.
- timedated and logind tests: Adjust to also accept output format of
systemd 215.
## [0.11.1] - 2014-08-08
- Rename bluez test classes to TestBlueZ4 and TestBlueZ5, to tell them apart
in the output.
- logind template: Fix type of IdleSinceHint property, and add
IdleSinceHintMonotonic and IdleActionUSec properties. (LP: #1348437)
- bluez4 template: Fix settings properties in StartDiscovery/StopDiscovery.
Thanks to Mathieu Trudel-Lapierre.
- NetworkManager template: Add "Devices" and "AccessPoints" properties of
NetworkManager 0.9.10. (LP: #1328579)
- NetworkManager template: Fix the types of the AccessPoint properties, and
add some more properties.
- Adjust NetworkManager template tests for changed strings in NM 0.9.10.
## [0.11] - 2014-07-24
- ofono: Fix GetOperators() to only apply to its own modem, not all modems.
Thanks to Jonas Grønås Drange.
- Add template for BlueZ 4. Thanks to Mathieu Trudel-Lapierre!
## [0.10.3] - 2014-07-16
- Fix upower tests to work for upower 0.99 in environments without a system
D-Bus.
## [0.10.2] - 2014-07-16
- ofono: Make Scan() do the same as GetOperators(). Thanks Iain Lane.
- README.rst: Clarify that the "True" argument to get_dbus() means the system
bus.
- Fix code to be compliant with pep8 1.5.
- Fix TestCLI.test_template_system test with upower 0.99. (LP: #1330037)
- ofono template: Support adding a second modem with AddModem().
(LP: #1340590)
## [0.10.1] - 2014-01-30
- Move code from bzr to git, adjust README.rst, do-release, and other files
accordingly.
- timedated template: Emit PropertiesChanged when setting properties. Thanks
Iain Lane.
## [0.10] - 2013-12-18
- Add dynamic properties to introspection XML, to make mocked properties work
with Qt's property support, d-feet, and other tools. Thanks Iain Lane!
- Fix frequent KeyError() when calling static methods from a template.
Regression from 0.9.
- Support having the same method name on different D-Bus interfaces.
- Add ofono template with support for the Manager, VoiceCallManager, and
NetworkRegistration/NetworkOperator interfaces.
- Add template for systemd's timedated. Thanks Iain Lane!
## [0.9.2] - 2013-12-13
- upower template: Emit DeviceAdded when adding new a new AC adapter or
battery. Thanks Iain Lane!
- Fix ResourceWarnings in test suite.
## [0.9.1] - 2013-12-10
- Fix UnicodeDecodeError in NEWS parsing when trying to build with a C locale.
Thanks Dmitry Shachnev.
## [0.9] - 2013-11-29
- Make static template methods appear in introspection. Thanks Philip
Whitnall! (LP: #1250096)
- Add support for the D-Bus ObjectManager interface. This can now be enabled
in templates (IS_OBJECT_MANAGER = True) or the CLI (--is-object-manager/-m).
Thanks Philip Whitnall!
- Add Reset() mock interface method to reset that object’s state (including
removing all its sub-objects) as if the object or template had been
destroyed and re-created. This is intended for use at the end of test cases
to reset state before the next test case is run, as an alternative to
killing python-dbusmock and re-starting it. Thanks Philip Whitnall!
- logind template: Fix return value of CanSuspend() and related methods,
calling them previously caused a crash.
- Don't close the log file multiple times if it is shared between objects.
Thanks Philip Whitnall!
- Log calls to Get(), GetAll(), and Set().
- Add templates for BlueZ 5 and BlueZ-OBEX. These templates are moderately
well featured, though currently targeted exclusively at testing OBEX vCard
transfers from phones to computers. Thanks Philip Whitnall!
## [0.8.1] - 2013-11-11
- Fix test failure if the "upower" tool is not installed.
- Fix bad upower test which assumed a locale with a comma decimal separator.
## [0.8] - 2013-11-08
- upower template: Change default daemon version to "0.9", and fix tests to
work with both upower 0.9 and 1.0 client tool.
- upower template: Provide 1.0 API if DaemonVersion property/template
parameter gets set to >= "0.99". (LP: #1245955)
- upower template: Add SetupDisplayDevice() method on the mock interface to
conveniently configure all defined properties of the DisplayDevice. Only
available when using the 1.0 API. (LP: #1245955)
## [0.7.2] - 2013-08-30
- Add optional "timeout" argument to DBusTestCase.wait_for_bus_object().
(LP: #1218318)
- DBusTestCase.start_system_bus(): Make the fake bus look more like a real
system bus by specifying a configuration file with type "system".
## [0.7.1] - 2013-08-02
- Handle the "Disconnected" signal only if it comes from the D-Bus object, to
avoid accidentally reacting to other signals (like from BlueZ). Thanks
Lucas De Marchi.
## [0.7] - 2013-07-30
- Accept *.py files for the--template command line option, similar to
AddTemplate(). Thanks Emanuele Aina.
- Pass log file to objects created with AddObject(). Thanks Emanuele Aina.
- NetworkManager template: Add new method AddWiFiConnection(), to simulate a
connection associated to a previously added device (with AddWiFiDevice()).
Thanks Pete Woods.
## [0.6.3] - 2013-06-13
- Drop "can-suspend" and "can-hibernate" checks from upower template test.
upower 1.0 deprecates this functionality, and defaults to not building it.
## [0.6.2] - 2013-06-13
- Fix test_api.TestSubclass test to work without an existing session D-Bus.
## [0.6.1] - 2013-06-13
- Add dbusmock.__version__ attribute, as per PEP-0396.
- Fix not being able to inherit from DBusMockObject. Thanks Lucas De Marchi!
- Allow subclassed DBusMockObject constructors to specify props arguments as
None.
- Fix failing notification-daemon tests for too old libnotify. (LP: #1190208)
- notification_daemon template: Generate new IDs for Notify() calls with an
input ID of 0. (LP: #1190209 part 1)
- notification_daemon template: Send NotificationClosed signal in
CloseNotification() method for valid IDs. (LP: #1190209 part 2)
## [0.6] - 2013-03-20
- Emit MethodCalled() signal on the DBusMock interface, as an alternative way
of verifying method calls. Thanks Lars Uebernickel.
- DBusMockObject.AddTemplate() and DBusTestCase.spawn_server_template() can
now load local templates by specifying a path to a *.py file as template
name. Thanks Lucas De Marchi.
- Quit mock process if the D-Bus it connected to goes down. (LP: #1156561)
- Support Get() and GetAll() with empty interface names, default to main
interface in this case.
- Add template for systemd's logind.
## [0.5] - 2013-02-03
- upower template: Change LidIsClosed property default value to False.
- Add polkitd template. (LP: #1112551)
## [0.4.0] - 2013-01-21
- Add GetCalls(), GetMethodCalls() and ClearCalls() methods on the mock D-Bus
interface to query the call log, for cases where parsing stdout is
inconvenient. Thanks to Robert Bruce Park for the patch! (LP: #1099483)
- Document how to keep internal state in AddMethod().
- Add template for gnome-screensaver. Thanks to Bastien Nocera!
- Fix logging of signal arguments to have the same format as method call
arguments.
## [0.3.1] - 2013-01-07
- upower template: Set Energy and EnergyFull properties.
## [0.3] - 2012-12-19
- In the logging of mock method calls, log the arguments as well.
- Fix race condition in waiting for mock to get online on the bus, by avoiding
triggering D-Bus service activation with system-installed services.
- Add "notification_daemon" mock template.
## [0.2.2] - 2012-11-27
- tests: Suppress "nmcli and NetworkManager versions don't match" warning.
- networkmanager template: Add DeviceState enum for easier specification of
states. Thanks Alberto Ruiz.
- Fix deprecation warnings with PyGObject 3.7.x.
## [0.2.1] - 2012-11-15
- Fix tests to skip instead of fail if NetworkManager or upower are not
installed.
## [0.2.0] - 2012-11-15
- Turn dbusmock from a module into a package. This is transparent for API
users, but necessary for adding future subpackages and also makes the code
more maintainable.
- Run pyflakes and pep8 during test suite, if available.
- Add support for templates: You can now call AddTemplate() on the
org.freedesktop.DBus.Mock interface to load a template into the mock, or in
Python, start a server process with loading a template with
DBusTestCase.spawn_server_template(). Templates set up the common structure
of these services (their main objects, properties, and methods) so that you
do not need to carry around this common code, and only need to set up the
particular properties and specific D-Bus objects that you need. These
templates can be parameterized for common customizations, and they can
provide additional convenience methods on the org.freedesktop.DBus.Mock
interface to provide more abstract functionality like "add a battery".
- Add a first simple "upower" template with convenience methods to add AC and
battery objects, and change tests/test_upower.py to use it.
- Add a first "networkmanager" template with convenience methods to add
Ethernet/WiFi devices and access points. Thanks Iftikhar Ahmad!
- Add symbol dbusmock.MOCK_IFACE for 'org.freedesktop.DBus.Mock'.
- Add test cases for command line invocation (python3-m dbusmock ...).
## [0.1.3] - 2012-11-03
- Ship NEWS in release tarballs.
## [0.1.2] - 2012-10-10
- dbusmock.py, EmitSignal(): Convert arguments to the right data types
according to the signature.
- dbusmock.py, method calls: Convert arguments to the right data types
according to the signature.
- tests/test_api.py: Check proper handling of signature vs. argument list
length and/or type mismatch. (LP: #1064836)
- tests/test_upower.py: Add test for handling "DeviceChanged" signal.
- setup.py: Get version from NEWS file.
## [0.1.1] - 2012-10-04
- setup.py: Use README.rst as long description.
- setup.py: Add download URL.
- tests/test_consolekit.py: Skip test if ck-list-sessions is not installed.
- setup.py: Import "multiprocessing" to work around "'NoneType' object
is not callable" error when running "setup.py test" with python 2.7.
## [0.1] - 2012-10-04
- tests/test_api.py: Add tests for adding existing objects.
- dbusmock.py: Also put first (main) object into the global objects map.
- dbusmock.py: Document global objects map in AddMethod().
- tests/test_upower.py: Silence "No ... property" warnings; the test does not
create them all.
- dbusmock.py: Fix stopping of local D-Bus daemons for test subclasses.
- dbusmock.py, stop_dbus(): Wait for local D-Bus to be killed, and avoid
getting SIGTERMed ourselves.
- dbusmock.py: Add AddProperties() method to conveniently add several
properties to one interface at once. Update upower test case to use this.
- dbusmock.py: Add EmitSignal() method to emit an arbitrary signal on any
interface.
- Make code compatible with Python 2.7 as well. (LP: #1060278)
## [0.0.3] - 2012-10-01
- tests/test_api.py: Add tests for GetAll()
- wait_for_bus_object(): Poll for Introspect() instead of GetAll(), as some
services such as gnome-session don't implement GetAll() properly.
- Rename "dbus_mock" module to "dbusmock", to be more consistent with the
project name and Python module name conventions.
- Support adding properties to different interfaces.
- Support adding methods to different interfaces.
## [0.0.2] - 2012-10-01
- setup.py: Add categories.
## [0.0.1] - 2012-09-28
Initial release.
././@PaxHeader 0000000 0000000 0000000 00000000034 00000000000 010212 x ustar 00 28 mtime=1740125723.9115314
python_dbusmock-0.34.3/PKG-INFO 0000644 0001751 0000166 00000063107 14756033034 015525 0 ustar 00runner docker Metadata-Version: 2.2
Name: python-dbusmock
Version: 0.34.3
Summary: Mock D-Bus objects
Home-page: https://github.com/martinpitt/python-dbusmock
Download-URL: https://pypi.python.org/pypi/python-dbusmock/
Author: Martin Pitt
Author-email: Martin Pitt
License: GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc.
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
Project-URL: homepage, https://github.com/martinpitt/python-dbusmock
Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)
Classifier: Programming Language :: Python :: 3
Classifier: Development Status :: 6 - Mature
Classifier: Operating System :: POSIX :: Linux
Classifier: Operating System :: POSIX :: BSD
Classifier: Operating System :: Unix
Classifier: Topic :: Software Development :: Quality Assurance
Classifier: Topic :: Software Development :: Testing
Classifier: Topic :: Software Development :: Testing :: Mocking
Classifier: Topic :: Software Development :: Testing :: Unit
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: COPYING
Requires-Dist: dbus-python
Dynamic: download-url

python-dbusmock
===============
## Purpose
With this program/Python library you can easily create mock objects on
D-Bus. This is useful for writing tests for software which talks to
D-Bus services such as upower, systemd, logind, gnome-session or others,
and it is hard (or impossible without root privileges) to set the state
of the real services to what you expect in your tests.
Suppose you want to write tests for gnome-settings-daemon's power
plugin, or another program that talks to upower. You want to verify that
after the configured idle time the program suspends the machine. So your
program calls `org.freedesktop.UPower.Suspend()` on the system D-Bus.
Now, your test suite should not really talk to the actual system D-Bus
and the real upower; a `make check` that suspends your machine will not
be considered very friendly by most people, and if you want to run this
in continuous integration test servers or package build environments,
chances are that your process does not have the privilege to suspend, or
there is no system bus or upower to begin with. Likewise, there is no
way for an user process to forcefully set the system/seat idle flag in
logind, so your tests cannot set up the expected test environment on the
real daemon.
That's where mock objects come into play: They look like the real API
(or at least the parts that you actually need), but they do not actually
do anything (or only some action that you specify yourself). You can
configure their state, behaviour and responses as you like in your test,
without making any assumptions about the real system status.
When using a local system/session bus, you can do unit or integration
testing without needing root privileges or disturbing a running system.
The Python API offers some convenience functions like
`start_session_bus()` and `start_system_bus()` for this, in a
`DBusTestCase` class (subclass of the standard `unittest.TestCase`) or
alternatively as a `@pytest.fixture`.
You can use this with any programming language, as you can run the
mocker as a normal program. The actual setup of the mock (adding
objects, methods, properties, and signals) all happen via D-Bus methods
on the `org.freedesktop.DBus.Mock` interface. You just don't have the
convenience D-Bus launch API that way.
## Simple example using Python's unittest
Picking up the above example about mocking upower's `Suspend()` method,
this is how you would set up a mock upower in your test case:
```python
import subprocess
import dbus
import dbusmock
class TestMyProgram(dbusmock.DBusTestCase):
@classmethod
def setUpClass(cls):
cls.start_system_bus()
cls.dbus_con = cls.get_dbus(system_bus=True)
def setUp(self):
self.p_mock = self.spawn_server('org.freedesktop.UPower',
'/org/freedesktop/UPower',
'org.freedesktop.UPower',
system_bus=True,
stdout=subprocess.PIPE)
# Get a proxy for the UPower object's Mock interface
self.dbus_upower_mock = dbus.Interface(self.dbus_con.get_object(
'org.freedesktop.UPower', '/org/freedesktop/UPower'),
dbusmock.MOCK_IFACE)
self.dbus_upower_mock.AddMethod('', 'Suspend', '', '', '')
def tearDown(self):
self.p_mock.stdout.close()
self.p_mock.terminate()
self.p_mock.wait()
def test_suspend_on_idle(self):
# run your program in a way that should trigger one suspend call
# now check the log that we got one Suspend() call
self.assertRegex(self.p_mock.stdout.readline(), b'^[0-9.]+ Suspend$')
```
Let's walk through:
- We derive our tests from `dbusmock.DBusTestCase` instead of
`unittest.TestCase` directly, to make use of the convenience API
to start a local system bus.
- `setUpClass()` starts a local system bus, and makes a connection
to it available to all methods as `dbus_con`. `True` means that we
connect to the system bus, not the session bus. We can use the
same bus for all tests, so doing this once in `setUpClass()`
instead of `setUp()` is enough.
- `setUp()` spawns the mock D-Bus server process for an initial
`/org/freedesktop/UPower` object with an `org.freedesktop.UPower`
D-Bus interface on the system bus. We capture its stdout to be
able to verify that methods were called.
We then call `org.freedesktop.DBus.Mock.AddMethod()` to add a
`Suspend()` method to our new object to the default D-Bus
interface. This will not do anything (except log its call to
stdout). It takes no input arguments, returns nothing, and does
not run any custom code.
- `tearDown()` stops our mock D-Bus server again. We do this so that
each test case has a fresh and clean upower instance, but of
course you can also set up everything in `setUpClass()` if tests
do not interfere with each other on setting up the mock.
- `test_suspend_on_idle()` is the actual test case. It needs to run
your program in a way that should trigger one suspend call. Your
program will try to call `Suspend()`, but as that's now being
served by our mock instead of upower, there will not be any actual
machine suspend. Our mock process will log the method call
together with a time stamp; you can use the latter for doing
timing related tests, but we just ignore it here.
## Simple example using pytest
The same functionality as above but instead using the pytest fixture provided
by this package.
```python
import subprocess
import dbus
import pytest
import dbusmock
@pytest.fixture
def upower_mock(dbusmock_system):
p_mock = dbusmock_system.spawn_server(
'org.freedesktop.UPower',
'/org/freedesktop/UPower',
'org.freedesktop.UPower',
system_bus=True,
stdout=subprocess.PIPE)
# Get a proxy for the UPower object's Mock interface
dbus_upower_mock = dbus.Interface(dbusmock_system.get_dbus(True).get_object(
'org.freedesktop.UPower',
'/org/freedesktop/UPower'
), dbusmock.MOCK_IFACE)
dbus_upower_mock.AddMethod('', 'Suspend', '', '', '')
yield p_mock
p_mock.stdout.close()
p_mock.terminate()
p_mock.wait()
def test_suspend_on_idle(upower_mock):
# run your program in a way that should trigger one suspend call
# now check the log that we got one Suspend() call
assert upower_mock.stdout.readline() == b'^[0-9.]+ Suspend$'
```
Let's walk through:
- We import the `dbusmock_system` fixture from dbusmock which provides us
with a system bus started for our test case wherever the
`dbusmock_system` argument is used by a test case and/or a pytest
fixture.
- The `upower_mock` fixture spawns the mock D-Bus server process for an initial
`/org/freedesktop/UPower` object with an `org.freedesktop.UPower`
D-Bus interface on the system bus. We capture its stdout to be
able to verify that methods were called.
We then call `org.freedesktop.DBus.Mock.AddMethod()` to add a
`Suspend()` method to our new object to the default D-Bus
interface. This will not do anything (except log its call to
stdout). It takes no input arguments, returns nothing, and does
not run any custom code.
This mock server process is yielded to the test function that uses
the `upower_mock` fixture - once the test is complete the process is
terminated again.
- `test_suspend_on_idle()` is the actual test case. It needs to run
your program in a way that should trigger one suspend call. Your
program will try to call `Suspend()`, but as that's now being
served by our mock instead of upower, there will not be any actual
machine suspend. Our mock process will log the method call
together with a time stamp; you can use the latter for doing
timing related tests, but we just ignore it here.
## Simple example from shell
We use the actual session bus for this example. You can use
`dbus-run-session` to start a private one as well if you want, but that
is not part of the actual mocking.
So let's start a mock at the D-Bus name `com.example.Foo` with an
initial "main" object on path /, with the main D-Bus interface
`com.example.Foo.Manager`:
python3 -m dbusmock com.example.Foo / com.example.Foo.Manager
On another terminal, let's first see what it does:
gdbus introspect --session -d com.example.Foo -o /
You'll see that it supports the standard D-Bus `Introspectable` and
`Properties` interfaces, as well as the `org.freedesktop.DBus.Mock`
interface for controlling the mock, but no "real" functionality yet.
So let's add a method:
gdbus call --session -d com.example.Foo -o / -m org.freedesktop.DBus.Mock.AddMethod '' Ping '' '' ''
Now you can see the new method in `introspect`, and call it:
gdbus call --session -d com.example.Foo -o / -m com.example.Foo.Manager.Ping
The mock process in the other terminal will log the method call with a
time stamp, and you'll see something like `1348832614.970 Ping`.
Now add another method with two int arguments and a return value and
call it:
gdbus call --session -d com.example.Foo -o / -m org.freedesktop.DBus.Mock.AddMethod \
'' Add 'ii' 'i' 'ret = args[0] + args[1]'
gdbus call --session -d com.example.Foo -o / -m com.example.Foo.Manager.Add 2 3
This will print `(5,)` as expected (remember that the return value is
always a tuple), and again the mock process will log the Add method
call.
You can do the same operations in e. g. d-feet or any other D-Bus
language binding.
## Interactive debugging
It's possible to use dbus-mock to run interactive sessions using something like:
python3 -m dbusmock com.example.Foo / com.example.Foo.Manager -e $SHELL
Where a shell session with the defined mocks is set and others can be added.
Or more complex ones such as:
python3 -m dbusmock --session -t upower -e \
python3 -m dbusmock com.example.Foo / com.example.Foo.Manager -e \
gdbus introspect --session -d com.example.Foo -o /
## Logging
Usually you want to verify which methods have been called on the mock
with which arguments. There are three ways to do that:
- By default, the mock process writes the call log to stdout.
- You can call the mock process with the `-l`/`--logfile` argument,
or specify a log file object in the `spawn_server()` method if you
are using Python.
- You can use the `GetCalls()`, `GetMethodCalls()` and
`ClearCalls()` methods on the `org.freedesktop.DBus.Mock` D-Bus
interface to get an array of tuples describing the calls.
## Templates
Some D-Bus services are commonly used in test suites, such as UPower or
NetworkManager. python-dbusmock provides "templates" which set up the
common structure of these services (their main objects, properties, and
methods) so that you do not need to carry around this common code, and
only need to set up the particular properties and specific D-Bus objects
that you need. These templates can be parameterized for common
customizations, and they can provide additional convenience methods on
the `org.freedesktop.DBus.Mock` interface to provide more abstract
functionality like "add a battery".
For example, for starting a server with the `upower` template in
Python you can run
(self.p_mock, self.obj_upower) = self.spawn_server_template(
'upower', {'OnBattery': True}, stdout=subprocess.PIPE)
or load a template into an already running server with the
`AddTemplate()` method; this is particularly useful if you are not using
Python:
python3 -m dbusmock --system org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower
gdbus call --system -d org.freedesktop.UPower -o /org/freedesktop/UPower -m org.freedesktop.DBus.Mock.AddTemplate 'upower' '{"OnBattery": }'
This creates all expected properties such as `DaemonVersion`, and
changes the default for one of them (`OnBattery`) through the (optional)
parameters dict.
If you do not need to specify parameters, you can do this in a simpler
way with
python3 -m dbusmock --template upower
The template does not create any devices by default. You can add some
with the template's convenience methods like
ac_path = self.dbusmock.AddAC('mock_AC', 'Mock AC')
bt_path = self.dbusmock.AddChargingBattery('mock_BAT', 'Mock Battery', 30.0, 1200)
or calling `AddObject()` yourself with the desired properties, of
course.
Templates commonly implement some non-trivial functionality with actual Python
methods and the standard [dbus-python](https://dbus.freedesktop.org/doc/dbus-python/)
[`@dbus.service.method`](https://dbus.freedesktop.org/doc/dbus-python/dbus.service.html#dbus.service.method)
decorator.
To build your own template, you can copy
[dbusmock/templates/SKELETON](./dbusmock/templates/SKELETON) to your
new template file name and replace `CHANGEME` with the actual code/values.
Look at [dbusmock/templates/upower.py](./dbusmock/templates/upower.py) for
a real-life implementation.
A template can be loaded from these locations:
* Provide a path to its `.py` file. This is intended for running tests out of
git/build trees with very project specific or unstable templates.
* From [`$XDG_DATA_DIRS/python-dbusmock/templates/`*name*`.py`](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html).
This is intended for shipping reusable templates in distribution development
packages. Load them by module name.
* python-dbusmock [ships a set of widely applicable templates](./dbusmock/templates/)
which are collaboratively maintained, like the `upower` one in the example
above. Load them by module name.
## More Examples
Have a look at the test suite for two real-live use cases:
- `tests/test_upower.py` simulates upowerd, in a more complete way
than in above example and using the `upower` template. It verifies
that `upower --dump` is convinced that it's talking to upower.
- `tests/test_api.py` runs a mock on the session bus and exercises
all available functionality, such as adding additional objects,
properties, multiple methods, input arguments, return values, code
in methods, sending signals, raising exceptions, and introspection.
## Documentation
The `dbusmock` module has extensive documentation built in, which you
can read with e. g. `pydoc3 dbusmock` or online at
https://martinpitt.github.io/python-dbusmock/
`pydoc3 dbusmock.DBusMockObject` shows the D-Bus API of the mock object,
i. e. methods like `AddObject()`, `AddMethod()` etc. which are used to
set up your mock object.
`pydoc3 dbusmock.DBusTestCase` shows the convenience Python API for
writing test cases with local private session/system buses and launching
the server.
`pydoc3 dbusmock.templates` shows all available templates.
`pydoc3 dbusmock.templates.NAME` shows the documentation and available
parameters for the `NAME` template.
`python3 -m dbusmock --help` shows the arguments and options for running
the mock server as a program.
## Development
python-dbusmock is hosted on https://github.com/martinpitt/python-dbusmock
Run the unit tests with `python3 -m unittest` or `pytest`.
In CI, the unit tests run in containers. You can run them locally with e.g.
tests/run registry.fedoraproject.org/fedora:latest
Check the [unit-tests GitHub workflow](.github/workflows/tests.yml) for the
operating systems/container images on which python-dbusmock is tested and
supported.
To debug failures interactively, run
DEBUG=1 tests/run [image]
which will sleep on failures. You can then attach to the running container
image with e.g. `podman exec -itl bash`. The `/source` directory is mounted from the
host, i.e. edit files in your normal git checkout outside of the container, and
re-run all tests in the container shell like above. You can also run a specific
test:
python3 -m unittest tests.test_api.TestAPI.test_onearg_ret
././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125649.0
python_dbusmock-0.34.3/README.md 0000644 0001751 0000166 00000037341 14756032721 015712 0 ustar 00runner docker 
python-dbusmock
===============
## Purpose
With this program/Python library you can easily create mock objects on
D-Bus. This is useful for writing tests for software which talks to
D-Bus services such as upower, systemd, logind, gnome-session or others,
and it is hard (or impossible without root privileges) to set the state
of the real services to what you expect in your tests.
Suppose you want to write tests for gnome-settings-daemon's power
plugin, or another program that talks to upower. You want to verify that
after the configured idle time the program suspends the machine. So your
program calls `org.freedesktop.UPower.Suspend()` on the system D-Bus.
Now, your test suite should not really talk to the actual system D-Bus
and the real upower; a `make check` that suspends your machine will not
be considered very friendly by most people, and if you want to run this
in continuous integration test servers or package build environments,
chances are that your process does not have the privilege to suspend, or
there is no system bus or upower to begin with. Likewise, there is no
way for an user process to forcefully set the system/seat idle flag in
logind, so your tests cannot set up the expected test environment on the
real daemon.
That's where mock objects come into play: They look like the real API
(or at least the parts that you actually need), but they do not actually
do anything (or only some action that you specify yourself). You can
configure their state, behaviour and responses as you like in your test,
without making any assumptions about the real system status.
When using a local system/session bus, you can do unit or integration
testing without needing root privileges or disturbing a running system.
The Python API offers some convenience functions like
`start_session_bus()` and `start_system_bus()` for this, in a
`DBusTestCase` class (subclass of the standard `unittest.TestCase`) or
alternatively as a `@pytest.fixture`.
You can use this with any programming language, as you can run the
mocker as a normal program. The actual setup of the mock (adding
objects, methods, properties, and signals) all happen via D-Bus methods
on the `org.freedesktop.DBus.Mock` interface. You just don't have the
convenience D-Bus launch API that way.
## Simple example using Python's unittest
Picking up the above example about mocking upower's `Suspend()` method,
this is how you would set up a mock upower in your test case:
```python
import subprocess
import dbus
import dbusmock
class TestMyProgram(dbusmock.DBusTestCase):
@classmethod
def setUpClass(cls):
cls.start_system_bus()
cls.dbus_con = cls.get_dbus(system_bus=True)
def setUp(self):
self.p_mock = self.spawn_server('org.freedesktop.UPower',
'/org/freedesktop/UPower',
'org.freedesktop.UPower',
system_bus=True,
stdout=subprocess.PIPE)
# Get a proxy for the UPower object's Mock interface
self.dbus_upower_mock = dbus.Interface(self.dbus_con.get_object(
'org.freedesktop.UPower', '/org/freedesktop/UPower'),
dbusmock.MOCK_IFACE)
self.dbus_upower_mock.AddMethod('', 'Suspend', '', '', '')
def tearDown(self):
self.p_mock.stdout.close()
self.p_mock.terminate()
self.p_mock.wait()
def test_suspend_on_idle(self):
# run your program in a way that should trigger one suspend call
# now check the log that we got one Suspend() call
self.assertRegex(self.p_mock.stdout.readline(), b'^[0-9.]+ Suspend$')
```
Let's walk through:
- We derive our tests from `dbusmock.DBusTestCase` instead of
`unittest.TestCase` directly, to make use of the convenience API
to start a local system bus.
- `setUpClass()` starts a local system bus, and makes a connection
to it available to all methods as `dbus_con`. `True` means that we
connect to the system bus, not the session bus. We can use the
same bus for all tests, so doing this once in `setUpClass()`
instead of `setUp()` is enough.
- `setUp()` spawns the mock D-Bus server process for an initial
`/org/freedesktop/UPower` object with an `org.freedesktop.UPower`
D-Bus interface on the system bus. We capture its stdout to be
able to verify that methods were called.
We then call `org.freedesktop.DBus.Mock.AddMethod()` to add a
`Suspend()` method to our new object to the default D-Bus
interface. This will not do anything (except log its call to
stdout). It takes no input arguments, returns nothing, and does
not run any custom code.
- `tearDown()` stops our mock D-Bus server again. We do this so that
each test case has a fresh and clean upower instance, but of
course you can also set up everything in `setUpClass()` if tests
do not interfere with each other on setting up the mock.
- `test_suspend_on_idle()` is the actual test case. It needs to run
your program in a way that should trigger one suspend call. Your
program will try to call `Suspend()`, but as that's now being
served by our mock instead of upower, there will not be any actual
machine suspend. Our mock process will log the method call
together with a time stamp; you can use the latter for doing
timing related tests, but we just ignore it here.
## Simple example using pytest
The same functionality as above but instead using the pytest fixture provided
by this package.
```python
import subprocess
import dbus
import pytest
import dbusmock
@pytest.fixture
def upower_mock(dbusmock_system):
p_mock = dbusmock_system.spawn_server(
'org.freedesktop.UPower',
'/org/freedesktop/UPower',
'org.freedesktop.UPower',
system_bus=True,
stdout=subprocess.PIPE)
# Get a proxy for the UPower object's Mock interface
dbus_upower_mock = dbus.Interface(dbusmock_system.get_dbus(True).get_object(
'org.freedesktop.UPower',
'/org/freedesktop/UPower'
), dbusmock.MOCK_IFACE)
dbus_upower_mock.AddMethod('', 'Suspend', '', '', '')
yield p_mock
p_mock.stdout.close()
p_mock.terminate()
p_mock.wait()
def test_suspend_on_idle(upower_mock):
# run your program in a way that should trigger one suspend call
# now check the log that we got one Suspend() call
assert upower_mock.stdout.readline() == b'^[0-9.]+ Suspend$'
```
Let's walk through:
- We import the `dbusmock_system` fixture from dbusmock which provides us
with a system bus started for our test case wherever the
`dbusmock_system` argument is used by a test case and/or a pytest
fixture.
- The `upower_mock` fixture spawns the mock D-Bus server process for an initial
`/org/freedesktop/UPower` object with an `org.freedesktop.UPower`
D-Bus interface on the system bus. We capture its stdout to be
able to verify that methods were called.
We then call `org.freedesktop.DBus.Mock.AddMethod()` to add a
`Suspend()` method to our new object to the default D-Bus
interface. This will not do anything (except log its call to
stdout). It takes no input arguments, returns nothing, and does
not run any custom code.
This mock server process is yielded to the test function that uses
the `upower_mock` fixture - once the test is complete the process is
terminated again.
- `test_suspend_on_idle()` is the actual test case. It needs to run
your program in a way that should trigger one suspend call. Your
program will try to call `Suspend()`, but as that's now being
served by our mock instead of upower, there will not be any actual
machine suspend. Our mock process will log the method call
together with a time stamp; you can use the latter for doing
timing related tests, but we just ignore it here.
## Simple example from shell
We use the actual session bus for this example. You can use
`dbus-run-session` to start a private one as well if you want, but that
is not part of the actual mocking.
So let's start a mock at the D-Bus name `com.example.Foo` with an
initial "main" object on path /, with the main D-Bus interface
`com.example.Foo.Manager`:
python3 -m dbusmock com.example.Foo / com.example.Foo.Manager
On another terminal, let's first see what it does:
gdbus introspect --session -d com.example.Foo -o /
You'll see that it supports the standard D-Bus `Introspectable` and
`Properties` interfaces, as well as the `org.freedesktop.DBus.Mock`
interface for controlling the mock, but no "real" functionality yet.
So let's add a method:
gdbus call --session -d com.example.Foo -o / -m org.freedesktop.DBus.Mock.AddMethod '' Ping '' '' ''
Now you can see the new method in `introspect`, and call it:
gdbus call --session -d com.example.Foo -o / -m com.example.Foo.Manager.Ping
The mock process in the other terminal will log the method call with a
time stamp, and you'll see something like `1348832614.970 Ping`.
Now add another method with two int arguments and a return value and
call it:
gdbus call --session -d com.example.Foo -o / -m org.freedesktop.DBus.Mock.AddMethod \
'' Add 'ii' 'i' 'ret = args[0] + args[1]'
gdbus call --session -d com.example.Foo -o / -m com.example.Foo.Manager.Add 2 3
This will print `(5,)` as expected (remember that the return value is
always a tuple), and again the mock process will log the Add method
call.
You can do the same operations in e. g. d-feet or any other D-Bus
language binding.
## Interactive debugging
It's possible to use dbus-mock to run interactive sessions using something like:
python3 -m dbusmock com.example.Foo / com.example.Foo.Manager -e $SHELL
Where a shell session with the defined mocks is set and others can be added.
Or more complex ones such as:
python3 -m dbusmock --session -t upower -e \
python3 -m dbusmock com.example.Foo / com.example.Foo.Manager -e \
gdbus introspect --session -d com.example.Foo -o /
## Logging
Usually you want to verify which methods have been called on the mock
with which arguments. There are three ways to do that:
- By default, the mock process writes the call log to stdout.
- You can call the mock process with the `-l`/`--logfile` argument,
or specify a log file object in the `spawn_server()` method if you
are using Python.
- You can use the `GetCalls()`, `GetMethodCalls()` and
`ClearCalls()` methods on the `org.freedesktop.DBus.Mock` D-Bus
interface to get an array of tuples describing the calls.
## Templates
Some D-Bus services are commonly used in test suites, such as UPower or
NetworkManager. python-dbusmock provides "templates" which set up the
common structure of these services (their main objects, properties, and
methods) so that you do not need to carry around this common code, and
only need to set up the particular properties and specific D-Bus objects
that you need. These templates can be parameterized for common
customizations, and they can provide additional convenience methods on
the `org.freedesktop.DBus.Mock` interface to provide more abstract
functionality like "add a battery".
For example, for starting a server with the `upower` template in
Python you can run
(self.p_mock, self.obj_upower) = self.spawn_server_template(
'upower', {'OnBattery': True}, stdout=subprocess.PIPE)
or load a template into an already running server with the
`AddTemplate()` method; this is particularly useful if you are not using
Python:
python3 -m dbusmock --system org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower
gdbus call --system -d org.freedesktop.UPower -o /org/freedesktop/UPower -m org.freedesktop.DBus.Mock.AddTemplate 'upower' '{"OnBattery": }'
This creates all expected properties such as `DaemonVersion`, and
changes the default for one of them (`OnBattery`) through the (optional)
parameters dict.
If you do not need to specify parameters, you can do this in a simpler
way with
python3 -m dbusmock --template upower
The template does not create any devices by default. You can add some
with the template's convenience methods like
ac_path = self.dbusmock.AddAC('mock_AC', 'Mock AC')
bt_path = self.dbusmock.AddChargingBattery('mock_BAT', 'Mock Battery', 30.0, 1200)
or calling `AddObject()` yourself with the desired properties, of
course.
Templates commonly implement some non-trivial functionality with actual Python
methods and the standard [dbus-python](https://dbus.freedesktop.org/doc/dbus-python/)
[`@dbus.service.method`](https://dbus.freedesktop.org/doc/dbus-python/dbus.service.html#dbus.service.method)
decorator.
To build your own template, you can copy
[dbusmock/templates/SKELETON](./dbusmock/templates/SKELETON) to your
new template file name and replace `CHANGEME` with the actual code/values.
Look at [dbusmock/templates/upower.py](./dbusmock/templates/upower.py) for
a real-life implementation.
A template can be loaded from these locations:
* Provide a path to its `.py` file. This is intended for running tests out of
git/build trees with very project specific or unstable templates.
* From [`$XDG_DATA_DIRS/python-dbusmock/templates/`*name*`.py`](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html).
This is intended for shipping reusable templates in distribution development
packages. Load them by module name.
* python-dbusmock [ships a set of widely applicable templates](./dbusmock/templates/)
which are collaboratively maintained, like the `upower` one in the example
above. Load them by module name.
## More Examples
Have a look at the test suite for two real-live use cases:
- `tests/test_upower.py` simulates upowerd, in a more complete way
than in above example and using the `upower` template. It verifies
that `upower --dump` is convinced that it's talking to upower.
- `tests/test_api.py` runs a mock on the session bus and exercises
all available functionality, such as adding additional objects,
properties, multiple methods, input arguments, return values, code
in methods, sending signals, raising exceptions, and introspection.
## Documentation
The `dbusmock` module has extensive documentation built in, which you
can read with e. g. `pydoc3 dbusmock` or online at
https://martinpitt.github.io/python-dbusmock/
`pydoc3 dbusmock.DBusMockObject` shows the D-Bus API of the mock object,
i. e. methods like `AddObject()`, `AddMethod()` etc. which are used to
set up your mock object.
`pydoc3 dbusmock.DBusTestCase` shows the convenience Python API for
writing test cases with local private session/system buses and launching
the server.
`pydoc3 dbusmock.templates` shows all available templates.
`pydoc3 dbusmock.templates.NAME` shows the documentation and available
parameters for the `NAME` template.
`python3 -m dbusmock --help` shows the arguments and options for running
the mock server as a program.
## Development
python-dbusmock is hosted on https://github.com/martinpitt/python-dbusmock
Run the unit tests with `python3 -m unittest` or `pytest`.
In CI, the unit tests run in containers. You can run them locally with e.g.
tests/run registry.fedoraproject.org/fedora:latest
Check the [unit-tests GitHub workflow](.github/workflows/tests.yml) for the
operating systems/container images on which python-dbusmock is tested and
supported.
To debug failures interactively, run
DEBUG=1 tests/run [image]
which will sleep on failures. You can then attach to the running container
image with e.g. `podman exec -itl bash`. The `/source` directory is mounted from the
host, i.e. edit files in your normal git checkout outside of the container, and
re-run all tests in the container shell like above. You can also run a specific
test:
python3 -m unittest tests.test_api.TestAPI.test_onearg_ret
././@PaxHeader 0000000 0000000 0000000 00000000034 00000000000 010212 x ustar 00 28 mtime=1740125723.9015315
python_dbusmock-0.34.3/dbusmock/ 0000755 0001751 0000166 00000000000 14756033034 016230 5 ustar 00runner docker ././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125649.0
python_dbusmock-0.34.3/dbusmock/__init__.py 0000644 0001751 0000166 00000001762 14756032721 020351 0 ustar 00runner docker """Mock D-Bus objects for test suites."""
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
# of the license.
__author__ = "Martin Pitt"
__copyright__ = """
(c) 2012 Canonical Ltd.
(c) 2017 - 2022 Martin Pitt
"""
from dbusmock.mockobject import MOCK_IFACE, OBJECT_MANAGER_IFACE, DBusMockObject, get_object, get_objects
from dbusmock.testcase import BusType, DBusTestCase, PrivateDBus, SpawnedMock
try:
# created by setuptools_scm
from dbusmock._version import __version__
except ImportError:
__version__ = "0.git"
__all__ = [
"MOCK_IFACE",
"OBJECT_MANAGER_IFACE",
"BusType",
"DBusMockObject",
"DBusTestCase",
"PrivateDBus",
"SpawnedMock",
"get_object",
"get_objects",
]
././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125649.0
python_dbusmock-0.34.3/dbusmock/__main__.py 0000644 0001751 0000166 00000013741 14756032721 020332 0 ustar 00runner docker """Main entry point for running mock server."""
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
# of the license.
__author__ = "Martin Pitt"
__copyright__ = """
(c) 2012 Canonical Ltd.
(c) 2017 - 2022 Martin Pitt
"""
import argparse
import json
import os
import platform
import subprocess
import sys
import dbusmock.mockobject
import dbusmock.testcase
def parse_args():
"""Parse command line arguments"""
parser = argparse.ArgumentParser(description="mock D-Bus object")
parser.add_argument(
"-s",
"--system",
action="store_true",
help="put object(s) on system bus (default: session bus or template's SYSTEM_BUS flag)",
)
parser.add_argument(
"--session",
action="store_true",
help="put object(s) on session bus (default without template; overrides template's SYSTEM_BUS flag)",
)
parser.add_argument(
"-l",
"--logfile",
metavar="PATH",
help="path of log file",
)
parser.add_argument(
"-t",
"--template",
metavar="NAME",
help="template to load (instead of specifying name, path, interface)",
)
parser.add_argument(
"name",
metavar="NAME",
nargs="?",
help='D-Bus name to claim (e. g. "com.example.MyService") (if not using -t)',
)
parser.add_argument(
"path",
metavar="PATH",
nargs="?",
help="D-Bus object path for initial/main object (if not using -t)",
)
parser.add_argument(
"interface",
metavar="INTERFACE",
nargs="?",
help="main D-Bus interface name for initial object (if not using -t)",
)
parser.add_argument(
"-m",
"--is-object-manager",
action="store_true",
help="automatically implement the org.freedesktop.DBus.ObjectManager interface",
)
parser.add_argument(
"-p",
"--parameters",
help="JSON dictionary of parameters to pass to the template",
)
parser.add_argument(
"-e",
"--exec",
nargs=argparse.REMAINDER,
help="Command to run in the mock environment",
)
arguments = parser.parse_args()
if arguments.template:
if arguments.name or arguments.path or arguments.interface:
parser.error("--template and specifying NAME/PATH/INTERFACE are mutually exclusive")
else:
if not arguments.name or not arguments.path or not arguments.interface:
parser.error("Not using a template, you must specify NAME, PATH, and INTERFACE")
if arguments.system and arguments.session:
parser.error("--system and --session are mutually exclusive")
return arguments
if __name__ == "__main__":
import ctypes
import dbus.mainloop.glib
import dbus.service
args = parse_args()
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
system_bus = args.system
if args.template:
module = dbusmock.mockobject.load_module(args.template)
args.name = module.BUS_NAME
args.path = module.MAIN_OBJ
if not args.session and not args.system:
system_bus = module.SYSTEM_BUS
if hasattr(module, "IS_OBJECT_MANAGER"):
args.is_object_manager = module.IS_OBJECT_MANAGER
else:
args.is_object_manager = False
if args.is_object_manager and not hasattr(module, "MAIN_IFACE"):
args.interface = dbusmock.mockobject.OBJECT_MANAGER_IFACE
else:
args.interface = module.MAIN_IFACE
bus = dbusmock.testcase.DBusTestCase.get_dbus(system_bus)
# quit mock when the bus is going down
should_run = {True}
bus.add_signal_receiver(
should_run.pop,
signal_name="Disconnected",
path="/org/freedesktop/DBus/Local",
dbus_interface="org.freedesktop.DBus.Local",
)
bus_name = dbus.service.BusName(args.name, bus, allow_replacement=True, replace_existing=True, do_not_queue=True)
main_object = dbusmock.mockobject.DBusMockObject(
bus_name, args.path, args.interface, {}, args.logfile, args.is_object_manager
)
parameters = None
if args.parameters:
try:
parameters = json.loads(args.parameters)
except ValueError as detail:
sys.stderr.write(f"Malformed JSON given for parameters: {detail}\n")
sys.exit(2)
if not isinstance(parameters, dict):
sys.stderr.write("JSON parameters must be a dictionary\n")
sys.exit(2)
if args.template:
main_object.AddTemplate(args.template, parameters)
if platform.system() == "Darwin":
libglib = ctypes.cdll.LoadLibrary("libglib-2.0.dylib")
else:
libglib = ctypes.cdll.LoadLibrary("libglib-2.0.so.0")
dbusmock.mockobject.objects[args.path] = main_object
if args.exec:
with subprocess.Popen(args.exec) as exec_proc:
exit_status = set()
@ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_int)
def on_process_watch(_pid, status):
"""Check if the launched process is still alive"""
if os.WIFEXITED(status):
exit_status.add(os.WEXITSTATUS(status))
else:
exit_status.add(1)
should_run.pop()
libglib.g_child_watch_add(exec_proc.pid, on_process_watch)
while should_run:
libglib.g_main_context_iteration(None, True)
try:
exec_proc.terminate()
exec_proc.wait()
except ProcessLookupError:
pass
sys.exit(exit_status.pop() if exit_status else exec_proc.returncode)
else:
while should_run:
libglib.g_main_context_iteration(None, True)
././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125723.0
python_dbusmock-0.34.3/dbusmock/_version.py 0000644 0001751 0000166 00000000027 14756033033 020424 0 ustar 00runner docker __version__ = "0.34.3"
././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125649.0
python_dbusmock-0.34.3/dbusmock/mockobject.py 0000644 0001751 0000166 00000107561 14756032721 020736 0 ustar 00runner docker """Mock D-Bus objects for test suites."""
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
# of the license.
__author__ = "Martin Pitt"
__copyright__ = """
(c) 2012 Canonical Ltd.
(c) 2017 - 2022 Martin Pitt
"""
import copy
import functools
import importlib
import importlib.util
import os
import sys
import time
import types
from pathlib import Path
from typing import Any, Dict, KeysView, List, Optional, Sequence, Tuple
from xml.etree import ElementTree as ET
import dbus
import dbus.service
# we do not use this ourselves, but mock methods often want to use this
os # pyflakes pylint: disable=pointless-statement # noqa: B018
# global path -> DBusMockObject mapping
objects: Dict[str, "DBusMockObject"] = {}
MOCK_IFACE = "org.freedesktop.DBus.Mock"
OBJECT_MANAGER_IFACE = "org.freedesktop.DBus.ObjectManager"
PropsType = Dict[str, Any]
# (in_signature, out_signature, code, dbus_wrapper_fn)
MethodType = Tuple[str, str, str, str]
# (timestamp, method_name, call_args)
CallLogType = Tuple[int, str, Sequence[Any]]
def load_module_from_path(path: Path, template_name: str):
"""Load a mock template from a file path"""
spec = importlib.util.spec_from_file_location(template_name, path)
assert spec
mod = importlib.util.module_from_spec(spec)
exec(path.read_text("UTF-8"), mod.__dict__, mod.__dict__) # pylint: disable=exec-used
return mod
def load_module(name: str):
"""Load a mock template Python module
This can be a path to the template's .py file, a bare module name in
``$XDG_DATA_DIRS/python-dbusmock/templates/``, or a bare module name in dbusmock's shipped templates.
"""
# specified by path
pname = Path(name)
if pname.exists() and pname.suffix == ".py":
return load_module_from_path(pname, pname.stem)
# XDG_DATA_DIRS
xdg_data_dirs = os.environ.get("XDG_DATA_DIRS") or "/usr/local/share/:/usr/share/"
for d in xdg_data_dirs.split(":"):
src = Path(d, "python-dbusmock", "templates", name + ".py")
if src.exists():
return load_module_from_path(src, name)
# shipped inside dbusmock package
return importlib.import_module("dbusmock.templates." + name)
def _format_args(args):
"""Format a D-Bus argument tuple into an appropriate logging string"""
def format_arg(a):
if isinstance(a, dbus.Boolean):
return str(bool(a))
if isinstance(a, (dbus.Byte, int)):
return str(int(a))
if isinstance(a, str):
return '"' + str(a) + '"'
if isinstance(a, list):
return "[" + ", ".join([format_arg(x) for x in a]) + "]"
if isinstance(a, dict):
fmta = "{"
first = True
for k, v in a.items():
if first:
first = False
else:
fmta += ", "
fmta += format_arg(k) + ": " + format_arg(v)
return fmta + "}"
# fallback
return repr(a)
s = ""
for a in args:
if s:
s += " "
s += format_arg(a)
if s:
s = " " + s
return s
def _wrap_in_dbus_variant(value):
dbus_types = [
dbus.types.ByteArray,
dbus.types.Int16,
dbus.types.ObjectPath,
dbus.types.Struct,
dbus.types.UInt64,
dbus.types.Boolean,
dbus.types.Dictionary,
dbus.types.Int32,
dbus.types.Signature,
dbus.types.UInt16,
dbus.types.UnixFd,
dbus.types.Byte,
dbus.types.Double,
dbus.types.Int64,
dbus.types.String,
dbus.types.UInt32,
]
if isinstance(value, dbus.String):
return dbus.String(str(value), variant_level=1)
if isinstance(value, dbus.types.Array):
return value
if type(value) in dbus_types:
return type(value)(value.conjugate(), variant_level=1)
if isinstance(value, str):
return dbus.String(value, variant_level=1)
raise dbus.exceptions.DBusException(f"could not wrap type {type(value)}")
def _convert_args(signature: str, args: Tuple[Any, ...]) -> List[Any]:
"""
Convert types of arguments according to signature, using
MethodCallMessage.append(); this will also provide type/length
checks, except for the case of an empty signature
"""
try:
if signature == "" and len(args) > 0:
raise TypeError("Fewer items found in D-Bus signature than in Python arguments")
m = dbus.connection.MethodCallMessage("a.b", "/a", "a.b", "a")
m.append(*args, signature=signature)
return m.get_args_list()
except Exception as e:
raise dbus.exceptions.DBusException(
f"Invalid arguments: {e!s}", name="org.freedesktop.DBus.Error.InvalidArgs"
) from e
def loggedmethod(self, func):
"""Decorator for a method to end in the call log"""
@functools.wraps(func)
def wrapper(*args, **kwargs):
fname = func.__name__
self_arg, args = args[0], args[1:]
in_signature = getattr(func, "_dbus_in_signature", "")
args = _convert_args(in_signature, args)
self.log(fname + _format_args(args))
self.call_log.append((int(time.time()), fname, args))
self.MethodCalled(fname, args)
return func(*[self_arg, *args], **kwargs)
return wrapper
class DBusMockObject(dbus.service.Object): # pylint: disable=too-many-instance-attributes
"""Mock D-Bus object
This can be configured to have arbitrary methods (including code execution)
and properties via methods on the org.freedesktop.DBus.Mock interface, so
that you can control the mock from any programming language.
Beyond that "remote control" API, this is a standard
`dbus-python service object `_.
"""
def __init__(
self,
bus_name: str,
path: str,
interface: str,
props: PropsType,
logfile: Optional[str] = None,
is_object_manager: bool = False,
mock_data: Any = None,
) -> None:
"""Create a new DBusMockObject
:param bus_name: A dbus.service.BusName instance where the object will be put on
:param path: D-Bus object path
:param interface: Primary D-Bus interface name of this object (where
properties and methods will be put on)
:param props: A property_name (string) → property (Variant) map with initial
properties on "interface"
:param logfile: When given, method calls will be logged into that file name;
if None, logging will be written to stdout. Note that you can
also query the called methods over D-Bus with GetCalls() and
GetMethodCalls().
:param is_object_manager: If True, the GetManagedObjects method will
automatically be implemented on the object, returning
all objects which have this one's path as a prefix of
theirs. Note that the InterfacesAdded and
InterfacesRemoved signals will not be automatically
emitted.
"""
dbus.service.Object.__init__(self, bus_name, path)
self.bus_name = bus_name
self.path = path
self.interface = interface
self.is_object_manager = is_object_manager
self.object_manager: Optional[DBusMockObject] = None
self._template: Optional[str] = None
self._template_parameters: Optional[PropsType] = None
# pylint: disable=consider-using-with
self.logfile = open(logfile, "wb") if logfile else None # noqa: SIM115
self.is_logfile_owner = True
self.call_log: List[CallLogType] = []
self.mock_data = mock_data
if props is None:
props = {}
self._reset(props)
def __del__(self) -> None:
try:
if self.logfile and self.is_logfile_owner:
self.logfile.close()
except AttributeError:
pass
def _set_up_object_manager(self) -> None:
"""Set up this mock object as a D-Bus ObjectManager."""
cond = "k != '/'" if self.path == "/" else f"k.startswith('{self.path}/')"
code = f"ret = {{dbus.ObjectPath(k): objects[k].props for k in objects.keys() if {cond} }}"
self.AddMethod(OBJECT_MANAGER_IFACE, "GetManagedObjects", "", "a{oa{sa{sv}}}", code)
self.object_manager = self
def _reset(self, props: PropsType) -> None:
# interface -> name -> value
self.props = {self.interface: props}
# interface -> name -> (in_signature, out_signature, code, dbus_wrapper_fn)
self.methods: Dict[str, Dict[str, MethodType]] = {self.interface: {}}
if self.is_object_manager:
self._set_up_object_manager()
@dbus.service.method(dbus.PROPERTIES_IFACE, in_signature="ss", out_signature="v")
def Get(self, interface_name: str, property_name: str) -> Any:
"""Standard D-Bus API for getting a property value"""
self.log(f"Get {self.path} {interface_name}.{property_name}")
if not interface_name:
interface_name = self.interface
try:
return self.GetAll(interface_name)[property_name]
except KeyError as e:
raise dbus.exceptions.DBusException(
"no such property " + property_name, name=self.interface + ".UnknownProperty"
) from e
@dbus.service.method(dbus.PROPERTIES_IFACE, in_signature="s", out_signature="a{sv}")
def GetAll(self, interface_name: str, *_, **__) -> PropsType:
"""Standard D-Bus API for getting all property values"""
self.log(f"GetAll {self.path} {interface_name}")
if not interface_name:
interface_name = self.interface
try:
return self.props[interface_name]
except KeyError as e:
raise dbus.exceptions.DBusException(
"no such interface " + interface_name, name=self.interface + ".UnknownInterface"
) from e
@dbus.service.method(dbus.PROPERTIES_IFACE, in_signature="ssv", out_signature="")
def Set(self, interface_name: str, property_name: str, value: Any, *_, **__) -> None:
"""Standard D-Bus API for setting a property value"""
self.log(f"Set {self.path} {interface_name}.{property_name}{_format_args((value,))}")
try:
iface_props = self.props[interface_name]
except KeyError as e:
raise dbus.exceptions.DBusException(
"no such interface " + interface_name, name=self.interface + ".UnknownInterface"
) from e
if property_name not in iface_props:
raise dbus.exceptions.DBusException(
"no such property " + property_name, name=self.interface + ".UnknownProperty"
)
iface_props[property_name] = value
self.EmitSignal(
"org.freedesktop.DBus.Properties",
"PropertiesChanged",
"sa{sv}as",
[interface_name, dbus.Dictionary({property_name: value}, signature="sv"), dbus.Array([], signature="s")],
)
@dbus.service.method(MOCK_IFACE, in_signature="ssa{sv}a(ssss)", out_signature="")
def AddObject(
self, path: str, interface: str, properties: PropsType, methods: List[MethodType], **kwargs
) -> None:
"""Dynamically add a new D-Bus object to the mock
:param path: D-Bus object path
:param interface: Primary D-Bus interface name of this object (where
properties and methods will be put on)
:param properties: A property_name (string) → value map with initial
properties on "interface"
:param methods: An array of 4-tuples (name, in_sig, out_sig, code) describing
methods to add to "interface"; see AddMethod() for details of
the tuple values
Keyword Arguments:
:param mock_class: A DBusMockObject derived class name (this is only possible
when the method is not called via dbus)
:param mock_data: Additional data which will be passed to the DBusMockObject
constructor
If this is a D-Bus ObjectManager instance, the InterfacesAdded signal
will *not* be emitted for the object automatically; it must be emitted
manually if desired. This is because AddInterface may be called after
AddObject, but before the InterfacesAdded signal should be emitted.
Example::
dbus_proxy.AddObject('/com/example/Foo/Manager',
'com.example.Foo.Control',
{
'state': dbus.String('online'),
},
[
('Start', '', '', ''),
('EchoInt', 'i', 'i', 'ret = args[0]'),
('GetClients', '', 'ao', 'ret = ["/com/example/Foo/Client1"]'),
])
Example 2 (in a template)::
class FooManager(dbusmock.mockobject.DBusMockObject):
def __init__(self, *args, **kwargs):
super(FooManager, self).__init__(*args, **kwargs)
self.magic = kwargs.get('mock_data')
@dbus.service.method('com.example.Foo.Control', in_signature='i', out_signature='i')
def EchoMagic(self, input):
return self.magic + input
dbus_proxy.AddObject('/com/example/Foo/Manager',
'com.example.Foo.Control',
{}, [],
mock_class=FooManager,
mock_data=42)
"""
if path in objects:
raise dbus.exceptions.DBusException(
f"object {path} already exists", name="org.freedesktop.DBus.Mock.NameError"
)
mock_class = kwargs.get("mock_class", DBusMockObject)
mock_data = kwargs.get("mock_data")
obj = mock_class(self.bus_name, path, interface, properties, mock_data=mock_data)
# make sure created objects inherit the log file stream
obj.logfile = self.logfile
obj.object_manager = self.object_manager
obj.is_logfile_owner = False
obj.AddMethods(interface, methods)
objects[path] = obj
@dbus.service.method(MOCK_IFACE, in_signature="s", out_signature="")
def RemoveObject(self, path: str) -> None:
"""Remove a D-Bus object from the mock
As with AddObject, this will *not* emit the InterfacesRemoved signal if
it's an ObjectManager instance.
"""
try:
objects[path].remove_from_connection()
del objects[path]
except KeyError as e:
raise dbus.exceptions.DBusException(
f"object {path} does not exist", name="org.freedesktop.DBus.Mock.NameError"
) from e
@dbus.service.method(MOCK_IFACE, in_signature="", out_signature="")
def Reset(self) -> None:
"""Reset the mock object state.
Remove all mock objects from the bus and tidy up so the state is as if
python-dbusmock had just been restarted. If the mock object was
originally created with a template (from the command line, the Python
API or by calling AddTemplate over D-Bus), it will be
re-instantiated with that template.
"""
# Clear other existing objects.
for obj_name, obj in objects.items():
if obj_name != self.path:
obj.remove_from_connection()
objects.clear()
# Reinitialise our state. Carefully remove new methods from our dict;
# they don't not actually exist if they are a statically defined
# template function
for method_name in self.methods[self.interface]:
try:
delattr(self.__class__, method_name)
except AttributeError:
pass
self._reset({})
if self._template is not None:
self.AddTemplate(self._template, self._template_parameters)
objects[self.path] = self
@dbus.service.method(MOCK_IFACE, in_signature="sssss", out_signature="")
def AddMethod(self, interface, name: str, in_sig: str, out_sig: str, code: str) -> None:
"""Dynamically add a method to this object
:param interface: D-Bus interface to add this to. For convenience you can
specify '' here to add the method to the object's main
interface (as specified on construction).
:param name: Name of the method
:param in_sig: Signature of input arguments; for example "ias" for a method
that takes an int32 and a string array as arguments; see
`the DBus spec `_.
:param out_sig: Signature of output arguments; for example "s" for a method
that returns a string; use '' for methods that do not return
anything.
:param code: Python 3 code to run in the method call; you have access to the
arguments through the "args" list, and can set the return value
by assigning a value to the "ret" variable. You can also read the
global "objects" variable, which is a dictionary mapping object
paths to DBusMockObject instances.
For keeping state across method calls, you are free to use normal
Python members of the "self" object, which will be persistent for
the whole mock's life time. E. g. you can have a method with
"self.my_state = True", and another method that returns it with
"ret = self.my_state".
Methods can raise exceptions in the usual way, in particular
`dbus.exceptions.DBusException `_.
When specifying '', the method will not do anything (except
logging) and return None.
This is meant for adding a method to a mock at runtime, from any programming language.
You can also use it in templates in the load() function.
For implementing non-trivial and static methods in templates, it is recommended to
implement them in the normal dbus-python way with using the @dbus.service.method
decorator instead.
"""
# pylint: disable=protected-access
if not interface:
interface = self.interface
n_args = len(dbus.Signature(in_sig))
# we need to have separate methods for dbus-python, so clone
# mock_method(); using message_keyword with this dynamic approach fails
# because inspect cannot handle those, so pass on interface and method
# name as first positional arguments
# pylint: disable=unnecessary-lambda-assignment
method = lambda self, *args, **kwargs: DBusMockObject.mock_method( # noqa: E731
self, interface, name, in_sig, *args, **kwargs
)
# we cannot specify in_signature here, as that trips over a consistency
# check in dbus-python; we need to set it manually instead
dbus_method = dbus.service.method(interface, out_signature=out_sig)(method)
dbus_method.__name__ = str(name)
dbus_method._dbus_in_signature = in_sig
dbus_method._dbus_args = [f"arg{i}" for i in range(1, n_args + 1)]
# for convenience, add mocked methods on the primary interface as
# callable methods
if interface == self.interface:
setattr(self.__class__, name, dbus_method)
self.methods.setdefault(interface, {})[str(name)] = (in_sig, out_sig, code, dbus_method)
@dbus.service.method(MOCK_IFACE, in_signature="sa(ssss)", out_signature="")
def AddMethods(self, interface: str, methods: List[MethodType]) -> None:
"""Add several methods to this object
:param interface: D-Bus interface to add this to. For convenience you can
specify '' here to add the method to the object's main
interface (as specified on construction).
:param methods: list of 4-tuples (name, in_sig, out_sig, code) describing one
method each. See AddMethod() for details of the tuple values.
"""
for method in methods:
self.AddMethod(interface, *method)
def _set_property(self, interface, name, value):
# copy.copy removes one level of variant-ness, which means that the
# types get exported in introspection data correctly, but we can't do
# this for container types.
if not isinstance(value, (dbus.Dictionary, dbus.Array)):
value = copy.copy(value)
self.props.setdefault(interface, {})[name] = value
@dbus.service.method(MOCK_IFACE, in_signature="sa{sv}", out_signature="")
def UpdateProperties(self, interface: str, properties: PropsType) -> None:
"""Update properties on this object and send a PropertiesChanged signal
:param interface: D-Bus interface to update this to. For convenience you can
specify '' here to add the property to the object's main
interface (as specified on construction).
:param properties: A property_name (string) → value map
"""
changed_props = {}
for name, value in properties.items():
if not interface:
interface = self.interface
if name not in self.props.get(interface, {}):
raise dbus.exceptions.DBusException(f"property {name} not found", name=interface + ".NoSuchProperty")
self._set_property(interface, name, value)
changed_props[name] = _wrap_in_dbus_variant(value)
self.EmitSignal(dbus.PROPERTIES_IFACE, "PropertiesChanged", "sa{sv}as", [interface, changed_props, []])
@dbus.service.method(MOCK_IFACE, in_signature="ssv", out_signature="")
def AddProperty(self, interface: str, name: str, value: Any) -> None:
"""Add property to this object
:param interface: D-Bus interface to add this to. For convenience you can
specify '' here to add the property to the object's main
interface (as specified on construction).
:param name: Property name.
:param value: Property value.
"""
if not interface:
interface = self.interface
if name in self.props.get(interface, {}):
raise dbus.exceptions.DBusException(
f"property {name} already exists", name=self.interface + ".PropertyExists"
)
self._set_property(interface, name, value)
@dbus.service.method(MOCK_IFACE, in_signature="sa{sv}", out_signature="")
def AddProperties(self, interface: str, properties: PropsType) -> None:
"""Add several properties to this object
:param interface: D-Bus interface to add this to. For convenience you can
specify '' here to add the property to the object's main
interface (as specified on construction).
:param properties: A property_name (string) → value map
"""
for k, v in properties.items():
self.AddProperty(interface, k, v)
@dbus.service.method(MOCK_IFACE, in_signature="sa{sv}", out_signature="")
def AddTemplate(self, template: str, parameters: PropsType) -> None:
"""Load a template into the mock.
python-dbusmock ships a set of standard mocks for common system
services such as UPower and NetworkManager. With these the actual tests
become a lot simpler, as they only have to set up the particular
properties for the tests, and not the skeleton of common properties,
interfaces, and methods.
:param template: Name of the template to load or the full path to a *.py file
for custom templates. See "pydoc dbusmock.templates" for a
list of available templates from python-dbusmock package, and
"pydoc dbusmock.templates.NAME" for documentation about
template NAME.
:param parameters: A parameter (string) → value (variant) map, for
parameterizing templates. Each template can define their
own, see documentation of that particular template for
details.
"""
try:
module = load_module(template)
except ImportError as e:
raise dbus.exceptions.DBusException(
f"Cannot add template {template}: {e!s}", name="org.freedesktop.DBus.Mock.TemplateError"
) from e
# If the template specifies this is an ObjectManager, set that up
if hasattr(module, "IS_OBJECT_MANAGER") and module.IS_OBJECT_MANAGER:
self._set_up_object_manager()
# pick out all D-Bus service methods and add them to our interface
for symbol in dir(module):
# pylint: disable=protected-access
fn = getattr(module, symbol)
if "_dbus_interface" in dir(fn) and ("_dbus_is_signal" not in dir(fn) or not fn._dbus_is_signal):
fn = loggedmethod(self, fn)
# for dbus-python compatibility, add methods as callables
setattr(self.__class__, symbol, fn)
self.methods.setdefault(fn._dbus_interface, {})[str(symbol)] = (
fn._dbus_in_signature,
fn._dbus_out_signature,
"",
fn,
)
if parameters is None:
parameters = {}
module.load(self, parameters)
# save the given template and parameters for re-instantiation on
# Reset()
self._template = template
self._template_parameters = parameters
def _emit_signal(
self, interface: str, name: str, signature: str, sigargs: Tuple[Any, ...], details: PropsType
) -> None:
# pylint: disable=protected-access
if not interface:
interface = self.interface
args = _convert_args(signature, sigargs)
path = details.get("path", self.path)
sig = dbus.lowlevel.SignalMessage(path, interface, name)
sig.append(*args, signature=signature)
dest = details.get("destination", None)
if dest is not None:
sig.set_destination(dest)
for location in self.locations:
conn = location[0]
conn.send_message(sig)
self.log(f"emit {path} {interface}.{name}{_format_args(args)}")
@dbus.service.method(MOCK_IFACE, in_signature="sssav", out_signature="")
def EmitSignal(self, interface: str, name: str, signature: str, sigargs: Tuple[Any, ...]) -> None:
"""Emit a signal from the object.
:param interface: D-Bus interface to send the signal from. For convenience you
can specify '' here to add the method to the object's main
interface (as specified on construction).
:param name: Name of the signal
:param signature: Signature of input arguments; for example "ias" for a signal
that takes an int32 and a string array as arguments; see
http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-signatures
:param args: variant array with signal arguments; must match order and type in
"signature"
"""
self._emit_signal(interface, name, signature, sigargs, {})
@dbus.service.method(MOCK_IFACE, in_signature="sssava{sv}", out_signature="")
def EmitSignalDetailed(
self, interface: str, name: str, signature: str, sigargs: Tuple[Any, ...], details: PropsType
) -> None:
"""Emit a signal from the object with extra details.
:param interface: D-Bus interface to send the signal from. For convenience you
can specify '' here to add the method to the object's main
interface (as specified on construction).
:param name: Name of the signal
:param signature: Signature of input arguments; for example "ias" for a signal
that takes an int32 and a string array as arguments; see
http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-signatures
:param args: variant array with signal arguments; must match order and type in
"signature"
:param details: dictionary with a string key/value entries. Supported keys are:
"destination": for the signal destination
"path": for the object path to send the signal from
"""
self._emit_signal(interface, name, signature, sigargs, details)
@dbus.service.method(MOCK_IFACE, in_signature="", out_signature="a(tsav)")
def GetCalls(self) -> List[CallLogType]:
"""List all the logged calls since the last call to ClearCalls().
Return a list of (timestamp, method_name, args_list) tuples.
"""
return self.call_log
@dbus.service.method(MOCK_IFACE, in_signature="s", out_signature="a(tav)")
def GetMethodCalls(self, method: str) -> List[Tuple[int, Sequence[Any]]]:
"""List all the logged calls of a particular method.
Return a list of (timestamp, args_list) tuples.
"""
return [(row[0], row[2]) for row in self.call_log if row[1] == method]
@dbus.service.method(MOCK_IFACE, in_signature="", out_signature="")
def ClearCalls(self) -> None:
"""Empty the log of mock call signatures."""
self.call_log = []
@dbus.service.signal(MOCK_IFACE, signature="sav")
def MethodCalled(self, name, args):
"""Signal emitted for every called mock method.
This is emitted for all mock method calls. This can be used to confirm
that a particular method was called with particular arguments, as an
alternative to reading the mock's log or GetCalls().
"""
def object_manager_emit_added(self, path: str) -> None:
"""Emit ObjectManager.InterfacesAdded signal"""
if self.object_manager is not None:
self.object_manager.EmitSignal(
OBJECT_MANAGER_IFACE, "InterfacesAdded", "oa{sa{sv}}", [dbus.ObjectPath(path), objects[path].props]
)
def object_manager_emit_removed(self, path: str) -> None:
"""Emit ObjectManager.InterfacesRemoved signal"""
if self.object_manager is not None:
self.object_manager.EmitSignal(
OBJECT_MANAGER_IFACE, "InterfacesRemoved", "oas", [dbus.ObjectPath(path), objects[path].props]
)
def mock_method(self, interface: str, dbus_method: str, in_signature: str, *m_args, **_) -> Any:
"""Master mock method.
This gets "instantiated" in AddMethod(). Execute the code snippet of
the method and return the "ret" variable if it was set.
"""
# print('mock_method', dbus_method, self, in_signature, args, _, file=sys.stderr)
try:
args = _convert_args(in_signature, m_args)
self.log(dbus_method + _format_args(args))
self.call_log.append((int(time.time()), str(dbus_method), args))
self.MethodCalled(dbus_method, args)
# The code may be a Python 3 string to interpret, or may be a function
# object (if AddMethod was called from within Python itself, rather than
# over D-Bus).
code = self.methods[interface][dbus_method][2]
if code and isinstance(code, types.FunctionType):
return code(self, *args)
if code:
loc = locals().copy()
exec(code, globals(), loc) # pylint: disable=exec-used
if "ret" in loc:
return loc["ret"]
except Exception as e:
self.log(dbus_method + " raised: " + str(e))
raise e
return None
def log(self, msg: str) -> None:
"""Log a message, prefixed with a timestamp.
If a log file was specified in the constructor, it is written there,
otherwise it goes to stdout.
"""
fd = self.logfile.fileno() if self.logfile else sys.stdout.fileno()
os.write(fd, f"{time.time():.3f} {msg}\n".encode())
@dbus.service.method(
dbus.INTROSPECTABLE_IFACE,
in_signature="",
out_signature="s",
path_keyword="object_path",
connection_keyword="connection",
)
def Introspect(self, object_path: str, connection: dbus.connection.Connection) -> str:
"""Return XML description of this object's interfaces, methods and signals.
This wraps dbus-python's Introspect() method to include the dynamic
methods and properties.
"""
# _dbus_class_table is an indirect private member of dbus.service.Object that pylint fails to see
# pylint: disable=no-member
# temporarily add our dynamic methods
cls = self.__class__.__module__ + "." + self.__class__.__name__
orig_interfaces = self._dbus_class_table[cls]
mock_interfaces = orig_interfaces.copy()
for iface, methods in self.methods.items():
for method, impl in methods.items():
mock_interfaces.setdefault(iface, {})[method] = impl[3]
self._dbus_class_table[cls] = mock_interfaces
xml = dbus.service.Object.Introspect(self, object_path, connection)
tree = ET.fromstring(xml)
for name, name_props in self.props.items():
# We might have properties for new interfaces we don't know about
# yet. Try to find an existing node named after our
# interface to append to, and create one if we can't.
interface = tree.find(f".//interface[@name='{name}']")
if interface is None:
interface = ET.Element("interface", {"name": name})
tree.append(interface)
for prop, val in name_props.items():
if val is None:
# can't guess type from None, skip
continue
elem = ET.Element(
"property",
{
"name": prop,
# We don't store the signature anywhere, so guess it.
"type": dbus.lowlevel.Message.guess_signature(val),
"access": "readwrite",
},
)
interface.append(elem)
xml = ET.tostring(tree, encoding="utf8", method="xml").decode("utf8")
# restore original class table
self._dbus_class_table[cls] = orig_interfaces
return xml
# Overwrite dbus-python's _method_lookup(), as that offers no way to have the
# same method name on different interfaces
orig_method_lookup = dbus.service._method_lookup # pylint: disable=protected-access
def _dbusmock_method_lookup(obj, method_name, dbus_interface):
try:
m = obj.methods[dbus_interface or obj.interface][method_name]
return (m[3], m[3])
except KeyError:
return orig_method_lookup(obj, method_name, dbus_interface)
dbus.service._method_lookup = _dbusmock_method_lookup # pylint: disable=protected-access
#
# Helper API for templates
#
def get_objects() -> KeysView[str]:
"""Return all existing object paths"""
return objects.keys()
def get_object(path) -> DBusMockObject:
"""Return object for a given object path"""
return objects[path]
././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125649.0
python_dbusmock-0.34.3/dbusmock/pytest_fixtures.py 0000644 0001751 0000166 00000001765 14756032721 022076 0 ustar 00runner docker """pytest fixtures for DBusMock"""
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
# of the license.
__author__ = "Martin Pitt"
__copyright__ = "(c) 2023 Martin Pitt "
from typing import Iterator
import pytest
from dbusmock.testcase import BusType, PrivateDBus
@pytest.fixture(scope="session")
def dbusmock_system() -> Iterator[PrivateDBus]:
"""Export the whole DBusTestCase as a fixture, with the system bus started"""
with PrivateDBus(BusType.SYSTEM) as bus:
yield bus
@pytest.fixture(scope="session")
def dbusmock_session() -> Iterator[PrivateDBus]:
"""Export the whole DBusTestCase as a fixture, with the session bus started"""
with PrivateDBus(BusType.SESSION) as bus:
yield bus
././@PaxHeader 0000000 0000000 0000000 00000000034 00000000000 010212 x ustar 00 28 mtime=1740125723.9045315
python_dbusmock-0.34.3/dbusmock/templates/ 0000755 0001751 0000166 00000000000 14756033034 020226 5 ustar 00runner docker ././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125649.0
python_dbusmock-0.34.3/dbusmock/templates/SKELETON 0000644 0001751 0000166 00000004456 14756032721 021410 0 ustar 00runner docker '''CHANGEME mock template
This creates the expected methods and properties of the main
CHANGEME object, but no devices. You can specify any property
such as CHANGEME in "parameters".
'''
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
# of the license.
__author__ = 'CHANGEME'
__copyright__ = 'CHANGEME'
import dbus
from dbusmock import MOCK_IFACE
# False for session bus, True for system bus; if not present, the bus has to be
# specified in the spawn_server_template() call
SYSTEM_BUS = True # CHANGEME
BUS_NAME = 'org.freedesktop.CHANGEME'
MAIN_OBJ = '/org/freedesktop/CHANGEME'
# If your top-level object is an org.freedesktop.DBus.ObjectManager, you can
# skip setting MAIN_IFACE and set IS_OBJECT_MANAGER to True; then dbusmock will
# automatically provide the GetManagedObjects() API. In all other cases,
# specify the interface name of the main object here.
MAIN_IFACE = 'org.freedesktop.CHANGEME'
# IS_OBJECT_MANAGER = True
def load(mock, parameters):
mock.AddMethods(MAIN_IFACE, [
# CHANGEME: Add some methods if required, otherwise drop the AddMethods call
('CHANGEME', '', 'b', 'ret = %s' % parameters.get('CHANGEME', True)),
])
mock.AddProperties(MAIN_IFACE,
dbus.Dictionary({
# CHANGEME: Add properties if required, otherwise
# drop this call
'MyProperty': parameters.get('MyProperty', 'CHANGEME'),
}, signature='sv'))
# CHANGEME: You can add convenience methods to the org.freedesktop.DBus.Mock
# interface to provide abstract functionality such as adding specific devices
@dbus.service.method(MOCK_IFACE,
in_signature='ss', out_signature='s')
def AddCHANGEME(self, device_name, _CHANGEME):
'''Convenience method to add a CHANGEME object
You have to specify a ...
Please note that this does not set any global properties.
Returns the new object path.
'''
path = '/org/freedesktop/CHANGEME/' + device_name
self.AddObject(path, ...)
return path
././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125649.0
python_dbusmock-0.34.3/dbusmock/templates/__init__.py 0000644 0001751 0000166 00000000576 14756032721 022351 0 ustar 00runner docker """Mock templates for common D-Bus services"""
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
# of the license.
././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125649.0
python_dbusmock-0.34.3/dbusmock/templates/bluez5-obex.py 0000644 0001751 0000166 00000024133 14756032721 022746 0 ustar 00runner docker """obexd mock template
This creates the expected methods and properties of the object manager
org.bluez.obex object (/), the manager object (/org/bluez/obex), but no agents
or clients.
This supports BlueZ 5 only.
"""
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
# of the license.
__author__ = "Philip Withnall"
__copyright__ = """
(c) 2013 Collabora Ltd.
(c) 2017 - 2022 Martin Pitt
"""
import tempfile
from pathlib import Path
import dbus
from dbusmock import OBJECT_MANAGER_IFACE, mockobject
BUS_NAME = "org.bluez.obex"
MAIN_OBJ = "/"
SYSTEM_BUS = False
IS_OBJECT_MANAGER = True
OBEX_MOCK_IFACE = "org.bluez.obex.Mock"
AGENT_MANAGER_IFACE = "org.bluez.AgentManager1"
CLIENT_IFACE = "org.bluez.obex.Client1"
SESSION_IFACE = "org.bluez.obex.Session1"
PHONEBOOK_ACCESS_IFACE = "org.bluez.obex.PhonebookAccess1"
TRANSFER_IFACE = "org.bluez.obex.Transfer1"
TRANSFER_MOCK_IFACE = "org.bluez.obex.transfer1.Mock"
def load(mock, _parameters):
mock.AddObject(
"/org/bluez/obex",
AGENT_MANAGER_IFACE,
{},
[
("RegisterAgent", "os", "", ""),
("UnregisterAgent", "o", "", ""),
],
)
obex = mockobject.objects["/org/bluez/obex"]
obex.AddMethods(
CLIENT_IFACE,
[
("CreateSession", "sa{sv}", "o", CreateSession),
("RemoveSession", "o", "", RemoveSession),
],
)
@dbus.service.method(CLIENT_IFACE, in_signature="sa{sv}", out_signature="o")
def CreateSession(self, destination, args):
"""OBEX method to create a new transfer session.
The destination must be the address of the destination Bluetooth device.
The given arguments must be a map from well-known keys to values,
containing at least the `Target` key, whose value must be `PBAP` (other
keys and values are accepted by the real daemon, but not by this mock
daemon at the moment). If the target is missing or incorrect, an
Unsupported error is returned on the bus.
Returns the path of a new Session object.
"""
if "Target" not in args or args["Target"].upper() != "PBAP":
raise dbus.exceptions.DBusException(
"Non-PBAP targets are not currently supported by this python-dbusmock template.",
name=OBEX_MOCK_IFACE + ".Unsupported",
)
# Find the first unused session ID.
client_path = "/org/bluez/obex/client"
session_id = 0
while client_path + "/session" + str(session_id) in mockobject.objects:
session_id += 1
path = client_path + "/session" + str(session_id)
properties = {
"Source": dbus.String("FIXME"),
"Destination": dbus.String(destination),
"Channel": dbus.Byte(0),
"Target": dbus.String("FIXME"),
"Root": dbus.String("FIXME"),
}
self.AddObject(
path,
SESSION_IFACE,
# Properties
properties,
# Methods
[
("GetCapabilities", "", "s", ""), # Currently a no-op
],
)
session = mockobject.objects[path]
session.AddMethods(
PHONEBOOK_ACCESS_IFACE,
[
("Select", "ss", "", ""), # Currently a no-op
# Currently a no-op
("List", "a{sv}", "a(ss)", 'ret = dbus.Array(signature="(ss)")'),
# Currently a no-op
("ListFilterFields", "", "as", 'ret = dbus.Array(signature="(s)")'),
("PullAll", "sa{sv}", "sa{sv}", PullAll),
("GetSize", "", "q", "ret = 0"),
],
)
manager = mockobject.objects["/"]
manager.EmitSignal(
OBJECT_MANAGER_IFACE,
"InterfacesAdded",
"oa{sa{sv}}",
[
dbus.ObjectPath(path),
{SESSION_IFACE: properties},
],
)
return path
@dbus.service.method(CLIENT_IFACE, in_signature="o", out_signature="")
def RemoveSession(self, session_path):
"""OBEX method to remove an existing transfer session.
This takes the path of the transfer Session object and removes it.
"""
manager = mockobject.objects["/"]
# Remove all the session's transfers.
transfer_id = 0
while session_path + "/transfer" + str(transfer_id) in mockobject.objects:
transfer_path = session_path + "/transfer" + str(transfer_id)
transfer_id += 1
self.RemoveObject(transfer_path)
manager.EmitSignal(
OBJECT_MANAGER_IFACE,
"InterfacesRemoved",
"oas",
[
dbus.ObjectPath(transfer_path),
[TRANSFER_IFACE],
],
)
# Remove the session itself.
self.RemoveObject(session_path)
manager.EmitSignal(
OBJECT_MANAGER_IFACE,
"InterfacesRemoved",
"oas",
[
dbus.ObjectPath(session_path),
[SESSION_IFACE, PHONEBOOK_ACCESS_IFACE],
],
)
@dbus.service.method(PHONEBOOK_ACCESS_IFACE, in_signature="sa{sv}", out_signature="sa{sv}")
def PullAll(self, target_file, filters):
"""OBEX method to start a pull transfer of a phone book.
This doesn't complete the transfer; code to mock up activating and
completing the transfer must be provided by the test driver, as it is
too complex and test-specific to put here.
The target_file is the absolute path for a file which will have zero or
more vCards, separated by new-line characters, written to it if the method
is successful (and the transfer is completed). This target_file is actually
emitted in a TransferCreated signal, which is a special part of the mock
interface designed to be handled by the test driver, which should then
populate that file and call UpdateStatus on the Transfer object. The test
driver is responsible for deleting the file once the test is complete.
The filters parameter is a map of filters to be applied to the results
device-side before transmitting them back to the adapter.
Returns a tuple containing the path for a new Transfer D-Bus object
representing the transfer, and a map of the initial properties of that
Transfer object.
"""
# Find the first unused session ID.
session_path = self.path
transfer_id = 0
while session_path + "/transfer" + str(transfer_id) in mockobject.objects:
transfer_id += 1
transfer_path = session_path + "/transfer" + str(transfer_id)
# Create a new temporary file to transfer to.
with tempfile.NamedTemporaryFile(suffix=".vcf", prefix="tmp-bluez5-obex-PullAll_", delete=False) as temp_file:
filename = Path(temp_file.name).resolve()
props = {
"Status": dbus.String("queued"),
"Session": dbus.ObjectPath(session_path),
"Name": dbus.String(target_file),
"Filename": dbus.String(filename),
"Transferred": dbus.UInt64(0),
}
self.AddObject(
transfer_path,
TRANSFER_IFACE,
# Properties
props,
# Methods
[
("Cancel", "", "", ""), # Currently a no-op
],
)
transfer = mockobject.objects[transfer_path]
transfer.AddMethods(
TRANSFER_MOCK_IFACE,
[
("UpdateStatus", "b", "", UpdateStatus),
],
)
manager = mockobject.objects["/"]
manager.EmitSignal(
OBJECT_MANAGER_IFACE,
"InterfacesAdded",
"oa{sa{sv}}",
[
dbus.ObjectPath(transfer_path),
{TRANSFER_IFACE: props},
],
)
# Emit a behind-the-scenes signal that a new transfer has been created.
manager.EmitSignal(OBEX_MOCK_IFACE, "TransferCreated", "sa{sv}s", [transfer_path, filters, str(filename)])
return (transfer_path, props)
@dbus.service.signal(OBEX_MOCK_IFACE, signature="sa{sv}s")
def TransferCreated(_self, _path, _filters, _transfer_filename):
"""Mock signal emitted when a new Transfer object is created.
This is not part of the BlueZ OBEX interface; it is purely for use by test
driver code. It is emitted by the PullAll method, and is intended to be
used as a signal to call UpdateStatus on the newly created Transfer
(potentially after a timeout).
The path is of the new Transfer object, and the filters are as provided to
PullAll. The transfer filename is the full path and filename of a newly
created empty temporary file which the test driver should write to.
The test driver should then call UpdateStatus on the Transfer object each
time a write is made to the transfer file. The final call to UpdateStatus
should mark the transfer as complete.
The test driver is responsible for deleting the transfer file once the test
is complete.
FIXME: Ideally the UpdateStatus method would only be used for completion,
and all intermediate updates would be found by watching the size of the
transfer file. However, that means adding a dependency on an inotify
package, which seems a little much.
"""
@dbus.service.method(TRANSFER_MOCK_IFACE, in_signature="b", out_signature="")
def UpdateStatus(self, is_complete):
"""Mock method to update the transfer status.
If is_complete is False, this marks the transfer is active; otherwise it
marks the transfer as complete. It is an error to call this method after
calling it with is_complete as True.
In both cases, it updates the number of bytes transferred to be the current
size of the transfer file (whose filename was emitted in the
TransferCreated signal).
"""
status = "complete" if is_complete else "active"
transferred = Path(self.props[TRANSFER_IFACE]["Filename"]).stat().st_size
self.props[TRANSFER_IFACE]["Status"] = status
self.props[TRANSFER_IFACE]["Transferred"] = dbus.UInt64(transferred)
self.EmitSignal(
dbus.PROPERTIES_IFACE,
"PropertiesChanged",
"sa{sv}as",
[
TRANSFER_IFACE,
{
"Status": dbus.String(status),
"Transferred": dbus.UInt64(transferred),
},
[],
],
)
././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125649.0
python_dbusmock-0.34.3/dbusmock/templates/bluez5.py 0000644 0001751 0000166 00000070054 14756032721 022016 0 ustar 00runner docker """bluetoothd mock template
This creates the expected methods and properties of the object manager
org.bluez object (/), the manager object (/org/bluez), but no adapters or
devices.
This supports BlueZ 5 only.
"""
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
# of the license.
__author__ = "Philip Withnall"
__copyright__ = """
(c) 2013 Collabora Ltd.
(c) 2017 - 2022 Martin Pitt
"""
from pathlib import Path
import dbus
from dbusmock import OBJECT_MANAGER_IFACE, mockobject
BUS_NAME = "org.bluez"
MAIN_OBJ = "/"
SYSTEM_BUS = True
IS_OBJECT_MANAGER = True
BLUEZ_MOCK_IFACE = "org.bluez.Mock"
AGENT_MANAGER_IFACE = "org.bluez.AgentManager1"
PROFILE_MANAGER_IFACE = "org.bluez.ProfileManager1"
ADAPTER_IFACE = "org.bluez.Adapter1"
MEDIA_IFACE = "org.bluez.Media1"
NETWORK_SERVER_IFACE = "org.bluez.Network1"
DEVICE_IFACE = "org.bluez.Device1"
LE_ADVERTISING_MANAGER_IFACE = "org.bluez.LEAdvertisingManager1"
LE_ADVERTISEMENT_IFACE = "org.bluez.LEAdvertisement1"
ADVERTISEMENT_MONITOR_MANAGER_IFACE = "org.bluez.AdvertisementMonitorManager1"
ADVERTISEMENT_MONITOR_IFACE = "org.bluez.AdvertisementMonitor1"
# The device class of some arbitrary Android phone.
MOCK_PHONE_CLASS = 5898764
# Maximum number of BLE advertisements supported per adapter.
MAX_ADVERTISEMENT_INSTANCES = 5
@dbus.service.method(AGENT_MANAGER_IFACE, in_signature="os", out_signature="")
def RegisterAgent(manager, agent_path, capability):
all_caps = ["DisplayOnly", "DisplayYesNo", "KeyboardOnly", "NoInputNoOutput", "KeyboardDisplay"]
if agent_path in manager.agent_paths:
raise dbus.exceptions.DBusException(
"Another agent is already registered " + agent_path, name="org.bluez.Error.AlreadyExists"
)
# Fallback to "KeyboardDisplay" as per BlueZ spec
if not capability:
capability = "KeyboardDisplay"
if capability not in all_caps:
raise dbus.exceptions.DBusException(
"Unsupported capability " + capability, name="org.bluez.Error.InvalidArguments"
)
if not manager.default_agent:
manager.default_agent = agent_path
manager.agent_paths += [agent_path]
manager.capabilities[str(agent_path)] = capability
@dbus.service.method(AGENT_MANAGER_IFACE, in_signature="o", out_signature="")
def UnregisterAgent(manager, agent_path):
if agent_path not in manager.agent_paths:
raise dbus.exceptions.DBusException("Agent not registered " + agent_path, name="org.bluez.Error.DoesNotExist")
manager.agent_paths.remove(agent_path)
del manager.capabilities[agent_path]
if manager.default_agent == agent_path:
if len(manager.agent_paths) > 0:
manager.default_agent = manager.agent_paths[-1]
else:
manager.default_agent = None
@dbus.service.method(AGENT_MANAGER_IFACE, in_signature="o", out_signature="")
def RequestDefaultAgent(manager, agent_path):
if agent_path not in manager.agent_paths:
raise dbus.exceptions.DBusException("Agent not registered " + agent_path, name="org.bluez.Error.DoesNotExist")
manager.default_agent = agent_path
def load(mock, parameters):
mock.AddObject(
"/org/bluez",
AGENT_MANAGER_IFACE,
{},
[
("RegisterAgent", "os", "", RegisterAgent),
("RequestDefaultAgent", "o", "", RequestDefaultAgent),
("UnregisterAgent", "o", "", UnregisterAgent),
],
)
bluez = mockobject.objects["/org/bluez"]
bluez.AddMethods(
PROFILE_MANAGER_IFACE,
[
("RegisterProfile", "osa{sv}", "", ""),
("UnregisterProfile", "o", "", ""),
],
)
bluez.agent_paths = []
bluez.capabilities = {}
bluez.default_agent = None
# whether to expose the LEAdvertisingManager1 interface on adapters (BLE advertising)
bluez.enable_advertise_api = parameters.get("enable_advertise_api", True)
# whether to expose the AdvertisementMonitorManager1 interface on adapters (Passive scanning)
bluez.enable_monitor_api = parameters.get("enable_monitor_api", True)
@dbus.service.method(ADAPTER_IFACE, in_signature="o", out_signature="")
def RemoveDevice(adapter, path):
adapter.RemoveObject(path)
manager = mockobject.objects["/"]
manager.EmitSignal(
OBJECT_MANAGER_IFACE,
"InterfacesRemoved",
"oas",
[
dbus.ObjectPath(path),
[DEVICE_IFACE],
],
)
@dbus.service.method(ADAPTER_IFACE, in_signature="", out_signature="")
def StartDiscovery(adapter):
adapter.props[ADAPTER_IFACE]["Discovering"] = True
# NOTE: discovery filter support is minimal to mock
# the Discoverable discovery filter
if adapter.props[ADAPTER_IFACE]["DiscoveryFilter"] is not None:
adapter.props[ADAPTER_IFACE]["Discoverable"] = True
adapter.EmitSignal(
dbus.PROPERTIES_IFACE,
"PropertiesChanged",
"sa{sv}as",
[
ADAPTER_IFACE,
{
"Discoverable": dbus.Boolean(adapter.props[ADAPTER_IFACE]["Discoverable"]),
"Discovering": dbus.Boolean(adapter.props[ADAPTER_IFACE]["Discovering"]),
},
[],
],
)
@dbus.service.method(ADAPTER_IFACE, in_signature="", out_signature="")
def StopDiscovery(adapter):
adapter.props[ADAPTER_IFACE]["Discovering"] = False
# NOTE: discovery filter support is minimal to mock
# the Discoverable discovery filter
if adapter.props[ADAPTER_IFACE]["DiscoveryFilter"] is not None:
adapter.props[ADAPTER_IFACE]["Discoverable"] = False
adapter.EmitSignal(
dbus.PROPERTIES_IFACE,
"PropertiesChanged",
"sa{sv}as",
[
ADAPTER_IFACE,
{
"Discoverable": dbus.Boolean(adapter.props[ADAPTER_IFACE]["Discoverable"]),
"Discovering": dbus.Boolean(adapter.props[ADAPTER_IFACE]["Discovering"]),
},
[],
],
)
@dbus.service.method(ADAPTER_IFACE, in_signature="a{sv}", out_signature="")
def SetDiscoveryFilter(adapter, discovery_filter):
adapter.props[ADAPTER_IFACE]["DiscoveryFilter"] = discovery_filter
@dbus.service.method(BLUEZ_MOCK_IFACE, in_signature="ss", out_signature="s")
def AddAdapter(self, device_name, system_name):
"""Convenience method to add a Bluetooth adapter
You have to specify a device name which must be a valid part of an object
path, e. g. "hci0", and an arbitrary system name (pretty hostname).
Returns the new object path.
"""
path = "/org/bluez/" + device_name
address_start = int(device_name[-1])
address = (
f"{address_start:02d}:{address_start + 1:02d}:{address_start + 2:02d}:"
f"{address_start + 3:02d}:{address_start + 4:02d}:{address_start + 5:02d}"
)
adapter_properties = {
"UUIDs": dbus.Array(
[
# Reference:
# http://git.kernel.org/cgit/bluetooth/bluez.git/tree/lib/uuid.h
# PNP
"00001200-0000-1000-8000-00805f9b34fb",
# Generic Access Profile
"00001800-0000-1000-8000-00805f9b34fb",
# Generic Attribute Profile
"00001801-0000-1000-8000-00805f9b34fb",
# Audio/Video Remote Control Profile (remote)
"0000110e-0000-1000-8000-00805f9b34fb",
# Audio/Video Remote Control Profile (target)
"0000110c-0000-1000-8000-00805f9b34fb",
],
),
"Discoverable": dbus.Boolean(False),
"Discovering": dbus.Boolean(False),
"Pairable": dbus.Boolean(True),
"Powered": dbus.Boolean(True),
"Address": dbus.String(address),
"AddressType": dbus.String("public"),
"Alias": dbus.String(system_name),
"Modalias": dbus.String("usb:v1D6Bp0245d050A"),
"Name": dbus.String(system_name),
# Reference:
# http://bluetooth-pentest.narod.ru/software/
# bluetooth_class_of_device-service_generator.html
"Class": dbus.UInt32(268), # Computer, Laptop
"DiscoverableTimeout": dbus.UInt32(180),
"PairableTimeout": dbus.UInt32(0),
"Roles": dbus.Array(["central", "peripheral"]),
}
self.AddObject(
path,
ADAPTER_IFACE,
# Properties
adapter_properties,
# Methods
[
("RemoveDevice", "o", "", RemoveDevice),
("StartDiscovery", "", "", StartDiscovery),
("StopDiscovery", "", "", StopDiscovery),
("SetDiscoveryFilter", "a{sv}", "", SetDiscoveryFilter),
],
)
adapter = mockobject.objects[path]
adapter.AddMethods(
MEDIA_IFACE,
[
("RegisterEndpoint", "oa{sv}", "", ""),
("UnregisterEndpoint", "o", "", ""),
],
)
adapter.AddMethods(
NETWORK_SERVER_IFACE,
[
("Register", "ss", "", ""),
("Unregister", "s", "", ""),
],
)
bluez = mockobject.objects["/org/bluez"]
# Advertising Manager
if bluez.enable_advertise_api:
# Example values below from an Intel AX200 adapter
advertising_manager_properties = {
"ActiveInstances": dbus.Byte(0),
"SupportedInstances": dbus.Byte(MAX_ADVERTISEMENT_INSTANCES),
"SupportedIncludes": dbus.Array(["tx-power", "appearance", "local-name", "rssi"]),
"SupportedSecondaryChannels": dbus.Array(["1M", "2M", "Coded"]),
"SupportedCapabilities": dbus.Dictionary(
{
"MaxAdvLen": dbus.Byte(251),
"MaxScnRspLen": dbus.Byte(251),
"MinTxPower": dbus.Int16(-34),
"MaxTxPower": dbus.Int16(7),
},
signature="sv",
),
"SupportedFeatures": dbus.Array(
[
"CanSetTxPower",
"HardwareOffload",
],
),
}
adapter.AddProperties(LE_ADVERTISING_MANAGER_IFACE, advertising_manager_properties)
adapter.AddMethods(
LE_ADVERTISING_MANAGER_IFACE,
[
("RegisterAdvertisement", "oa{sv}", "", RegisterAdvertisement),
("UnregisterAdvertisement", "o", "", UnregisterAdvertisement),
],
)
# Track advertisements per adapter
adapter.advertisements = []
# Advertisement Monitor Manager
if bluez.enable_monitor_api:
advertisement_monitor_manager_properties = {
"SupportedMonitorTypes": dbus.Array(["or_patterns"]),
}
adapter.AddProperties(ADVERTISEMENT_MONITOR_MANAGER_IFACE, advertisement_monitor_manager_properties)
adapter.AddMethods(
ADVERTISEMENT_MONITOR_MANAGER_IFACE,
[
("RegisterMonitor", "o", "", RegisterMonitor),
("UnregisterMonitor", "o", "", UnregisterMonitor),
],
)
# Track advertisement monitors per adapter
adapter.monitors = []
manager = mockobject.objects["/"]
manager.EmitSignal(
OBJECT_MANAGER_IFACE,
"InterfacesAdded",
"oa{sa{sv}}",
[
dbus.ObjectPath(path),
{ADAPTER_IFACE: adapter_properties},
],
)
return path
@dbus.service.method(BLUEZ_MOCK_IFACE, in_signature="s")
def RemoveAdapter(self, device_name):
"""Convenience method to remove a Bluetooth adapter"""
path = "/org/bluez/" + device_name
# We could remove the devices related to the adapters here, but
# when bluez crashes, the InterfacesRemoved aren't necessarily sent
# devices first, so in effect, our laziness is testing an edge case
# in the clients
self.RemoveObject(path)
manager = mockobject.objects["/"]
manager.EmitSignal(
OBJECT_MANAGER_IFACE,
"InterfacesRemoved",
"oas",
[
dbus.ObjectPath(path),
[ADAPTER_IFACE],
],
)
@dbus.service.method(BLUEZ_MOCK_IFACE, in_signature="s")
def RemoveAdapterWithDevices(self, device_name):
"""Convenience method to remove a Bluetooth adapter and all
the devices associated to it
"""
adapter_path = "/org/bluez/" + device_name
adapter = mockobject.objects[adapter_path]
manager = mockobject.objects["/"]
to_remove = []
for path in mockobject.objects:
if path.startswith(adapter_path + "/"):
to_remove.append(path)
for path in to_remove:
adapter.RemoveObject(path)
manager.EmitSignal(
OBJECT_MANAGER_IFACE,
"InterfacesRemoved",
"oas",
[
dbus.ObjectPath(path),
[DEVICE_IFACE],
],
)
self.RemoveObject(adapter_path)
manager.EmitSignal(
OBJECT_MANAGER_IFACE,
"InterfacesRemoved",
"oas",
[
dbus.ObjectPath(adapter_path),
[ADAPTER_IFACE],
],
)
@dbus.service.method(DEVICE_IFACE, in_signature="", out_signature="")
def Pair(device):
if device.paired:
raise dbus.exceptions.DBusException("Device already paired", name="org.bluez.Error.AlreadyExists")
device_address = device.props[DEVICE_IFACE]["Address"]
adapter_device_name = Path(device.props[DEVICE_IFACE]["Adapter"]).name
device.PairDevice(adapter_device_name, device_address)
@dbus.service.method(DEVICE_IFACE, in_signature="", out_signature="")
def Connect(device):
if device.connected:
raise dbus.exceptions.DBusException("Already Connected", name="org.bluez.Error.AlreadyConnected")
device.connected = True
device.EmitSignal(
dbus.PROPERTIES_IFACE,
"PropertiesChanged",
"sa{sv}as",
[
DEVICE_IFACE,
{
"Connected": dbus.Boolean(device.connected),
},
[],
],
)
@dbus.service.method(DEVICE_IFACE, in_signature="", out_signature="")
def Disconnect(device):
if not device.connected:
raise dbus.exceptions.DBusException("Not Connected", name="org.bluez.Error.NotConnected")
device.connected = False
device.EmitSignal(
dbus.PROPERTIES_IFACE,
"PropertiesChanged",
"sa{sv}as",
[
DEVICE_IFACE,
{
"Connected": dbus.Boolean(device.connected),
},
[],
],
)
@dbus.service.method(BLUEZ_MOCK_IFACE, in_signature="sss", out_signature="s")
def AddDevice(self, adapter_device_name, device_address, alias):
"""Convenience method to add a Bluetooth device
You have to specify a device address which must be a valid Bluetooth
address (e.g. 'AA:BB:CC:DD:EE:FF'). The alias is the human-readable name
for the device (e.g. as set on the device itself), and the adapter device
name is the device_name passed to AddAdapter.
This will create a new, unpaired and unconnected device with some default properties
like MOCK_PHONE_CLASS "Class" and a static "Modalias". Especially when working with more
than one device, you may want to change these after creation.
Returns the new object path.
"""
device_name = "dev_" + device_address.replace(":", "_").upper()
adapter_path = "/org/bluez/" + adapter_device_name
path = adapter_path + "/" + device_name
if adapter_path not in mockobject.objects:
raise dbus.exceptions.DBusException(
f"Adapter {adapter_device_name} does not exist.", name=BLUEZ_MOCK_IFACE + ".NoSuchAdapter"
)
properties = {
"Address": dbus.String(device_address),
"AddressType": dbus.String("public"),
"Name": dbus.String(alias),
"Icon": dbus.String("phone"),
"Class": dbus.UInt32(MOCK_PHONE_CLASS),
"Appearance": dbus.UInt16(0),
"UUIDs": dbus.Array([], signature="s"),
"Paired": dbus.Boolean(False),
"Connected": dbus.Boolean(False),
"Trusted": dbus.Boolean(False),
"Blocked": dbus.Boolean(False),
"WakeAllowed": dbus.Boolean(False),
"Alias": dbus.String(alias),
"Adapter": dbus.ObjectPath(adapter_path),
"LegacyPairing": dbus.Boolean(False),
"Modalias": dbus.String("bluetooth:v000Fp1200d1436"),
"RSSI": dbus.Int16(-79), # arbitrary
"TxPower": dbus.Int16(0),
"ManufacturerData": dbus.Array([], signature="a{qv}"),
"ServiceData": dbus.Array([], signature="a{sv}"),
"ServicesResolved": dbus.Boolean(False),
"AdvertisingFlags": dbus.Array([], signature="ay"),
"AdvertisingData": dbus.Array([], signature="a{yv}"),
}
self.AddObject(
path,
DEVICE_IFACE,
# Properties
properties,
# Methods
[
("CancelPairing", "", "", ""),
("Connect", "", "", Connect),
("ConnectProfile", "s", "", ""),
("Disconnect", "", "", Disconnect),
("DisconnectProfile", "s", "", ""),
("Pair", "", "", Pair),
],
)
device = mockobject.objects[path]
device.paired = False
device.connected = False
manager = mockobject.objects["/"]
manager.EmitSignal(
OBJECT_MANAGER_IFACE,
"InterfacesAdded",
"oa{sa{sv}}",
[
dbus.ObjectPath(path),
{DEVICE_IFACE: properties},
],
)
return path
@dbus.service.method(BLUEZ_MOCK_IFACE, in_signature="ss", out_signature="")
def PairDevice(_self, adapter_device_name, device_address):
"""Convenience method to mark an existing device as paired.
You have to specify a device address which must be a valid Bluetooth
address (e.g. 'AA:BB:CC:DD:EE:FF'). The adapter device name is the
device_name passed to AddAdapter.
This unblocks the device if it was blocked.
If the specified adapter or device doesn't exist, a NoSuchAdapter or
NoSuchDevice error will be returned on the bus.
Returns nothing.
"""
device_name = "dev_" + device_address.replace(":", "_").upper()
adapter_path = "/org/bluez/" + adapter_device_name
device_path = adapter_path + "/" + device_name
if adapter_path not in mockobject.objects:
raise dbus.exceptions.DBusException(
f"Adapter {adapter_device_name} does not exist.", name=BLUEZ_MOCK_IFACE + ".NoSuchAdapter"
)
if device_path not in mockobject.objects:
raise dbus.exceptions.DBusException(
f"Device {device_name} does not exist.", name=BLUEZ_MOCK_IFACE + ".NoSuchDevice"
)
device = mockobject.objects[device_path]
device.paired = True
# Based off pairing with an Android phone.
uuids = [
"00001105-0000-1000-8000-00805f9b34fb",
"0000110a-0000-1000-8000-00805f9b34fb",
"0000110c-0000-1000-8000-00805f9b34fb",
"00001112-0000-1000-8000-00805f9b34fb",
"00001115-0000-1000-8000-00805f9b34fb",
"00001116-0000-1000-8000-00805f9b34fb",
"0000111f-0000-1000-8000-00805f9b34fb",
"0000112f-0000-1000-8000-00805f9b34fb",
"00001200-0000-1000-8000-00805f9b34fb",
]
device.UpdateProperties(
DEVICE_IFACE,
{
"UUIDs": dbus.Array(uuids),
"Paired": dbus.Boolean(True),
"LegacyPairing": dbus.Boolean(True),
"Blocked": dbus.Boolean(False),
},
)
@dbus.service.method(BLUEZ_MOCK_IFACE, in_signature="ss", out_signature="")
def BlockDevice(_self, adapter_device_name, device_address):
"""Convenience method to mark an existing device as blocked.
You have to specify a device address which must be a valid Bluetooth
address (e.g. 'AA:BB:CC:DD:EE:FF'). The adapter device name is the
device_name passed to AddAdapter.
This disconnects the device if it was connected.
If the specified adapter or device doesn't exist, a NoSuchAdapter or
NoSuchDevice error will be returned on the bus.
Returns nothing.
"""
device_name = "dev_" + device_address.replace(":", "_").upper()
adapter_path = "/org/bluez/" + adapter_device_name
device_path = adapter_path + "/" + device_name
if adapter_path not in mockobject.objects:
raise dbus.exceptions.DBusException(
f"Adapter {adapter_device_name} does not exist.", name=BLUEZ_MOCK_IFACE + ".NoSuchAdapter"
)
if device_path not in mockobject.objects:
raise dbus.exceptions.DBusException(
f"Device {device_name} does not exist.", name=BLUEZ_MOCK_IFACE + ".NoSuchDevice"
)
device = mockobject.objects[device_path]
device.props[DEVICE_IFACE]["Blocked"] = dbus.Boolean(True)
device.props[DEVICE_IFACE]["Connected"] = dbus.Boolean(False)
device.EmitSignal(
dbus.PROPERTIES_IFACE,
"PropertiesChanged",
"sa{sv}as",
[
DEVICE_IFACE,
{
"Blocked": dbus.Boolean(True),
"Connected": dbus.Boolean(False),
},
[],
],
)
@dbus.service.method(BLUEZ_MOCK_IFACE, in_signature="ss", out_signature="")
def ConnectDevice(_self, adapter_device_name, device_address):
"""Convenience method to mark an existing device as connected.
You have to specify a device address which must be a valid Bluetooth
address (e.g. 'AA:BB:CC:DD:EE:FF'). The adapter device name is the
device_name passed to AddAdapter.
This unblocks the device if it was blocked.
If the specified adapter or device doesn't exist, a NoSuchAdapter or
NoSuchDevice error will be returned on the bus.
Returns nothing.
"""
device_name = "dev_" + device_address.replace(":", "_").upper()
adapter_path = "/org/bluez/" + adapter_device_name
device_path = adapter_path + "/" + device_name
if adapter_path not in mockobject.objects:
raise dbus.exceptions.DBusException(
f"Adapter {adapter_device_name} does not exist.", name=BLUEZ_MOCK_IFACE + ".NoSuchAdapter"
)
if device_path not in mockobject.objects:
raise dbus.exceptions.DBusException(
f"Device {device_name} does not exist.", name=BLUEZ_MOCK_IFACE + ".NoSuchDevice"
)
device = mockobject.objects[device_path]
device.props[DEVICE_IFACE]["Blocked"] = dbus.Boolean(False)
device.props[DEVICE_IFACE]["Connected"] = dbus.Boolean(True)
device.EmitSignal(
dbus.PROPERTIES_IFACE,
"PropertiesChanged",
"sa{sv}as",
[
DEVICE_IFACE,
{
"Blocked": dbus.Boolean(False),
"Connected": dbus.Boolean(True),
},
[],
],
)
@dbus.service.method(BLUEZ_MOCK_IFACE, in_signature="ss", out_signature="")
def DisconnectDevice(_self, adapter_device_name, device_address):
"""Convenience method to mark an existing device as disconnected.
You have to specify a device address which must be a valid Bluetooth
address (e.g. 'AA:BB:CC:DD:EE:FF'). The adapter device name is the
device_name passed to AddAdapter.
This does not change the device's blocked status.
If the specified adapter or device doesn't exist, a NoSuchAdapter or
NoSuchDevice error will be returned on the bus.
Returns nothing.
"""
device_name = "dev_" + device_address.replace(":", "_").upper()
adapter_path = "/org/bluez/" + adapter_device_name
device_path = adapter_path + "/" + device_name
if adapter_path not in mockobject.objects:
raise dbus.exceptions.DBusException(
f"Adapter {adapter_device_name} does not exist.", name=BLUEZ_MOCK_IFACE + ".NoSuchAdapter"
)
if device_path not in mockobject.objects:
raise dbus.exceptions.DBusException(
f"Device {device_name} does not exist.", name=BLUEZ_MOCK_IFACE + ".NoSuchDevice"
)
device = mockobject.objects[device_path]
device.props[DEVICE_IFACE]["Connected"] = dbus.Boolean(False)
device.EmitSignal(
dbus.PROPERTIES_IFACE,
"PropertiesChanged",
"sa{sv}as",
[
DEVICE_IFACE,
{
"Connected": dbus.Boolean(False),
},
[],
],
)
def RegisterAdvertisement(manager, adv_path, options): # pylint: disable=unused-argument
if adv_path in manager.advertisements:
raise dbus.exceptions.DBusException("Already registered: " + adv_path, name="org.bluez.Error.AlreadyExists")
if len(manager.advertisements) >= MAX_ADVERTISEMENT_INSTANCES:
raise dbus.exceptions.DBusException(
f"Maximum number of advertisements reached: {MAX_ADVERTISEMENT_INSTANCES}",
name="org.bluez.Error.NotPermitted",
)
manager.advertisements.append(adv_path)
manager.UpdateProperties(
LE_ADVERTISING_MANAGER_IFACE,
{
"ActiveInstances": dbus.Byte(len(manager.advertisements)),
"SupportedInstances": dbus.Byte(MAX_ADVERTISEMENT_INSTANCES - len(manager.advertisements)),
},
)
def UnregisterAdvertisement(manager, adv_path):
try:
manager.advertisements.remove(adv_path)
except ValueError:
raise dbus.exceptions.DBusException(
"Unknown advertisement: " + adv_path, name="org.bluez.Error.DoesNotExist"
) from None
manager.UpdateProperties(
LE_ADVERTISING_MANAGER_IFACE,
{
"ActiveInstances": dbus.Byte(len(manager.advertisements)),
"SupportedInstances": dbus.Byte(MAX_ADVERTISEMENT_INSTANCES - len(manager.advertisements)),
},
)
@dbus.service.method(BLUEZ_MOCK_IFACE, in_signature="s", out_signature="s")
def AddAdvertisement(self, adv_name):
"""Convenience method to add an Advertisement object
Creates a simple broadcast advertisement with some manufacturer data.
Returns the new object path.
"""
path = "/org/dbusmock/bluez/advertisement/" + adv_name
adv_properties = {
"Type": dbus.String("broadcast"),
"ManufacturerData": dbus.Dictionary(
# 0xFFFF is the Bluetooth Company Identifier reserved for internal use and testing.
{dbus.UInt16(0xFFFF): dbus.Array([0x00, 0x01])},
signature="qv",
),
"Includes": dbus.Array(["local-name"]),
}
self.AddObject(
path,
LE_ADVERTISEMENT_IFACE,
adv_properties,
[
("Release", "", "", ""),
],
)
return path
@dbus.service.method(BLUEZ_MOCK_IFACE, in_signature="s", out_signature="s")
def AddMonitor(self, monitor_name):
"""Convenience method to add an Advertisement Monitor
Returns the new object path.
"""
path = "/org/dbusmock/bluez/monitor/" + monitor_name
monitor_properties = {
"Type": dbus.String("or_patterns"),
# Example pattern that could be used to scan for an advertisement created by AddAdvertisement()
"Patterns": dbus.Struct(
(
# Start position: 0
dbus.Byte(0),
# AD data type: Manufacturer data
dbus.Byte(0xFF),
# Vaue of the pattern: 0xFFFF (company identifier), followed by 0x01
dbus.Array(
[
dbus.UInt16(0xFFFF),
dbus.Byte(0x01),
]
),
),
signature="yyay",
),
}
self.AddObject(
path,
ADVERTISEMENT_MONITOR_IFACE,
monitor_properties,
[
("Release", "", "", ""),
("Activate", "", "", ""),
("DeviceFound", "o", "", ""),
("DeviceLost", "o", "", ""),
],
)
return path
def RegisterMonitor(manager, monitor_path):
if monitor_path in manager.monitors:
raise dbus.exceptions.DBusException(
"Already registered: " + monitor_path, name="org.bluez.Error.AlreadyExists"
)
manager.monitors.append(monitor_path)
def UnregisterMonitor(manager, monitor_path):
try:
manager.monitors.remove(monitor_path)
except ValueError:
raise dbus.exceptions.DBusException(
"Unknown monitor: " + monitor_path, name="org.bluez.Error.DoesNotExist"
) from None
././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125649.0
python_dbusmock-0.34.3/dbusmock/templates/gnome_screensaver.py 0000644 0001751 0000166 00000002500 14756032721 024304 0 ustar 00runner docker """gnome-shell screensaver mock template
This creates the expected methods and properties of the
org.gnome.ScreenSaver object.
"""
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
# of the license.
__author__ = "Bastien Nocera"
__copyright__ = """
(c) 2013 Red Hat Inc.
(c) 2017 - 2022 Martin Pitt
"""
BUS_NAME = "org.gnome.ScreenSaver"
MAIN_OBJ = "/org/gnome/ScreenSaver"
MAIN_IFACE = "org.gnome.ScreenSaver"
SYSTEM_BUS = False
def load(mock, _parameters):
mock.AddMethods(
MAIN_IFACE,
[
("GetActive", "", "b", "ret = self.is_active"),
("GetActiveTime", "", "u", "ret = 1"),
(
"SetActive",
"b",
"",
'self.is_active = args[0]; self.EmitSignal("", "ActiveChanged", "b", [self.is_active])',
),
("Lock", "", "", "time.sleep(1); self.SetActive(True)"),
("ShowMessage", "sss", "", ""),
("SimulateUserActivity", "", "", ""),
],
)
# default state
mock.is_active = False
././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125649.0
python_dbusmock-0.34.3/dbusmock/templates/gsd_rfkill.py 0000644 0001751 0000166 00000004116 14756032721 022724 0 ustar 00runner docker """gsd-rfkill mock template
This creates the expected properties of the GNOME Settings Daemon's
rfkill object. You can specify any property such as AirplaneMode in
"parameters".
"""
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
# of the license.
__author__ = "Guido Günther"
__copyright__ = "2024 The Phosh Developers"
import dbus
from dbusmock import MOCK_IFACE
SYSTEM_BUS = False
BUS_NAME = "org.gnome.SettingsDaemon.Rfkill"
MAIN_OBJ = "/org/gnome/SettingsDaemon/Rfkill"
MAIN_IFACE = "org.gnome.SettingsDaemon.Rfkill"
def load(mock, parameters):
props = dbus.Dictionary(
{
"AirplaneMode": parameters.get("AirplaneMode", False),
"BluetoothAirplaneMode": parameters.get("BluetoothAirplaneMode", False),
"BluetoothHardwareAirplaneMode": parameters.get("BluetoothHardwareAirplaneMode", False),
"BluetoothHasAirplaneMode": parameters.get("BluetoothHasAirplanemode", True),
"HardwareAirplaneMode": parameters.get("HardwareAirplaneMode", False),
"HasAirplaneMode": parameters.get("HasAirplaneMode", True),
"ShouldShowAirplaneMode": parameters.get("ShouldShowAirplaneMode", True),
"WwanAirplaneMode": parameters.get("WwanAirplaneMode", False),
"WwanHardwareAirplaneMode": parameters.get("WwanHardwareAirplaneMode", False),
"WwanHasAirplaneMode": parameters.get("WwanHasAirplaneMode", True),
},
signature="sv",
)
mock.AddProperties(MAIN_IFACE, props)
@dbus.service.method(MOCK_IFACE, in_signature="b", out_signature="b")
def SetAirplaneMode(self, mode):
"""
Convenience method to toggle airplane mode
"""
self.props[MAIN_IFACE]["AirplaneMode"] = mode
self.props[MAIN_IFACE]["BluetoothAirplaneMode"] = mode
self.props[MAIN_IFACE]["WwanAirplaneMode"] = mode
return mode
././@PaxHeader 0000000 0000000 0000000 00000000026 00000000000 010213 x ustar 00 22 mtime=1740125649.0
python_dbusmock-0.34.3/dbusmock/templates/iio-sensors-proxy.py 0000644 0001751 0000166 00000015304 14756032721 024236 0 ustar 00runner docker """sensors proxy mock template
"""
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
# of the license.
__author__ = "Marco Trevisan"
__copyright__ = """
(c) 2021 Canonical Ltd.
(c) 2017 - 2022 Martin Pitt
"""
import re
import dbus
from dbusmock import MOCK_IFACE
BUS_NAME = "net.hadess.SensorProxy"
MAIN_OBJ = "/net/hadess/SensorProxy"
MAIN_IFACE = "net.hadess.SensorProxy"
COMPASS_IFACE = "net.hadess.SensorProxy.Compass"
SYSTEM_BUS = True
CAMEL_TO_SNAKE_CASE_RE = re.compile(r"(?