changeset 38:c10322a646d9

First working version of the keygen command.
author Brian Neal <bgneal@gmail.com>
date Sun, 30 Jun 2013 14:03:03 -0500
parents efdb3b57fb46
children 4705429d3b21
files m209/keylist/config.py m209/keylist/tests/test_config.py m209/main.py
diffstat 3 files changed, 23 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/m209/keylist/config.py	Thu Jun 27 21:54:55 2013 -0500
+++ b/m209/keylist/config.py	Sun Jun 30 14:03:03 2013 -0500
@@ -59,31 +59,20 @@
             letter_check=section['check'])
 
 
-def write_key_list(fname, key_list):
-    """Updates the file named by fname with the given key_list.
+def write(fname, key_lists):
+    """Writes the key lists to the file named fname in config file format.
 
-    If the file doesn't exist, it is created and the key_list is written to it.
-
-    If the file already exists, it is read and searched for a section that
-    matches the key_list indicator name. If found, this section is updated and
-    the file is written back out. If the section is not found, one for the
-    key_list is added and the file is written out.
+    key_lists must be an iterable of KeyList objects.
 
     """
     config = configparser.ConfigParser(interpolation=None)
-    config.read(fname)
 
-    # If the section for this key list doesn't exist, add one
-    if not config.has_section(key_list.indicator):
-        config.add_section(key_list.indicator)
+    for key_list in key_lists:
+        config[key_list.indicator] = {}
+        config[key_list.indicator]['lugs'] = key_list.lugs
+        for n, wheel in enumerate(WHEELS):
+            config[key_list.indicator][wheel] = key_list.pin_list[n]
+        config[key_list.indicator]['check'] = key_list.letter_check
 
-    # Now update it
-    section = config[key_list.indicator]
-    section['lugs'] = key_list.lugs
-    for n, wheel in enumerate(WHEELS):
-        section[wheel] = key_list.pin_list[n]
-    section['check'] = key_list.letter_check
-
-    # Write the file
     with open(fname, 'w') as fp:
         config.write(fp)
--- a/m209/keylist/tests/test_config.py	Thu Jun 27 21:54:55 2013 -0500
+++ b/m209/keylist/tests/test_config.py	Sun Jun 30 14:03:03 2013 -0500
@@ -13,7 +13,7 @@
 import unittest
 
 from ..key_list import KeyList
-from ..config import read_key_list, write_key_list
+from ..config import read_key_list, write
 
 
 @contextmanager
@@ -48,11 +48,10 @@
         os.close(fd)
 
         with file_remover(path):
-            write_key_list(path, key_list1)
+            write(path, [key_list1])
             key_list2 = read_key_list(path, key_list1.indicator)
 
             self.assertEqual(key_list1, key_list2)
 
             kl3 = read_key_list(path, 'BB')
             self.assertTrue(kl3 is None)
-
--- a/m209/main.py	Thu Jun 27 21:54:55 2013 -0500
+++ b/m209/main.py	Sun Jun 30 14:03:03 2013 -0500
@@ -14,6 +14,7 @@
 
 from .keylist.generate import generate_key_list
 from .keylist.key_list import valid_indicator, IndicatorIter
+from .keylist.config import write as write_config
 
 
 DESC = "M-209 simulator and utility program"
@@ -25,7 +26,7 @@
     """Validation/conversion function for validating the supplied starting key
     list indicator.
 
-    Returns the string valud if valid, otherwise raises an ArgumentTypeError.
+    Returns the string value if valid, otherwise raises an ArgumentTypeError.
 
     """
     if s == '*' or valid_indicator(s):
@@ -65,13 +66,14 @@
 
 def keygen(args):
     """Key list generation subcommand processor"""
-    print('Creating key list!', args)
+    logging.info("Creating key list file: %s", args.file)
 
     if not args.overwrite and os.path.exists(args.file):
         sys.exit("File '{}' exists. Use -o to overwrite\n".format(args.file))
 
     if args.start == '*':   # random indicators
         indicators = random.sample([i for i in IndicatorIter()], args.number)
+        indicators.sort()
     else:
         it = IndicatorIter(args.start)
         n = len(it)
@@ -83,8 +85,7 @@
 
     key_lists = (generate_key_list(indicator) for indicator in indicators)
 
-    for key_list in key_lists:
-        print(key_list)
+    write_config(args.file, key_lists)
 
 
 def main(argv=None):
@@ -140,9 +141,14 @@
     args = parser.parse_args(args=argv)
 
     log_level = getattr(logging, args.log.upper())
-    logging.basicConfig(level=log_level, format='%(levelname)s:%(message)s')
+    logging.basicConfig(level=log_level, format='%(message)s')
 
-    args.subcommand(args)
+    try:
+        args.subcommand(args)
+    except EnvironmentError as ex:
+        sys.exit('{}\n'.format(ex))
+    except KeyboardInterrupt:
+        sys.exit('Interrupted\n')
 
 
 if __name__ == '__main__':