Created
September 14, 2010 00:29
-
-
Save sumeet/578322 to your computer and use it in GitHub Desktop.
TextMate over sshfs server and client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
HTTP server listens for GET requests on `PORT` with query *filename* and | |
opens *~/filename* with TextMate. | |
""" | |
import BaseHTTPServer | |
import os | |
import urlparse | |
PORT = 3999 | |
def _mate_open(filename): | |
"""Run `mate <filename>` from the shell.""" | |
if not os.fork(): | |
os.execvp('mate', ('mate', filename,)) | |
return os.wait() | |
class MateRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
def do_GET(self): | |
"""Take `filename` out of the query string and open it in | |
TextMate. | |
""" | |
query_string = urlparse.urlparse(self.path).query | |
filename = urlparse.parse_qs(query_string)['filename'][0] | |
_mate_open(filename) | |
self.send_response(200) | |
if __name__ == '__main__': | |
print 'Starting mate-server' | |
os.chdir(os.path.expanduser('~')) | |
BaseHTTPServer.HTTPServer(('', PORT), MateRequestHandler).serve_forever() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Client for **mate-server.py**. Send a request to `REMOTE` to open a file in | |
TextMate. | |
Use this with an alias so you can `mate filename`. | |
""" | |
import os.path | |
import posixpath | |
import sys | |
import urllib | |
REMOTE = 'http://10.10.6.217:3999/' | |
# Stolen from a dumb blog. Didn't even work without changes: | |
# http://www.saltycrane.com/blog/2010/03/ospathrelpath-source-code-python-25/ | |
def relpath(path, start=posixpath.curdir): | |
if not path: | |
raise ValueError("no path specified") | |
start_list = posixpath.abspath(start).split(posixpath.sep) | |
path_list = posixpath.abspath(path).split(posixpath.sep) | |
# Work out how much of the filepath is shared by start and path. | |
i = len(posixpath.commonprefix([start_list, path_list])) | |
rel_list = [posixpath.pardir] * (len(start_list)-i) + path_list[i:] | |
if not rel_list: | |
return posixpath.curdir | |
return posixpath.join(*rel_list) | |
def relative_path_from_home(file): | |
return relpath(os.path.abspath(file), start=os.path.expanduser('~')) | |
def send_request_to_remote(filename, remote=REMOTE): | |
return urllib.urlopen( | |
REMOTE + '?' + urllib.urlencode({'filename': filename}) | |
) | |
if __name__ == '__main__': | |
send_request_to_remote(relative_path_from_home(sys.argv[1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment