def update_tag(self, tag_struct): """Update tag""" try: tag_btype = btype.Tag << tag_struct tag = self.session.query(models.Tag).filter( (models.Tag.id == tag_btype.id) & (models.Tag.action != const.ACTION_DELETE) ).one() if self.session.query(models.Tag).filter( (models.Tag.id != tag_btype.id) & (models.Tag.name == tag_btype.name) ).count(): raise DBusException( 'Tag with this name already exist', ) tag.action = const.ACTION_CHANGE tag_btype.give_to_obj(tag) self.session.commit() self.data_changed() return btype.Tag >> tag except NoResultFound: raise DBusException('Tag does not exist')
def update_notebook(self, notebook_struct): """Update notebook""" try: notebook_btype = btype.Notebook << notebook_struct notebook = self.session.query(models.Notebook).filter( (models.Notebook.id == notebook_btype.id) & (models.Notebook.action != const.ACTION_DELETE) ).one() if self.session.query(models.Notebook).filter( (models.Notebook.id != notebook_btype.id) & (models.Notebook.name == notebook_btype.name) ).count(): raise DBusException( 'Notebook with this name already exist', ) notebook.action = const.ACTION_CHANGE notebook_btype.give_to_obj(notebook) self.session.commit() self.data_changed() return btype.Notebook >> notebook except NoResultFound: raise DBusException('Notebook does not exist')
def call_blocking(self, bus_name, object_path, dbus_interface, method, signature, args, timeout=-1.0, byte_arrays=False, **kwargs): """Call the given method, synchronously. :Since: 0.81.0 """ if object_path == LOCAL_PATH: raise DBusException('Methods may not be called on the reserved ' 'path %s' % LOCAL_PATH) if dbus_interface == LOCAL_IFACE: raise DBusException('Methods may not be called on the reserved ' 'interface %s' % LOCAL_IFACE) # no need to validate other args - MethodCallMessage ctor will do get_args_opts = dict(byte_arrays=byte_arrays) if is_py2: get_args_opts['utf8_strings'] = kwargs.get('utf8_strings', False) elif 'utf8_strings' in kwargs: raise TypeError("unexpected keyword argument 'utf8_strings'") message = MethodCallMessage(destination=bus_name, path=object_path, interface=dbus_interface, method=method) # Add the arguments to the function try: message.append(signature=signature, *args) except Exception as e: logging.basicConfig() _logger.error( 'Unable to set arguments %r according to ' 'signature %r: %s: %s', args, signature, e.__class__, e) raise # make a blocking call reply_message = self.send_message_with_reply_and_block( message, timeout) args_list = reply_message.get_args_list(**get_args_opts) if len(args_list) == 0: return None elif len(args_list) == 1: return args_list[0] else: return tuple(args_list)
def call_async(self, bus_name, object_path, dbus_interface, method, signature, args, reply_handler, error_handler, timeout=-1.0, utf8_strings=False, byte_arrays=False, require_main_loop=True): """Call the given method, asynchronously. If the reply_handler is None, successful replies will be ignored. If the error_handler is None, failures will be ignored. If both are None, the implementation may request that no reply is sent. :Returns: The dbus.lowlevel.PendingCall. :Since: 0.81.0 """ if object_path == LOCAL_PATH: raise DBusException('Methods may not be called on the reserved ' 'path %s' % LOCAL_PATH) if dbus_interface == LOCAL_IFACE: raise DBusException('Methods may not be called on the reserved ' 'interface %s' % LOCAL_IFACE) # no need to validate other args - MethodCallMessage ctor will do get_args_opts = { 'utf8_strings': utf8_strings, 'byte_arrays': byte_arrays } message = MethodCallMessage(destination=bus_name, path=object_path, interface=dbus_interface, method=method) # Add the arguments to the function try: message.append(signature=signature, *args) except Exception, e: logging.basicConfig() _logger.error( 'Unable to set arguments %r according to ' 'signature %r: %s: %s', args, signature, e.__class__, e) raise
def error_handler_with_conversion(e): # We can’t use log.exception() because the traceback is no longer available. # So the three cases in dbus_handle_exceptions amount to just this. if not isinstance(e, DBusException): log.error("{0}: {1}".format(type(e), str(e))) e = DBusException(str(e)) error_handler(e)
def update_note_resources(self, note_id, resources_struct): """Update note resources""" try: note = self.session.query(models.Note).filter( models.Note.id == note_id, ).one() except NoResultFound: raise DBusException('models.Note not found') self.session.query(models.Resource).filter( models.Resource.note_id == note.id, ).delete() for resource_btype in btype.Resource.list << resources_struct: resource = models.Resource( action=const.ACTION_CREATE, note_id=note.id, ) resource_btype.give_to_obj(resource) resource.id = None self.session.add(resource) if note.action != const.ACTION_CREATE: note.action = const.ACTION_CHANGE self.session.commit() self.data_changed() return btype.Note >> note
def get_note(self, id): try: return btype.Note.from_obj(self.sq(Note).filter( and_(Note.id == id, Note.action != ACTION_DELETE), ).one()).struct except NoResultFound: raise DBusException('Note not found')
def dbus_handle_exceptions(func, *args, **kwargs): """Decorator to handle exceptions, log and report them into D-BUS :Raises DBusException: on a firewall error code problems. """ try: return func(*args, **kwargs) except FirewallError as error: log.error(str(error)) raise DBusException(str(error)) except DBusException as e: # only log DBusExceptions once raise e except Exception as e: log.exception() raise DBusException(str(e))
def delete_note(self, id): try: self.sq(Note).filter(Note.id == id).one().action = ACTION_DELETE self.session.commit() self.data_changed() return True except NoResultFound: raise DBusException('Note not found')
def dbus_handle_exceptions(func, *args, **kwargs): """Decorator to handle exceptions, log and convert into DBusExceptions. :Raises DBusException: on any exception raised by the decorated function. """ # Keep this in sync with async.start_with_dbus_callbacks() try: return func(*args, **kwargs) except RolekitError as error: log.error(str(error)) raise DBusException(str(error)) except DBusException: # only log DBusExceptions once, pass it through raise except Exception as e: log.exception() raise DBusException(str(e))
def update_notebook(self, notebook_struct): try: notebook = btype.Notebook.from_tuple(notebook_struct) nb = self.sq(Notebook).filter(Notebook.id == notebook.id, ).one() if self.sq(Notebook).filter( and_( Notebook.id != notebook.id, Notebook.name == notebook.name, )).count(): raise DBusException('Notebook with this name already exist', ) nb.action = ACTION_CHANGE self.session.commit() notebook.give_to_obj(nb) self.data_changed() return btype.Notebook.from_obj(nb).struct except NoResultFound: raise DBusException('Notebook does not exist')
def get_notebook(self, id): try: return btype.Notebook.from_obj(self.sq(Notebook).filter( and_(Notebook.id == id, Notebook.action != ACTION_DELETE, )).one()).struct except NoResultFound: raise DBusException('Notebook does not exist')
def Set(self, interface_name, property_name, value): """Set value of property on named interface to value. interface_name may be empty, but if there are many properties with the same name the behaviour is undefined. """ prop = self._get_decorator(interface_name, property_name) if not prop.fset: raise DBusException("Property %s not writable" % property_name) return prop.fset(self, value)
def _get_decorator(self, interface_name, property_name): interfaces = self._dbus_interface_table if interface_name: interface = interfaces.get(interface_name) if interface is None: raise DBusException("No interface %s on object" % interface_name) prop = interface.get(property_name) if prop is None: raise DBusException("No property %s on object interface %s" % (property_name, interface_name)) if not isinstance(prop, dbus_property): raise DBusException("Name %s on object interface %s is not a property" % (property_name, interface_name)) return prop else: for interface in interfaces.values(): prop = interface.get(property_name) if prop and isinstance(prop, dbus_property): return prop raise DBusException("No property %s found" % (property_name,))
def msg_reply_handler(message): if isinstance(message, MethodReturnMessage): reply_handler(*message.get_args_list(**get_args_opts)) elif isinstance(message, ErrorMessage): error_handler(DBusException(name=message.get_error_name(), *message.get_args_list())) else: error_handler(TypeError('Unexpected type for reply ' 'message: %r' % message))
def delete_notebook(self, id): try: self.sq(Notebook).filter( Notebook.id == id, ).one().action = ACTION_DELETE self.session.commit() self.data_changed() return True except NoResultFound: raise DBusException('Notebook does not exist')
def share_note(self, note_id): try: note = self.sq(Note).filter( and_(Note.id == note_id, Note.action != ACTION_DELETE), ).one() note.share_status = SHARE_NEED_SHARE self.session.commit() self.sync() except NoResultFound: raise DBusException('Note not found')
def get_notebook(self, id): """Get notebook by id""" try: notebook = self.session.query(models.Notebook).filter( (models.Notebook.id == id) & (models.Notebook.action != const.ACTION_DELETE) ).one() return btype.Notebook >> notebook except NoResultFound: raise DBusException('Notebook does not exist')
def get_note(self, id): """Get note by id""" try: note = self.session.query(models.Note).filter( (models.Note.id == id) & (models.Note.action != const.ACTION_DELETE) ).one() return btype.Note >> note except NoResultFound: raise DBusException('models.Note not found')
def get_note_by_guid(self, guid): """Get note by guid""" try: note = self.session.query(models.Note).filter( (models.Note.guid == guid) & ~models.Note.action.in_(const.DISABLED_ACTIONS) ).one() return btype.Note >> note except NoResultFound: raise DBusException('Note not found')
def create_notebook(self, name): if self.sq(Note).filter(Notebook.name == name, ).count(): raise DBusException('Notebook with this name already exist', ) notebook = Notebook( action=ACTION_CREATE, name=name, default=False, ) self.session.add(notebook) self.session.commit() self.data_changed() return btype.Notebook.from_obj(notebook).struct
def update_tag(self, tag_struct): try: tag = btype.Tag.from_tuple(tag_struct) tg = self.sq(Tag).filter( and_(Tag.id == tag.id, Tag.action != ACTION_DELETE, )).one() if self.sq(Tag).filter(and_( Tag.id != tag.id, Tag.name == tag.name, )).count(): raise DBusException( 'Tag with this name already exist', ) tg.action = ACTION_CHANGE self.session.commit() tag.give_to_obj(tg) self.data_changed() return btype.Tag.from_obj(tg).struct except NoResultFound: raise DBusException('Tag does not exist')
def share_note(self, note_id): """Share note""" try: note = self.session.query(models.Note).filter( (models.Note.id == note_id) & (models.Note.action != const.ACTION_DELETE) ).one() note.share_status = const.SHARE_NEED_SHARE self.session.commit() self.sync() except NoResultFound: raise DBusException('models.Note not found')
def delete_notebook(self, id): """Delete notebook""" try: notebook = self.session.query(models.Notebook).filter( models.Notebook.id == id, ).one() notebook.action = const.ACTION_DELETE self.session.commit() self.data_changed() return True except NoResultFound: raise DBusException('Notebook does not exist')
def call_blocking(self, bus_name, object_path, dbus_interface, method, signature, args, timeout=-1.0, utf8_strings=False, byte_arrays=False): """Call the given method, synchronously. :Since: 0.81.0 """ if object_path == LOCAL_PATH: raise DBusException('Methods may not be called on the reserved ' 'path %s' % LOCAL_PATH) if dbus_interface == LOCAL_IFACE: raise DBusException('Methods may not be called on the reserved ' 'interface %s' % LOCAL_IFACE) # no need to validate other args - MethodCallMessage ctor will do get_args_opts = { 'utf8_strings': utf8_strings, 'byte_arrays': byte_arrays } message = MethodCallMessage(destination=bus_name, path=object_path, interface=dbus_interface, method=method) # Add the arguments to the function try: message.append(signature=signature, *args) except Exception, e: logging.basicConfig() _logger.error( 'Unable to set arguments %r according to ' 'signature %r: %s: %s', args, signature, e.__class__, e) raise
def update_note(self, note): received_note = btype.Note.from_tuple(note) try: note = self.sq(Note).filter(Note.id == received_note.id, ).one() except NoResultFound: raise DBusException('Note not found') received_note.give_to_obj(note) if note.action == ACTION_NOEXSIST: note.action = ACTION_CREATE elif note.action != ACTION_CREATE: note.action = ACTION_CHANGE note.updated = int(time.time() * 1000) self.session.commit() self.data_changed() return btype.Note.from_obj(note).struct
def delete_tag(self, id): try: tag = self.sq(Tag).filter(and_( Tag.id == id, Tag.action != ACTION_DELETE, )).one() tag.action = ACTION_DELETE for note in self.sq(Note).filter( Note.tags.contains(tag), ).all(): note.tags.remove(tag) self.session.commit() self.data_changed() return True except NoResultFound: raise DBusException('Tag does not exist')
def delete_note(self, id): try: note = self.sq(Note).filter(Note.id == id).one() if note.action == ACTION_CONFLICT: # prevent circular dependency error note.conflict_parent_id = None note.conflict_parent = [] self.session.commit() self.session.delete(note) else: note.action = ACTION_DELETE self.session.commit() self.data_changed() return True except NoResultFound: raise DBusException('Note not found')
def create_notebook(self, name, stack): """Create new notebook""" if self.session.query(models.Note).filter( models.Notebook.name == name, ).count(): raise DBusException( 'models.Notebook with this name already exist', ) notebook = models.Notebook( action=const.ACTION_CREATE, name=name, default=False, stack=stack, ) self.session.add(notebook) self.session.commit() self.data_changed() return btype.Notebook >> notebook
def __init__(self, proxy, connection, bus_name, object_path, method_name, iface): if object_path == LOCAL_PATH: raise DBusException('Methods may not be called on the reserved ' 'path %s' % LOCAL_PATH) # trust that the proxy, and the properties it had, are OK self._proxy = proxy self._connection = connection self._named_service = bus_name self._object_path = object_path # fail early if the method name is bad _dbus_bindings.validate_member_name(method_name) # the test suite relies on the existence of this property self._method_name = method_name # fail early if the interface name is bad if iface is not None: _dbus_bindings.validate_interface_name(iface) self._dbus_interface = iface
def __init__(self, msg=''): DBusException.__init__(self, msg)
def __init__(self, msg): DBusException.__init__(self, msg) self._dbus_error_name = PRESENCE_INTERFACE + ".WrongConnection"
def __init__(self, msg): DBusException.__init__(self, msg) self._dbus_error_name = PRESENCE_INTERFACE + ".NotFound"