Mercurial > public > m209
changeset 14:da830ef4ba52
Finish config file style key list read/write routines.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Thu, 06 Jun 2013 20:32:37 -0500 |
parents | e242ef3b5a84 |
children | 2e2692fb7de6 |
files | m209/keylist/config.py m209/keylist/tests/__init__.py m209/keylist/tests/test_config.py |
diffstat | 2 files changed, 89 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- 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)
--- /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) +