# HG changeset patch # User Brian Neal # Date 1370568757 18000 # Node ID da830ef4ba52a479589d2c709d8aab0123183615 # Parent e242ef3b5a840b7b7e25f647b6d5404c129c88ea Finish config file style key list read/write routines. diff -r e242ef3b5a84 -r da830ef4ba52 m209/keylist/config.py --- a/m209/keylist/config.py Wed Jun 05 21:23:30 2013 -0500 +++ b/m209/keylist/config.py Thu Jun 06 20:32:37 2013 -0500 @@ -38,15 +38,15 @@ WHEELS = ['wheel{}'.format(n) for n in range(1, 7)] -def read_key_list(fp, indicator): - """Reads from fp, which must be an iterable that yields unicode strings. +def read_key_list(fname, indicator): + """Reads key list information from the file given by fname. Searches the config file for the key list with the given indicator. If found, returns a KeyList object. Returns None if not found. """ config = configparser.ConfigParser(interpolation=None) - config.read_file(fp) + config.read(fname) if indicator not in config.sections(): return None @@ -59,5 +59,31 @@ letter_check=section['check']) -def write_key_list(fp, key_list): - pass +def write_key_list(fname, key_list): + """Updates the file named by fname with the given key_list. + + 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. + + """ + 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) + + # 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) diff -r e242ef3b5a84 -r da830ef4ba52 m209/keylist/tests/test_config.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/m209/keylist/tests/test_config.py Thu Jun 06 20:32:37 2013 -0500 @@ -0,0 +1,58 @@ +# Copyright (C) 2013 by Brian Neal. +# This file is part of m209, the M-209 simulation. +# m209 is released under the MIT License (see LICENSE.txt). + +"""Unit tests for the key list read/write routines for the config file +format. + +""" + +from contextlib import contextmanager +import os +import tempfile +import unittest + +from ..key_list import KeyList +from ..config import read_key_list, write_key_list + + +@contextmanager +def file_remover(path): + """Ensures a file is deleted.""" + try: + yield + except Exception: + raise + finally: + os.remove(path) + + +class ConfigFileTestCase(unittest.TestCase): + + def test_round_trip(self): + + key_list1 = KeyList( + indicator='AA', + lugs='0-4 0-5*4 0-6*6 1-0*5 1-2 1-5*4 3-0*3 3-4 3-6 5-6', + pin_list= [ + 'FGIKOPRSUVWYZ', + 'DFGKLMOTUY', + 'ADEFGIORTUVX', + 'ACFGHILMRSU', + 'BCDEFJKLPS', + 'EFGHIJLMNP' + ], + letter_check='QLRRN TPTFU TRPTN MWQTV JLIJE J') + + fd, path = tempfile.mkstemp(suffix='.ini', text=True) + os.close(fd) + + with file_remover(path): + write_key_list(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) +