#!/usr/bin/env python ''' th2svx - Convert Therion .th files to Survex .svx format Copyright (C) 2007 David A. Riggs This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see: http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt ''' import sys USAGE = ''' usage: th2svx FILE ... Convert Therion .th files to Survex .svx format. ''' def convert_filename(fname): '''convert_filename(foo.th) -> foo.svx''' return fname[:fname.rindex('.')] + '.svx' def get_indent(line): '''return the indentation whitespace from a line of text''' chars = len(line) - len(line.lstrip()) return line[:chars] def convert_line(line): indent = get_indent(line) outline = line.strip() # these commands or tokens are different in therion vs. survex replacements = ('#', ';'), ('survey', '*begin'), ('endsurvey', '*end'), ('input', '*include'), ('encoding', '; encoding') for k,v in replacements: if outline.startswith(k): outline = v + outline[len(k):] # these are identical commands, just need prepended with '*' commands = ('calibrate', 'data', 'date', 'equate', 'fix', 'flags', 'team', 'title', 'units') for command in commands: if outline.startswith(command): outline = '*'+outline # replace all comment characters, anywhere (dumb op) outline = outline.replace('#',';') # replace -title option with command (uses newline, must be final op!) outline = outline.replace('-title', '\n'+indent+'*title') return indent + outline def th2svx(f): '''convert a file from therion .th to survex .svx''' infile = open(f) outfile = open(convert_filename(f), 'w') for line in infile: outline = convert_line(line) print >> outfile, outline def main(): files = sys.argv[1:] if not files or files[0] == '--help': print >> sys.stderr, USAGE exit(2) for f in files: th2svx(f) if __name__ == '__main__': main()