1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# filename: totalopenstation-cli-parser.py
# Copyright 2008-2009 Stefano Costa <steko@iosa.it>
# This file is part of Total Open Station.
# Total Open Station is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# Total Open Station 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 Total Open Station. If not, see
# <http://www.gnu.org/licenses/>.
import sys
import os
import gettext
from optparse import OptionParser
from totalopenstation.formats import BUILTIN_INPUT_FORMATS
from totalopenstation.output import BUILTIN_OUTPUT_FORMATS
t = gettext.translation('totalopenstation', './locale', fallback=True)
_ = t.lgettext
usage = _("usage: %prog [option] arg1 [option] arg2 ...")
parser = OptionParser(usage=usage)
parser.add_option("-i",
"--infile",
action="store",
type="string",
dest="infile",
help=_("select input FILE (do not specify for stdin)"),
metavar="FILE")
parser.add_option("-o",
"--outfile",
action="store",
type="string",
dest="outfile",
help=_("select output FILE (do not specify for stdout)"),
metavar="FILE")
parser.add_option("-f",
"--input-format",
action="store",
type="string",
dest="informat",
help=_("select input FORMAT"),
metavar="FORMAT")
parser.add_option("-t",
"--output-format",
action="store",
type="string",
dest="outformat",
help=_("select input FORMAT"),
metavar="FORMAT")
parser.add_option(
"--overwrite",
action="store_true",
dest="overwrite",
default=False,
help=_("overwrite existing output file"))
parser.add_option(
"--list",
action="store_true",
dest="list",
default=False,
help=_("list the available input and output formats"))
(options, args) = parser.parse_args()
def list_formats():
'''Print a list of the supported input and output formats.'''
from totalopenstation.formats import BUILTIN_INPUT_FORMATS
mod_string = "List of supported input formats:\n" + "-" * 30 + "\n"
for k, v in sorted(BUILTIN_INPUT_FORMATS.items()):
mod_string += k.ljust(20) + v[2] + "\n"
mod_string += "\n\n"
mod_string += "List of supported output formats:\n" + "-" * 30 + "\n"
for k, v in sorted(BUILTIN_OUTPUT_FORMATS.items()):
mod_string += k.ljust(20) + v[2] + "\n"
mod_string += "\n"
return mod_string
if options.list:
sys.stdout.write(list_formats())
sys.exit()
if options.informat:
inputclass = BUILTIN_INPUT_FORMATS[options.informat]
if isinstance(inputclass, tuple):
try:
# builtin format parser
mod, cls, name = inputclass
inputclass = getattr(
__import__('totalopenstation.formats.' + mod, None, None, [cls]), cls)
except ImportError, message:
sys.exit(_("\nError:\n%s\n\n%s") % (message, list_formats()))
else:
sys.exit(_("Please specify an input format"))
if options.outformat:
outputclass = BUILTIN_OUTPUT_FORMATS[options.outformat]
if isinstance(outputclass, tuple):
try:
# builtin output builder
mod, cls, name = outputclass
outputclass = getattr(
__import__('totalopenstation.output.' + mod, None, None, [cls]), cls)
except ImportError, message:
sys.exit(_("\nError:\n%s\n\n%s") % (message, list_formats()))
if options.infile:
infile = open(options.infile, 'r').read()
else:
infile = sys.stdin.read()
def main(infile):
'''After setting up all parameters, finally try to process input data.'''
parsed_data = inputclass(infile)
parsed_points = parsed_data.points
output = outputclass(parsed_points)
def write_to_file(outfile):
e = open(outfile, 'w')
e.write(output.process())
e.close()
if options.outfile:
if not os.path.exists(options.outfile):
write_to_file(options.outfile)
print _("Downloaded data saved to out file %s") % options.outfile
else:
if options.overwrite:
write_to_file(options.outfile)
print _("Downloaded data saved to file %s,") % options.outfile,
print _("overwriting the existing file")
else:
sys.exit(_("Specified output file already exists\n"))
else:
sys.stdout.write(output.process())
if __name__ == '__main__':
main(infile)
|