[refactor] Move all dbus action in a specific class

This commit is contained in:
Alex 2021-05-09 19:34:50 +02:00
parent 815b27ae23
commit bcfe2af140
1 changed files with 78 additions and 63 deletions

141
mms2mail
View File

@ -81,51 +81,16 @@ class MMS2Mail:
mbox_file = self.config.get('mail', 'mailbox',
fallback=f"/var/mail/{self.user}")
self.mailbox = mailbox.mbox(mbox_file)
if self.disable_dbus:
return
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
self.bus = dbus.SessionBus()
self.dbus = None
def get_bus(self):
def set_dbus(self, dbusmmsd):
"""
Return the DBus SessionBus.
:rtype dbus.SessionBus()
:return: an active SessionBus
:param dbusmmsd: The DBus MMSd abstraction class
:type dbusmmsd: DbusMMSd()
"""
if self.disable_dbus:
return None
return self.bus
def mark_mms_read(self, dbus_path):
"""
Ask mmsd to mark the mms as read.
:param dbus_path: the mms dbus path
:type dbus_path: str
"""
if self.disable_dbus:
return None
message = dbus.Interface(self.bus.get_object('org.ofono.mms',
dbus_path),
'org.ofono.mms.Message')
log.debug(f"Marking MMS as read {dbus_path}")
message.MarkRead()
def delete_mms(self, dbus_path):
"""
Ask mmsd to delete the mms.
:param dbus_path: the mms dbus path
:type dbus_path: str
"""
if self.disable_dbus:
return None
message = dbus.Interface(self.bus.get_object('org.ofono.mms',
dbus_path),
'org.ofono.mms.Message')
log.debug(f"Deleting MMS {dbus_path}")
message.Delete()
self.dbus = dbusmmsd
def check_mms(self, path):
"""
@ -155,6 +120,14 @@ class MMS2Mail:
return False
return True
def message_added(self, name, value, member, path, interface):
"""Trigger conversion on MessageAdded signal."""
if value['Status'] == 'downloaded' or value['Status'] == 'received':
log.debug(f"New incoming MMS found ({name.split('/')[-1]})")
self.convert(value['Attachments'][0][2], name)
else:
log.debug(f"New outgoing MMS found ({name.split('/')[-1]})")
def convert(self, path, dbus_path=None):
"""
Convert a provided mms file to a mail stored in a mbox.
@ -257,15 +230,17 @@ class MMS2Mail:
time.sleep(5)
# Ask mmsd to mark message as read and delete it
self.mark_mms_read(dbus_path)
if self.disable_dbus:
return
self.dbus.mark_mms_read(dbus_path)
if self.delete:
self.delete_mms(dbus_path)
self.dbus.delete_mms(dbus_path)
class DbusWatcher():
"""Use DBus Signal notification to watch for new MMS."""
class DbusMMSd():
"""Use DBus communication with mmsd."""
def __init__(self, mms2mail):
def __init__(self, mms2mail=None):
"""
Return a DBusWatcher instance.
@ -273,16 +248,61 @@ class DbusWatcher():
:type mms2mail: mms2mail()
"""
self.mms2mail = mms2mail
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
self.bus = dbus.SessionBus()
def set_mms2mail(self, mms2mail):
"""
Set mms2mail instance handling dbus event.
:param mms2mail: An mms2mail instance to convert new mms
:type mms2mail: mms2mail()
"""
self.mms2mail = mms2mail
def mark_mms_read(self, dbus_path):
"""
Ask mmsd to mark the mms as read.
:param dbus_path: the mms dbus path
:type dbus_path: str
"""
message = dbus.Interface(self.bus.get_object('org.ofono.mms',
dbus_path),
'org.ofono.mms.Message')
log.debug(f"Marking MMS as read {dbus_path}")
message.MarkRead()
def delete_mms(self, dbus_path):
"""
Ask mmsd to delete the mms.
:param dbus_path: the mms dbus path
:type dbus_path: str
"""
if self.disable_dbus:
return None
message = dbus.Interface(self.bus.get_object('org.ofono.mms',
dbus_path),
'org.ofono.mms.Message')
log.debug(f"Deleting MMS {dbus_path}")
message.Delete()
def add_signal_receiver(self):
"""Add a signal receiver to the current bus."""
if self.mms2mail:
self.bus.add_signal_receiver(self.mms2mail.message_added,
bus_name="org.ofono.mms",
signal_name="MessageAdded",
member_keyword="member",
path_keyword="path",
interface_keyword="interface")
return True
else:
return False
def run(self):
"""Run the watcher mainloop."""
bus = self.mms2mail.get_bus()
bus.add_signal_receiver(self.message_added,
bus_name="org.ofono.mms",
signal_name="MessageAdded",
member_keyword="member",
path_keyword="path",
interface_keyword="interface")
"""Run the dbus mainloop."""
mainloop = GLib.MainLoop()
log.info("Starting DBus watcher mainloop")
try:
@ -291,14 +311,6 @@ class DbusWatcher():
log.info("Stopping DBus watcher mainloop")
mainloop.quit()
def message_added(self, name, value, member, path, interface):
"""Trigger conversion on MessageAdded signal."""
if value['Status'] == 'downloaded' or value['Status'] == 'received':
log.debug(f"New incoming MMS found ({name.split('/')[-1]})")
self.mms2mail.convert(value['Attachments'][0][2], name)
else:
log.debug(f"New outgoing MMS found ({name.split('/')[-1]})")
def main():
"""Run the different functions handling mms and mail."""
@ -324,18 +336,21 @@ def main():
after a few minutes /!\\")
args = parser.parse_args()
d = DbusMMSd()
m = MMS2Mail(args.delete, args.force_read,
args.disable_dbus, args.force_unlock)
m.set_dbus(d)
if args.files:
for mms_file in args.files:
m.convert(mms_file)
elif args.watcher:
log.info("Starting mms2mail in daemon mode")
w = DbusWatcher(m)
w.run()
d.set_mms2mail(m)
d.add_signal_receiver()
else:
parser.print_help()
d.run()
if __name__ == '__main__':