Connect DBUS Service method to another method - python

I have 4 DBUS Services python script (a.py,b.py,c.py,d.py) and Run it in 'main.py'
The Reason why i want to merge dbus services, is because of %Memory per process running. 2.0% Memory per dbus service. I'll create a 15 of dbus services.
main.py
#!/usr/bin/env python
import sys
import gobject
from dbus.mainloop.glib import DBusGMainLoop
listofdbusfilenames = ['a','b','b','d']
def importDbusServices():
for dbusfilename in listofdbusfilenames:
globals()[dbusfilename] = __import__(dbusfilename)
def callservices():
for dbusfilename in listofdbusfilenames:
globals()[dbusfilename +'_var'] = eval(dbusfilename +'.ServiceClass()')
if __name__ == '__main__':
importDbusDervices()
DBusGMainLoop(set_as_default = True)
callservices()
loop = gobject.MainLoop()
loop.run()
a.py wants to get the return method from b.py
b.py will get the return method of c.py and d.py
-a.py
--|b.py
----|c.py
----|d.py
The PROBLEMS:
I can't get the return method ''get_dbus_method'', Introspect Error appears.
I tried signal and receiver BUT it takes longer than ''get_dbus_method''.
my dbus service format
import dbus
import dbus.service
class ServiceClass(dbus.service.Object):
def __init__(self):
busName = dbus.service.BusName('test.a', bus = dbus.SystemBus())
dbus.service.Object.__init__(self, busName, '/test/a')
#dbus.service.method('test.a')
def aMethod1(self):
#get the b.py method value 'get_dbus_method' here
return #value from b.py method
Is there any other way to get the method directly?
Thanks in advance for response and reading this. :D

Related

Run function in another thread

So say there are two running codes: script1 and script2.
I want script2 to be able to run a function in script1.
script1 will be some kind of background process that will run "forever".
The point is to be able to make an API for a background process, E.G. a server.
The unclean way to do it would be to have a file transmit the orders from script2. script1 would then execute it with exec(). However, I would like to use a module or something cleaner because then I would be able to output classes and not only text.
EDIT: example:
script1:
def dosomething(args):
# do something
return information
while True:
# Do something in a loop
script2:
# "import" the background process
print(backgroundprocess.dosomething(["hello", (1, 2, 3)]))
The execution would look like this:
Run script1
Run script2 in a parallel window
Summary
The XMLRPC modules are designed for this purpose.
The docs include a worked out example for a server (script1) and a client (script2).
Server Example
from xmlrpc.server import SimpleXMLRPCServer
from xmlrpc.server import SimpleXMLRPCRequestHandler
class RequestHandler(SimpleXMLRPCRequestHandler):
rpc_paths = ('/RPC2',)
# Create server
with SimpleXMLRPCServer(('localhost', 8000),
requestHandler=RequestHandler) as server:
server.register_introspection_functions()
# Register pow() function; this will use the value of
# pow.__name__ as the name, which is just 'pow'.
server.register_function(pow)
# Register a function under a different name
def adder_function(x, y):
return x + y
server.register_function(adder_function, 'add')
# Register an instance; all the methods of the instance are
# published as XML-RPC methods (in this case, just 'mul').
class MyFuncs:
def mul(self, x, y):
return x * y
server.register_instance(MyFuncs())
# Run the server's main loop
server.serve_forever()
Client Example
import xmlrpc.client
s = xmlrpc.client.ServerProxy('http://localhost:8000')
print(s.pow(2,3)) # Returns 2**3 = 8
print(s.add(2,3)) # Returns 5
print(s.mul(5,2)) # Returns 5*2 = 10
# Print list of available methods
print(s.system.listMethods())

How can I mock.patch in web script via HTTPServer?

I'd like to unittest web script on HTTPServer.
But mock.patch isn't working via HTTPServer.
it seems kicking subprocess inside.
For example, my web scripts has some external web access.
web script:
#!/usr/bin/python3
import requests
class Script:
def main(self):
res = requests.put('http://www.google.co.jp') # get response code 405
print('Content-type: text/html; charset=UTF-8\n')
print(res.content)
if __name__ == '__main__':
Script().main()
And my test script seems it can't mock the external web access.
test script:
import unittest
import requests
from http.server import HTTPServer, CGIHTTPRequestHandler
from threading import Thread
from unittest import TestCase, mock
class MockTreadTest(TestCase):
def test_default(self):
server = HTTPServer(('', 80), CGIHTTPRequestHandler)
server_thread = Thread(target=server.serve_forever)
server_thread.start()
try:
with mock.patch('requests.put', return_value='<html>mocked response</html>') as put:
res = requests.get('http://localhost/cgi-bin/script.py')
self.assertRegex(str(res.content), 'mocked response') # fail
self.assertRegex(put.call_args_list[0][0][0], 'http://www.google.co.jp')
finally:
server.shutdown()
server_thread.join()
if __name__ == "__main__":
unittest.main()
The MockTreadTest is presently not testing the webscript. It is right now starting a WSGI server and it looks like the try block is calling a non-existent script. I recommend reading more about testing and mocking. I think you are trying to test the main() function in the Script class. Here is a test function to help you with:
from unittest.mock import TestCase, patch
# replace `your_script` with the name of your script
from your_script import Script
# import the requests object from your script. IMPORTANT, do not do import request
from your_script import requests
class ScriptTestCase(TestCase):
def test_main(self):
script = Script()
# Now patch the requests.put function call going to the server
with patch('requests.put', return_value="<html>mocked response</html>") as mockput:
script.main() # now call the main function <-- this is what we are testing
mockput.assert_called_with('http://www.google.co.jp')
Presently you are just printing in the response in you script. So there is no way to test the return value. Use return statement in the main() function to achieve it. Then you can do as given below in the with block.
response = script.main()
self.assertIn('mocked response', response.content)

How can i move python functions into different .py file?

i want to move functions because of lot of files, into separate python files.
But if i do it, it dont work.
I tried:
File: server.py:
import os, cherrypy, json
from customers.py import *
class application(object):
def get_webpage(self):
....
def get_data(self):
....
File: customers.py:
import os, cherrypy, json
def get_customer_data(self):
....
I use the python as server,
the data in the function: get_customer_data is in this case not processed, get a 404 Not Found,
means the function is not included in main file (server.py)
I removed the self from get_webpages() because it was not indented, which means it was not part of the class.
application.py:
class application(object):
def __init__(self):
pass
def get_webpage():
print('From application')
customers.py:
from application import *
get_webpage() # From application
You could indent get_webpages() and make it part of the class. The way you call it would change. (I put the self back and capitalized the name of the class.)
application.py:
class Application(object):
def __init__(self):
pass
def get_webpage(self):
print('From application')
customers.py:
from application import *
a = Application()
a.get_webpage() # From application

twisted: How to send and receive the same object with Perspective Broker?

I have a simple 'echo' PB client and server where the client sends an object to the server which echo the same object back to the client:
The client:
from twisted.spread import pb
from twisted.internet import reactor
from twisted.python import util
from amodule import aClass
factory = pb.PBClientFactory()
reactor.connectTCP("localhost", 8282, factory)
d = factory.getRootObject()
d.addCallback(lambda object: object.callRemote("echo", aClass()))
d.addCallback(lambda response: 'server echoed: '+response)
d.addErrback(lambda reason: 'error: '+str(reason.value))
d.addCallback(util.println)
d.addCallback(lambda _: reactor.stop())
reactor.run()
The server:
from twisted.application import internet, service
from twisted.internet import protocol
from twisted.spread import pb
from amodule import aClass
class RemoteClass(pb.RemoteCopy, aClass):
pass
pb.setUnjellyableForClass(aClass, RemoteClass)
class PBServer(pb.Root):
def remote_echo(self, a):
return a
application = service.Application("Test app")
# Prepare managers
clientManager = internet.TCPServer(8282, pb.PBServerFactory(PBServer()));
clientManager.setServiceParent(application)
if __name__ == '__main__':
print "Run with twistd"
import sys
sys.exit(1)
The aClass is a simple class implementing Copyable:
from twisted.spread import pb
class aClass(pb.Copyable):
pass
When i run the above code, i get this error:
twisted.spread.jelly.InsecureJelly: Module builtin not allowed (in type builtin.RemoteClass).
In fact, the object is sent to the server without any problem since it was secured with pb.setUnjellyableForClass(aClass, RemoteClass) on the server side, but once it gets returned to the client, that error is raised.
Am looking for a way to get an easy way to send/receive my objects between two peers.
Perspective broker identifies classes by name when talking about them over the network. A class gets its name in part from the module in which it is defined. A tricky problem with defining classes in a file that you run from the command line (ie, your "main script") is that they may end up with a surprising name. When you do this:
python foo.py
The module name Python gives to the code in foo.py is not "foo" as one might expect. Instead it is something like "__main__" (which is why the if __name__ == "__main__": trick works).
However, if some other part of your application later tries to import something from foo.py, then Python re-evaluates its contents to create a new module named "foo".
Additionally, the classes defined in the "__main__" module of one process may have nothing to do with the classes defined in the "__main__" module of another process. This is the case in your example, where __main__.RemoteClass is defined in your server process but there is no RemoteClass in the __main__ module of your client process.
So, PB gets mixed up and can't complete the object transfer.
The solution is to keep the amount of code in your main script to a minimum, and in particular to never define things with names there (no classes, no function definitions).
However, another problem is the expectation that a RemoteCopy can be sent over PB without additional preparation. A Copyable can be sent, creating a RemoteCopy on the peer, but this is not a symmetric relationship. Your client also needs to allow this by making a similar (or different) pb.setUnjellyableForClass call.

Python and d-bus: How to set up main loop?

I have a problem with python and dbus. I checked out the developer docs and specifications, but I don't understand how to set up a main loop. I want to listen for notification events.
See
http://dbus.freedesktop.org/doc/dbus-python/doc/
and
http://www.galago-project.org/specs/notification/0.9/index.html
My example script:
import dbus
from dbus.mainloop.glib import DBusGMainLoop
class MessageListener:
def __init__(self):
DBusGMainLoop(set_as_default=True)
self.bus = dbus.SessionBus()
self.proxy = self.bus.get_object('org.freedesktop.Notifications',
'/org/freedesktop/Notifications')
self.proxy.connect_to_signal('NotificationClosed',
self.handle_notification)
def handle_notification(self, *args, **kwargs):
print args, kwargs
if __name__ == '__main__':
MessageListener()
DBusGMainLoop has no further methods like run().
If I use a loop from gobject and change the sourcecode:
import gobject
loop = gobject.MainLoop()
dbus.set_default_main_loop(loop)
...
loop.run()
I get following error message:
Traceback (most recent call last):
File "dbus_example.py", line 40, in <module>
MessageListener()
File "dbus_example.py", line 9, in __init__
dbus.set_default_main_loop(loop)
TypeError: A dbus.mainloop.NativeMainLoop instance is required
Any idea what to do about it?
Thanks in advance.
phineas
Put import gobject at the top of your code, and after instantiating your object, do gobject.MainLoop().run(). I think that the MainLoop has to be created after the DBusGMainLoop is created.
I had the same problem. After getting my code to work, I came across this question.
Dan's answer is partially correct. You import gobject first, but you can also instantiate your MainLoop before the DBusGMainLoop is created. You should run it after the DBusGMainLoop is created.

Categories