Skip to content

Commit

Permalink
Separated sdbus.dbus_common in to two files
Browse files Browse the repository at this point in the history
  • Loading branch information
igo95862 committed Jun 11, 2022
1 parent eacd64d commit 1e86871
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 99 deletions.
4 changes: 2 additions & 2 deletions src/sdbus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: LGPL-2.1-or-later

# Copyright (C) 2020, 2021 igo95862
# Copyright (C) 2020-2022 igo95862

# This file is part of python-sdbus

Expand All @@ -18,7 +18,7 @@
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

from .dbus_common import (
from .dbus_common_funcs import (
get_default_bus,
request_default_bus_name,
request_default_bus_name_async,
Expand Down
2 changes: 1 addition & 1 deletion src/sdbus/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def run_gen_from_connection(namespace: Namespace) -> None:
from .dbus_proxy_sync import DbusInterfaceCommon

if namespace.system:
from .dbus_common import set_default_bus
from .dbus_common_funcs import set_default_bus
from .sd_bus_internals import sd_bus_open_system
set_default_bus(sd_bus_open_system())

Expand Down
96 changes: 6 additions & 90 deletions src/sdbus/dbus_common.py → src/sdbus/dbus_common_elements.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: LGPL-2.1-or-later

# Copyright (C) 2020, 2021 igo95862
# Copyright (C) 2020-2022 igo95862

# This file is part of python-sdbus

Expand All @@ -19,99 +19,15 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
from __future__ import annotations

from asyncio import get_running_loop
from inspect import getfullargspec
from types import FunctionType
from typing import Any, Dict, Iterator, List, Optional, Sequence, Tuple

from .sd_bus_internals import (
DbusPropertyConstFlag,
DbusPropertyEmitsChangeFlag,
DbusPropertyEmitsInvalidationFlag,
DbusPropertyExplicitFlag,
SdBus,
is_interface_name_valid,
is_member_name_valid,
sd_bus_open,
)

DEFAULT_BUS: Optional[SdBus] = None
from typing import Any, Dict, List, Optional, Sequence, Tuple

PROPERTY_FLAGS_MASK = (
DbusPropertyConstFlag | DbusPropertyEmitsChangeFlag |
DbusPropertyEmitsInvalidationFlag | DbusPropertyExplicitFlag
from .dbus_common_funcs import (
_is_property_flags_correct,
_method_name_converter,
)


def count_bits(i: int) -> int:
return bin(i).count('1')


def _is_property_flags_correct(flags: int) -> bool:
num_of_flag_bits = count_bits(PROPERTY_FLAGS_MASK & flags)
return (0 <= num_of_flag_bits <= 1)


def get_default_bus() -> SdBus:
global DEFAULT_BUS
old_bus = DEFAULT_BUS
if old_bus is None:
new_bus = sd_bus_open()
DEFAULT_BUS = new_bus
return new_bus
else:
return old_bus


def set_default_bus(new_default: SdBus) -> None:
global DEFAULT_BUS

DEFAULT_BUS = new_default


async def request_default_bus_name_async(
new_name: str,
flags: int = 0,) -> None:
default_bus = get_default_bus()
await default_bus.request_name_async(new_name, flags)


async def request_default_bus_name(
new_name: str,
flags: int = 0,) -> None:
default_bus = get_default_bus()
default_bus.request_name(new_name, flags)


def _method_name_converter(python_name: str) -> Iterator[str]:
char_iter = iter(python_name)
# Name starting with upper case letter
try:
first_char = next(char_iter)
except StopIteration:
return

yield first_char.upper()

upper_next_one = False
for c in char_iter:
# Every under score remove and uppercase next one
if c != '_':
if upper_next_one:
yield c.upper()
upper_next_one = False
else:
yield c
else:
upper_next_one = True


def _check_sync_in_async_env() -> bool:
try:
get_running_loop()
return False
except RuntimeError:
return True
from .sd_bus_internals import is_interface_name_valid, is_member_name_valid


class DbusSomethingCommon:
Expand Down
111 changes: 111 additions & 0 deletions src/sdbus/dbus_common_funcs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@

# SPDX-License-Identifier: LGPL-2.1-or-later

# Copyright (C) 2020-2022 igo95862

# This file is part of python-sdbus

# This library 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 2.1 of the License, or (at your option) any later version.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
from __future__ import annotations

from asyncio import get_running_loop
from typing import Iterator, Optional

from .sd_bus_internals import (
DbusPropertyConstFlag,
DbusPropertyEmitsChangeFlag,
DbusPropertyEmitsInvalidationFlag,
DbusPropertyExplicitFlag,
SdBus,
sd_bus_open,
)

DEFAULT_BUS: Optional[SdBus] = None

PROPERTY_FLAGS_MASK = (
DbusPropertyConstFlag | DbusPropertyEmitsChangeFlag |
DbusPropertyEmitsInvalidationFlag | DbusPropertyExplicitFlag
)


def count_bits(i: int) -> int:
return bin(i).count('1')


def _is_property_flags_correct(flags: int) -> bool:
num_of_flag_bits = count_bits(PROPERTY_FLAGS_MASK & flags)
return (0 <= num_of_flag_bits <= 1)


def get_default_bus() -> SdBus:
global DEFAULT_BUS
old_bus = DEFAULT_BUS
if old_bus is None:
new_bus = sd_bus_open()
DEFAULT_BUS = new_bus
return new_bus
else:
return old_bus


def set_default_bus(new_default: SdBus) -> None:
global DEFAULT_BUS

DEFAULT_BUS = new_default


async def request_default_bus_name_async(
new_name: str,
flags: int = 0,) -> None:
default_bus = get_default_bus()
await default_bus.request_name_async(new_name, flags)


async def request_default_bus_name(
new_name: str,
flags: int = 0,) -> None:
default_bus = get_default_bus()
default_bus.request_name(new_name, flags)


def _method_name_converter(python_name: str) -> Iterator[str]:
char_iter = iter(python_name)
# Name starting with upper case letter
try:
first_char = next(char_iter)
except StopIteration:
return

yield first_char.upper()

upper_next_one = False
for c in char_iter:
# Every under score remove and uppercase next one
if c != '_':
if upper_next_one:
yield c.upper()
upper_next_one = False
else:
yield c
else:
upper_next_one = True


def _check_sync_in_async_env() -> bool:
try:
get_running_loop()
return False
except RuntimeError:
return True
4 changes: 2 additions & 2 deletions src/sdbus/dbus_proxy_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@
from warnings import warn
from weakref import ref as weak_ref

from .dbus_common import (
from .dbus_common_elements import (
DbusInterfaceMetaCommon,
DbusMethodCommon,
DbusPropertyCommon,
DbusSingalCommon,
DbusSomethingAsync,
DbusSomethingSync,
get_default_bus,
)
from .dbus_common_funcs import get_default_bus
from .dbus_exceptions import DbusFailedError
from .sd_bus_internals import (
DbusNoReplyFlag,
Expand Down
5 changes: 2 additions & 3 deletions src/sdbus/dbus_proxy_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@
cast,
)

from .dbus_common import (
from .dbus_common_elements import (
DbusInterfaceMetaCommon,
DbusMethodCommon,
DbusPropertyCommon,
DbusSomethingAsync,
DbusSomethingSync,
_check_sync_in_async_env,
get_default_bus,
)
from .dbus_common_funcs import _check_sync_in_async_env, get_default_bus
from .sd_bus_internals import SdBus

DEFAULT_BUS: Optional[SdBus] = None
Expand Down
2 changes: 1 addition & 1 deletion test/test_sd_bus_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from typing import Tuple
from unittest import SkipTest

from sdbus.dbus_common import PROPERTY_FLAGS_MASK, count_bits
from sdbus.dbus_common_funcs import PROPERTY_FLAGS_MASK, count_bits
from sdbus.sd_bus_internals import (
DBUS_ERROR_TO_EXCEPTION,
DbusDeprecatedFlag,
Expand Down

0 comments on commit 1e86871

Please sign in to comment.