Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move all build time constants to a json file #1951

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions apps/blueman-adapters.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,32 @@ import gettext


resource_file = "@pkgdatadir@/blueman.gresource"
settings_path = "@pkgdatadir@/settings.json"
src_paths = {}

# support running uninstalled
_dirname = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if 'BLUEMAN_SOURCE' in os.environ:
sys.path = [_dirname, os.path.join(_dirname, 'module', '.libs')] + sys.path
os.environ["GSETTINGS_SCHEMA_DIR"] = os.path.join(_dirname, "data")
resource_file = os.path.join(_dirname, "data", "blueman.gresource")
settings_path = os.path.join(_dirname, "data", "configs", "settings.json")
src_paths.update({
"bindir": os.path.join(_dirname, "apps"),
"rfcomm_watcher_path": os.path.join(_dirname, "apps")
})

gettext.textdomain("@GETTEXT_PACKAGE@")

from blueman.Functions import create_parser, create_logger, set_proc_title
from blueman.Functions import create_parser, create_logger, set_proc_title, load_json
from blueman.config.Settings import BluemanSettings
from blueman.main.Adapter import BluemanAdapters

settings = load_json(settings_path, src_paths)
bm_settings = BluemanSettings(**settings)

if __name__ == '__main__':
parser = parser = create_parser()
parser = create_parser()
parser.add_argument("--socket-id", dest="socket_id", action="store", type=int, metavar="ID")
parser.add_argument("adapter", nargs="?", metavar="ADAPTER NAME")
args = parser.parse_args()
Expand All @@ -42,5 +53,6 @@ if __name__ == '__main__':

set_proc_title()

app = blueman_adapters = BluemanAdapters(args.adapter, args.socket_id, resource_file)
app = blueman_adapters = BluemanAdapters(args.adapter, args.socket_id, resource_file, bm_settings)

app.run()
18 changes: 15 additions & 3 deletions apps/blueman-applet.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,30 @@ import logging
import gettext

resource_file = "@pkgdatadir@/blueman.gresource"
settings_path = os.path.join("@settings_path@", "settings.json")
src_paths = {}

# support running uninstalled
_dirname = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
_dirname = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if 'BLUEMAN_SOURCE' in os.environ:
sys.path = [_dirname, os.path.join(_dirname, 'module', '.libs')] + sys.path
os.environ["GSETTINGS_SCHEMA_DIR"] = os.path.join(_dirname, "data")
resource_file = os.path.join(_dirname, "data", "blueman.gresource")
settings_path = os.path.join(_dirname, "data", "configs", "settings.json")
src_paths.update({
"bindir": os.path.join(_dirname, "apps"),
"rfcomm_watcher_path": os.path.join(_dirname, "apps")
})


gettext.textdomain("@GETTEXT_PACKAGE@")

from blueman.Functions import create_logger, create_parser, set_proc_title
from blueman.main.Applet import BluemanApplet
from blueman.config.Settings import BluemanSettings
from blueman.Functions import set_proc_title, create_parser, create_logger, load_json

settings = load_json(settings_path, src_paths)
bm_settings = BluemanSettings(**settings)

if __name__ == '__main__':
parser = create_parser()
Expand All @@ -38,5 +50,5 @@ if __name__ == '__main__':
create_logger(log_level, "blueman-applet", syslog=args.syslog)

set_proc_title()
app = BluemanApplet(resource_file)
app = BluemanApplet(resource_file, bm_settings)
app.run()
16 changes: 14 additions & 2 deletions apps/blueman-manager.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,30 @@ import logging
import gettext

resource_file = "@pkgdatadir@/blueman.gresource"
settings_path = os.path.join("@settings_path@", "settings.json")
src_paths = {}

# support running uninstalled
_dirname = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if 'BLUEMAN_SOURCE' in os.environ:
sys.path = [_dirname, os.path.join(_dirname, 'module', '.libs')] + sys.path
os.environ["GSETTINGS_SCHEMA_DIR"] = os.path.join(_dirname, "data")
resource_file = os.path.join(_dirname, "data", "blueman.gresource")
settings_path = os.path.join(_dirname, "data", "configs", "settings.json")
src_paths.update({
"bindir": os.path.join(_dirname, "apps"),
"rfcomm_watcher_path": os.path.join(_dirname, "apps")
})


gettext.textdomain("@GETTEXT_PACKAGE@")

from blueman.main.Manager import Blueman
from blueman.Functions import set_proc_title, create_parser, create_logger
from blueman.config.Settings import BluemanSettings
from blueman.Functions import set_proc_title, create_parser, create_logger, load_json

settings = load_json(settings_path, src_paths)
bm_settings = BluemanSettings(**settings)

if __name__ == '__main__':
parser = create_parser()
Expand All @@ -37,6 +49,6 @@ if __name__ == '__main__':

create_logger(log_level, "blueman-manager", syslog=args.syslog)

app = Blueman(resource_file)
app = Blueman(resource_file, bm_settings)
set_proc_title()
app.run()
19 changes: 17 additions & 2 deletions apps/blueman-mechanism.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,28 @@ import logging

from blueman.main.MechanismApplication import MechanismApplication

resource_file = "@pkgdatadir@/blueman.gresource"
settings_path = os.path.join("@settings_path@", "settings.json")
src_paths = {}

# support running uninstalled
_dirname = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if 'BLUEMAN_SOURCE' in os.environ:
sys.path = [_dirname, os.path.join(_dirname, 'module', '.libs')] + sys.path
os.environ["GSETTINGS_SCHEMA_DIR"] = os.path.join(_dirname, "data")
resource_file = os.path.join(_dirname, "data", "blueman.gresource")
settings_path = os.path.join(_dirname, "data", "configs", "settings.json")
src_paths.update({
"bindir": os.path.join(_dirname, "apps"),
"rfcomm_watcher_path": os.path.join(_dirname, "apps")
})


from blueman.config.Settings import BluemanSettings
from blueman.Functions import set_proc_title, create_parser, create_logger, load_json

from blueman.Functions import set_proc_title, create_logger, create_parser
settings = load_json(settings_path, src_paths)
bm_settings = BluemanSettings(**settings)


class StreamToLogger:
Expand Down Expand Up @@ -65,5 +80,5 @@ logging.info("Starting blueman-mechanism")
os.environ["PATH"] = "/usr/bin:/bin:/usr/sbin:/sbin"

set_proc_title()
app = MechanismApplication(args.stoptimer)
app = MechanismApplication(args.stoptimer, bm_settings)
app.run()
19 changes: 17 additions & 2 deletions apps/blueman-sendto.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ gi.require_version("Gdk", "3.0")
from gi.repository import Gtk, Gio

resource_file = "@pkgdatadir@/blueman.gresource"
settings_path = os.path.join("@settings_path@", "settings.json")
src_paths = {}

# support running uninstalled
_dirname = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if 'BLUEMAN_SOURCE' in os.environ:
sys.path = [_dirname, os.path.join(_dirname, 'module', '.libs')] + sys.path
os.environ["GSETTINGS_SCHEMA_DIR"] = os.path.join(_dirname, "data")
resource_file = os.path.join(_dirname, "data", "blueman.gresource")
settings_path = os.path.join(_dirname, "data", "configs", "settings.json")
src_paths.update({
"bindir": os.path.join(_dirname, "apps"),
"rfcomm_watcher_path": os.path.join(_dirname, "apps")
})


_ = gettext.gettext
gettext.textdomain("@GETTEXT_PACKAGE@")
Expand All @@ -27,8 +36,11 @@ from blueman.Functions import (
set_proc_title,
create_parser,
create_logger,
bmexit
bmexit,
load_json
)
from blueman.config.Settings import BluemanSettings
from blueman.main.Config import Config
from blueman.main.Sendto import Sender
from blueman.bluez.Manager import Manager
from blueman.bluez.errors import DBusNoSuchAdapterError
Expand All @@ -37,6 +49,9 @@ from blueman.gui.DeviceSelectorDialog import DeviceSelectorDialog
# Workaround introspection bug, gnome bug 622084
signal.signal(signal.SIGINT, signal.SIG_DFL)

settings = load_json(settings_path, src_paths)
bm_settings = BluemanSettings(**settings)


class SendTo:
def __init__(self, parsed_args):
Expand Down Expand Up @@ -98,7 +113,7 @@ class SendTo:
logging.warning("No files to send")
bmexit()

sender = Sender(self.device, self.adapter_path, self.files)
sender = Sender(self.device, self.adapter_path, self.files, bm_settings)

def on_result(sender, res):
Gtk.main_quit()
Expand Down
16 changes: 14 additions & 2 deletions apps/blueman-services.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,31 @@ import signal
import gettext

resource_file = "@pkgdatadir@/blueman.gresource"
settings_path = os.path.join("@settings_path@", "settings.json")
src_paths = {}

# support running uninstalled
_dirname = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if 'BLUEMAN_SOURCE' in os.environ:
sys.path = [_dirname, os.path.join(_dirname, 'module', '.libs')] + sys.path
os.environ["GSETTINGS_SCHEMA_DIR"] = os.path.join(_dirname, "data")
resource_file = os.path.join(_dirname, "data", "blueman.gresource")
settings_path = os.path.join(_dirname, "data", "configs", "settings.json")
src_paths.update({
"bindir": os.path.join(_dirname, "apps"),
"rfcomm_watcher_path": os.path.join(_dirname, "apps")
})


_ = gettext.gettext
gettext.textdomain("@GETTEXT_PACKAGE@")

from blueman.Functions import set_proc_title, create_logger, create_parser
from blueman.Functions import set_proc_title, create_logger, create_parser, load_json
from blueman.config.Settings import BluemanSettings
from blueman.main.Services import BluemanServices

settings = load_json(settings_path, src_paths)
bm_settings = BluemanSettings(**settings)

if __name__ == '__main__':
parser = create_parser()
Expand All @@ -41,5 +53,5 @@ if __name__ == '__main__':
create_logger(log_level, "blueman-services", syslog=args.syslog)

set_proc_title()
app = BluemanServices(resource_file)
app = BluemanServices(resource_file, bm_settings)
app.run()
18 changes: 0 additions & 18 deletions blueman/Constants.py.in

This file was deleted.

24 changes: 17 additions & 7 deletions blueman/Functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
import socket
import array
import time
import json

import cairo

from blueman.main.DBusProxies import AppletService, DBusProxyFailed
from blueman.Constants import BIN_DIR

import gi
gi.require_version("Gtk", "3.0")
Expand Down Expand Up @@ -87,7 +87,6 @@ def check_bluetooth_status(message: str, exitfunc: Callable[[], Any]) -> None:
def launch(
cmd: str,
paths: Optional[Iterable[str]] = None,
system: bool = False,
icon_name: Optional[str] = None,
name: str = "blueman",
sn: bool = True,
Expand All @@ -113,11 +112,6 @@ def launch(
env = os.environ
env["BLUEMAN_EVENT_TIME"] = str(timestamp)

if not system:
cmd = os.path.join(BIN_DIR, cmd)
else:
cmd = os.path.expanduser(cmd)

if paths:
files: Optional[List[Gio.File]] = [Gio.File.new_for_commandline_arg(p) for p in paths]
else:
Expand Down Expand Up @@ -329,3 +323,19 @@ def get_local_interfaces() -> Dict[str, Tuple[str, Optional[str]]]:

def bmexit(msg: Optional[Union[str, int]] = None) -> None:
raise SystemExit(msg)


def load_json(json_path: str, updates: Dict[str, str]) -> dict[str, Union[str, bool]]:
settings_dict = {}
try:
with open(json_path) as f:
data = json.load(f)
settings_dict.update(data)
settings_dict.update(updates)
return settings_dict
except json.JSONDecodeError:
logging.error("Could not decode json file.")
return {}
except FileNotFoundError:
logging.error("Settings file not found. Reinstall blueman.")
return {}
5 changes: 0 additions & 5 deletions blueman/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ SUBDIRS = \

bluemandir = $(pythondir)/blueman
blueman_PYTHON = \
Constants.py \
DeviceClass.py \
Functions.py \
Sdp.py \
Expand All @@ -18,14 +17,10 @@ blueman_PYTHON = \
__init__.py

CLEANFILES = \
Constants.py \
$(BUILT_SOURCES)

DISTCLEANFILES = \
$(CLEANFILES)

EXTRA_DIST = \
Constants.py.in

clean-local:
find . \( -name \*.pyc -o -name \*.pyo -o -name __pycache__ \) -prune -exec rm -rf {} +
Expand Down
8 changes: 6 additions & 2 deletions blueman/Service.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from abc import ABC, abstractmethod
from typing import Optional, Callable, List, Set, Collection
from typing import Optional, Callable, List, Set, Collection, TYPE_CHECKING

from blueman.Sdp import ServiceUUID
from blueman.bluez.Device import Device

if TYPE_CHECKING:
from blueman.config.Settings import BluemanSettings


class Instance:
def __init__(self, name: str, port: int = 0) -> None:
Expand Down Expand Up @@ -31,9 +34,10 @@ class Service(ABC):
__icon__: str
__priority__: int

def __init__(self, device: Device, uuid: str):
def __init__(self, device: Device, uuid: str, settings: "BluemanSettings"):
self.__device = device
self.__uuid = uuid
self._settings = settings

@property
def name(self) -> str:
Expand Down
15 changes: 15 additions & 0 deletions blueman/config/Settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from dataclasses import dataclass


@dataclass
class BluemanSettings:
version: str
package: str
website: str
prefix: str
bindir: str
localedir: str
dhcp_config_file: str
polkit: bool
gettext_package: str
rfcomm_watcher_path: str
Loading