diff --git a/ChangeLog b/ChangeLog index a1c2c42..e3dd695 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2012-12-02 Thomas Vander Stichele + + * morituri/common/config.py (added): + * morituri/test/test_common_config.py (added): + * morituri/common/Makefile.am: + * morituri/test/Makefile.am: + First stab at adding a configuration file to store + drive read offsets. + 2012-11-28 Thomas Vander Stichele * morituri/common/program.py: diff --git a/morituri/common/Makefile.am b/morituri/common/Makefile.am index b211fd1..b3304f6 100644 --- a/morituri/common/Makefile.am +++ b/morituri/common/Makefile.am @@ -7,6 +7,7 @@ morituri_PYTHON = \ accurip.py \ checksum.py \ common.py \ + config.py \ drive.py \ encode.py \ gstreamer.py \ diff --git a/morituri/common/config.py b/morituri/common/config.py new file mode 100644 index 0000000..b2b0b86 --- /dev/null +++ b/morituri/common/config.py @@ -0,0 +1,92 @@ +# -*- Mode: Python; test-case-name: morituri.test.test_common_config -*- +# vi:si:et:sw=4:sts=4:ts=4 + +# Morituri - for those about to RIP + +# Copyright (C) 2009 Thomas Vander Stichele + +# This file is part of morituri. +# +# morituri is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# morituri is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with morituri. If not, see . + +import os.path +import urllib +import codecs +import tempfile +import ConfigParser + +from morituri.common import log + + +class Config(log.Loggable): + + def __init__(self, path=None): + if not path: + path = os.path.expanduser('~/.moriturirc') + + self._path = path + + self._parser = ConfigParser.SafeConfigParser() + + self.open() + + def open(self): + # Open the file with the correct encoding + if os.path.exists(self._path): + with codecs.open(self._path, 'r', encoding='utf-8') as f: + self._parser.readfp(f) + + + def setReadOffset(self, vendor, model, release, offset): + """ + Set a read offset for the given drive. + """ + try: + section = self._findDriveSection(vendor, model, release) + except KeyError: + section = 'drive:' + urllib.quote('%s:%s:%s' % (vendor, model, release)) + self._parser.add_section(section) + read_offset = str(offset) + for key in ['vendor', 'model', 'release', 'read_offset']: + self._parser.set(section, key, locals()[key]) + + def getReadOffset(self, vendor, model, release): + """ + Get a read offset for the given drive. + """ + section = self._findDriveSection(vendor, model, release) + + return int(self._parser.get(section, 'read_offset')) + + def _findDriveSection(self, vendor, model, release): + for name in self._parser.sections(): + if not name.startswith('drive:'): + continue + + if vendor != self._parser.get(name, 'vendor'): + continue + if model != self._parser.get(name, 'model'): + continue + if release != self._parser.get(name, 'release'): + continue + + return name + + raise KeyError + + def write(self): + fd, path = tempfile.mkstemp(suffix=u'.moriturirc') + self._parser.write(fd) + os.close(fd) + os.rename(path, self._path) diff --git a/morituri/test/Makefile.am b/morituri/test/Makefile.am index b12725d..046ad07 100644 --- a/morituri/test/Makefile.am +++ b/morituri/test/Makefile.am @@ -5,6 +5,7 @@ EXTRA_DIST = \ common.py \ test_common_accurip.py \ test_common_checksum.py \ + test_common_config.py \ test_common_drive.py \ test_common_musicbrainzngs.py \ test_common_program.py \ diff --git a/morituri/test/test_common_config.py b/morituri/test/test_common_config.py new file mode 100644 index 0000000..68d4c25 --- /dev/null +++ b/morituri/test/test_common_config.py @@ -0,0 +1,36 @@ +# -*- Mode: Python; test-case-name: morituri.test.test_common_config -*- +# vi:si:et:sw=4:sts=4:ts=4 + +import os +import tempfile + +from morituri.common import config + +from morituri.test import common as tcommon + + +class OffsetTestCase(tcommon.TestCase): + + def setUp(self): + fd, self._path = tempfile.mkstemp(suffix=u'.morituri.test.config') + os.close(fd) + self._config = config.Config(self._path) + + def tearDown(self): + os.unlink(self._path) + + def testAddReadOffset(self): + self.assertRaises(KeyError, + self._config.getReadOffset, 'PLEXTOR ', 'DVDR PX-L890SA', '1.05') + self._config.setReadOffset('PLEXTOR ', 'DVDR PX-L890SA', '1.05', 6) + + # getting it from memory should work + offset = self._config.getReadOffset('PLEXTOR ', 'DVDR PX-L890SA', + '1.05') + self.assertEquals(offset, 6) + + # and so should getting it after reading it again + self._config.open() + offset = self._config.getReadOffset('PLEXTOR ', 'DVDR PX-L890SA', + '1.05') + self.assertEquals(offset, 6)