0

I searched for an answer to this question in several places, but I could not find a consistent solution, and some are too old and unclear.

I have a classe where the interface path for dbus is generated in runtime, so I need to export methods with the properly interface, for example:

One instance of my service start dbus with interface br.example.MyInterface.Number1, and a second instance of service start dbus with br.example.MyInterface.Number2, so the decorator for each method will be:

br.example.MyInterface.Number1 ; and br.example.MyInterface.Number2

Cannot make this work with static decorator like @dbus.service.method('com.example.MyInterface.Number1'), because they are distincts.

How can I export methods to dbus in runtime using python?

Juliao
  • 127
  • 3
  • 12

1 Answers1

0
@dbus.service.method('com.example.MyInterface.Number%d' % (instancenum,))
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Cannot believe that this works, I don't found a single page that cited this before, one of my tries was: `@dbus.service.method('com.example.MyInterface.Number' + str(123))`, but don't work, I was near :) THANKS! Unfortunally I spend half day searching a solution. But still a problem, I tested and cannot access member classes with `self.number` for example, just with a global variable. – Juliao Dec 10 '12 at 23:25
  • Only because `self` isn't in scope there. Declare it in a method, such as `__init__()`. – Ignacio Vazquez-Abrams Dec 11 '12 at 00:05
  • If I understood you right, I put the method `def test()` with the @decorator inside `__init__()`, the service starts ok, but when I call the method test using `dbus-send` command I got a error `"test is not a valid method of interface"`. With the method test() in the right place and a hardcoded number in @decorator, works. Sorry, I still new in python programming. – Juliao Dec 11 '12 at 01:09
  • I don't find a way to export the methods dynamically, but I solved my original problem posted here: http://stackoverflow.com/questions/13765124/how-export-methods-with-dbus-in-a-extended-class-in-python-inherited-methods . Because I don't find a way to change the `instancenum` (of your example) inside a class, I cannot mark this as a answer. – Juliao Dec 28 '12 at 13:09
  • @Msum: That's what closures are for. – Ignacio Vazquez-Abrams Dec 28 '12 at 13:11
  • Maybe you can improve your answer, I don't know closures yet, but will search for it now that you have cited, Thanks! – Juliao Dec 28 '12 at 13:15