From f79b771a38421e86fdde2a393d7a5642795a56a6 Mon Sep 17 00:00:00 2001 From: Andrew Williams Date: Fri, 29 Jan 2010 12:03:48 +0000 Subject: [PATCH] Moved config parsing out to a custom dict, in prep to handle defaults --- tests/testtvorganise.py | 3 +- tvorganise/__init__.py | 41 ++------------------------- tvorganise/config.py | 62 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 39 deletions(-) create mode 100644 tvorganise/config.py diff --git a/tests/testtvorganise.py b/tests/testtvorganise.py index 7d85e2e..1c806ff 100755 --- a/tests/testtvorganise.py +++ b/tests/testtvorganise.py @@ -6,6 +6,7 @@ import unittest import os import shutil import tempfile +import tvorganise.config class testTvOrganise(unittest.TestCase): """ @@ -17,7 +18,7 @@ class testTvOrganise(unittest.TestCase): Simple test to check to see if the config parser actually returns a dict on completion """ - dict = self.tvo._get_config('tvorganise.cfg') + dict = tvorganise.config.Config('tvorganise.cfg') self.assertTrue(dict) def testConfigSettings(self): diff --git a/tvorganise/__init__.py b/tvorganise/__init__.py index 87e234c..847aff2 100755 --- a/tvorganise/__init__.py +++ b/tvorganise/__init__.py @@ -16,6 +16,7 @@ import shutil import ConfigParser import logging +import config def same_partition(path1, path2): """ @@ -66,42 +67,6 @@ class TvOrganiser(): self.__logger = logging.getLogger(self.__class__.__name__) return self.__logger - def _get_config(self, cfile): - """ - Parses the TVOrganiser style config file and produces a dict - with all the elements contained within. - - Also, all regex specified in the file are compiled - """ - - config = {} - - configpsr = ConfigParser.RawConfigParser() - configpsr.read(cfile) - - if configpsr.has_section('main'): - for key, value in configpsr.items('main'): - config[key] = value - - if configpsr.has_section('regex'): - - regex_config = {} - regex = [] - - # Load in subs before reading in the regex - for key, value in configpsr.items('regex'): - if key[:5] != 'regex': - regex_config[key] = value - - for key, value in configpsr.items('regex'): - if key[:5] == 'regex': - regex.append(re.compile(value % regex_config)) - - config['regex'] = regex - - self._config = config - return config - def parse_filenames(self, names): """ Takes list of names, runs them though the regexs and breaks them down @@ -180,7 +145,7 @@ class TvOrganiser(): sys.exit(1) self._logger.info('Using config file: %s' % cfile) - config = self._get_config(cfile) + self._config = config.Config(cfile) files = find_files(args) files = self.parse_filenames(files) @@ -195,7 +160,7 @@ class TvOrganiser(): for name in files: filename = "%s.%s" % (name['filename'], name['ext']) oldfile = os.path.join(name['filepath'], filename) - newpath = config['target_path'] % name + newpath = self._config['target_path'] % name newfile = os.path.join(newpath, filename) self._logger.info("Old path: %s" % oldfile) diff --git a/tvorganise/config.py b/tvorganise/config.py new file mode 100644 index 0000000..1cd722e --- /dev/null +++ b/tvorganise/config.py @@ -0,0 +1,62 @@ +import ConfigParser +import re + +def defaults(): + """ + Creates a ConfigParser instance and fills it with the default settings + """ + + config = ConfigParser.ConfigParser() + + config.add_section('main') + config.set('main', 'target_path', '/media/%(showname)s/Season %(seasonnum)s/') + + config.add_section('regex') + config.set('regex', 'valid_in_names', "[\\w\\(\\).,\\[\\]'\\ \\-?!#:]") + + return config + + +class Config(dict): + """ + Config class loads and parses TVOrganiser style config files, presenting + as a dict with compiled re objects as needed. + """ + + def __init__(self, cfile=None): + super(Config, self).__init__() + + if cfile: + self.load(cfile) + + def load(self, cfile): + """ + Parses the TVOrganiser style config file and produces a dict + with all the elements contained within. + + Also, all regex specified in the file are compiled + """ + + configpsr = ConfigParser.RawConfigParser() + configpsr.read(cfile) + + if configpsr.has_section('main'): + for key, value in configpsr.items('main'): + self[key] = value + + if configpsr.has_section('regex'): + + regex_config = {} + regex = [] + + # Load in subs before reading in the regex + for key, value in configpsr.items('regex'): + if key[:5] != 'regex': + regex_config[key] = value + + for key, value in configpsr.items('regex'): + if key[:5] == 'regex': + regex.append(re.compile(value % regex_config)) + + self['regex'] = regex +