Hi all,
I'm trying to develop a bundle for communicating with a TCP server, and would like to create 1) a bundle command that opens the connection, and 2) other commands that use that connection object (eg via grabbing text selection in the editor window).
In Python, I've created a bundle command like this:
import socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = "127.0.0.1" PORT = 7098
sock.connect((HOST, PORT))
sock.send("some command...") print sock.recv(512)
That sets up the socket correctly, in fact you can send messages to the TCP server.
However each time I send a command, the socket is obviously re-created; I wondered if I TextMate provides a mechanism to save the 'sock' binding in the current environment - so that I can reuse it later within other commands.
Hope this makes sense - thanks in advance for any help.
Mike
On 11 Aug 2013, at 19:02, Michele Pasin wrote:
[…] each time I send a command, the socket is obviously re-created; I wondered if I TextMate provides a mechanism to save the 'sock' binding in the current environment - so that I can reuse it later within other commands.
Not per se, but TextMate does give you a TM_DOCUMENT_UUID and TM_PROJECT_UUID variable.
The former is unique per document and the latter is unique per project (window).
You can use these as keys into shared storage (such as files in TMPDIR) to store things that you wish to associate with a single document or project.
Presently though there is no cleanup callback, so if you spawn a server process for a document or project, you should have it shutdown automatically if it hasn’t seen any activity for some amount of time.
Thanks a lot Allan, I'll try that approach.
On Mon, Aug 12, 2013 at 12:00 PM, Allan Odgaard mailinglist@textmate.orgwrote:
On 11 Aug 2013, at 19:02, Michele Pasin wrote:
[…] each time I send a command, the socket is obviously re-created; I
wondered if I TextMate provides a mechanism to save the 'sock' binding in the current environment - so that I can reuse it later within other commands.
Not per se, but TextMate does give you a TM_DOCUMENT_UUID and TM_PROJECT_UUID variable.
The former is unique per document and the latter is unique per project (window).
You can use these as keys into shared storage (such as files in TMPDIR) to store things that you wish to associate with a single document or project.
Presently though there is no cleanup callback, so if you spawn a server process for a document or project, you should have it shutdown automatically if it hasn’t seen any activity for some amount of time.
______________________________**_________________ textmate mailing list textmate@lists.macromates.com http://lists.macromates.com/**listinfo/textmatehttp://lists.macromates.com/listinfo/textmate
hi again
just a clarification: when I do this
os.environ['TM_PROJECT_UUID'] = someobject
I get an error saying that only strings are accepted as possible values.
is there any other approach for storing a non-string in the environment?
many tx, mikele
On Mon, Aug 12, 2013 at 12:00 PM, Allan Odgaard mailinglist@textmate.orgwrote:
On 11 Aug 2013, at 19:02, Michele Pasin wrote:
[…] each time I send a command, the socket is obviously re-created; I
wondered if I TextMate provides a mechanism to save the 'sock' binding in the current environment - so that I can reuse it later within other commands.
Not per se, but TextMate does give you a TM_DOCUMENT_UUID and TM_PROJECT_UUID variable.
The former is unique per document and the latter is unique per project (window).
You can use these as keys into shared storage (such as files in TMPDIR) to store things that you wish to associate with a single document or project.
Presently though there is no cleanup callback, so if you spawn a server process for a document or project, you should have it shutdown automatically if it hasn’t seen any activity for some amount of time.
______________________________**_________________ textmate mailing list textmate@lists.macromates.com http://lists.macromates.com/**listinfo/textmatehttp://lists.macromates.com/listinfo/textmate
Use YAML to serialize your objected into a string.
require 'yaml'
os.environ['TM_PROJECT_UUID'] = YAML.dump(someobject)
…
someobject = YAML.load(os.environ['TM_PROJECT_UUID'] )
If someobject is just a composite of standard ruby classes then this will work fine, but if it contains your own class definitions you will need to add a method to code the ivars such as
def to_yaml_properties
%w{ @var1 @var2 }
end
Dave.
From: Michele Pasin <michele.pasin@gmail.commailto:michele.pasin@gmail.com> Reply-To: TextMate users <textmate@lists.macromates.commailto:textmate@lists.macromates.com> Date: Wednesday, 14 August 2013 01:41 To: TextMate users <textmate@lists.macromates.commailto:textmate@lists.macromates.com> Subject: [TxMt] Re: Creating a global variable via bundle command (using Python)
hi again
just a clarification: when I do this
os.environ['TM_PROJECT_UUID'] = someobject
I get an error saying that only strings are accepted as possible values.
is there any other approach for storing a non-string in the environment?
many tx, mikele
On Mon, Aug 12, 2013 at 12:00 PM, Allan Odgaard <mailinglist@textmate.orgmailto:mailinglist@textmate.org> wrote: On 11 Aug 2013, at 19:02, Michele Pasin wrote:
[…] each time I send a command, the socket is obviously re-created; I
wondered if I TextMate provides a mechanism to save the 'sock' binding in the current environment - so that I can reuse it later within other commands.
Not per se, but TextMate does give you a TM_DOCUMENT_UUID and TM_PROJECT_UUID variable.
The former is unique per document and the latter is unique per project (window).
You can use these as keys into shared storage (such as files in TMPDIR) to store things that you wish to associate with a single document or project.
Presently though there is no cleanup callback, so if you spawn a server process for a document or project, you should have it shutdown automatically if it hasn’t seen any activity for some amount of time.
_______________________________________________ textmate mailing list textmate@lists.macromates.commailto:textmate@lists.macromates.com http://lists.macromates.com/listinfo/textmate
--
-------------------------------- www.michelepasin.orghttp://www.michelepasin.org/
--------------------------------------------------------------------- Intel Corporation (UK) Limited Registered No. 1134945 (England) Registered Office: Pipers Way, Swindon SN3 1RJ VAT No: 860 2173 47
This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.
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.
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', "127.0.0.1")) 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-m... )
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
-------- Michele Pasin Email: michele.pasin@gmail.com Web: www.michelepasin.org --------
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 textmate@lists.macromates.com http://lists.macromates.com/listinfo/textmate
On 17 Sep 2015, at 1:03, Michele Pasin wrote:
The problem is, Python's pickle doesn't support all kinds of objects, and sockets are among those […] 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.
I am not sure why you want to reuse the socket.
The server will run and keep listening on port 7099.
Each time you run the command, it will have to create a new connection to the server, but the server can keep state, as it will not terminate between connections/command invocations.
You can use TM_DOCUMENT_UUID or TM_PROJECT_UUID as client ID for the server, so that the server can maintain a “session” per document/project.
Creating a new connection to the server per request is not uncommon or expensive when server is running on localhost.
Thanks Allan - I (wrongly) assumed that it was good practice to avoid reopening the connection each time. Things do work fine with your suggested solution then.. now moving on to grammars and scopes :-) Best, m
---- Michele Pasin http://www.michelepasin.org
On 3 October 2015 at 12:15, Allan Odgaard mailinglist@textmate.org wrote:
On 17 Sep 2015, at 1:03, Michele Pasin wrote:
The problem is, Python's pickle doesn't support all kinds of objects, and
sockets are among those […] 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.
I am not sure why you want to reuse the socket.
The server will run and keep listening on port 7099.
Each time you run the command, it will have to create a new connection to the server, but the server can keep state, as it will not terminate between connections/command invocations.
You can use TM_DOCUMENT_UUID or TM_PROJECT_UUID as client ID for the server, so that the server can maintain a “session” per document/project.
Creating a new connection to the server per request is not uncommon or expensive when server is running on localhost.
textmate mailing list textmate@lists.macromates.com http://lists.macromates.com/listinfo/textmate