Content-type: multipart/related; boundary="Boundary_(ID_ZlgU7bom3M14NKu5YYPRhg)"; type="text/html" --Boundary_(ID_ZlgU7bom3M14NKu5YYPRhg) Content-type: text/html; CHARSET=US-ASCII Content-transfer-encoding: quoted-printable
Ken & Georg,Thanks for your replies.&nb= sp;I've already gotten this far -- i.e. I'm runn= ing the interpreter in process, executing arbitrary python scripts in it, = and trading objects back and forth via NSClassFromString and other API wit= h provided wrappers. What I was hoping to achieve was "first-class" suppor= t for my classes -- like the framework wrappers that ship with PyObjC for = the system frameworks.I've made some progr= ess by brute force. I've gotten objective-metadata-tool installed, and I'm= currently working through getting it to scan my framework's headers. This= process appears under-documented, so my experience is basically, "Try an = invocation of objective-metadata-tool in the debugger. See what fails. Fig= ure out why it failed. Adjust invocation to avoid that condition. Repeat."= I'll report back with my findings if I have any eventual success.= div>= --Boundary_(ID_ZlgU7bom3M14NKu5YYPRhg)--Thanks,Ian
=<= /blockquote>= On Wed, Feb 19, 2014 at 2:56 AM, Ken Thomases <firstname.lastname@example.org> wrote:On Feb 18, 2014, at 5:23 PM, Ian McCul= lough wrote:It is. I don't know if this is the best way, but o= ne way is to link an otherwise ordinary Cocoa app, written in Objective-C,= to the Python framework. You would then use the Python embedding AP= I to set up the Python runtime and load and run some Python code. Yo= u might do something like:
> I'm trying to determine the best way to expose = custom,
> application-specific, non-system-framework (i.e. not in o= ne of the
> framework wrappers provided with PyObjC) classes writte= n in Objective-C to
> python across the PyObjC bridge. My eventual = hope is to develop a "hybrid"
> application in which some portions = of the app are in Objective-C and others
> are in python, with the = two side integrating via the PyObjC bridge. Most of
> the documenta= tion I've been able to find seems to focus on situations where
> th= e goal is to have all the app-specific code be in python, and to have the<= br> > python call out to AppKit classes, etc via the bridge and the pre= -packaged
> system framework wrappers.
> I've not s= een any indication that it's possible to expose classes from an
> a= pp.
NSString* pythonPath =3D= /* path within your app bundle to its Python files */;
= setenv("PYTHONPATH", [pythonPath fileSystemRepresentation], 1);
&= nbsp; Py_SetProgramName("/usr/bin/python2.6"); // or whatever versi= on of Python you link against; don't know how relevant this is
&nb= sp; PyGILState_STATE gilState =3D PyGILState_Ensure();
&nb= sp; NSString* script =3D /* path to a initialization script in your= app bundle */;
static char* arg0 =3D strdup([script fil= eSystemRepresentation]); // don't deallocate until Python runtime is termi= nated, if ever
static char** argv =3D &arg0; // ditt= o
FILE* s= criptFile =3D fopen([script fileSystemRepresentation], "r");
&n= bsp; int result =3D PyRun_SimpleFileEx(scriptFile, (char *)[[script lastPa= thComponent] fileSystemRepresentation], 1);
if (result) = /* handle error */;
PyGILState_Release(gilState); if (gilState =3D=3D PyGILState_LOCKED)
&nb= sp; PyEval_ReleaseLock();
The s= cript file mentioned above would just import modules and do initialization= and then exit. It wouldn't be a long-running script. Since th= e application keeps running and the Python runtime is persistent, the stat= e which is set up by the script persists, too.
After that, you ha= ve a few ways of interacting between the Python code and the Objective-C c= ode. Any Python class which inherits from a Cocoa class is registere= d with the Objective-C runtime. It can be looked up with NSClassFrom= String(). Then, it's methods can be invoked directly. You can = make that simpler by declaring Objective-C interfaces for those classes.
The Objective-C code can pass its objects into the methods of the = Python classes and the Python code can then operate on those objects.
Also, the Python code can find some objects itself, such as NSApp or = various singletons. It can then do things like set itself as delegat= es or register for notifications.
I believe that the classes of y= our app, since they were registered with the Objective-C runtime at the ti= me that Python was loaded and your initialization script was run, can just= be used directly in your Python scripts. If that doesn't work, I'm = sure you can use Foundation.NSClassFromString() to get access.
I = hope that helps.
<= /div>---------------------------------------------= ---------------------------------
Managing the Performance of Cloud-Ba= sed Applications
Take advantage of what the Cloud has to offer - Avoid= Common Pitfalls.
Read the Whitepaper.
http://pubads.g.doubleclick.net/gampad/clk?id=3D1= 21054471&iu=3D/4140/ostg.clktrk_____= __________________________________________
Pyobjc-dev mailing list
= Pyobjcemail@example.com<= br> ht= tps://lists.sourceforge.net/lists/listinfo/pyobjc-dev