From 589e7cebdc388bbcef9f18567cfdbb6c71dd6ff6 Mon Sep 17 00:00:00 2001 From: Xavier Claude Date: Tue, 16 Dec 2014 21:26:00 +0100 Subject: [PATCH] Initial commit --- README.md | 19 +++++++++++ nato_converter.py | 81 +++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 15 +++++++++ test.py | 28 ++++++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 README.md create mode 100755 nato_converter.py create mode 100644 setup.py create mode 100755 test.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..efa545b --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +#Nato converter + +Convert list of character to the equivalent in NATO alphabet. And it takes care +of the capitals: + + $ ./nato_converter.py abc + aBc: alpha BRAVO charlie + +This is useful to give password orally and avoid confusion: + + $ ./nato_converter.py $( pwgen 8 1) + Mahngi8i: MIKE alpha hotel november golf india 8 india + +If you don't specify an argument, it reads stdint: + + $ pwgen 3 2 | ./nato_converter.py + Ym2 0Ge : YANKEE mike 2 0 GOLF echo + + diff --git a/nato_converter.py b/nato_converter.py new file mode 100755 index 0000000..c5631aa --- /dev/null +++ b/nato_converter.py @@ -0,0 +1,81 @@ +#!/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: diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..8ee7c8f --- /dev/null +++ b/setup.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python + +from distutils.core import setup + +if __name__ == '__main__': + setup(name="Nato_converter", + version="0.1", + description="Convert list of character to the equivalent in Nato"+ + "alphabet", + author="Xavier Claude", + author_email="contact+python@xavierclaude.be", + url="todo", + license="GPL2+", + scripts=["nato_converter.py"], + ) diff --git a/test.py b/test.py new file mode 100755 index 0000000..a9bfdad --- /dev/null +++ b/test.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import nato_converter +import unittest + +class TestFindInTable(unittest.TestCase): + def test_find_lower(self): + self.assertEqual("alpha", nato_converter.find_in_table("a")) + + def test_find_upper(self): + self.assertEqual("ALPHA", nato_converter.find_in_table("A")) + + def test_find_space(self): + self.assertEqual("", nato_converter.find_in_table(" ")) + self.assertEqual("", nato_converter.find_in_table("\t")) + self.assertEqual("", nato_converter.find_in_table("\n")) + + def test_find_multiple(self): + self.assertRaises(nato_converter.IllegalStringSize, + nato_converter.find_in_table, "aa") + + def test_find_special_char(self): + self.assertEqual("1", nato_converter.find_in_table("1")) + self.assertEqual(u"é", nato_converter.find_in_table(u"é")) + +if __name__ == '__main__': + unittest.main()