Hi Allan, 

apologies for resurrecting this very old thread, but I'm kind of stuck again. 

In short, I'm developing a bundle for sending messages to the Extempore compiler (http://extempore.moso.com.au/; http://benswift.me/2012/09/26/interacting-with-the-extempore-compiler/) which should happen via a TCP socket connection. 

Ideally, I'd like to create the following commands:

a) start the server  [done, see https://github.com/lambdamusic/extempore-bundle]
b) set up a reusable socket connection object 
c) evaluate expressions by sending them to the server using the connection object 

I've set up environment variables in Textmate to hold my connection settings; also, I've created a variable called EXTEMPORE_CONNECTION which in theory should keep the pickled version of my server socket.  

Here's my Python command:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os, socket
import pickle

xtm_host = str(os.environ.get('EXTEMPORE_HOST', ""))
xtm_port = int(os.environ.get('EXTEMPORE_PORT', 7099)) 

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

HOST = xtm_host 
PORT = xtm_port

sock.connect((HOST, PORT))
data = sock.recv(1024)
print(data)  # welcome message, works.

os.environ['EXTEMPORE_CONNECTION'] = pickle.dumps(sock, -1)  # *fails*


The problem is, Python's pickle doesn't support all kinds of objects, and sockets are among those (see http://stackoverflow.com/questions/2204155/why-am-i-getting-an-error-about-my-class-defining-slots-when-trying-to-pickl)

So I wonder if there's another way of approaching the problem; or, if by any chance, the same approach done via another language (eg Ruby) would not encounter any limitation.. 

Thanks in advance for the help, 


Michele Pasin

On 14 August 2013 at 09:55, Allan Odgaard <mailinglist@textmate.org> wrote:
On 14 Aug 2013, at 2:41, Michele Pasin wrote:

[…] when I do this
os.environ['TM_PROJECT_UUID'] = someobject
I get an error […]

You can’t assign to environment variables. The variable is to be used as a key into a database, in the simplest case something like:

    uuid = os.environ['TM_PROJECT_UUID'] # Same when window is the same
    file = os.path.join(tempfile.gettempdir(), "tm_«your_command»_" + uuid + ".pickle")
    pickle.dump(someobject, file)

For each invocation of your command, ‘uuid’ will be the same, as long as it’s called for the same (project) window.

Your command would start by testing if ‘file’ already exists, and if so, read ‘someobj’ by unpickling it.

textmate mailing list