nato-converter/nato_converter.py

82 lines
1.7 KiB
Python
Raw Normal View History

2014-12-16 20:26:00 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
table = {
"a": "alpha",
"b": "bravo",
"c": "charlie",
"d": "delta",
"e": "echo",
"f": "foxtrot",
"g": "golf",
"h": "hotel",
"i": "india",
"j": "juliett",
"k": "kilo",
"l": "lima",
"m": "mike",
"n": "november",
"o": "oscar",
"p": "papa",
"q": "quebec",
"r": "romeo",
"s": "sierra",
"t": "tango",
"u": "uniform",
"v": "victor",
"w": "whiskey",
"x": "x-ray",
"y": "yankee",
"z": "zulu"
}
class IllegalStringSize(Exception):
def __init__(self, msg):
super(Exception, self).__init__(msg)
def find_in_table(letter):
if len(letter) != 1:
raise IllegalStringSize(
"The letter parameter should only contains one character")
if letter in table:
res = table[letter]
elif letter.lower() in table:
res = table[letter.lower()].upper()
elif not letter or letter.isspace():
res = ""
else:
res = letter
return res
def convert(orig):
result = ""
for letter in orig:
res = find_in_table(letter)
if result:
result = "%s %s" % (result, res)
else:
result = res
return result
if __name__ == '__main__':
orig = ""
if len(sys.argv) > 1:
for arg in sys.argv[1:]:
orig = orig + arg
elif not sys.stdin.isatty():
for arg in sys.stdin.readlines():
orig = orig + arg
if orig:
orig = orig.replace("\n", " ")
result = convert(orig)
print "%s: %s" % (orig, result)
else:
print "usage"
# vim: set ts=4 sw=4 expandtab: