How do I communicate between python and a mac application? - python

This might be a vague question, but I failed to rephrase it properly. So here's an explanation.
I developed an app that was originally developed as a Mac application, using API's from both Carbon/Cocoa to achieve my task. (The goal of the application is to send 'keystrokes' to an app after mouse movements / other keystrokes. I use Accessibility API's for that).
But now, I want to add scripting support, so there can be conditional keystrokes. Such as: after 5 times of pressing 'Y', I want to press the button 'B'.
Within OSX, there's no possibility for a Python app to do such a thing, and I can't find any libraries that allow me to do it. So I figured I'd make the app two-fold. First: I'll write an app in Cocoa/Carbon with the basic functions: SendKeyToApp(pid,key) and WaitForMovement().
The python app will communicate with that app, using those method calls. The question here is: how do I communicate between those two apps? Will I use a HTTP Server, a Socket Server, some kind of subprocess.Popen() hacks. I frankly have no idea.
Does anyone have any ideas to achieve such a task?

Easier. I went to use Accessibility API's from within Python, and it all allowed me to easily do this without any Cocoa / Carbon at all.
For the ones interested, it's called atomac.

PyObjC is perhaps what you're looking for:
PyObjC (pronounced pie-obz-see) is the key piece which makes it possible to write Cocoa applications in Python. It enables Python objects to message Objective-C objects as if they're fellow Python objects, and likewise facilitates Objective-C objects to message Python objects as brethren.
You could write a bridge between your python app and your cocoa app using PyObjC.

There are many ways to synchronize/communicate the data between two applications, but let me explain the simplest :
RESTful Serialization:
Serialize the objects into XML/JSON or any other custom format both app frameworks can parse. eg. http://docs.python.org/library/json.html
If both apps are on the same server/machine I'm expecting all you have to do is to encode/store the data into the same file(s) and read/parse the data from respective file(s) in other application.
Otherwise you may need to create a web service for accessing the data file.
Let me know in comments if you have further queries.

In my eyes the simplest way to establish communication between two applications is the client-server protocol XMLRPC. Both Cocoa and Python do support it.
The Python part is fairly simple:
import xmlrpc.client
rpcProxy = xmlrpc.client.ServerProxy(URL_OF_SERVER)
rpcProxy.SendKeyToApp(pid,key)
As for the Cocoa-part, I don't know, but it seems to be possible: XML-RPC Server in Cocoa or Best way to use XML-RPC in Cocoa application?

On osx, many native applications support AppleScript (aka OSA) as their native scripting API. Thus your question becomes one of interacting between Python and Applescript (and figuring out how to talk to your target application in AppleScript in the first place).
There is some OSA support in the Python standard library and a third-party module py-applescript you might want to look at.

Related

Right tools to make a GUI / web interface app

What I want to create is a music player that can interact with YouTube using its Data API.
I actually have already developed my application (only for Windows, multi-platform is not a priority at the moment) using AutoIt and it's working properly. For now I createad a GUI to simply execute mpv.io, send commands to it and get its STDOUT to manage informations/errors.
Now I wanted to create a web interface to control this application using my smartphone in particular. I tried to look into TCP implementation and also WebSockets to send real time information like the playback position, but I'm not too sure what to look into.
As you probably can understand, I don't have much experience especially in network stuff so a help would be much appreciated. In the past I've fiddled with HTML,CSS,JavaScript so the web interface should not be a problem but for the communication part I don't have many ideas.
Since I created some very basic application with Python in the past, someone already suggested me to use a web framework like Flask, and so I started to properly learn Python but I find difficulties with GUIs...
So, to do a little summary I'm currently asking what can be a proper method to develop this application, also starting from scratch and changing programming language, since I'm always open to learn something new.

Gwt Front end with python web server

I am building a web app where I'd like to use GWT for generating the java script that runs in the browser but because I use other python packages (specifically scipy) I would like for the web server to be python (not a servlet engine). Has anybody done so ? Any pointers would be appreciated.
Thank you.
Ranga
GWT is a front-end framework. You can use any backend technologies, whether it is Python, PHP, Java, Node.js or any Server-side technologies. There is no limitations on this. I haven't use GWT for quite long now, You have the request builder to fetch data from the server.
Anyway, the simplest solutions that gonna scale for wide range of your devices is to build a REST API on the backend with python, its very simple, you have many simply python technologies like Bottle or Flask that does it in a giffy for you. Build your backend and make it follow REST conventions.
Then, on the level of GWT, it has to have something for fetching data from the webserver. I'll advise to make your data in JSON and then you'll be able to use technologies like GSON to convert your JSON serialised string into Java objects on the fly
Pyjamas looks like it was originally a port of GWT to Python:
https://wiki.python.org/moin/Pyjamas
This guy doesn't like it though, so you might consider the cons to doing this as well:
http://blog.pyjeon.com/?p=302

What tracking solutions are available for server side code?

I'm working on a tracking proxy (for want of a better term) written in Python. It's a simple http (wsgi) application that will run on one (maybe more) server and accepts event data from a desktop client. This service would then forward the tracking data on to some actual tracking platform (DeskMetrics, MixPanel, Google Analytics) so that we don't have to deal with the slicing and dicing of data.
The reason for this implementation is that it's much easier and faster to make changes to a server process that we control rather than having to ensure every client in the wild gets updated if the tracking backend changes in some way.
I've been looking up info on the various options and I was hoping somebody here would have some good advice from their own experiences. Ideally we'd be able to use Google Analytics as it's free for any amount of usage but paid options are fine.
My only real requirement is either a good Python library or a well documented api that I can write a wrapper for (this seems somewhat lacking in GA when it comes to triggering events through any method other than their js or other provided libs).
N.B. We're not really tracking server code so something like NewRelic isn't appropriate, we're just decoupling a desktop application from the specifics of the tracking backend.
We ran into this same problem a bunch of times, we ended up building a suite of server-side analytics libraries to make this easier.
Segment.io has libraries for Python, Ruby, Java, Node, .NET and PHP that abstract the APIs for Mixpanel, KISSmetrics, Google Analytics and a bunch of other analytics services.
You could integrate the Python library once, and then send your data wherever you want. The data is proxied through Segment.io's hosted service. Hopefully this cleans up the mess of integrating a bunch of libraries, each with slightly different APIs. (The service is free for the first million events.)
Have you tried anything below?
The Google Data APIs Python Client Library has source specific to analytics
http://code.google.com/p/gdata-python-client/
http://code.google.com/p/gdata-python-client/source/browse/#hg%2Fsamples%2Fanalytics
https://developers.google.com/gdata/articles/python_client_lib
You might be able to borrow from these sources as well;
Google has something they are working on for mobile and source is available in PHP, JSP, ASP.net and Perl: https://developers.google.com/analytics/devguides/collection/other/mobileWebsites
I also came accross this in PHP http://code.google.com/p/php-ga/
As for others:
KissMetrics: http://support.kissmetrics.com/apis/python
MixPanel: https://mixpanel.com/docs/integration-libraries/python
DeskMetrics: don't seem to have python, http://docs.deskmetrics.com/index.html
Sorry I cannot provide information based off extensive experience with anything python related other then providing a few of these resources. I would be interested to see what you come up with.

What's the best way to implement a Live chat system on my webserver?

I'm interested in something based on Jabber but I didn't find a free/opensource one so I'm thinking of writing one.
I've installed a Jabber server and now thinking about the ways in which I can write the client. I'm thinking of one of either these two methods.
1) An ajax call made to a jabber script running on the webserver that takes care of connecting to the server. But then I thought because of the dependencies involved in the jabber client, it might end up consuming too much memory when a few clients connect.
2) The other method is to run a client running as a daemon that takes care of all the heavy lifting. This way I need to have only one instance of the client that sends a spoofed message (sender's name as that of whatever the user entered on the site). A simple script running on the webserver talks to this daemon over some sort of API (XMLRPC or Msgpack maybe?)
I think #2 is better but I'm not sure. Are there other ways I can implement this? I'm considering using Perl or Python for this.
Jabber is usually called XMPP nowadays, and there are dozens of clients and servers, something for every language. If you are using Javascript (you mention Ajax), you probably want Strophe. Most servers are modular, so you only load the features you need (consider Tigase, ejabberd, or xmpppy). Writing your own is even worse an idea than it sounds.
BOSH
Install prosody because it is really eaSily installed and has BOSH support built-in. You could skip this but then you need to find out how to use BOSH via ejabberd.
use strophe.js to implement this(using BOSH). New browsers support cross-domain request(CORS -> read Proxy-less BOSH part). The old browsers you could use proxy or use flash in the middle as proxy.
read Professional XMPP Programming with JavaScript and jQuery to learn strophe. It even has chapters explaining how to create chat.
Node.js
Or you could consider installing node.js to create your chat system using socket.io.

Can I "embed" a Python back-end in an AIR application?

I'm trying to find out if there is a way I could embed a Python back-end into an AIR application? I'm looking to employ an approach similar to the one outlined here to implement the business logic for my application, but additionally, I would like to provide the user with a single binary which they can load. I don't want the user to have to fire up a seperate server process to make this work. Is this possible in some way or am I out of luck?
You cannot embed your Python server in an AIR application. So basically you are out of luck.
The simplest solution probably is to run a server on a central location that all your users can connect to from their AIR apps. That means that all/most of the data will be on your server, and not on the users computer, I don't know if that is a big issue but I guess it is.
Also depending on your target systems you could create the program you want yourself without (fully) depending on AIR. You can generate executables for windows and osx from Flash CS3/4 or you can use a special (commercial) executable-maker that provides some more functionality. Wrapping this exe and your python program in a meta-executable that launches both should be possible with some work. Of course you won't have the benefits if the AIR installer etc in this case.
OK, so since it didn't seem possible to go that way around, I came up with an alternative that seems to work for what I want.
Instead of trying to embed Python inside AIR, I've gone the other way around: I'm building my Python code into a stand-alone executable using PyInstaller and bundling the AIR application as a resource. The Python code then starts up it's webserver and fires off the AIR app which can then connect to the (local) remote services as required.
Probably. We are using a J2EE server side which uses SOAP webservices to talk to our AIR application on the frontend. You should be able to do the same because soap doesn't care which technology sits on either side of it.
You can always have the application launch from a single binary which first fires up the server, then the client, if both are expected to sit on the users system. Also it gives you flexibility to have a more service oriented model later, if you want to. Without knowing what your app does, it is hard to know if that makes sense or not.
For setting up the python side of SOAP webservices, here's a useful link to a diveintopython article. Then, if you have your server running with the wsdl, FlexBuilder can generate the AIR side of the webservices for you.

Categories