Skip to content

Commit

Permalink
Use black as code formatter
Browse files Browse the repository at this point in the history
Integrated into pyproject.toml and tests/test_code.py so we can enforce
the formatting in the CI.

Fixes #187
  • Loading branch information
whot committed Dec 1, 2023
1 parent 6368b5f commit 09ce8ca
Show file tree
Hide file tree
Showing 42 changed files with 3,881 additions and 3,770 deletions.
23 changes: 16 additions & 7 deletions dbusmock/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# coding: UTF-8
'''Mock D-Bus objects for test suites.'''
"""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__ = '''
__author__ = "Martin Pitt"
__copyright__ = """
(c) 2012 Canonical Ltd.
(c) 2017 - 2022 Martin Pitt <martin@piware.de>
'''
"""


from dbusmock.mockobject import MOCK_IFACE, OBJECT_MANAGER_IFACE, DBusMockObject, get_object, get_objects
Expand All @@ -21,8 +21,17 @@
# created by setuptools_scm
from dbusmock._version import __version__
except ImportError:
__version__ = '0.git'
__version__ = "0.git"


__all__ = ['DBusMockObject', 'MOCK_IFACE', 'OBJECT_MANAGER_IFACE',
'DBusTestCase', 'PrivateDBus', 'BusType', 'SpawnedMock', 'get_object', 'get_objects']
__all__ = [
"DBusMockObject",
"MOCK_IFACE",
"OBJECT_MANAGER_IFACE",
"DBusTestCase",
"PrivateDBus",
"BusType",
"SpawnedMock",
"get_object",
"get_objects",
]
104 changes: 55 additions & 49 deletions dbusmock/__main__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# coding: UTF-8
'''Main entry point for running mock server.'''
"""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__ = '''
__author__ = "Martin Pitt"
__copyright__ = """
(c) 2012 Canonical Ltd.
(c) 2017 - 2022 Martin Pitt <martin@piware.de>
'''
"""

import argparse
import json
Expand All @@ -24,46 +24,54 @@


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')
"""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')
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')
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')
parser.error("--system and --session are mutually exclusive")

return arguments


if __name__ == '__main__':
if __name__ == "__main__":
import ctypes

import dbus.mainloop.glib
Expand All @@ -80,12 +88,12 @@ def parse_args():
if not args.session and not args.system:
system_bus = module.SYSTEM_BUS

if hasattr(module, 'IS_OBJECT_MANAGER'):
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'):
if args.is_object_manager and not hasattr(module, "MAIN_IFACE"):
args.interface = dbusmock.mockobject.OBJECT_MANAGER_IFACE
else:
args.interface = module.MAIN_IFACE
Expand All @@ -94,37 +102,35 @@ def parse_args():

# 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.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)
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)
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.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.stderr.write("JSON parameters must be a dictionary\n")
sys.exit(2)

if args.template:
main_object.AddTemplate(args.template, parameters)

libglib = ctypes.cdll.LoadLibrary('libglib-2.0.so.0')
libglib = ctypes.cdll.LoadLibrary("libglib-2.0.so.0")

dbusmock.mockobject.objects[args.path] = main_object

Expand All @@ -134,7 +140,7 @@ def parse_args():

@ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_int)
def on_process_watch(_pid, status):
""" Check if the launched process is still alive """
"""Check if the launched process is still alive"""
if os.WIFEXITED(status):
exit_status.add(os.WEXITSTATUS(status))
else:
Expand Down
Loading

0 comments on commit 09ce8ca

Please sign in to comment.