updates to dbus and database
This commit is contained in:
parent
5b81b1a339
commit
83b407cb3e
|
@ -242,10 +242,12 @@ def print_methods(object, include_underscore=False):
|
||||||
'''
|
'''
|
||||||
|
|
||||||
for line in dir(object):
|
for line in dir(object):
|
||||||
if line.startswith('_') and include_underscore:
|
if line.startswith('_'):
|
||||||
print(line)
|
if include_underscore:
|
||||||
|
print(line)
|
||||||
|
|
||||||
print(line)
|
else:
|
||||||
|
print(line)
|
||||||
|
|
||||||
|
|
||||||
def prompt(prompt, default=None, valtype=str, options=[], password=False):
|
def prompt(prompt, default=None, valtype=str, options=[], password=False):
|
||||||
|
|
|
@ -4,8 +4,10 @@ from dasbus.connection import SessionMessageBus, SystemMessageBus
|
||||||
from dasbus.error import DBusError
|
from dasbus.error import DBusError
|
||||||
from dasbus.identifier import DBusServiceIdentifier
|
from dasbus.identifier import DBusServiceIdentifier
|
||||||
from dasbus.loop import EventLoop
|
from dasbus.loop import EventLoop
|
||||||
|
from functools import partial
|
||||||
from izzylib import DotDict, Path, logging
|
from izzylib import DotDict, Path, logging
|
||||||
from pathlib import Path as Pathlib
|
from pathlib import Path as Pathlib
|
||||||
|
from xml.etree import ElementTree
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from .template import Template
|
from .template import Template
|
||||||
|
@ -34,14 +36,12 @@ class DBusBase(DBusServiceIdentifier):
|
||||||
|
|
||||||
|
|
||||||
class DBusClientBase(DBusBase):
|
class DBusClientBase(DBusBase):
|
||||||
def __init__(self, *args, methods=[], **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
self.proxy = None
|
self.proxy = None
|
||||||
self.set_method('Introspect')
|
self.methods = DotDict()
|
||||||
|
self.signals = DotDict()
|
||||||
for name in methods:
|
|
||||||
self.set_method(name)
|
|
||||||
|
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
|
@ -57,14 +57,18 @@ class DBusClientBase(DBusBase):
|
||||||
self.proxy = self.get_proxy(self.dbuspath)
|
self.proxy = self.get_proxy(self.dbuspath)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.Introspect()
|
self.set_methods_and_signals()
|
||||||
|
return True
|
||||||
|
|
||||||
|
## Make errors shorter and just reuse ConnectionError
|
||||||
except DBusError as e:
|
except DBusError as e:
|
||||||
if 'was not provided by any .service files' in str(e):
|
if str(e) == f'The name {self.interface_name} was not provided by any .service files':
|
||||||
self.proxy = None
|
raise ConnectionError(f'Namespace "{self.interface_name}" does not exist') from None
|
||||||
return
|
|
||||||
|
|
||||||
traceback.print_exc()
|
elif str(e) == f'No such object path \'{self.dbuspath}\'':
|
||||||
|
raise ConnectionError(f'Path "{self.dbuspath}" does not exist') from None
|
||||||
|
|
||||||
|
raise e from None
|
||||||
|
|
||||||
|
|
||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
|
@ -82,12 +86,40 @@ class DBusClientBase(DBusBase):
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def set_method(self, name):
|
def sig_connect(self, signal, callback, *args, original_args=True, **kwargs):
|
||||||
if not getattr(self, name, False):
|
if original_args:
|
||||||
setattr(self, name, lambda *args, **kwargs: self.cmd(name, *args, **kwargs))
|
self.signals[signal].connect(lambda *sigargs, **sigkwargs: callback(*sigargs, *args, **sigkwargs, **kwargs))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
logging.warning('Tried to add an existing method:', name)
|
self.signals[signal].connect(lambda *sigargs, **sigkargs: callback(*args, **kwargs))
|
||||||
|
|
||||||
|
|
||||||
|
def set_method(self, name):
|
||||||
|
self.methods[name] = partial(self.cmd, name)
|
||||||
|
setattr(self, name, partial(self.cmd, name))
|
||||||
|
|
||||||
|
|
||||||
|
def set_signal(self, name):
|
||||||
|
signal = getattr(self.proxy, name)
|
||||||
|
self.signals[name] = signal
|
||||||
|
setattr(self, name, partial(self.connect, name))
|
||||||
|
|
||||||
|
|
||||||
|
def set_methods_and_signals(self, namespace=None):
|
||||||
|
for element in ElementTree.fromstring(self.cmd('Introspect')):
|
||||||
|
if element.attrib['name'] == namespace or self.interface_name:
|
||||||
|
for e in element:
|
||||||
|
name = e.attrib['name']
|
||||||
|
|
||||||
|
if getattr(self, name, None):
|
||||||
|
logging.verbose('Tried to add an existing method or signal:', name)
|
||||||
|
continue
|
||||||
|
|
||||||
|
if e.tag == 'method':
|
||||||
|
self.set_method(name)
|
||||||
|
|
||||||
|
elif e.tag == 'signal':
|
||||||
|
self.set_signal(name)
|
||||||
|
|
||||||
|
|
||||||
class DBusServerBase(DBusBase):
|
class DBusServerBase(DBusBase):
|
||||||
|
|
|
@ -264,11 +264,18 @@ class SqlSession(object):
|
||||||
row = self.execute(f'DELETE FROM {table} WHERE id={rowid}')
|
row = self.execute(f'DELETE FROM {table} WHERE id={rowid}')
|
||||||
|
|
||||||
|
|
||||||
|
def drop_table(self, name):
|
||||||
|
if name not in self.table:
|
||||||
|
raise KeyError(f'Table does not exist: {name}')
|
||||||
|
|
||||||
|
self.execute(f'DROP TABLE {table}')
|
||||||
|
|
||||||
|
|
||||||
def drop_tables(self):
|
def drop_tables(self):
|
||||||
tables = self.get_tables()
|
tables = self.get_tables()
|
||||||
|
|
||||||
for table in tables:
|
for table in tables:
|
||||||
self.execute(f'DROP TABLE {table}')
|
self.drop_table(table)
|
||||||
|
|
||||||
|
|
||||||
def get_tables(self):
|
def get_tables(self):
|
||||||
|
|
Loading…
Reference in a new issue