Skip to content

Instantly share code, notes, and snippets.

@jonathansick
Created September 23, 2010 05:16
Show Gist options
  • Save jonathansick/593155 to your computer and use it in GitHub Desktop.
Save jonathansick/593155 to your computer and use it in GitHub Desktop.
Makes LaTeX files available to Writer on the iPad
#!/usr/bin/env python
# encoding: utf-8
"""
writer.py
Makes LaTeX files available to Writer on the iPad, and retrieves
the changed documents later. Could be modified to work with Elements,
etc. on the iPad. The idea is to copy the latex files to the
~/Dropbox/Writer directory, and retain its project membership
by appending the host directory name to the original file name.
e.g. thesis/abstract.tex is synced to ~/Dropbox/thesis_abstract.txt
To send files to Dropbox, from the project's directory, do
writer.py -s
And to get the files back from Dropbox, from the projects directory do
writer.py -g
That is, always run this script from the local host directory of the
project on your Mac (e.g. a git checkout).
Created by Jonathan Sick on 2010-09-22.
Copyright (c) 2010 Jonathan Sick. All rights reserved.
"""
import sys
import getopt
import os
import shutil
import glob
help_message = '''
To send latex in this directory to the Writer directory on DropBox:
writer.py -s
(or) writer.py --send
To get latex from dropbox that belongs to this directory:
writer.py -g
(or) writer.py --get
'''
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def main(argv=None):
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "hgs", ["help", "get", "send"])
except getopt.error, msg:
raise Usage(msg)
# option processing
for option, value in opts:
if option in ("-h", "--help"):
raise Usage(help_message)
if option in ("-g", "--get"):
get()
elif option in ("-s", "--send"):
send()
except Usage, err:
print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
print >> sys.stderr, "\t for help use --help"
return 2
def makePathPairs():
"""Make pairs of (latex path, dropbox text path) for all latex files
in the Current Working Directory.
"""
pathPairs = []
cwd = os.getcwd()
dirname = os.path.basename(cwd)
latexFilePaths = glob.glob("*.tex")
for latexFilePath in latexFilePaths:
basename = os.path.splitext(latexFilePath)[0]
dbPath = os.path.join(
os.path.expanduser("~/Dropbox/Writer"), "%s_%s.txt" % (dirname, basename)
)
pathPairs.append((latexFilePath, dbPath))
return pathPairs
def send():
"""Send latex documents in the current working directory to DropBox."""
for latexFilePath, dbPath in makePathPairs():
if os.path.exists(dbPath):
os.remove(dbPath)
shutil.copy(latexFilePath, dbPath)
print "Sent %s" % latexFilePath
def get():
"""Get latex documents belonging to this directory from DropBox."""
for latexFilePath, dbPath in makePathPairs():
if os.path.exists(dbPath):
os.remove(latexFilePath)
shutil.copy(dbPath, latexFilePath)
print "Got %s" % (latexFilePath)
else:
print "Couldn't find %s as %s" (latexFilePath, dbPath)
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment